"Das U-Boot" Source Tree
at master 491 lines 11 kB view raw
1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * Logging support 4 * 5 * Copyright (c) 2017 Google, Inc 6 * Written by Simon Glass <sjg@chromium.org> 7 */ 8 9#include <display_options.h> 10#include <log.h> 11#include <malloc.h> 12#include <asm/global_data.h> 13#include <dm/uclass.h> 14 15DECLARE_GLOBAL_DATA_PTR; 16 17static const char *const log_cat_name[] = { 18 "none", 19 "arch", 20 "board", 21 "core", 22 "driver-model", 23 "device-tree", 24 "efi", 25 "alloc", 26 "sandbox", 27 "bloblist", 28 "devres", 29 "acpi", 30 "boot", 31 "event", 32 "fs", 33 "expo", 34 "console", 35 "test", 36}; 37 38_Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE, 39 "log_cat_name size"); 40 41static const char *const log_level_name[] = { 42 "EMERG", 43 "ALERT", 44 "CRIT", 45 "ERR", 46 "WARNING", 47 "NOTICE", 48 "INFO", 49 "DEBUG", 50 "CONTENT", 51 "IO", 52}; 53 54_Static_assert(ARRAY_SIZE(log_level_name) == LOGL_COUNT, "log_level_name size"); 55 56/* All error responses MUST begin with '<' */ 57const char *log_get_cat_name(enum log_category_t cat) 58{ 59 const char *name; 60 61 if (cat < 0 || cat >= LOGC_COUNT) 62 return "<invalid>"; 63 if (cat >= LOGC_NONE) 64 return log_cat_name[cat - LOGC_NONE]; 65 66#if CONFIG_IS_ENABLED(DM) 67 name = uclass_get_name((enum uclass_id)cat); 68#else 69 name = NULL; 70#endif 71 72 return name ? name : "<missing>"; 73} 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 109struct log_device *log_device_find_by_name(const char *drv_name) 110{ 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 121bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat) 122{ 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 133/** 134 * log_has_member() - check if a string is in a comma separated list 135 * 136 * @list: Comma separated list of strings 137 * @member: String to find 138 * 139 * Return: ``true`` if @member is in @list, else ``false`` 140 */ 141static bool log_has_member(const char *list, const char *member) 142{ 143 int member_len = strlen(member); 144 const char *s, *p; 145 int substr_len; 146 147 for (s = list; *s; s = p + (*p != '\0')) { 148 p = strchrnul(s, ','); 149 substr_len = p - s; 150 if (member_len >= substr_len && 151 !strncmp(member + member_len - substr_len, s, substr_len)) 152 return true; 153 } 154 155 return false; 156} 157 158/** 159 * log_passes_filters() - check if a log record passes the filters for a device 160 * 161 * @ldev: Log device to check 162 * @rec: Log record to check 163 * Return: true if @rec is not blocked by the filters in @ldev, false if it is 164 */ 165static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec) 166{ 167 struct log_filter *filt; 168 169 if (rec->flags & LOGRECF_FORCE_DEBUG) 170 return true; 171 172 /* If there are no filters, filter on the default log level */ 173 if (list_empty(&ldev->filter_head)) { 174 if (rec->level > gd->default_log_level) 175 return false; 176 return true; 177 } 178 179 list_for_each_entry(filt, &ldev->filter_head, sibling_node) { 180 if (filt->flags & LOGFF_LEVEL_MIN) { 181 if (rec->level < filt->level) 182 continue; 183 } else if (rec->level > filt->level) { 184 continue; 185 } 186 187 if ((filt->flags & LOGFF_HAS_CAT) && 188 !log_has_cat(filt->cat_list, rec->cat)) 189 continue; 190 191 if (filt->file_list && 192 !log_has_member(filt->file_list, rec->file)) 193 continue; 194 195 if (filt->func_list && 196 !log_has_member(filt->func_list, rec->func)) 197 continue; 198 199 if (filt->flags & LOGFF_DENY) 200 return false; 201 else 202 return true; 203 } 204 205 return false; 206} 207 208/** 209 * log_dispatch() - Send a log record to all log devices for processing 210 * 211 * The log record is sent to each log device in turn, skipping those which have 212 * filters which block the record. 213 * 214 * All log messages created while processing log record @rec are ignored. 215 * 216 * @rec: log record to dispatch 217 * Return: 0 msg sent, 1 msg not sent while already dispatching another msg 218 */ 219static int log_dispatch(struct log_rec *rec, const char *fmt, va_list args) 220{ 221 struct log_device *ldev; 222 char buf[CONFIG_SYS_CBSIZE]; 223 224 /* 225 * When a log driver writes messages (e.g. via the network stack) this 226 * may result in further generated messages. We cannot process them here 227 * as this might result in infinite recursion. 228 */ 229 if (gd->processing_msg) 230 return 1; 231 232 /* Emit message */ 233 gd->processing_msg = true; 234 list_for_each_entry(ldev, &gd->log_head, sibling_node) { 235 if ((ldev->flags & LOGDF_ENABLE) && 236 log_passes_filters(ldev, rec)) { 237 if (!rec->msg) { 238 int len; 239 240 len = vsnprintf(buf, sizeof(buf), fmt, args); 241 rec->msg = buf; 242 gd->log_cont = len && buf[len - 1] != '\n'; 243 } 244 ldev->drv->emit(ldev, rec); 245 } 246 } 247 gd->processing_msg = false; 248 return 0; 249} 250 251int _log(enum log_category_t cat, enum log_level_t level, const char *file, 252 int line, const char *func, const char *fmt, ...) 253{ 254 struct log_rec rec; 255 va_list args; 256 257 if (!gd) 258 return -ENOSYS; 259 260 /* Check for message continuation */ 261 if (cat == LOGC_CONT) 262 cat = gd->logc_prev; 263 if (level == LOGL_CONT) 264 level = gd->logl_prev; 265 266 rec.cat = cat; 267 rec.level = level & LOGL_LEVEL_MASK; 268 rec.flags = 0; 269 if (level & LOGL_FORCE_DEBUG) 270 rec.flags |= LOGRECF_FORCE_DEBUG; 271 if (gd->log_cont) 272 rec.flags |= LOGRECF_CONT; 273 rec.file = file; 274 rec.line = line; 275 rec.func = func; 276 rec.msg = NULL; 277 278 if (!(gd->flags & GD_FLG_LOG_READY)) { 279 gd->log_drop_count++; 280 281 /* display dropped traces with console puts and DEBUG_UART */ 282 if (rec.level <= CONFIG_LOG_DEFAULT_LEVEL || 283 rec.flags & LOGRECF_FORCE_DEBUG) { 284 char buf[CONFIG_SYS_CBSIZE]; 285 286 va_start(args, fmt); 287 vsnprintf(buf, sizeof(buf), fmt, args); 288 puts(buf); 289 va_end(args); 290 } 291 292 return -ENOSYS; 293 } 294 va_start(args, fmt); 295 if (!log_dispatch(&rec, fmt, args)) { 296 gd->logc_prev = cat; 297 gd->logl_prev = level; 298 } 299 va_end(args); 300 301 return 0; 302} 303 304#define MAX_LINE_LENGTH_BYTES 64 305#define DEFAULT_LINE_LENGTH_BYTES 16 306 307int _log_buffer(enum log_category_t cat, enum log_level_t level, 308 const char *file, int line, const char *func, ulong addr, 309 const void *data, uint width, uint count, uint linelen) 310{ 311 if (linelen * width > MAX_LINE_LENGTH_BYTES) 312 linelen = MAX_LINE_LENGTH_BYTES / width; 313 if (linelen < 1) 314 linelen = DEFAULT_LINE_LENGTH_BYTES / width; 315 316 while (count) { 317 uint thislinelen; 318 char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)]; 319 320 thislinelen = hexdump_line(addr, data, width, count, linelen, 321 buf, sizeof(buf)); 322 assert(thislinelen >= 0); 323 _log(cat, level, file, line, func, "%s\n", buf); 324 325 /* update references */ 326 data += thislinelen * width; 327 addr += thislinelen * width; 328 count -= thislinelen; 329 } 330 331 return 0; 332} 333 334int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[], 335 enum log_level_t level, const char *file_list, 336 const char *func_list, int flags) 337{ 338 struct log_filter *filt; 339 struct log_device *ldev; 340 int ret; 341 int i; 342 343 ldev = log_device_find_by_name(drv_name); 344 if (!ldev) 345 return -ENOENT; 346 filt = calloc(1, sizeof(*filt)); 347 if (!filt) 348 return -ENOMEM; 349 350 filt->flags = flags; 351 if (cat_list) { 352 filt->flags |= LOGFF_HAS_CAT; 353 for (i = 0; ; i++) { 354 if (i == ARRAY_SIZE(filt->cat_list)) { 355 ret = -ENOSPC; 356 goto err; 357 } 358 filt->cat_list[i] = cat_list[i]; 359 if (cat_list[i] == LOGC_END) 360 break; 361 } 362 } 363 filt->level = level; 364 if (file_list) { 365 filt->file_list = strdup(file_list); 366 if (!filt->file_list) { 367 ret = -ENOMEM; 368 goto err; 369 } 370 } 371 if (func_list) { 372 filt->func_list = strdup(func_list); 373 if (!filt->func_list) { 374 ret = -ENOMEM; 375 goto err; 376 } 377 } 378 filt->filter_num = ldev->next_filter_num++; 379 /* Add deny filters to the beginning of the list */ 380 if (flags & LOGFF_DENY) 381 list_add(&filt->sibling_node, &ldev->filter_head); 382 else 383 list_add_tail(&filt->sibling_node, &ldev->filter_head); 384 385 return filt->filter_num; 386 387err: 388 free(filt); 389 return ret; 390} 391 392int log_remove_filter(const char *drv_name, int filter_num) 393{ 394 struct log_filter *filt; 395 struct log_device *ldev; 396 397 ldev = log_device_find_by_name(drv_name); 398 if (!ldev) 399 return -ENOENT; 400 401 list_for_each_entry(filt, &ldev->filter_head, sibling_node) { 402 if (filt->filter_num == filter_num) { 403 list_del(&filt->sibling_node); 404 free(filt); 405 406 return 0; 407 } 408 } 409 410 return -ENOENT; 411} 412 413/** 414 * log_find_device_by_drv() - Find a device by its driver 415 * 416 * @drv: Log driver 417 * Return: Device associated with that driver, or NULL if not found 418 */ 419static struct log_device *log_find_device_by_drv(struct log_driver *drv) 420{ 421 struct log_device *ldev; 422 423 list_for_each_entry(ldev, &gd->log_head, sibling_node) { 424 if (ldev->drv == drv) 425 return ldev; 426 } 427 /* 428 * It is quite hard to pass an invalid driver since passing an unknown 429 * LOG_GET_DRIVER(xxx) would normally produce a compilation error. But 430 * it is possible to pass NULL, for example, so this 431 */ 432 433 return NULL; 434} 435 436int log_device_set_enable(struct log_driver *drv, bool enable) 437{ 438 struct log_device *ldev; 439 440 ldev = log_find_device_by_drv(drv); 441 if (!ldev) 442 return -ENOENT; 443 if (enable) 444 ldev->flags |= LOGDF_ENABLE; 445 else 446 ldev->flags &= ~LOGDF_ENABLE; 447 448 return 0; 449} 450 451void log_fixup_for_gd_move(struct global_data *new_gd) 452{ 453 new_gd->log_head.prev->next = &new_gd->log_head; 454} 455 456int log_init(void) 457{ 458 struct log_driver *drv = ll_entry_start(struct log_driver, log_driver); 459 const int count = ll_entry_count(struct log_driver, log_driver); 460 struct log_driver *end = drv + count; 461 462 /* 463 * We cannot add runtime data to the driver since it is likely stored 464 * in rodata. Instead, set up a 'device' corresponding to each driver. 465 * We only support having a single device for each driver. 466 */ 467 INIT_LIST_HEAD((struct list_head *)&gd->log_head); 468 while (drv < end) { 469 struct log_device *ldev; 470 471 ldev = calloc(1, sizeof(*ldev)); 472 if (!ldev) { 473 debug("%s: Cannot allocate memory\n", __func__); 474 return -ENOMEM; 475 } 476 INIT_LIST_HEAD(&ldev->filter_head); 477 ldev->drv = drv; 478 ldev->flags = drv->flags; 479 list_add_tail(&ldev->sibling_node, 480 (struct list_head *)&gd->log_head); 481 drv++; 482 } 483 gd->flags |= GD_FLG_LOG_READY; 484 if (!gd->default_log_level) 485 gd->default_log_level = CONFIG_LOG_DEFAULT_LEVEL; 486 gd->log_fmt = log_get_default_format(); 487 gd->logc_prev = LOGC_NONE; 488 gd->logl_prev = LOGL_INFO; 489 490 return 0; 491}