[51dbadf3] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[51dbadf3] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[b2951e2] | 29 | /** @addtogroup klog KLog
|
---|
[d8fcfc0] | 30 | * @brief HelenOS KLog
|
---|
[b2951e2] | 31 | * @{
|
---|
[d8fcfc0] | 32 | */
|
---|
[b2951e2] | 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[51dbadf3] | 37 | #include <stdio.h>
|
---|
| 38 | #include <async.h>
|
---|
| 39 | #include <as.h>
|
---|
[6119f24] | 40 | #include <ddi.h>
|
---|
[05641a9e] | 41 | #include <event.h>
|
---|
[1c03c17] | 42 | #include <errno.h>
|
---|
[5a9f4d7] | 43 | #include <str_error.h>
|
---|
[0c25c10] | 44 | #include <io/klog.h>
|
---|
[6119f24] | 45 | #include <sysinfo.h>
|
---|
[4e1a2f5] | 46 | #include <malloc.h>
|
---|
| 47 | #include <fibril_synch.h>
|
---|
| 48 | #include <adt/list.h>
|
---|
| 49 | #include <adt/prodcons.h>
|
---|
[1c03c17] | 50 |
|
---|
[5a9f4d7] | 51 | #define NAME "klog"
|
---|
| 52 | #define LOG_FNAME "/log/klog"
|
---|
[1c03c17] | 53 |
|
---|
[4e1a2f5] | 54 | /* Producer/consumer buffers */
|
---|
| 55 | typedef struct {
|
---|
| 56 | link_t link;
|
---|
| 57 | size_t length;
|
---|
| 58 | wchar_t *data;
|
---|
| 59 | } item_t;
|
---|
| 60 |
|
---|
| 61 | static prodcons_t pc;
|
---|
| 62 |
|
---|
[51dbadf3] | 63 | /* Pointer to klog area */
|
---|
[171f9a1] | 64 | static wchar_t *klog;
|
---|
[0c25c10] | 65 | static size_t klog_length;
|
---|
[51dbadf3] | 66 |
|
---|
[4e1a2f5] | 67 | /* Notification mutex */
|
---|
| 68 | static FIBRIL_MUTEX_INITIALIZE(mtx);
|
---|
[5a9f4d7] | 69 |
|
---|
[4e1a2f5] | 70 | /** Klog producer
|
---|
| 71 | *
|
---|
| 72 | * Copies the contents of a character buffer to local
|
---|
| 73 | * producer/consumer queue.
|
---|
| 74 | *
|
---|
| 75 | * @param length Number of characters to copy.
|
---|
| 76 | * @param data Pointer to the kernel klog buffer.
|
---|
| 77 | *
|
---|
| 78 | */
|
---|
| 79 | static void producer(size_t length, wchar_t *data)
|
---|
[51dbadf3] | 80 | {
|
---|
[4e1a2f5] | 81 | item_t *item = (item_t *) malloc(sizeof(item_t));
|
---|
| 82 | if (item == NULL)
|
---|
| 83 | return;
|
---|
| 84 |
|
---|
| 85 | size_t sz = sizeof(wchar_t) * length;
|
---|
| 86 | wchar_t *buf = (wchar_t *) malloc(sz);
|
---|
| 87 | if (data == NULL) {
|
---|
| 88 | free(item);
|
---|
| 89 | return;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | memcpy(buf, data, sz);
|
---|
| 93 |
|
---|
| 94 | link_initialize(&item->link);
|
---|
| 95 | item->length = length;
|
---|
| 96 | item->data = buf;
|
---|
| 97 | prodcons_produce(&pc, &item->link);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /** Klog consumer
|
---|
| 101 | *
|
---|
| 102 | * Waits in an infinite loop for the character data created by
|
---|
| 103 | * the producer and outputs them to stdout and optionally into
|
---|
| 104 | * a file.
|
---|
| 105 | *
|
---|
| 106 | * @param data Unused.
|
---|
| 107 | *
|
---|
| 108 | * @return Always EOK (unreachable).
|
---|
| 109 | *
|
---|
| 110 | */
|
---|
| 111 | static int consumer(void *data)
|
---|
| 112 | {
|
---|
| 113 | FILE *log = fopen(LOG_FNAME, "a");
|
---|
| 114 | if (log == NULL)
|
---|
| 115 | printf("%s: Unable to create log file %s (%s)\n", NAME, LOG_FNAME,
|
---|
| 116 | str_error(errno));
|
---|
[3636964] | 117 |
|
---|
[4e1a2f5] | 118 | while (true) {
|
---|
| 119 | link_t *link = prodcons_consume(&pc);
|
---|
| 120 | item_t *item = list_get_instance(link, item_t, link);
|
---|
| 121 |
|
---|
| 122 | for (size_t i = 0; i < item->length; i++)
|
---|
| 123 | putchar(item->data[i]);
|
---|
[5a9f4d7] | 124 |
|
---|
[4e1a2f5] | 125 | if (log != NULL) {
|
---|
| 126 | for (size_t i = 0; i < item->length; i++)
|
---|
| 127 | fputc(item->data[i], log);
|
---|
| 128 |
|
---|
| 129 | fflush(log);
|
---|
| 130 | fsync(fileno(log));
|
---|
| 131 | }
|
---|
[5a9f4d7] | 132 |
|
---|
[4e1a2f5] | 133 | free(item->data);
|
---|
| 134 | free(item);
|
---|
[5a9f4d7] | 135 | }
|
---|
| 136 |
|
---|
[4e1a2f5] | 137 | fclose(log);
|
---|
| 138 | return EOK;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /** Kernel notification handler
|
---|
| 142 | *
|
---|
| 143 | * Receives kernel klog notifications.
|
---|
| 144 | *
|
---|
[3815efb] | 145 | * @param callid IPC call ID
|
---|
| 146 | * @param call IPC call structure
|
---|
| 147 | * @param arg Local argument
|
---|
[4e1a2f5] | 148 | *
|
---|
| 149 | */
|
---|
| 150 | static void notification_received(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 151 | {
|
---|
| 152 | /*
|
---|
| 153 | * Make sure we process only a single notification
|
---|
| 154 | * at any time to limit the chance of the consumer
|
---|
| 155 | * starving.
|
---|
| 156 | *
|
---|
| 157 | * Note: Usually the automatic masking of the klog
|
---|
| 158 | * notifications on the kernel side does the trick
|
---|
| 159 | * of limiting the chance of accidentally copying
|
---|
| 160 | * the same data multiple times. However, due to
|
---|
| 161 | * the non-blocking architecture of klog notifications,
|
---|
| 162 | * this possibility cannot be generally avoided.
|
---|
| 163 | */
|
---|
| 164 |
|
---|
| 165 | fibril_mutex_lock(&mtx);
|
---|
| 166 |
|
---|
| 167 | size_t klog_start = (size_t) IPC_GET_ARG1(*call);
|
---|
| 168 | size_t klog_len = (size_t) IPC_GET_ARG2(*call);
|
---|
| 169 | size_t klog_stored = (size_t) IPC_GET_ARG3(*call);
|
---|
| 170 |
|
---|
| 171 | size_t offset = (klog_start + klog_len - klog_stored) % klog_length;
|
---|
| 172 |
|
---|
| 173 | /* Copy data from the ring buffer */
|
---|
| 174 | if (offset + klog_stored >= klog_length) {
|
---|
| 175 | size_t split = klog_length - offset;
|
---|
| 176 |
|
---|
| 177 | producer(split, klog + offset);
|
---|
| 178 | producer(klog_stored - split, klog);
|
---|
| 179 | } else
|
---|
| 180 | producer(klog_stored, klog + offset);
|
---|
[f9061b4] | 181 |
|
---|
| 182 | event_unmask(EVENT_KLOG);
|
---|
[4e1a2f5] | 183 | fibril_mutex_unlock(&mtx);
|
---|
[51dbadf3] | 184 | }
|
---|
| 185 |
|
---|
| 186 | int main(int argc, char *argv[])
|
---|
| 187 | {
|
---|
[6119f24] | 188 | size_t pages;
|
---|
| 189 | int rc = sysinfo_get_value("klog.pages", &pages);
|
---|
| 190 | if (rc != EOK) {
|
---|
| 191 | fprintf(stderr, "%s: Unable to get number of klog pages\n",
|
---|
| 192 | NAME);
|
---|
| 193 | return rc;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | uintptr_t faddr;
|
---|
| 197 | rc = sysinfo_get_value("klog.faddr", &faddr);
|
---|
| 198 | if (rc != EOK) {
|
---|
| 199 | fprintf(stderr, "%s: Unable to get klog physical address\n",
|
---|
| 200 | NAME);
|
---|
| 201 | return rc;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | size_t size = pages * PAGE_SIZE;
|
---|
| 205 | klog_length = size / sizeof(wchar_t);
|
---|
| 206 |
|
---|
[fbcdeb8] | 207 | rc = physmem_map((void *) faddr, pages,
|
---|
| 208 | AS_AREA_READ | AS_AREA_CACHEABLE, (void *) &klog);
|
---|
[6119f24] | 209 | if (rc != EOK) {
|
---|
| 210 | fprintf(stderr, "%s: Unable to map klog\n", NAME);
|
---|
| 211 | return rc;
|
---|
[51dbadf3] | 212 | }
|
---|
[3ad953c] | 213 |
|
---|
[4e1a2f5] | 214 | prodcons_initialize(&pc);
|
---|
| 215 | async_set_interrupt_received(notification_received);
|
---|
[6119f24] | 216 | rc = event_subscribe(EVENT_KLOG, 0);
|
---|
| 217 | if (rc != EOK) {
|
---|
| 218 | fprintf(stderr, "%s: Unable to register klog notifications\n",
|
---|
| 219 | NAME);
|
---|
| 220 | return rc;
|
---|
[05641a9e] | 221 | }
|
---|
[1c03c17] | 222 |
|
---|
[4e1a2f5] | 223 | fid_t fid = fibril_create(consumer, NULL);
|
---|
| 224 | if (!fid) {
|
---|
| 225 | fprintf(stderr, "%s: Unable to create consumer fibril\n",
|
---|
| 226 | NAME);
|
---|
| 227 | return ENOMEM;
|
---|
| 228 | }
|
---|
[5a9f4d7] | 229 |
|
---|
[4e1a2f5] | 230 | fibril_add_ready(fid);
|
---|
| 231 | event_unmask(EVENT_KLOG);
|
---|
[1c03c17] | 232 | klog_update();
|
---|
[4e1a2f5] | 233 |
|
---|
| 234 | task_retval(0);
|
---|
[51dbadf3] | 235 | async_manager();
|
---|
[3636964] | 236 |
|
---|
[51dbadf3] | 237 | return 0;
|
---|
| 238 | }
|
---|
[b2951e2] | 239 |
|
---|
| 240 | /** @}
|
---|
| 241 | */
|
---|