Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
| 9 | #include <dm.h> |
Sean Anderson | 3e40976 | 2020-10-27 19:55:38 -0400 | [diff] [blame] | 10 | #include <getopt.h> |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 11 | #include <log.h> |
Sean Anderson | 3e40976 | 2020-10-27 19:55:38 -0400 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 13 | |
Simon Glass | ad0e503 | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 14 | static char log_fmt_chars[LOGF_COUNT] = "clFLfm"; |
| 15 | |
Sean Anderson | fed9c2f | 2020-10-27 19:55:33 -0400 | [diff] [blame] | 16 | static 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 Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 36 | static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc, |
| 37 | char *const argv[]) |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 38 | { |
Sean Anderson | 018aad8 | 2020-10-27 19:55:35 -0400 | [diff] [blame] | 39 | enum log_level_t log_level; |
| 40 | |
Heinrich Schuchardt | 77007f9 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 41 | if (argc > 1) { |
Sean Anderson | 018aad8 | 2020-10-27 19:55:35 -0400 | [diff] [blame] | 42 | log_level = parse_log_level(argv[1]); |
Heinrich Schuchardt | 77007f9 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 43 | |
Sean Anderson | fed9c2f | 2020-10-27 19:55:33 -0400 | [diff] [blame] | 44 | if (log_level == LOGL_NONE) |
Heinrich Schuchardt | 77007f9 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 45 | return CMD_RET_FAILURE; |
Heinrich Schuchardt | 77007f9 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 46 | gd->default_log_level = log_level; |
| 47 | } else { |
Sean Anderson | 018aad8 | 2020-10-27 19:55:35 -0400 | [diff] [blame] | 48 | 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 Schuchardt | 77007f9 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 53 | } |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 54 | |
Sean Anderson | fed9c2f | 2020-10-27 19:55:33 -0400 | [diff] [blame] | 55 | return CMD_RET_SUCCESS; |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Sean Anderson | 1c593a1 | 2020-10-27 19:55:34 -0400 | [diff] [blame] | 58 | static 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 | |
| 78 | static 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 Anderson | 3e40976 | 2020-10-27 19:55:38 -0400 | [diff] [blame] | 89 | static 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 | |
| 147 | static 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 | |
| 243 | static 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 Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 304 | static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc, |
| 305 | char *const argv[]) |
Simon Glass | ad0e503 | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 306 | { |
| 307 | int i; |
| 308 | |
| 309 | if (argc > 1) { |
| 310 | const char *str = argv[1]; |
| 311 | |
| 312 | if (!strcmp(str, "default")) { |
Heinrich Schuchardt | 3c21d77 | 2020-06-17 21:52:44 +0200 | [diff] [blame] | 313 | gd->log_fmt = log_get_default_format(); |
Simon Glass | ad0e503 | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 314 | } 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 Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 340 | static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc, |
| 341 | char *const argv[]) |
Simon Glass | 3fd24fa | 2017-12-28 13:14:20 -0700 | [diff] [blame] | 342 | { |
| 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]); |
| 354 | level = simple_strtoul(argv[2], &end, 10); |
| 355 | 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]; |
| 368 | line = simple_strtoul(argv[4], NULL, 10); |
| 369 | 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 | |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 377 | #ifdef CONFIG_SYS_LONGHELP |
| 378 | static char log_help_text[] = |
Sean Anderson | 018aad8 | 2020-10-27 19:55:35 -0400 | [diff] [blame] | 379 | "level [<level>] - get/set log level\n" |
Sean Anderson | 1c593a1 | 2020-10-27 19:55:34 -0400 | [diff] [blame] | 380 | "categories - list log categories\n" |
| 381 | "drivers - list log drivers\n" |
Sean Anderson | 3e40976 | 2020-10-27 19:55:38 -0400 | [diff] [blame] | 382 | "log filter-list [OPTIONS] - list all filters for a log driver\n" |
| 383 | "\t-d <driver> - Specify the log driver to list filters from; defaults\n" |
| 384 | "\t to console\n" |
| 385 | "log filter-add [OPTIONS] - add a new filter to a driver\n" |
| 386 | "\t-A - Allow messages matching this filter; mutually exclusive with -D\n" |
| 387 | "\t This is the default.\n" |
| 388 | "\t-c <category> - Category to match; may be specified multiple times\n" |
| 389 | "\t-d <driver> - Specify the log driver to add the filter to; defaults\n" |
| 390 | "\t to console\n" |
| 391 | "\t-D - Deny messages matching this filter; mutually exclusive with -A\n" |
| 392 | "\t-f <files_list> - A comma-separated list of files to match\n" |
| 393 | "\t-l <level> - Match log levels less than or equal to <level>;\n" |
| 394 | "\t mutually-exclusive with -L\n" |
| 395 | "\t-L <level> - Match log levels greather than or equal to <level>;\n" |
| 396 | "\t mutually-exclusive with -l\n" |
| 397 | "\t-p - Print the filter number on success\n" |
| 398 | "log filter-remove [OPTIONS] [<num>] - Remove filter number <num>\n" |
| 399 | "\t-a - Remove ALL filters\n" |
| 400 | "\t-d <driver> - Specify the log driver to remove the filter from;\n" |
| 401 | "\t defaults to console\n" |
Simon Glass | ad0e503 | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 402 | "log format <fmt> - set log output format. <fmt> is a string where\n" |
| 403 | "\teach letter indicates something that should be displayed:\n" |
| 404 | "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n" |
Heinrich Schuchardt | 3c21d77 | 2020-06-17 21:52:44 +0200 | [diff] [blame] | 405 | "\tor 'default', or 'all' for all\n" |
Simon Glass | 3fd24fa | 2017-12-28 13:14:20 -0700 | [diff] [blame] | 406 | "log rec <category> <level> <file> <line> <func> <message> - " |
| 407 | "output a log record" |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 408 | ; |
| 409 | #endif |
| 410 | |
Sean Anderson | f48b5b5 | 2020-10-27 19:55:32 -0400 | [diff] [blame] | 411 | U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text, |
| 412 | U_BOOT_SUBCMD_MKENT(level, 2, 1, do_log_level), |
Sean Anderson | 1c593a1 | 2020-10-27 19:55:34 -0400 | [diff] [blame] | 413 | U_BOOT_SUBCMD_MKENT(categories, 1, 1, do_log_categories), |
| 414 | U_BOOT_SUBCMD_MKENT(drivers, 1, 1, do_log_drivers), |
Sean Anderson | 3e40976 | 2020-10-27 19:55:38 -0400 | [diff] [blame] | 415 | U_BOOT_SUBCMD_MKENT(filter-list, 3, 1, do_log_filter_list), |
| 416 | U_BOOT_SUBCMD_MKENT(filter-add, CONFIG_SYS_MAXARGS, 1, |
| 417 | do_log_filter_add), |
| 418 | U_BOOT_SUBCMD_MKENT(filter-remove, 4, 1, do_log_filter_remove), |
Sean Anderson | f48b5b5 | 2020-10-27 19:55:32 -0400 | [diff] [blame] | 419 | U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format), |
| 420 | U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec), |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 421 | ); |