blob: 519ec76f3b5100338991bee570616ec8f32dc00a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd5f61f22017-12-04 13:48:26 -07002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassd5f61f22017-12-04 13:48:26 -07005 */
6
Simon Glassd5f61f22017-12-04 13:48:26 -07007#include <command.h>
8#include <dm.h>
Sean Anderson3e409762020-10-27 19:55:38 -04009#include <getopt.h>
Simon Glassd5f61f22017-12-04 13:48:26 -070010#include <log.h>
Sean Anderson3e409762020-10-27 19:55:38 -040011#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glassd5f61f22017-12-04 13:48:26 -070013
Simon Glassad0e5032017-12-28 13:14:19 -070014static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
15
Sean Andersonfed9c2f2020-10-27 19:55:33 -040016static enum log_level_t parse_log_level(char *const arg)
17{
18 enum log_level_t ret;
19 ulong level;
20
21 if (!strict_strtoul(arg, 10, &level)) {
22 if (level > _LOG_MAX_LEVEL) {
23 printf("Only log levels <= %d are supported\n",
24 _LOG_MAX_LEVEL);
25 return LOGL_NONE;
26 }
27 return level;
28 }
29
30 ret = log_get_level_by_name(arg);
31 if (ret == LOGL_NONE)
32 printf("Unknown log level \"%s\"\n", arg);
33 return ret;
34}
35
Simon Glass09140112020-05-10 11:40:03 -060036static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
37 char *const argv[])
Simon Glassd5f61f22017-12-04 13:48:26 -070038{
Sean Anderson018aad82020-10-27 19:55:35 -040039 enum log_level_t log_level;
40
Heinrich Schuchardt77007f92020-05-31 13:44:51 +000041 if (argc > 1) {
Sean Anderson018aad82020-10-27 19:55:35 -040042 log_level = parse_log_level(argv[1]);
Heinrich Schuchardt77007f92020-05-31 13:44:51 +000043
Sean Andersonfed9c2f2020-10-27 19:55:33 -040044 if (log_level == LOGL_NONE)
Heinrich Schuchardt77007f92020-05-31 13:44:51 +000045 return CMD_RET_FAILURE;
Heinrich Schuchardt77007f92020-05-31 13:44:51 +000046 gd->default_log_level = log_level;
47 } else {
Sean Anderson018aad82020-10-27 19:55:35 -040048 for (log_level = LOGL_FIRST; log_level <= _LOG_MAX_LEVEL;
49 log_level++)
50 printf("%s%s\n", log_get_level_name(log_level),
51 log_level == gd->default_log_level ?
52 " (default)" : "");
Heinrich Schuchardt77007f92020-05-31 13:44:51 +000053 }
Simon Glassd5f61f22017-12-04 13:48:26 -070054
Sean Andersonfed9c2f2020-10-27 19:55:33 -040055 return CMD_RET_SUCCESS;
Simon Glassd5f61f22017-12-04 13:48:26 -070056}
57
Sean Anderson1c593a12020-10-27 19:55:34 -040058static int do_log_categories(struct cmd_tbl *cmdtp, int flag, int argc,
59 char *const argv[])
60{
61 enum log_category_t cat;
62 const char *name;
63
64 for (cat = LOGC_FIRST; cat < LOGC_COUNT; cat++) {
65 name = log_get_cat_name(cat);
66 /*
67 * Invalid category names (e.g. <invalid> or <missing>) begin
68 * with '<'.
69 */
70 if (name[0] == '<')
71 continue;
72 printf("%s\n", name);
73 }
74
75 return CMD_RET_SUCCESS;
76}
77
78static int do_log_drivers(struct cmd_tbl *cmdtp, int flag, int argc,
79 char *const argv[])
80{
81 struct log_device *ldev;
82
83 list_for_each_entry(ldev, &gd->log_head, sibling_node)
84 printf("%s\n", ldev->drv->name);
85
86 return CMD_RET_SUCCESS;
87}
88
Sean Anderson3e409762020-10-27 19:55:38 -040089static int do_log_filter_list(struct cmd_tbl *cmdtp, int flag, int argc,
90 char *const argv[])
91{
92 int opt;
93 const char *drv_name = "console";
94 struct getopt_state gs;
95 struct log_filter *filt;
96 struct log_device *ldev;
97
98 getopt_init_state(&gs);
99 while ((opt = getopt(&gs, argc, argv, "d:")) > 0) {
100 switch (opt) {
101 case 'd':
102 drv_name = gs.arg;
103 break;
104 default:
105 return CMD_RET_USAGE;
106 }
107 }
108
109 if (gs.index != argc)
110 return CMD_RET_USAGE;
111
112 ldev = log_device_find_by_name(drv_name);
113 if (!ldev) {
114 printf("Could not find log device for \"%s\"\n", drv_name);
115 return CMD_RET_FAILURE;
116 }
117
118 /* <3> < 6 > <2+1 + 7 > < 16 > < unbounded... */
119 printf("num policy level categories files\n");
120 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
121 printf("%3d %6.6s %s %-7.7s ", filt->filter_num,
122 filt->flags & LOGFF_DENY ? "deny" : "allow",
123 filt->flags & LOGFF_LEVEL_MIN ? ">=" : "<=",
124 log_get_level_name(filt->level));
125
126 if (filt->flags & LOGFF_HAS_CAT) {
127 int i;
128
129 if (filt->cat_list[0] != LOGC_END)
130 printf("%16.16s %s\n",
131 log_get_cat_name(filt->cat_list[0]),
132 filt->file_list ? filt->file_list : "");
133
134 for (i = 1; i < LOGF_MAX_CATEGORIES &&
135 filt->cat_list[i] != LOGC_END; i++)
136 printf("%21c %16.16s\n", ' ',
137 log_get_cat_name(filt->cat_list[i]));
138 } else {
139 printf("%16c %s\n", ' ',
140 filt->file_list ? filt->file_list : "");
141 }
142 }
143
144 return CMD_RET_SUCCESS;
145}
146
147static int do_log_filter_add(struct cmd_tbl *cmdtp, int flag, int argc,
148 char *const argv[])
149{
150 bool level_set = false;
151 bool print_num = false;
152 bool type_set = false;
153 char *file_list = NULL;
154 const char *drv_name = "console";
155 int opt, err;
156 int cat_count = 0;
157 int flags = 0;
158 enum log_category_t cat_list[LOGF_MAX_CATEGORIES + 1];
159 enum log_level_t level = LOGL_MAX;
160 struct getopt_state gs;
161
162 getopt_init_state(&gs);
163 while ((opt = getopt(&gs, argc, argv, "Ac:d:Df:l:L:p")) > 0) {
164 switch (opt) {
165 case 'A':
166#define do_type() do { \
167 if (type_set) { \
168 printf("Allow or deny set twice\n"); \
169 return CMD_RET_USAGE; \
170 } \
171 type_set = true; \
172} while (0)
173 do_type();
174 break;
175 case 'c': {
176 enum log_category_t cat;
177
178 if (cat_count >= LOGF_MAX_CATEGORIES) {
179 printf("Too many categories\n");
180 return CMD_RET_FAILURE;
181 }
182
183 cat = log_get_cat_by_name(gs.arg);
184 if (cat == LOGC_NONE) {
185 printf("Unknown category \"%s\"\n", gs.arg);
186 return CMD_RET_FAILURE;
187 }
188
189 cat_list[cat_count++] = cat;
190 break;
191 }
192 case 'd':
193 drv_name = gs.arg;
194 break;
195 case 'D':
196 do_type();
197 flags |= LOGFF_DENY;
198 break;
199 case 'f':
200 file_list = gs.arg;
201 break;
202 case 'l':
203#define do_level() do { \
204 if (level_set) { \
205 printf("Log level set twice\n"); \
206 return CMD_RET_USAGE; \
207 } \
208 level = parse_log_level(gs.arg); \
209 if (level == LOGL_NONE) \
210 return CMD_RET_FAILURE; \
211 level_set = true; \
212} while (0)
213 do_level();
214 break;
215 case 'L':
216 do_level();
217 flags |= LOGFF_LEVEL_MIN;
218 break;
219 case 'p':
220 print_num = true;
221 break;
222 default:
223 return CMD_RET_USAGE;
224 }
225 }
226
227 if (gs.index != argc)
228 return CMD_RET_USAGE;
229
230 cat_list[cat_count] = LOGC_END;
231 err = log_add_filter_flags(drv_name, cat_count ? cat_list : NULL, level,
232 file_list, flags);
233 if (err < 0) {
234 printf("Could not add filter (err = %d)\n", err);
235 return CMD_RET_FAILURE;
236 } else if (print_num) {
237 printf("%d\n", err);
238 }
239
240 return CMD_RET_SUCCESS;
241}
242
243static int do_log_filter_remove(struct cmd_tbl *cmdtp, int flag, int argc,
244 char *const argv[])
245{
246 bool all = false;
247 int opt, err;
248 ulong filter_num;
249 const char *drv_name = "console";
250 struct getopt_state gs;
251
252 getopt_init_state(&gs);
253 while ((opt = getopt(&gs, argc, argv, "ad:")) > 0) {
254 switch (opt) {
255 case 'a':
256 all = true;
257 break;
258 case 'd':
259 drv_name = gs.arg;
260 break;
261 default:
262 return CMD_RET_USAGE;
263 }
264 }
265
266 if (all) {
267 struct log_filter *filt, *tmp_filt;
268 struct log_device *ldev;
269
270 if (gs.index != argc)
271 return CMD_RET_USAGE;
272
273 ldev = log_device_find_by_name(drv_name);
274 if (!ldev) {
275 printf("Could not find log device for \"%s\"\n",
276 drv_name);
277 return CMD_RET_FAILURE;
278 }
279
280 list_for_each_entry_safe(filt, tmp_filt, &ldev->filter_head,
281 sibling_node) {
282 list_del(&filt->sibling_node);
283 free(filt);
284 }
285 } else {
286 if (gs.index + 1 != argc)
287 return CMD_RET_USAGE;
288
289 if (strict_strtoul(argv[gs.index], 10, &filter_num)) {
290 printf("Invalid filter number \"%s\"\n", argv[gs.index]);
291 return CMD_RET_FAILURE;
292 }
293
294 err = log_remove_filter(drv_name, filter_num);
295 if (err) {
296 printf("Could not remove filter (err = %d)\n", err);
297 return CMD_RET_FAILURE;
298 }
299 }
300
301 return CMD_RET_SUCCESS;
302}
303
Simon Glass09140112020-05-10 11:40:03 -0600304static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
305 char *const argv[])
Simon Glassad0e5032017-12-28 13:14:19 -0700306{
307 int i;
308
309 if (argc > 1) {
310 const char *str = argv[1];
311
312 if (!strcmp(str, "default")) {
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200313 gd->log_fmt = log_get_default_format();
Simon Glassad0e5032017-12-28 13:14:19 -0700314 } else if (!strcmp(str, "all")) {
315 gd->log_fmt = LOGF_ALL;
316 } else {
317 gd->log_fmt = 0;
318 for (; *str; str++) {
319 char *ptr = strchr(log_fmt_chars, *str);
320
321 if (!ptr) {
322 printf("Invalid log char '%c'\n", *str);
323 return CMD_RET_FAILURE;
324 }
325 gd->log_fmt |= 1 << (ptr - log_fmt_chars);
326 }
327 }
328 } else {
329 printf("Log format: ");
330 for (i = 0; i < LOGF_COUNT; i++) {
331 if (gd->log_fmt & (1 << i))
332 printf("%c", log_fmt_chars[i]);
333 }
334 printf("\n");
335 }
336
337 return 0;
338}
339
Simon Glass09140112020-05-10 11:40:03 -0600340static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
341 char *const argv[])
Simon Glass3fd24fa2017-12-28 13:14:20 -0700342{
343 enum log_category_t cat;
344 enum log_level_t level;
345 const char *file;
346 uint line;
347 const char *func;
348 const char *msg;
349 char *end;
350
351 if (argc < 7)
352 return CMD_RET_USAGE;
353 cat = log_get_cat_by_name(argv[1]);
Simon Glass0b1284e2021-07-24 09:03:30 -0600354 level = dectoul(argv[2], &end);
Simon Glass3fd24fa2017-12-28 13:14:20 -0700355 if (end == argv[2]) {
356 level = log_get_level_by_name(argv[2]);
357
358 if (level == LOGL_NONE) {
359 printf("Invalid log level '%s'\n", argv[2]);
360 return CMD_RET_USAGE;
361 }
362 }
363 if (level >= LOGL_MAX) {
364 printf("Invalid log level %u\n", level);
365 return CMD_RET_USAGE;
366 }
367 file = argv[3];
Simon Glass0b1284e2021-07-24 09:03:30 -0600368 line = dectoul(argv[4], NULL);
Simon Glass3fd24fa2017-12-28 13:14:20 -0700369 func = argv[5];
370 msg = argv[6];
371 if (_log(cat, level, file, line, func, "%s\n", msg))
372 return CMD_RET_FAILURE;
373
374 return 0;
375}
376
Tom Rini36162182023-10-07 15:13:08 -0400377U_BOOT_LONGHELP(log,
Sean Anderson018aad82020-10-27 19:55:35 -0400378 "level [<level>] - get/set log level\n"
Sean Anderson1c593a12020-10-27 19:55:34 -0400379 "categories - list log categories\n"
380 "drivers - list log drivers\n"
Sean Anderson3e409762020-10-27 19:55:38 -0400381 "log filter-list [OPTIONS] - list all filters for a log driver\n"
382 "\t-d <driver> - Specify the log driver to list filters from; defaults\n"
383 "\t to console\n"
384 "log filter-add [OPTIONS] - add a new filter to a driver\n"
385 "\t-A - Allow messages matching this filter; mutually exclusive with -D\n"
386 "\t This is the default.\n"
387 "\t-c <category> - Category to match; may be specified multiple times\n"
388 "\t-d <driver> - Specify the log driver to add the filter to; defaults\n"
389 "\t to console\n"
390 "\t-D - Deny messages matching this filter; mutually exclusive with -A\n"
391 "\t-f <files_list> - A comma-separated list of files to match\n"
392 "\t-l <level> - Match log levels less than or equal to <level>;\n"
393 "\t mutually-exclusive with -L\n"
394 "\t-L <level> - Match log levels greather than or equal to <level>;\n"
395 "\t mutually-exclusive with -l\n"
396 "\t-p - Print the filter number on success\n"
397 "log filter-remove [OPTIONS] [<num>] - Remove filter number <num>\n"
398 "\t-a - Remove ALL filters\n"
399 "\t-d <driver> - Specify the log driver to remove the filter from;\n"
400 "\t defaults to console\n"
Simon Glassad0e5032017-12-28 13:14:19 -0700401 "log format <fmt> - set log output format. <fmt> is a string where\n"
402 "\teach letter indicates something that should be displayed:\n"
403 "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200404 "\tor 'default', or 'all' for all\n"
Simon Glass3fd24fa2017-12-28 13:14:20 -0700405 "log rec <category> <level> <file> <line> <func> <message> - "
Tom Rini36162182023-10-07 15:13:08 -0400406 "output a log record");
Simon Glassd5f61f22017-12-04 13:48:26 -0700407
Sean Andersonf48b5b52020-10-27 19:55:32 -0400408U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text,
409 U_BOOT_SUBCMD_MKENT(level, 2, 1, do_log_level),
Sean Anderson1c593a12020-10-27 19:55:34 -0400410 U_BOOT_SUBCMD_MKENT(categories, 1, 1, do_log_categories),
411 U_BOOT_SUBCMD_MKENT(drivers, 1, 1, do_log_drivers),
Sean Anderson3e409762020-10-27 19:55:38 -0400412 U_BOOT_SUBCMD_MKENT(filter-list, 3, 1, do_log_filter_list),
413 U_BOOT_SUBCMD_MKENT(filter-add, CONFIG_SYS_MAXARGS, 1,
414 do_log_filter_add),
415 U_BOOT_SUBCMD_MKENT(filter-remove, 4, 1, do_log_filter_remove),
Sean Andersonf48b5b52020-10-27 19:55:32 -0400416 U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format),
417 U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec),
Simon Glassd5f61f22017-12-04 13:48:26 -0700418);