blob: 57b71ed1b365c438f9f1625273abadbe77270076 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd95645d2017-12-04 13:48:24 -07002/*
3 * Logging support
4 *
5 * Copyright (c) 2017 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
Simon Glassd95645d2017-12-04 13:48:24 -07007 */
8
9#include <common.h>
Simon Glass1ab16922022-07-31 12:28:48 -060010#include <display_options.h>
Simon Glassd95645d2017-12-04 13:48:24 -070011#include <log.h>
12#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glass4f0fdb72017-12-28 13:14:16 -070014#include <dm/uclass.h>
Simon Glassd95645d2017-12-04 13:48:24 -070015
16DECLARE_GLOBAL_DATA_PTR;
17
Sean Anderson3215fbb2020-10-27 19:55:22 -040018static const char *const log_cat_name[] = {
Simon Glass4f0fdb72017-12-28 13:14:16 -070019 "none",
20 "arch",
21 "board",
22 "core",
23 "driver-model",
24 "device-tree",
Heinrich Schuchardt90979f92018-01-22 20:10:45 +010025 "efi",
Simon Glass31120e12020-09-27 18:46:13 -060026 "alloc",
27 "sandbox",
28 "bloblist",
29 "devres",
30 "acpi",
Simon Glass842ff442020-11-04 09:59:13 -070031 "boot",
Simon Glassaa4bce92022-03-04 08:43:00 -070032 "event",
Simon Glass4f0fdb72017-12-28 13:14:16 -070033};
34
Heinrich Schuchardt7e7041d2020-10-23 13:00:01 +020035_Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE,
36 "log_cat_name size");
37
Sean Anderson3215fbb2020-10-27 19:55:22 -040038static const char *const log_level_name[] = {
Simon Glass4f0fdb72017-12-28 13:14:16 -070039 "EMERG",
40 "ALERT",
41 "CRIT",
42 "ERR",
43 "WARNING",
44 "NOTICE",
45 "INFO",
46 "DEBUG",
47 "CONTENT",
48 "IO",
49};
50
Heinrich Schuchardt7e7041d2020-10-23 13:00:01 +020051_Static_assert(ARRAY_SIZE(log_level_name) == LOGL_COUNT, "log_level_name size");
52
53/* All error responses MUST begin with '<' */
Simon Glass4f0fdb72017-12-28 13:14:16 -070054const char *log_get_cat_name(enum log_category_t cat)
55{
Simon Glass89096562018-06-12 00:04:55 -060056 const char *name;
57
58 if (cat < 0 || cat >= LOGC_COUNT)
59 return "<invalid>";
Simon Glass4f0fdb72017-12-28 13:14:16 -070060 if (cat >= LOGC_NONE)
61 return log_cat_name[cat - LOGC_NONE];
62
Heinrich Schuchardt60075ea2020-06-08 18:04:22 +020063#if CONFIG_IS_ENABLED(DM)
Simon Glass89096562018-06-12 00:04:55 -060064 name = uclass_get_name((enum uclass_id)cat);
Heinrich Schuchardt60075ea2020-06-08 18:04:22 +020065#else
66 name = NULL;
67#endif
Simon Glass89096562018-06-12 00:04:55 -060068
69 return name ? name : "<missing>";
Simon Glass4f0fdb72017-12-28 13:14:16 -070070}
71
72enum log_category_t log_get_cat_by_name(const char *name)
73{
74 enum uclass_id id;
75 int i;
76
77 for (i = LOGC_NONE; i < LOGC_COUNT; i++)
78 if (!strcmp(name, log_cat_name[i - LOGC_NONE]))
79 return i;
80 id = uclass_get_by_name(name);
81 if (id != UCLASS_INVALID)
82 return (enum log_category_t)id;
83
84 return LOGC_NONE;
85}
86
87const char *log_get_level_name(enum log_level_t level)
88{
89 if (level >= LOGL_COUNT)
90 return "INVALID";
91 return log_level_name[level];
92}
93
94enum log_level_t log_get_level_by_name(const char *name)
95{
96 int i;
97
98 for (i = 0; i < LOGL_COUNT; i++) {
99 if (!strcasecmp(log_level_name[i], name))
100 return i;
101 }
102
103 return LOGL_NONE;
104}
105
Sean Andersoncc4960d2020-10-27 19:55:24 -0400106struct log_device *log_device_find_by_name(const char *drv_name)
Simon Glassd95645d2017-12-04 13:48:24 -0700107{
108 struct log_device *ldev;
109
110 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
111 if (!strcmp(drv_name, ldev->drv->name))
112 return ldev;
113 }
114
115 return NULL;
116}
117
Sean Andersoncc4960d2020-10-27 19:55:24 -0400118bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
Simon Glassd95645d2017-12-04 13:48:24 -0700119{
120 int i;
121
122 for (i = 0; i < LOGF_MAX_CATEGORIES && cat_list[i] != LOGC_END; i++) {
123 if (cat_list[i] == cat)
124 return true;
125 }
126
127 return false;
128}
129
Sean Andersoncc4960d2020-10-27 19:55:24 -0400130bool log_has_file(const char *file_list, const char *file)
Simon Glassd95645d2017-12-04 13:48:24 -0700131{
132 int file_len = strlen(file);
133 const char *s, *p;
134 int substr_len;
135
136 for (s = file_list; *s; s = p + (*p != '\0')) {
137 p = strchrnul(s, ',');
138 substr_len = p - s;
139 if (file_len >= substr_len &&
140 !strncmp(file + file_len - substr_len, s, substr_len))
141 return true;
142 }
143
144 return false;
145}
146
147/**
148 * log_passes_filters() - check if a log record passes the filters for a device
149 *
150 * @ldev: Log device to check
151 * @rec: Log record to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100152 * Return: true if @rec is not blocked by the filters in @ldev, false if it is
Simon Glassd95645d2017-12-04 13:48:24 -0700153 */
154static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec)
155{
156 struct log_filter *filt;
157
Simon Glassf0803432021-01-20 20:10:52 -0700158 if (rec->flags & LOGRECF_FORCE_DEBUG)
Simon Glass9c83cac2020-09-12 11:13:34 -0600159 return true;
160
Simon Glassd95645d2017-12-04 13:48:24 -0700161 /* If there are no filters, filter on the default log level */
162 if (list_empty(&ldev->filter_head)) {
163 if (rec->level > gd->default_log_level)
164 return false;
165 return true;
166 }
167
168 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
Sean Anderson34dc8042020-10-27 19:55:30 -0400169 if (filt->flags & LOGFF_LEVEL_MIN) {
170 if (rec->level < filt->level)
171 continue;
172 } else if (rec->level > filt->level) {
Simon Glassd95645d2017-12-04 13:48:24 -0700173 continue;
Sean Anderson34dc8042020-10-27 19:55:30 -0400174 }
175
Simon Glassd95645d2017-12-04 13:48:24 -0700176 if ((filt->flags & LOGFF_HAS_CAT) &&
177 !log_has_cat(filt->cat_list, rec->cat))
178 continue;
Sean Anderson34dc8042020-10-27 19:55:30 -0400179
Simon Glassd95645d2017-12-04 13:48:24 -0700180 if (filt->file_list &&
181 !log_has_file(filt->file_list, rec->file))
182 continue;
Sean Andersond5032122020-10-27 19:55:26 -0400183
184 if (filt->flags & LOGFF_DENY)
185 return false;
186 else
187 return true;
Simon Glassd95645d2017-12-04 13:48:24 -0700188 }
189
190 return false;
191}
192
193/**
194 * log_dispatch() - Send a log record to all log devices for processing
195 *
196 * The log record is sent to each log device in turn, skipping those which have
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200197 * filters which block the record.
Simon Glassd95645d2017-12-04 13:48:24 -0700198 *
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200199 * All log messages created while processing log record @rec are ignored.
200 *
201 * @rec: log record to dispatch
202 * Return: 0 msg sent, 1 msg not sent while already dispatching another msg
Simon Glassd95645d2017-12-04 13:48:24 -0700203 */
Patrick Delaunay03593f32020-11-27 11:20:59 +0100204static int log_dispatch(struct log_rec *rec, const char *fmt, va_list args)
Simon Glassd95645d2017-12-04 13:48:24 -0700205{
206 struct log_device *ldev;
Patrick Delaunay03593f32020-11-27 11:20:59 +0100207 char buf[CONFIG_SYS_CBSIZE];
Simon Glassd95645d2017-12-04 13:48:24 -0700208
Heinrich Schuchardtb5ecfca2020-09-14 10:01:00 +0200209 /*
210 * When a log driver writes messages (e.g. via the network stack) this
211 * may result in further generated messages. We cannot process them here
212 * as this might result in infinite recursion.
213 */
Heinrich Schuchardtfdf55992020-10-17 14:31:57 +0200214 if (gd->processing_msg)
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200215 return 1;
Heinrich Schuchardtb5ecfca2020-09-14 10:01:00 +0200216
217 /* Emit message */
Heinrich Schuchardtfdf55992020-10-17 14:31:57 +0200218 gd->processing_msg = true;
Simon Glassd95645d2017-12-04 13:48:24 -0700219 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
Simon Glass885cf5f2020-09-12 12:28:47 -0600220 if ((ldev->flags & LOGDF_ENABLE) &&
Patrick Delaunay03593f32020-11-27 11:20:59 +0100221 log_passes_filters(ldev, rec)) {
222 if (!rec->msg) {
Simon Glass5fc47e32021-01-20 20:10:53 -0700223 int len;
224
225 len = vsnprintf(buf, sizeof(buf), fmt, args);
Patrick Delaunay03593f32020-11-27 11:20:59 +0100226 rec->msg = buf;
Simon Glass5fc47e32021-01-20 20:10:53 -0700227 gd->log_cont = len && buf[len - 1] != '\n';
Patrick Delaunay03593f32020-11-27 11:20:59 +0100228 }
Simon Glassd95645d2017-12-04 13:48:24 -0700229 ldev->drv->emit(ldev, rec);
Patrick Delaunay03593f32020-11-27 11:20:59 +0100230 }
Simon Glassd95645d2017-12-04 13:48:24 -0700231 }
Heinrich Schuchardtfdf55992020-10-17 14:31:57 +0200232 gd->processing_msg = false;
Simon Glassd95645d2017-12-04 13:48:24 -0700233 return 0;
234}
235
236int _log(enum log_category_t cat, enum log_level_t level, const char *file,
237 int line, const char *func, const char *fmt, ...)
238{
Simon Glassd95645d2017-12-04 13:48:24 -0700239 struct log_rec rec;
240 va_list args;
241
Patrick Delaunayfd70f9a2020-11-27 11:20:52 +0100242 if (!gd)
243 return -ENOSYS;
244
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200245 /* Check for message continuation */
246 if (cat == LOGC_CONT)
247 cat = gd->logc_prev;
248 if (level == LOGL_CONT)
249 level = gd->logl_prev;
250
Simon Glassd95645d2017-12-04 13:48:24 -0700251 rec.cat = cat;
Simon Glass9c83cac2020-09-12 11:13:34 -0600252 rec.level = level & LOGL_LEVEL_MASK;
Simon Glassf0803432021-01-20 20:10:52 -0700253 rec.flags = 0;
254 if (level & LOGL_FORCE_DEBUG)
255 rec.flags |= LOGRECF_FORCE_DEBUG;
Simon Glass5fc47e32021-01-20 20:10:53 -0700256 if (gd->log_cont)
257 rec.flags |= LOGRECF_CONT;
Simon Glassd95645d2017-12-04 13:48:24 -0700258 rec.file = file;
259 rec.line = line;
260 rec.func = func;
Patrick Delaunay03593f32020-11-27 11:20:59 +0100261 rec.msg = NULL;
Patrick Delaunayfd70f9a2020-11-27 11:20:52 +0100262
263 if (!(gd->flags & GD_FLG_LOG_READY)) {
264 gd->log_drop_count++;
Patrick Delaunay9d36aca2020-11-27 11:20:54 +0100265
266 /* display dropped traces with console puts and DEBUG_UART */
Simon Glassf0803432021-01-20 20:10:52 -0700267 if (rec.level <= CONFIG_LOG_DEFAULT_LEVEL ||
268 rec.flags & LOGRECF_FORCE_DEBUG) {
Patrick Delaunay03593f32020-11-27 11:20:59 +0100269 char buf[CONFIG_SYS_CBSIZE];
270
Patrick Delaunay9d36aca2020-11-27 11:20:54 +0100271 va_start(args, fmt);
272 vsnprintf(buf, sizeof(buf), fmt, args);
273 puts(buf);
274 va_end(args);
275 }
276
Patrick Delaunayfd70f9a2020-11-27 11:20:52 +0100277 return -ENOSYS;
278 }
Simon Glassd95645d2017-12-04 13:48:24 -0700279 va_start(args, fmt);
Patrick Delaunay03593f32020-11-27 11:20:59 +0100280 if (!log_dispatch(&rec, fmt, args)) {
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200281 gd->logc_prev = cat;
282 gd->logl_prev = level;
283 }
Patrick Delaunay03593f32020-11-27 11:20:59 +0100284 va_end(args);
Simon Glassd95645d2017-12-04 13:48:24 -0700285
286 return 0;
287}
288
Simon Glass7fb97592021-05-08 07:00:06 -0600289#define MAX_LINE_LENGTH_BYTES 64
290#define DEFAULT_LINE_LENGTH_BYTES 16
291
292int _log_buffer(enum log_category_t cat, enum log_level_t level,
293 const char *file, int line, const char *func, ulong addr,
294 const void *data, uint width, uint count, uint linelen)
295{
296 if (linelen * width > MAX_LINE_LENGTH_BYTES)
297 linelen = MAX_LINE_LENGTH_BYTES / width;
298 if (linelen < 1)
299 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
300
301 while (count) {
302 uint thislinelen;
303 char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)];
304
305 thislinelen = hexdump_line(addr, data, width, count, linelen,
306 buf, sizeof(buf));
307 assert(thislinelen >= 0);
308 _log(cat, level, file, line, func, "%s\n", buf);
309
310 /* update references */
311 data += thislinelen * width;
312 addr += thislinelen * width;
313 count -= thislinelen;
314 }
315
316 return 0;
317}
318
Sean Anderson8c410182020-10-27 19:55:25 -0400319int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
Sean Anderson34dc8042020-10-27 19:55:30 -0400320 enum log_level_t level, const char *file_list,
Sean Anderson8c410182020-10-27 19:55:25 -0400321 int flags)
Simon Glassd95645d2017-12-04 13:48:24 -0700322{
323 struct log_filter *filt;
324 struct log_device *ldev;
Simon Glassf14f3932018-04-02 02:42:39 -0600325 int ret;
Simon Glassd95645d2017-12-04 13:48:24 -0700326 int i;
327
328 ldev = log_device_find_by_name(drv_name);
329 if (!ldev)
330 return -ENOENT;
Heinrich Schuchardtbe9ab322020-04-19 10:48:36 +0200331 filt = calloc(1, sizeof(*filt));
Simon Glassd95645d2017-12-04 13:48:24 -0700332 if (!filt)
333 return -ENOMEM;
334
Sean Anderson8c410182020-10-27 19:55:25 -0400335 filt->flags = flags;
Simon Glassd95645d2017-12-04 13:48:24 -0700336 if (cat_list) {
337 filt->flags |= LOGFF_HAS_CAT;
338 for (i = 0; ; i++) {
Simon Glassf14f3932018-04-02 02:42:39 -0600339 if (i == ARRAY_SIZE(filt->cat_list)) {
340 ret = -ENOSPC;
341 goto err;
342 }
Simon Glassd95645d2017-12-04 13:48:24 -0700343 filt->cat_list[i] = cat_list[i];
344 if (cat_list[i] == LOGC_END)
345 break;
346 }
347 }
Sean Anderson34dc8042020-10-27 19:55:30 -0400348 filt->level = level;
Simon Glassd95645d2017-12-04 13:48:24 -0700349 if (file_list) {
350 filt->file_list = strdup(file_list);
Simon Glassf14f3932018-04-02 02:42:39 -0600351 if (!filt->file_list) {
Sean Anderson638b65a2020-10-27 19:55:20 -0400352 ret = -ENOMEM;
Simon Glassf14f3932018-04-02 02:42:39 -0600353 goto err;
354 }
Simon Glassd95645d2017-12-04 13:48:24 -0700355 }
356 filt->filter_num = ldev->next_filter_num++;
Sean Andersond5032122020-10-27 19:55:26 -0400357 /* Add deny filters to the beginning of the list */
358 if (flags & LOGFF_DENY)
359 list_add(&filt->sibling_node, &ldev->filter_head);
360 else
361 list_add_tail(&filt->sibling_node, &ldev->filter_head);
Simon Glassd95645d2017-12-04 13:48:24 -0700362
363 return filt->filter_num;
364
Simon Glassf14f3932018-04-02 02:42:39 -0600365err:
Simon Glassd95645d2017-12-04 13:48:24 -0700366 free(filt);
Simon Glassf14f3932018-04-02 02:42:39 -0600367 return ret;
Simon Glassd95645d2017-12-04 13:48:24 -0700368}
369
370int log_remove_filter(const char *drv_name, int filter_num)
371{
372 struct log_filter *filt;
373 struct log_device *ldev;
374
375 ldev = log_device_find_by_name(drv_name);
376 if (!ldev)
377 return -ENOENT;
378
379 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
380 if (filt->filter_num == filter_num) {
381 list_del(&filt->sibling_node);
382 free(filt);
383
384 return 0;
385 }
386 }
387
388 return -ENOENT;
389}
390
Simon Glass133350a2020-09-12 12:28:49 -0600391/**
392 * log_find_device_by_drv() - Find a device by its driver
393 *
394 * @drv: Log driver
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100395 * Return: Device associated with that driver, or NULL if not found
Simon Glass133350a2020-09-12 12:28:49 -0600396 */
397static struct log_device *log_find_device_by_drv(struct log_driver *drv)
398{
399 struct log_device *ldev;
400
401 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
402 if (ldev->drv == drv)
403 return ldev;
404 }
405 /*
406 * It is quite hard to pass an invalid driver since passing an unknown
407 * LOG_GET_DRIVER(xxx) would normally produce a compilation error. But
408 * it is possible to pass NULL, for example, so this
409 */
410
411 return NULL;
412}
413
414int log_device_set_enable(struct log_driver *drv, bool enable)
415{
416 struct log_device *ldev;
417
418 ldev = log_find_device_by_drv(drv);
419 if (!ldev)
420 return -ENOENT;
421 if (enable)
422 ldev->flags |= LOGDF_ENABLE;
423 else
424 ldev->flags &= ~LOGDF_ENABLE;
425
426 return 0;
427}
428
Simon Glassd95645d2017-12-04 13:48:24 -0700429int log_init(void)
430{
431 struct log_driver *drv = ll_entry_start(struct log_driver, log_driver);
432 const int count = ll_entry_count(struct log_driver, log_driver);
433 struct log_driver *end = drv + count;
434
435 /*
436 * We cannot add runtime data to the driver since it is likely stored
437 * in rodata. Instead, set up a 'device' corresponding to each driver.
438 * We only support having a single device.
439 */
440 INIT_LIST_HEAD((struct list_head *)&gd->log_head);
441 while (drv < end) {
442 struct log_device *ldev;
443
444 ldev = calloc(1, sizeof(*ldev));
445 if (!ldev) {
446 debug("%s: Cannot allocate memory\n", __func__);
447 return -ENOMEM;
448 }
449 INIT_LIST_HEAD(&ldev->filter_head);
450 ldev->drv = drv;
Simon Glass885cf5f2020-09-12 12:28:47 -0600451 ldev->flags = drv->flags;
Simon Glassd95645d2017-12-04 13:48:24 -0700452 list_add_tail(&ldev->sibling_node,
453 (struct list_head *)&gd->log_head);
454 drv++;
455 }
Simon Glass55e32ba2017-12-04 13:48:28 -0700456 gd->flags |= GD_FLG_LOG_READY;
Simon Glass4e9a64d2018-10-01 11:55:11 -0600457 if (!gd->default_log_level)
Simon Glass38617e82019-02-16 20:24:35 -0700458 gd->default_log_level = CONFIG_LOG_DEFAULT_LEVEL;
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200459 gd->log_fmt = log_get_default_format();
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200460 gd->logc_prev = LOGC_NONE;
461 gd->logl_prev = LOGL_INFO;
Simon Glassd95645d2017-12-04 13:48:24 -0700462
463 return 0;
464}