blob: c9fe35230d60a2ec55548fada28d0dcc7555c52b [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
Simon Glass1ab16922022-07-31 12:28:48 -06009#include <display_options.h>
Simon Glassd95645d2017-12-04 13:48:24 -070010#include <log.h>
11#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glass4f0fdb72017-12-28 13:14:16 -070013#include <dm/uclass.h>
Simon Glassd95645d2017-12-04 13:48:24 -070014
15DECLARE_GLOBAL_DATA_PTR;
16
Sean Anderson3215fbb2020-10-27 19:55:22 -040017static const char *const log_cat_name[] = {
Simon Glass4f0fdb72017-12-28 13:14:16 -070018 "none",
19 "arch",
20 "board",
21 "core",
22 "driver-model",
23 "device-tree",
Heinrich Schuchardt90979f92018-01-22 20:10:45 +010024 "efi",
Simon Glass31120e12020-09-27 18:46:13 -060025 "alloc",
26 "sandbox",
27 "bloblist",
28 "devres",
29 "acpi",
Simon Glass842ff442020-11-04 09:59:13 -070030 "boot",
Simon Glassaa4bce92022-03-04 08:43:00 -070031 "event",
Simon Glass468247d2023-01-28 15:00:16 -070032 "fs",
Simon Glassfe4c1e22023-06-01 10:22:43 -060033 "expo",
Simon Glass5e392f12024-09-01 16:26:18 -060034 "console",
Simon Glass563eb3d2024-10-28 13:47:52 +010035 "test",
Simon Glass4f0fdb72017-12-28 13:14:16 -070036};
37
Heinrich Schuchardt7e7041d2020-10-23 13:00:01 +020038_Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE,
39 "log_cat_name size");
40
Sean Anderson3215fbb2020-10-27 19:55:22 -040041static const char *const log_level_name[] = {
Simon Glass4f0fdb72017-12-28 13:14:16 -070042 "EMERG",
43 "ALERT",
44 "CRIT",
45 "ERR",
46 "WARNING",
47 "NOTICE",
48 "INFO",
49 "DEBUG",
50 "CONTENT",
51 "IO",
52};
53
Heinrich Schuchardt7e7041d2020-10-23 13:00:01 +020054_Static_assert(ARRAY_SIZE(log_level_name) == LOGL_COUNT, "log_level_name size");
55
56/* All error responses MUST begin with '<' */
Simon Glass4f0fdb72017-12-28 13:14:16 -070057const char *log_get_cat_name(enum log_category_t cat)
58{
Simon Glass89096562018-06-12 00:04:55 -060059 const char *name;
60
61 if (cat < 0 || cat >= LOGC_COUNT)
62 return "<invalid>";
Simon Glass4f0fdb72017-12-28 13:14:16 -070063 if (cat >= LOGC_NONE)
64 return log_cat_name[cat - LOGC_NONE];
65
Heinrich Schuchardt60075ea2020-06-08 18:04:22 +020066#if CONFIG_IS_ENABLED(DM)
Simon Glass89096562018-06-12 00:04:55 -060067 name = uclass_get_name((enum uclass_id)cat);
Heinrich Schuchardt60075ea2020-06-08 18:04:22 +020068#else
69 name = NULL;
70#endif
Simon Glass89096562018-06-12 00:04:55 -060071
72 return name ? name : "<missing>";
Simon Glass4f0fdb72017-12-28 13:14:16 -070073}
74
75enum log_category_t log_get_cat_by_name(const char *name)
76{
77 enum uclass_id id;
78 int i;
79
80 for (i = LOGC_NONE; i < LOGC_COUNT; i++)
81 if (!strcmp(name, log_cat_name[i - LOGC_NONE]))
82 return i;
83 id = uclass_get_by_name(name);
84 if (id != UCLASS_INVALID)
85 return (enum log_category_t)id;
86
87 return LOGC_NONE;
88}
89
90const char *log_get_level_name(enum log_level_t level)
91{
92 if (level >= LOGL_COUNT)
93 return "INVALID";
94 return log_level_name[level];
95}
96
97enum log_level_t log_get_level_by_name(const char *name)
98{
99 int i;
100
101 for (i = 0; i < LOGL_COUNT; i++) {
102 if (!strcasecmp(log_level_name[i], name))
103 return i;
104 }
105
106 return LOGL_NONE;
107}
108
Sean Andersoncc4960d2020-10-27 19:55:24 -0400109struct log_device *log_device_find_by_name(const char *drv_name)
Simon Glassd95645d2017-12-04 13:48:24 -0700110{
111 struct log_device *ldev;
112
113 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
114 if (!strcmp(drv_name, ldev->drv->name))
115 return ldev;
116 }
117
118 return NULL;
119}
120
Sean Andersoncc4960d2020-10-27 19:55:24 -0400121bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
Simon Glassd95645d2017-12-04 13:48:24 -0700122{
123 int i;
124
125 for (i = 0; i < LOGF_MAX_CATEGORIES && cat_list[i] != LOGC_END; i++) {
126 if (cat_list[i] == cat)
127 return true;
128 }
129
130 return false;
131}
132
Sean Andersoncc4960d2020-10-27 19:55:24 -0400133bool log_has_file(const char *file_list, const char *file)
Simon Glassd95645d2017-12-04 13:48:24 -0700134{
135 int file_len = strlen(file);
136 const char *s, *p;
137 int substr_len;
138
139 for (s = file_list; *s; s = p + (*p != '\0')) {
140 p = strchrnul(s, ',');
141 substr_len = p - s;
142 if (file_len >= substr_len &&
143 !strncmp(file + file_len - substr_len, s, substr_len))
144 return true;
145 }
146
147 return false;
148}
149
150/**
151 * log_passes_filters() - check if a log record passes the filters for a device
152 *
153 * @ldev: Log device to check
154 * @rec: Log record to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100155 * Return: true if @rec is not blocked by the filters in @ldev, false if it is
Simon Glassd95645d2017-12-04 13:48:24 -0700156 */
157static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec)
158{
159 struct log_filter *filt;
160
Simon Glassf0803432021-01-20 20:10:52 -0700161 if (rec->flags & LOGRECF_FORCE_DEBUG)
Simon Glass9c83cac2020-09-12 11:13:34 -0600162 return true;
163
Simon Glassd95645d2017-12-04 13:48:24 -0700164 /* If there are no filters, filter on the default log level */
165 if (list_empty(&ldev->filter_head)) {
166 if (rec->level > gd->default_log_level)
167 return false;
168 return true;
169 }
170
171 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
Sean Anderson34dc8042020-10-27 19:55:30 -0400172 if (filt->flags & LOGFF_LEVEL_MIN) {
173 if (rec->level < filt->level)
174 continue;
175 } else if (rec->level > filt->level) {
Simon Glassd95645d2017-12-04 13:48:24 -0700176 continue;
Sean Anderson34dc8042020-10-27 19:55:30 -0400177 }
178
Simon Glassd95645d2017-12-04 13:48:24 -0700179 if ((filt->flags & LOGFF_HAS_CAT) &&
180 !log_has_cat(filt->cat_list, rec->cat))
181 continue;
Sean Anderson34dc8042020-10-27 19:55:30 -0400182
Simon Glassd95645d2017-12-04 13:48:24 -0700183 if (filt->file_list &&
184 !log_has_file(filt->file_list, rec->file))
185 continue;
Sean Andersond5032122020-10-27 19:55:26 -0400186
187 if (filt->flags & LOGFF_DENY)
188 return false;
189 else
190 return true;
Simon Glassd95645d2017-12-04 13:48:24 -0700191 }
192
193 return false;
194}
195
196/**
197 * log_dispatch() - Send a log record to all log devices for processing
198 *
199 * The log record is sent to each log device in turn, skipping those which have
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200200 * filters which block the record.
Simon Glassd95645d2017-12-04 13:48:24 -0700201 *
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200202 * All log messages created while processing log record @rec are ignored.
203 *
204 * @rec: log record to dispatch
205 * Return: 0 msg sent, 1 msg not sent while already dispatching another msg
Simon Glassd95645d2017-12-04 13:48:24 -0700206 */
Patrick Delaunay03593f32020-11-27 11:20:59 +0100207static int log_dispatch(struct log_rec *rec, const char *fmt, va_list args)
Simon Glassd95645d2017-12-04 13:48:24 -0700208{
209 struct log_device *ldev;
Patrick Delaunay03593f32020-11-27 11:20:59 +0100210 char buf[CONFIG_SYS_CBSIZE];
Simon Glassd95645d2017-12-04 13:48:24 -0700211
Heinrich Schuchardtb5ecfca2020-09-14 10:01:00 +0200212 /*
213 * When a log driver writes messages (e.g. via the network stack) this
214 * may result in further generated messages. We cannot process them here
215 * as this might result in infinite recursion.
216 */
Heinrich Schuchardtfdf55992020-10-17 14:31:57 +0200217 if (gd->processing_msg)
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200218 return 1;
Heinrich Schuchardtb5ecfca2020-09-14 10:01:00 +0200219
220 /* Emit message */
Heinrich Schuchardtfdf55992020-10-17 14:31:57 +0200221 gd->processing_msg = true;
Simon Glassd95645d2017-12-04 13:48:24 -0700222 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
Simon Glass885cf5f2020-09-12 12:28:47 -0600223 if ((ldev->flags & LOGDF_ENABLE) &&
Patrick Delaunay03593f32020-11-27 11:20:59 +0100224 log_passes_filters(ldev, rec)) {
225 if (!rec->msg) {
Simon Glass5fc47e32021-01-20 20:10:53 -0700226 int len;
227
228 len = vsnprintf(buf, sizeof(buf), fmt, args);
Patrick Delaunay03593f32020-11-27 11:20:59 +0100229 rec->msg = buf;
Simon Glass5fc47e32021-01-20 20:10:53 -0700230 gd->log_cont = len && buf[len - 1] != '\n';
Patrick Delaunay03593f32020-11-27 11:20:59 +0100231 }
Simon Glassd95645d2017-12-04 13:48:24 -0700232 ldev->drv->emit(ldev, rec);
Patrick Delaunay03593f32020-11-27 11:20:59 +0100233 }
Simon Glassd95645d2017-12-04 13:48:24 -0700234 }
Heinrich Schuchardtfdf55992020-10-17 14:31:57 +0200235 gd->processing_msg = false;
Simon Glassd95645d2017-12-04 13:48:24 -0700236 return 0;
237}
238
239int _log(enum log_category_t cat, enum log_level_t level, const char *file,
240 int line, const char *func, const char *fmt, ...)
241{
Simon Glassd95645d2017-12-04 13:48:24 -0700242 struct log_rec rec;
243 va_list args;
244
Patrick Delaunayfd70f9a2020-11-27 11:20:52 +0100245 if (!gd)
246 return -ENOSYS;
247
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200248 /* Check for message continuation */
249 if (cat == LOGC_CONT)
250 cat = gd->logc_prev;
251 if (level == LOGL_CONT)
252 level = gd->logl_prev;
253
Simon Glassd95645d2017-12-04 13:48:24 -0700254 rec.cat = cat;
Simon Glass9c83cac2020-09-12 11:13:34 -0600255 rec.level = level & LOGL_LEVEL_MASK;
Simon Glassf0803432021-01-20 20:10:52 -0700256 rec.flags = 0;
257 if (level & LOGL_FORCE_DEBUG)
258 rec.flags |= LOGRECF_FORCE_DEBUG;
Simon Glass5fc47e32021-01-20 20:10:53 -0700259 if (gd->log_cont)
260 rec.flags |= LOGRECF_CONT;
Simon Glassd95645d2017-12-04 13:48:24 -0700261 rec.file = file;
262 rec.line = line;
263 rec.func = func;
Patrick Delaunay03593f32020-11-27 11:20:59 +0100264 rec.msg = NULL;
Patrick Delaunayfd70f9a2020-11-27 11:20:52 +0100265
266 if (!(gd->flags & GD_FLG_LOG_READY)) {
267 gd->log_drop_count++;
Patrick Delaunay9d36aca2020-11-27 11:20:54 +0100268
269 /* display dropped traces with console puts and DEBUG_UART */
Simon Glassf0803432021-01-20 20:10:52 -0700270 if (rec.level <= CONFIG_LOG_DEFAULT_LEVEL ||
271 rec.flags & LOGRECF_FORCE_DEBUG) {
Patrick Delaunay03593f32020-11-27 11:20:59 +0100272 char buf[CONFIG_SYS_CBSIZE];
273
Patrick Delaunay9d36aca2020-11-27 11:20:54 +0100274 va_start(args, fmt);
275 vsnprintf(buf, sizeof(buf), fmt, args);
276 puts(buf);
277 va_end(args);
278 }
279
Patrick Delaunayfd70f9a2020-11-27 11:20:52 +0100280 return -ENOSYS;
281 }
Simon Glassd95645d2017-12-04 13:48:24 -0700282 va_start(args, fmt);
Patrick Delaunay03593f32020-11-27 11:20:59 +0100283 if (!log_dispatch(&rec, fmt, args)) {
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200284 gd->logc_prev = cat;
285 gd->logl_prev = level;
286 }
Patrick Delaunay03593f32020-11-27 11:20:59 +0100287 va_end(args);
Simon Glassd95645d2017-12-04 13:48:24 -0700288
289 return 0;
290}
291
Simon Glass7fb97592021-05-08 07:00:06 -0600292#define MAX_LINE_LENGTH_BYTES 64
293#define DEFAULT_LINE_LENGTH_BYTES 16
294
295int _log_buffer(enum log_category_t cat, enum log_level_t level,
296 const char *file, int line, const char *func, ulong addr,
297 const void *data, uint width, uint count, uint linelen)
298{
299 if (linelen * width > MAX_LINE_LENGTH_BYTES)
300 linelen = MAX_LINE_LENGTH_BYTES / width;
301 if (linelen < 1)
302 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
303
304 while (count) {
305 uint thislinelen;
306 char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)];
307
308 thislinelen = hexdump_line(addr, data, width, count, linelen,
309 buf, sizeof(buf));
310 assert(thislinelen >= 0);
311 _log(cat, level, file, line, func, "%s\n", buf);
312
313 /* update references */
314 data += thislinelen * width;
315 addr += thislinelen * width;
316 count -= thislinelen;
317 }
318
319 return 0;
320}
321
Sean Anderson8c410182020-10-27 19:55:25 -0400322int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
Sean Anderson34dc8042020-10-27 19:55:30 -0400323 enum log_level_t level, const char *file_list,
Sean Anderson8c410182020-10-27 19:55:25 -0400324 int flags)
Simon Glassd95645d2017-12-04 13:48:24 -0700325{
326 struct log_filter *filt;
327 struct log_device *ldev;
Simon Glassf14f3932018-04-02 02:42:39 -0600328 int ret;
Simon Glassd95645d2017-12-04 13:48:24 -0700329 int i;
330
331 ldev = log_device_find_by_name(drv_name);
332 if (!ldev)
333 return -ENOENT;
Heinrich Schuchardtbe9ab322020-04-19 10:48:36 +0200334 filt = calloc(1, sizeof(*filt));
Simon Glassd95645d2017-12-04 13:48:24 -0700335 if (!filt)
336 return -ENOMEM;
337
Sean Anderson8c410182020-10-27 19:55:25 -0400338 filt->flags = flags;
Simon Glassd95645d2017-12-04 13:48:24 -0700339 if (cat_list) {
340 filt->flags |= LOGFF_HAS_CAT;
341 for (i = 0; ; i++) {
Simon Glassf14f3932018-04-02 02:42:39 -0600342 if (i == ARRAY_SIZE(filt->cat_list)) {
343 ret = -ENOSPC;
344 goto err;
345 }
Simon Glassd95645d2017-12-04 13:48:24 -0700346 filt->cat_list[i] = cat_list[i];
347 if (cat_list[i] == LOGC_END)
348 break;
349 }
350 }
Sean Anderson34dc8042020-10-27 19:55:30 -0400351 filt->level = level;
Simon Glassd95645d2017-12-04 13:48:24 -0700352 if (file_list) {
353 filt->file_list = strdup(file_list);
Simon Glassf14f3932018-04-02 02:42:39 -0600354 if (!filt->file_list) {
Sean Anderson638b65a2020-10-27 19:55:20 -0400355 ret = -ENOMEM;
Simon Glassf14f3932018-04-02 02:42:39 -0600356 goto err;
357 }
Simon Glassd95645d2017-12-04 13:48:24 -0700358 }
359 filt->filter_num = ldev->next_filter_num++;
Sean Andersond5032122020-10-27 19:55:26 -0400360 /* Add deny filters to the beginning of the list */
361 if (flags & LOGFF_DENY)
362 list_add(&filt->sibling_node, &ldev->filter_head);
363 else
364 list_add_tail(&filt->sibling_node, &ldev->filter_head);
Simon Glassd95645d2017-12-04 13:48:24 -0700365
366 return filt->filter_num;
367
Simon Glassf14f3932018-04-02 02:42:39 -0600368err:
Simon Glassd95645d2017-12-04 13:48:24 -0700369 free(filt);
Simon Glassf14f3932018-04-02 02:42:39 -0600370 return ret;
Simon Glassd95645d2017-12-04 13:48:24 -0700371}
372
373int log_remove_filter(const char *drv_name, int filter_num)
374{
375 struct log_filter *filt;
376 struct log_device *ldev;
377
378 ldev = log_device_find_by_name(drv_name);
379 if (!ldev)
380 return -ENOENT;
381
382 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
383 if (filt->filter_num == filter_num) {
384 list_del(&filt->sibling_node);
385 free(filt);
386
387 return 0;
388 }
389 }
390
391 return -ENOENT;
392}
393
Simon Glass133350a2020-09-12 12:28:49 -0600394/**
395 * log_find_device_by_drv() - Find a device by its driver
396 *
397 * @drv: Log driver
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100398 * Return: Device associated with that driver, or NULL if not found
Simon Glass133350a2020-09-12 12:28:49 -0600399 */
400static struct log_device *log_find_device_by_drv(struct log_driver *drv)
401{
402 struct log_device *ldev;
403
404 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
405 if (ldev->drv == drv)
406 return ldev;
407 }
408 /*
409 * It is quite hard to pass an invalid driver since passing an unknown
410 * LOG_GET_DRIVER(xxx) would normally produce a compilation error. But
411 * it is possible to pass NULL, for example, so this
412 */
413
414 return NULL;
415}
416
417int log_device_set_enable(struct log_driver *drv, bool enable)
418{
419 struct log_device *ldev;
420
421 ldev = log_find_device_by_drv(drv);
422 if (!ldev)
423 return -ENOENT;
424 if (enable)
425 ldev->flags |= LOGDF_ENABLE;
426 else
427 ldev->flags &= ~LOGDF_ENABLE;
428
429 return 0;
430}
431
Thomas Weißschuh1bcd4882024-02-13 18:13:28 +0100432void log_fixup_for_gd_move(struct global_data *new_gd)
433{
434 new_gd->log_head.prev->next = &new_gd->log_head;
435}
436
Simon Glassd95645d2017-12-04 13:48:24 -0700437int log_init(void)
438{
439 struct log_driver *drv = ll_entry_start(struct log_driver, log_driver);
440 const int count = ll_entry_count(struct log_driver, log_driver);
441 struct log_driver *end = drv + count;
442
443 /*
444 * We cannot add runtime data to the driver since it is likely stored
445 * in rodata. Instead, set up a 'device' corresponding to each driver.
Simon Glass5cc6ae12023-07-15 21:38:49 -0600446 * We only support having a single device for each driver.
Simon Glassd95645d2017-12-04 13:48:24 -0700447 */
448 INIT_LIST_HEAD((struct list_head *)&gd->log_head);
449 while (drv < end) {
450 struct log_device *ldev;
451
452 ldev = calloc(1, sizeof(*ldev));
453 if (!ldev) {
454 debug("%s: Cannot allocate memory\n", __func__);
455 return -ENOMEM;
456 }
457 INIT_LIST_HEAD(&ldev->filter_head);
458 ldev->drv = drv;
Simon Glass885cf5f2020-09-12 12:28:47 -0600459 ldev->flags = drv->flags;
Simon Glassd95645d2017-12-04 13:48:24 -0700460 list_add_tail(&ldev->sibling_node,
461 (struct list_head *)&gd->log_head);
462 drv++;
463 }
Simon Glass55e32ba2017-12-04 13:48:28 -0700464 gd->flags |= GD_FLG_LOG_READY;
Simon Glass4e9a64d2018-10-01 11:55:11 -0600465 if (!gd->default_log_level)
Simon Glass38617e82019-02-16 20:24:35 -0700466 gd->default_log_level = CONFIG_LOG_DEFAULT_LEVEL;
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200467 gd->log_fmt = log_get_default_format();
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200468 gd->logc_prev = LOGC_NONE;
469 gd->logl_prev = LOGL_INFO;
Simon Glassd95645d2017-12-04 13:48:24 -0700470
471 return 0;
472}