log.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) 2017-2019 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <assert.h>
  18. #include <errno.h>
  19. #include <limits.h>
  20. #include <stdarg.h>
  21. #include <stdbool.h>
  22. #include <stddef.h>
  23. #include <stdio.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <kern/arg.h>
  27. #include <kern/bulletin.h>
  28. #include <kern/init.h>
  29. #include <kern/log.h>
  30. #include <kern/macros.h>
  31. #include <kern/mbuf.h>
  32. #include <kern/panic.h>
  33. #include <kern/shell.h>
  34. #include <kern/spinlock.h>
  35. #include <kern/thread.h>
  36. #include <machine/boot.h>
  37. #include <machine/cpu.h>
  38. #define LOG_BUFFER_SIZE 16384
  39. #if !ISP2 (LOG_BUFFER_SIZE)
  40. #error "log buffer size must be a power-of-two"
  41. #endif
  42. #define LOG_MSG_SIZE 128
  43. #define LOG_PRINT_LEVEL LOG_INFO
  44. static struct thread *log_thread;
  45. static struct mbuf log_mbuf;
  46. static char log_buffer[LOG_BUFFER_SIZE];
  47. static unsigned int log_nr_overruns;
  48. static struct bulletin log_bulletin;
  49. /*
  50. * Global lock.
  51. *
  52. * Interrupts must be disabled when holding this lock.
  53. */
  54. static struct spinlock log_lock;
  55. struct log_record
  56. {
  57. uint8_t level;
  58. char msg[LOG_MSG_SIZE];
  59. };
  60. struct log_consumer
  61. {
  62. struct mbuf *mbuf;
  63. size_t index;
  64. };
  65. static void
  66. log_consumer_init (struct log_consumer *ctx, struct mbuf *mbuf)
  67. {
  68. ctx->mbuf = mbuf;
  69. ctx->index = mbuf_start (mbuf);
  70. }
  71. static int
  72. log_consumer_pop (struct log_consumer *ctx, struct log_record *record)
  73. {
  74. while (1)
  75. {
  76. size_t size = sizeof (*record);
  77. int error = mbuf_read (ctx->mbuf, &ctx->index, record, &size);
  78. if (error != EINVAL)
  79. return (error);
  80. ctx->index = mbuf_start (ctx->mbuf);
  81. }
  82. }
  83. static const char*
  84. log_level2str (unsigned int level)
  85. {
  86. switch (level)
  87. {
  88. case LOG_EMERG:
  89. return ("emerg");
  90. case LOG_ALERT:
  91. return ("alert");
  92. case LOG_CRIT:
  93. return ("crit");
  94. case LOG_ERR:
  95. return ("error");
  96. case LOG_WARNING:
  97. return ("warning");
  98. case LOG_NOTICE:
  99. return ("notice");
  100. case LOG_INFO:
  101. return ("info");
  102. case LOG_DEBUG:
  103. return ("debug");
  104. default:
  105. return (NULL);
  106. }
  107. }
  108. static void
  109. log_print_record (const struct log_record *record, unsigned int level)
  110. {
  111. if (record->level > level)
  112. return;
  113. if (record->level <= LOG_WARNING)
  114. printf ("%7s %s\n", log_level2str (record->level), record->msg);
  115. else
  116. printf ("%s\n", record->msg);
  117. }
  118. static void
  119. log_run (void *arg __unused)
  120. {
  121. bool published = false;
  122. cpu_flags_t flags;
  123. spinlock_lock_intr_save (&log_lock, &flags);
  124. struct log_consumer ctx;
  125. log_consumer_init (&ctx, &log_mbuf);
  126. while (1)
  127. {
  128. struct log_record record;
  129. while (1)
  130. {
  131. int error = log_consumer_pop (&ctx, &record);
  132. if (! error)
  133. break;
  134. else if (log_nr_overruns)
  135. {
  136. record.level = LOG_ERR;
  137. snprintf (record.msg, sizeof (record.msg),
  138. "log: buffer overruns, %u messages dropped",
  139. log_nr_overruns);
  140. log_nr_overruns = 0;
  141. break;
  142. }
  143. if (! published)
  144. {
  145. spinlock_unlock_intr_restore (&log_lock, flags);
  146. bulletin_publish (&log_bulletin, 0);
  147. spinlock_lock_intr_save (&log_lock, &flags);
  148. published = true;
  149. }
  150. thread_sleep (&log_lock, &log_mbuf, "log_mbuf");
  151. }
  152. spinlock_unlock_intr_restore (&log_lock, flags);
  153. log_print_record (&record, LOG_PRINT_LEVEL);
  154. spinlock_lock_intr_save (&log_lock, &flags);
  155. }
  156. }
  157. #ifdef CONFIG_SHELL
  158. static void
  159. log_dump (unsigned int level)
  160. {
  161. cpu_flags_t flags;
  162. spinlock_lock_intr_save (&log_lock, &flags);
  163. struct log_consumer ctx;
  164. log_consumer_init (&ctx, &log_mbuf);
  165. while (1)
  166. {
  167. struct log_record record;
  168. int error = log_consumer_pop (&ctx, &record);
  169. if (error)
  170. break;
  171. spinlock_unlock_intr_restore (&log_lock, flags);
  172. log_print_record (&record, level);
  173. spinlock_lock_intr_save (&log_lock, &flags);
  174. }
  175. spinlock_unlock_intr_restore (&log_lock, flags);
  176. }
  177. static void
  178. log_shell_dump (struct shell *shell __unused, int argc, char **argv)
  179. {
  180. unsigned int level;
  181. if (argc != 2)
  182. level = LOG_PRINT_LEVEL;
  183. else
  184. {
  185. int ret = sscanf (argv[1], "%u", &level);
  186. if (ret != 1 || level >= LOG_NR_LEVELS)
  187. {
  188. printf ("log: dump: invalid arguments\n");
  189. return;
  190. }
  191. }
  192. log_dump (level);
  193. }
  194. static struct shell_cmd log_shell_cmds[] =
  195. {
  196. SHELL_CMD_INITIALIZER2 ("log_dump", log_shell_dump,
  197. "log_dump [<level>]",
  198. "dump the log buffer",
  199. "Only records of level less than or equal to the given level"
  200. " are printed. Level may be one of :\n"
  201. " 0: emergency\n"
  202. " 1: alert\n"
  203. " 2: critical\n"
  204. " 3: error\n"
  205. " 4: warning\n"
  206. " 5: notice\n"
  207. " 6: info\n"
  208. " 7: debug"),
  209. };
  210. static int __init
  211. log_setup_shell (void)
  212. {
  213. SHELL_REGISTER_CMDS (log_shell_cmds, shell_get_main_cmd_set ());
  214. return (0);
  215. }
  216. INIT_OP_DEFINE (log_setup_shell,
  217. INIT_OP_DEP (log_setup, true),
  218. INIT_OP_DEP (shell_setup, true));
  219. #endif // CONFIG_SHELL
  220. struct logger_stream
  221. {
  222. struct stream base;
  223. struct log_record record;
  224. struct spinlock lock;
  225. uint32_t off;
  226. };
  227. static size_t
  228. logger_stream_cap (const struct logger_stream *stream)
  229. {
  230. return (sizeof (stream->record.msg) - 1 - stream->off);
  231. }
  232. static int log_puts (const struct log_record *, int);
  233. static void
  234. logger_stream_write (struct stream *stream, const void *data, uint32_t bytes)
  235. {
  236. _Auto logstr = structof (stream, struct logger_stream, base);
  237. size_t cap = logger_stream_cap (logstr);
  238. const char *newl = memchr (data, '\n', bytes);
  239. if (bytes < cap && !newl)
  240. {
  241. memcpy (logstr->record.msg + logstr->off, data, bytes);
  242. logstr->off += bytes;
  243. return;
  244. }
  245. if (newl)
  246. bytes = newl - (const char *) data;
  247. bytes = MIN (bytes, cap);
  248. memcpy (logstr->record.msg + logstr->off, data, bytes);
  249. logstr->off += bytes;
  250. logstr->record.msg[logstr->off] = 0;
  251. log_puts (&logstr->record, (int) logstr->off);
  252. logstr->off = 0;
  253. }
  254. static void
  255. logger_stream_lock (struct stream *stream)
  256. {
  257. _Auto logstr = structof (stream, struct logger_stream, base);
  258. spinlock_lock (&logstr->lock);
  259. }
  260. static void
  261. logger_stream_unlock (struct stream *stream)
  262. {
  263. _Auto logstr = structof (stream, struct logger_stream, base);
  264. spinlock_unlock (&logstr->lock);
  265. }
  266. static const struct stream_ops logger_stream_ops =
  267. {
  268. .write = logger_stream_write,
  269. .lock = logger_stream_lock,
  270. .unlock = logger_stream_unlock
  271. };
  272. static struct logger_stream logger_streams[LOG_NR_LEVELS];
  273. static void
  274. logger_stream_init (struct logger_stream *stream)
  275. {
  276. stream_init (&stream->base, &logger_stream_ops);
  277. stream->record.level = stream - &logger_streams[0];
  278. }
  279. struct stream*
  280. log_stream (unsigned int level)
  281. {
  282. assert (level < ARRAY_SIZE (logger_streams));
  283. return (&logger_streams[level].base);
  284. }
  285. static int __init
  286. log_setup (void)
  287. {
  288. mbuf_init (&log_mbuf, log_buffer, sizeof (log_buffer),
  289. sizeof (struct log_record));
  290. spinlock_init (&log_lock);
  291. bulletin_init (&log_bulletin);
  292. for (size_t i = 0; i < ARRAY_SIZE (logger_streams); ++i)
  293. logger_stream_init (&logger_streams[i]);
  294. boot_log_info ();
  295. arg_log_info ();
  296. cpu_log_info (cpu_current ());
  297. return (0);
  298. }
  299. INIT_OP_DEFINE (log_setup,
  300. INIT_OP_DEP (arg_setup, true),
  301. INIT_OP_DEP (cpu_setup, true),
  302. INIT_OP_DEP (spinlock_setup, true));
  303. static int __init
  304. log_start (void)
  305. {
  306. struct thread_attr attr;
  307. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "log_run");
  308. thread_attr_set_detached (&attr);
  309. int error = thread_create (&log_thread, &attr, log_run, NULL);
  310. if (error)
  311. panic ("log: unable to create thread");
  312. return (0);
  313. }
  314. INIT_OP_DEFINE (log_start,
  315. INIT_OP_DEP (log_setup, true),
  316. INIT_OP_DEP (thread_setup, true));
  317. int
  318. log_msg (unsigned int level, const char *format, ...)
  319. {
  320. va_list ap;
  321. va_start (ap, format);
  322. int ret = log_vmsg (level, format, ap);
  323. va_end (ap);
  324. return (ret);
  325. }
  326. static int
  327. log_puts (const struct log_record *record, int nr_chars)
  328. {
  329. if ((unsigned int)nr_chars >= sizeof (record->msg))
  330. {
  331. log_msg (LOG_ERR, "log: message too large");
  332. goto out;
  333. }
  334. char *ptr = strchr (record->msg, '\n');
  335. if (ptr)
  336. {
  337. *ptr = '\0';
  338. nr_chars = ptr - record->msg;
  339. }
  340. assert (nr_chars >= 0);
  341. size_t size = offsetof (struct log_record, msg) + nr_chars + 1;
  342. cpu_flags_t flags;
  343. spinlock_lock_intr_save (&log_lock, &flags);
  344. int error = mbuf_push (&log_mbuf, record, size, true);
  345. if (error)
  346. log_nr_overruns++;
  347. thread_wakeup (log_thread);
  348. spinlock_unlock_intr_restore (&log_lock, flags);
  349. out:
  350. return (nr_chars);
  351. }
  352. int
  353. log_vmsg (unsigned int level, const char *format, va_list ap)
  354. {
  355. assert (level < LOG_NR_LEVELS);
  356. struct log_record record = { .level = level };
  357. int nr_chars = vsnprintf (record.msg, sizeof (record.msg), format, ap);
  358. return (log_puts (&record, nr_chars));
  359. }
  360. struct bulletin*
  361. log_get_bulletin (void)
  362. {
  363. return (&log_bulletin);
  364. }