[0caa075e] | 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 <errno.h>
|
---|
| 43 | #include <str_error.h>
|
---|
| 44 | #include <io/klog.h>
|
---|
| 45 | #include <sysinfo.h>
|
---|
| 46 | #include <malloc.h>
|
---|
| 47 | #include <fibril_synch.h>
|
---|
| 48 | #include <adt/list.h>
|
---|
| 49 | #include <adt/prodcons.h>
|
---|
| 50 | #include <io/log.h>
|
---|
| 51 | #include <io/logctl.h>
|
---|
| 52 |
|
---|
[193d280c] | 53 | #define NAME "klog"
|
---|
[0caa075e] | 54 |
|
---|
[3385dd3] | 55 | typedef size_t __attribute__ ((aligned(1))) unaligned_size_t;
|
---|
| 56 |
|
---|
[0caa075e] | 57 | typedef struct {
|
---|
| 58 | size_t entry_len;
|
---|
| 59 | uint32_t serial;
|
---|
| 60 | uint32_t facility;
|
---|
| 61 | uint32_t level;
|
---|
| 62 | char message[0];
|
---|
| 63 |
|
---|
| 64 | } __attribute__((__packed__)) log_entry_t;
|
---|
| 65 |
|
---|
| 66 | /* Producer/consumer buffers */
|
---|
| 67 | typedef struct {
|
---|
| 68 | link_t link;
|
---|
| 69 | size_t size;
|
---|
| 70 | log_entry_t *data;
|
---|
| 71 | } item_t;
|
---|
| 72 |
|
---|
| 73 | static prodcons_t pc;
|
---|
| 74 |
|
---|
| 75 | /* Pointer to buffer where kernel stores new entries */
|
---|
| 76 | #define BUFFER_SIZE PAGE_SIZE
|
---|
| 77 | static void *buffer;
|
---|
| 78 |
|
---|
| 79 | /* Notification mutex */
|
---|
| 80 | static FIBRIL_MUTEX_INITIALIZE(mtx);
|
---|
| 81 |
|
---|
| 82 | static log_t kernel_ctx;
|
---|
| 83 | static const char *facility_name[] = {
|
---|
| 84 | "other",
|
---|
| 85 | "uspace",
|
---|
| 86 | "arch"
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | #define facility_len (sizeof(facility_name) / sizeof(const char *))
|
---|
| 90 | static log_t facility_ctx[facility_len];
|
---|
| 91 |
|
---|
| 92 | /** Klog producer
|
---|
| 93 | *
|
---|
| 94 | * Copies the log entries to a producer/consumer queue.
|
---|
| 95 | *
|
---|
| 96 | * @param length Number of characters to copy.
|
---|
| 97 | * @param data Pointer to the kernel klog buffer.
|
---|
| 98 | *
|
---|
| 99 | */
|
---|
[193d280c] | 100 | static void producer(void)
|
---|
[0caa075e] | 101 | {
|
---|
| 102 | int read = klog_read(buffer, BUFFER_SIZE);
|
---|
| 103 |
|
---|
| 104 | if (read < 0) {
|
---|
| 105 | log_msg(LOG_DEFAULT, LVL_ERROR, "klog_read failed, rc = %d",
|
---|
| 106 | read);
|
---|
| 107 | return;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | size_t len = read;
|
---|
| 111 | size_t offset = 0;
|
---|
| 112 | while (offset < len) {
|
---|
[3385dd3] | 113 | size_t entry_len = *((unaligned_size_t *) (buffer + offset));
|
---|
[0caa075e] | 114 |
|
---|
| 115 | if (offset + entry_len > len || entry_len < sizeof(log_entry_t))
|
---|
| 116 | break;
|
---|
| 117 |
|
---|
| 118 | log_entry_t *buf = malloc(entry_len + 1);
|
---|
| 119 | if (buf == NULL)
|
---|
| 120 | break;
|
---|
| 121 |
|
---|
| 122 | item_t *item = malloc(sizeof(item_t));
|
---|
| 123 | if (item == NULL) {
|
---|
| 124 | free(buf);
|
---|
| 125 | break;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | memcpy(buf, buffer + offset, entry_len);
|
---|
| 129 | *((uint8_t *) buf + entry_len) = 0;
|
---|
| 130 | link_initialize(&item->link);
|
---|
| 131 | item->size = entry_len;
|
---|
| 132 | item->data = buf;
|
---|
| 133 | prodcons_produce(&pc, &item->link);
|
---|
| 134 |
|
---|
| 135 | offset += entry_len;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /** Klog consumer
|
---|
| 140 | *
|
---|
| 141 | * Waits in an infinite loop for the log data created by
|
---|
| 142 | * the producer and logs them to the logger.
|
---|
| 143 | *
|
---|
| 144 | * @param data Unused.
|
---|
| 145 | *
|
---|
| 146 | * @return Always EOK (unreachable).
|
---|
| 147 | *
|
---|
| 148 | */
|
---|
| 149 | static int consumer(void *data)
|
---|
| 150 | {
|
---|
| 151 |
|
---|
| 152 | while (true) {
|
---|
| 153 | link_t *link = prodcons_consume(&pc);
|
---|
| 154 | item_t *item = list_get_instance(link, item_t, link);
|
---|
| 155 |
|
---|
| 156 | if (item->size < sizeof(log_entry_t)) {
|
---|
| 157 | free(item->data);
|
---|
| 158 | free(item);
|
---|
| 159 | continue;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | if (item->data->facility == LF_USPACE) {
|
---|
| 163 | /* Avoid reposting messages */
|
---|
| 164 | free(item->data);
|
---|
| 165 | free(item);
|
---|
| 166 | continue;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | log_t ctx = kernel_ctx;
|
---|
| 170 | if (item->data->facility < facility_len) {
|
---|
| 171 | ctx = facility_ctx[item->data->facility];
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | log_level_t lvl = item->data->level;
|
---|
| 175 | if (lvl > LVL_LIMIT)
|
---|
| 176 | lvl = LVL_NOTE;
|
---|
| 177 |
|
---|
| 178 | log_msg(ctx, lvl, "%s", item->data->message);
|
---|
| 179 |
|
---|
| 180 | free(item->data);
|
---|
| 181 | free(item);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | return EOK;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /** Kernel notification handler
|
---|
| 188 | *
|
---|
| 189 | * Receives kernel klog notifications.
|
---|
| 190 | *
|
---|
| 191 | * @param call IPC call structure
|
---|
| 192 | * @param arg Local argument
|
---|
| 193 | *
|
---|
| 194 | */
|
---|
[01c3bb4] | 195 | static void klog_notification_received(ipc_call_t *call, void *arg)
|
---|
[0caa075e] | 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 |
|
---|
[8820544] | 207 | async_event_unmask(EVENT_KLOG);
|
---|
[0caa075e] | 208 | fibril_mutex_unlock(&mtx);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | int 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);
|
---|
[8820544] | 231 | rc = async_event_subscribe(EVENT_KLOG, klog_notification_received, NULL);
|
---|
[0caa075e] | 232 | if (rc != EOK) {
|
---|
| 233 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 234 | "Unable to register klog notifications");
|
---|
| 235 | return rc;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | fid_t fid = fibril_create(consumer, NULL);
|
---|
| 239 | if (!fid) {
|
---|
| 240 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 241 | "Unable to create consumer fibril");
|
---|
| 242 | return ENOMEM;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | fibril_add_ready(fid);
|
---|
[8820544] | 246 | async_event_unmask(EVENT_KLOG);
|
---|
[0caa075e] | 247 |
|
---|
| 248 | fibril_mutex_lock(&mtx);
|
---|
| 249 | producer();
|
---|
| 250 | fibril_mutex_unlock(&mtx);
|
---|
| 251 |
|
---|
| 252 | task_retval(0);
|
---|
| 253 | async_manager();
|
---|
| 254 |
|
---|
| 255 | return 0;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /** @}
|
---|
| 259 | */
|
---|