source: mainline/uspace/srv/klog/klog.c@ 83f29e0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 83f29e0 was 0caa075e, checked in by Martin Sucha <sucha14@…>, 12 years ago

Cherrypick klog server that synchronizes kernel log to uspace log

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * Copyright (c) 2013 Martin Sucha
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup klog KLog
31 * @brief HelenOS KLog
32 * @{
33 */
34/**
35 * @file
36 */
37
38#include <stdio.h>
39#include <async.h>
40#include <as.h>
41#include <ddi.h>
42#include <event.h>
43#include <errno.h>
44#include <str_error.h>
45#include <io/klog.h>
46#include <sysinfo.h>
47#include <malloc.h>
48#include <fibril_synch.h>
49#include <adt/list.h>
50#include <adt/prodcons.h>
51#include <io/log.h>
52#include <io/logctl.h>
53
54#define NAME "klog"
55
56typedef struct {
57 size_t entry_len;
58 uint32_t serial;
59 uint32_t facility;
60 uint32_t level;
61 char message[0];
62
63} __attribute__((__packed__)) log_entry_t;
64
65/* Producer/consumer buffers */
66typedef struct {
67 link_t link;
68 size_t size;
69 log_entry_t *data;
70} item_t;
71
72static prodcons_t pc;
73
74/* Pointer to buffer where kernel stores new entries */
75#define BUFFER_SIZE PAGE_SIZE
76static void *buffer;
77
78/* Notification mutex */
79static FIBRIL_MUTEX_INITIALIZE(mtx);
80
81static log_t kernel_ctx;
82static const char *facility_name[] = {
83 "other",
84 "uspace",
85 "arch"
86};
87
88#define facility_len (sizeof(facility_name) / sizeof(const char *))
89static log_t facility_ctx[facility_len];
90
91/** Klog producer
92 *
93 * Copies the log entries to a producer/consumer queue.
94 *
95 * @param length Number of characters to copy.
96 * @param data Pointer to the kernel klog buffer.
97 *
98 */
99static void producer()
100{
101 int read = klog_read(buffer, BUFFER_SIZE);
102
103 if (read < 0) {
104 log_msg(LOG_DEFAULT, LVL_ERROR, "klog_read failed, rc = %d",
105 read);
106 return;
107 }
108
109 size_t len = read;
110 size_t offset = 0;
111 while (offset < len) {
112 size_t entry_len = *((size_t *) (buffer + offset));
113
114 if (offset + entry_len > len || entry_len < sizeof(log_entry_t))
115 break;
116
117 log_entry_t *buf = malloc(entry_len + 1);
118 if (buf == NULL)
119 break;
120
121 item_t *item = malloc(sizeof(item_t));
122 if (item == NULL) {
123 free(buf);
124 break;
125 }
126
127 memcpy(buf, buffer + offset, entry_len);
128 *((uint8_t *) buf + entry_len) = 0;
129 link_initialize(&item->link);
130 item->size = entry_len;
131 item->data = buf;
132 prodcons_produce(&pc, &item->link);
133
134 offset += entry_len;
135 }
136}
137
138/** Klog consumer
139 *
140 * Waits in an infinite loop for the log data created by
141 * the producer and logs them to the logger.
142 *
143 * @param data Unused.
144 *
145 * @return Always EOK (unreachable).
146 *
147 */
148static int consumer(void *data)
149{
150
151 while (true) {
152 link_t *link = prodcons_consume(&pc);
153 item_t *item = list_get_instance(link, item_t, link);
154
155 if (item->size < sizeof(log_entry_t)) {
156 free(item->data);
157 free(item);
158 continue;
159 }
160
161 if (item->data->facility == LF_USPACE) {
162 /* Avoid reposting messages */
163 free(item->data);
164 free(item);
165 continue;
166 }
167
168 log_t ctx = kernel_ctx;
169 if (item->data->facility < facility_len) {
170 ctx = facility_ctx[item->data->facility];
171 }
172
173 log_level_t lvl = item->data->level;
174 if (lvl > LVL_LIMIT)
175 lvl = LVL_NOTE;
176
177 log_msg(ctx, lvl, "%s", item->data->message);
178
179 free(item->data);
180 free(item);
181 }
182
183 return EOK;
184}
185
186/** Kernel notification handler
187 *
188 * Receives kernel klog notifications.
189 *
190 * @param callid IPC call ID
191 * @param call IPC call structure
192 * @param arg Local argument
193 *
194 */
195static void notification_received(ipc_callid_t callid, ipc_call_t *call)
196{
197 /*
198 * Make sure we process only a single notification
199 * at any time to limit the chance of the consumer
200 * starving.
201 */
202
203 fibril_mutex_lock(&mtx);
204
205 producer();
206
207 event_unmask(EVENT_KLOG);
208 fibril_mutex_unlock(&mtx);
209}
210
211int main(int argc, char *argv[])
212{
213 int rc = log_init(NAME);
214 if (rc != EOK) {
215 fprintf(stderr, "%s: Unable to initialize log\n", NAME);
216 return rc;
217 }
218
219 kernel_ctx = log_create("kernel", LOG_NO_PARENT);
220 for (unsigned int i = 0; i < facility_len; i++) {
221 facility_ctx[i] = log_create(facility_name[i], kernel_ctx);
222 }
223
224 buffer = malloc(BUFFER_SIZE);
225 if (buffer == NULL) {
226 log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to allocate buffer");
227 return 1;
228 }
229
230 prodcons_initialize(&pc);
231 async_set_interrupt_received(notification_received);
232 rc = event_subscribe(EVENT_KLOG, 0);
233 if (rc != EOK) {
234 log_msg(LOG_DEFAULT, LVL_ERROR,
235 "Unable to register klog notifications");
236 return rc;
237 }
238
239 fid_t fid = fibril_create(consumer, NULL);
240 if (!fid) {
241 log_msg(LOG_DEFAULT, LVL_ERROR,
242 "Unable to create consumer fibril");
243 return ENOMEM;
244 }
245
246 fibril_add_ready(fid);
247 event_unmask(EVENT_KLOG);
248
249 fibril_mutex_lock(&mtx);
250 producer();
251 fibril_mutex_unlock(&mtx);
252
253 task_retval(0);
254 async_manager();
255
256 return 0;
257}
258
259/** @}
260 */
Note: See TracBrowser for help on using the repository browser.