log.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #include <common.h>
  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. DECLARE_GLOBAL_DATA_PTR;
  15. static const char *const log_cat_name[] = {
  16. "none",
  17. "arch",
  18. "board",
  19. "core",
  20. "driver-model",
  21. "device-tree",
  22. "efi",
  23. "alloc",
  24. "sandbox",
  25. "bloblist",
  26. "devres",
  27. "acpi",
  28. "boot",
  29. "event",
  30. "fs",
  31. "expo",
  32. };
  33. _Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE,
  34. "log_cat_name size");
  35. static const char *const log_level_name[] = {
  36. "EMERG",
  37. "ALERT",
  38. "CRIT",
  39. "ERR",
  40. "WARNING",
  41. "NOTICE",
  42. "INFO",
  43. "DEBUG",
  44. "CONTENT",
  45. "IO",
  46. };
  47. _Static_assert(ARRAY_SIZE(log_level_name) == LOGL_COUNT, "log_level_name size");
  48. /* All error responses MUST begin with '<' */
  49. const char *log_get_cat_name(enum log_category_t cat)
  50. {
  51. const char *name;
  52. if (cat < 0 || cat >= LOGC_COUNT)
  53. return "<invalid>";
  54. if (cat >= LOGC_NONE)
  55. return log_cat_name[cat - LOGC_NONE];
  56. #if CONFIG_IS_ENABLED(DM)
  57. name = uclass_get_name((enum uclass_id)cat);
  58. #else
  59. name = NULL;
  60. #endif
  61. return name ? name : "<missing>";
  62. }
  63. enum log_category_t log_get_cat_by_name(const char *name)
  64. {
  65. enum uclass_id id;
  66. int i;
  67. for (i = LOGC_NONE; i < LOGC_COUNT; i++)
  68. if (!strcmp(name, log_cat_name[i - LOGC_NONE]))
  69. return i;
  70. id = uclass_get_by_name(name);
  71. if (id != UCLASS_INVALID)
  72. return (enum log_category_t)id;
  73. return LOGC_NONE;
  74. }
  75. const char *log_get_level_name(enum log_level_t level)
  76. {
  77. if (level >= LOGL_COUNT)
  78. return "INVALID";
  79. return log_level_name[level];
  80. }
  81. enum log_level_t log_get_level_by_name(const char *name)
  82. {
  83. int i;
  84. for (i = 0; i < LOGL_COUNT; i++) {
  85. if (!strcasecmp(log_level_name[i], name))
  86. return i;
  87. }
  88. return LOGL_NONE;
  89. }
  90. struct log_device *log_device_find_by_name(const char *drv_name)
  91. {
  92. struct log_device *ldev;
  93. list_for_each_entry(ldev, &gd->log_head, sibling_node) {
  94. if (!strcmp(drv_name, ldev->drv->name))
  95. return ldev;
  96. }
  97. return NULL;
  98. }
  99. bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
  100. {
  101. int i;
  102. for (i = 0; i < LOGF_MAX_CATEGORIES && cat_list[i] != LOGC_END; i++) {
  103. if (cat_list[i] == cat)
  104. return true;
  105. }
  106. return false;
  107. }
  108. bool log_has_file(const char *file_list, const char *file)
  109. {
  110. int file_len = strlen(file);
  111. const char *s, *p;
  112. int substr_len;
  113. for (s = file_list; *s; s = p + (*p != '\0')) {
  114. p = strchrnul(s, ',');
  115. substr_len = p - s;
  116. if (file_len >= substr_len &&
  117. !strncmp(file + file_len - substr_len, s, substr_len))
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * log_passes_filters() - check if a log record passes the filters for a device
  124. *
  125. * @ldev: Log device to check
  126. * @rec: Log record to check
  127. * Return: true if @rec is not blocked by the filters in @ldev, false if it is
  128. */
  129. static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec)
  130. {
  131. struct log_filter *filt;
  132. if (rec->flags & LOGRECF_FORCE_DEBUG)
  133. return true;
  134. /* If there are no filters, filter on the default log level */
  135. if (list_empty(&ldev->filter_head)) {
  136. if (rec->level > gd->default_log_level)
  137. return false;
  138. return true;
  139. }
  140. list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
  141. if (filt->flags & LOGFF_LEVEL_MIN) {
  142. if (rec->level < filt->level)
  143. continue;
  144. } else if (rec->level > filt->level) {
  145. continue;
  146. }
  147. if ((filt->flags & LOGFF_HAS_CAT) &&
  148. !log_has_cat(filt->cat_list, rec->cat))
  149. continue;
  150. if (filt->file_list &&
  151. !log_has_file(filt->file_list, rec->file))
  152. continue;
  153. if (filt->flags & LOGFF_DENY)
  154. return false;
  155. else
  156. return true;
  157. }
  158. return false;
  159. }
  160. /**
  161. * log_dispatch() - Send a log record to all log devices for processing
  162. *
  163. * The log record is sent to each log device in turn, skipping those which have
  164. * filters which block the record.
  165. *
  166. * All log messages created while processing log record @rec are ignored.
  167. *
  168. * @rec: log record to dispatch
  169. * Return: 0 msg sent, 1 msg not sent while already dispatching another msg
  170. */
  171. static int log_dispatch(struct log_rec *rec, const char *fmt, va_list args)
  172. {
  173. struct log_device *ldev;
  174. char buf[CONFIG_SYS_CBSIZE];
  175. /*
  176. * When a log driver writes messages (e.g. via the network stack) this
  177. * may result in further generated messages. We cannot process them here
  178. * as this might result in infinite recursion.
  179. */
  180. if (gd->processing_msg)
  181. return 1;
  182. /* Emit message */
  183. gd->processing_msg = true;
  184. list_for_each_entry(ldev, &gd->log_head, sibling_node) {
  185. if ((ldev->flags & LOGDF_ENABLE) &&
  186. log_passes_filters(ldev, rec)) {
  187. if (!rec->msg) {
  188. int len;
  189. len = vsnprintf(buf, sizeof(buf), fmt, args);
  190. rec->msg = buf;
  191. gd->log_cont = len && buf[len - 1] != '\n';
  192. }
  193. ldev->drv->emit(ldev, rec);
  194. }
  195. }
  196. gd->processing_msg = false;
  197. return 0;
  198. }
  199. int _log(enum log_category_t cat, enum log_level_t level, const char *file,
  200. int line, const char *func, const char *fmt, ...)
  201. {
  202. struct log_rec rec;
  203. va_list args;
  204. if (!gd)
  205. return -ENOSYS;
  206. /* Check for message continuation */
  207. if (cat == LOGC_CONT)
  208. cat = gd->logc_prev;
  209. if (level == LOGL_CONT)
  210. level = gd->logl_prev;
  211. rec.cat = cat;
  212. rec.level = level & LOGL_LEVEL_MASK;
  213. rec.flags = 0;
  214. if (level & LOGL_FORCE_DEBUG)
  215. rec.flags |= LOGRECF_FORCE_DEBUG;
  216. if (gd->log_cont)
  217. rec.flags |= LOGRECF_CONT;
  218. rec.file = file;
  219. rec.line = line;
  220. rec.func = func;
  221. rec.msg = NULL;
  222. if (!(gd->flags & GD_FLG_LOG_READY)) {
  223. gd->log_drop_count++;
  224. /* display dropped traces with console puts and DEBUG_UART */
  225. if (rec.level <= CONFIG_LOG_DEFAULT_LEVEL ||
  226. rec.flags & LOGRECF_FORCE_DEBUG) {
  227. char buf[CONFIG_SYS_CBSIZE];
  228. va_start(args, fmt);
  229. vsnprintf(buf, sizeof(buf), fmt, args);
  230. puts(buf);
  231. va_end(args);
  232. }
  233. return -ENOSYS;
  234. }
  235. va_start(args, fmt);
  236. if (!log_dispatch(&rec, fmt, args)) {
  237. gd->logc_prev = cat;
  238. gd->logl_prev = level;
  239. }
  240. va_end(args);
  241. return 0;
  242. }
  243. #define MAX_LINE_LENGTH_BYTES 64
  244. #define DEFAULT_LINE_LENGTH_BYTES 16
  245. int _log_buffer(enum log_category_t cat, enum log_level_t level,
  246. const char *file, int line, const char *func, ulong addr,
  247. const void *data, uint width, uint count, uint linelen)
  248. {
  249. if (linelen * width > MAX_LINE_LENGTH_BYTES)
  250. linelen = MAX_LINE_LENGTH_BYTES / width;
  251. if (linelen < 1)
  252. linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  253. while (count) {
  254. uint thislinelen;
  255. char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)];
  256. thislinelen = hexdump_line(addr, data, width, count, linelen,
  257. buf, sizeof(buf));
  258. assert(thislinelen >= 0);
  259. _log(cat, level, file, line, func, "%s\n", buf);
  260. /* update references */
  261. data += thislinelen * width;
  262. addr += thislinelen * width;
  263. count -= thislinelen;
  264. }
  265. return 0;
  266. }
  267. int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
  268. enum log_level_t level, const char *file_list,
  269. int flags)
  270. {
  271. struct log_filter *filt;
  272. struct log_device *ldev;
  273. int ret;
  274. int i;
  275. ldev = log_device_find_by_name(drv_name);
  276. if (!ldev)
  277. return -ENOENT;
  278. filt = calloc(1, sizeof(*filt));
  279. if (!filt)
  280. return -ENOMEM;
  281. filt->flags = flags;
  282. if (cat_list) {
  283. filt->flags |= LOGFF_HAS_CAT;
  284. for (i = 0; ; i++) {
  285. if (i == ARRAY_SIZE(filt->cat_list)) {
  286. ret = -ENOSPC;
  287. goto err;
  288. }
  289. filt->cat_list[i] = cat_list[i];
  290. if (cat_list[i] == LOGC_END)
  291. break;
  292. }
  293. }
  294. filt->level = level;
  295. if (file_list) {
  296. filt->file_list = strdup(file_list);
  297. if (!filt->file_list) {
  298. ret = -ENOMEM;
  299. goto err;
  300. }
  301. }
  302. filt->filter_num = ldev->next_filter_num++;
  303. /* Add deny filters to the beginning of the list */
  304. if (flags & LOGFF_DENY)
  305. list_add(&filt->sibling_node, &ldev->filter_head);
  306. else
  307. list_add_tail(&filt->sibling_node, &ldev->filter_head);
  308. return filt->filter_num;
  309. err:
  310. free(filt);
  311. return ret;
  312. }
  313. int log_remove_filter(const char *drv_name, int filter_num)
  314. {
  315. struct log_filter *filt;
  316. struct log_device *ldev;
  317. ldev = log_device_find_by_name(drv_name);
  318. if (!ldev)
  319. return -ENOENT;
  320. list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
  321. if (filt->filter_num == filter_num) {
  322. list_del(&filt->sibling_node);
  323. free(filt);
  324. return 0;
  325. }
  326. }
  327. return -ENOENT;
  328. }
  329. /**
  330. * log_find_device_by_drv() - Find a device by its driver
  331. *
  332. * @drv: Log driver
  333. * Return: Device associated with that driver, or NULL if not found
  334. */
  335. static struct log_device *log_find_device_by_drv(struct log_driver *drv)
  336. {
  337. struct log_device *ldev;
  338. list_for_each_entry(ldev, &gd->log_head, sibling_node) {
  339. if (ldev->drv == drv)
  340. return ldev;
  341. }
  342. /*
  343. * It is quite hard to pass an invalid driver since passing an unknown
  344. * LOG_GET_DRIVER(xxx) would normally produce a compilation error. But
  345. * it is possible to pass NULL, for example, so this
  346. */
  347. return NULL;
  348. }
  349. int log_device_set_enable(struct log_driver *drv, bool enable)
  350. {
  351. struct log_device *ldev;
  352. ldev = log_find_device_by_drv(drv);
  353. if (!ldev)
  354. return -ENOENT;
  355. if (enable)
  356. ldev->flags |= LOGDF_ENABLE;
  357. else
  358. ldev->flags &= ~LOGDF_ENABLE;
  359. return 0;
  360. }
  361. int log_init(void)
  362. {
  363. struct log_driver *drv = ll_entry_start(struct log_driver, log_driver);
  364. const int count = ll_entry_count(struct log_driver, log_driver);
  365. struct log_driver *end = drv + count;
  366. /*
  367. * We cannot add runtime data to the driver since it is likely stored
  368. * in rodata. Instead, set up a 'device' corresponding to each driver.
  369. * We only support having a single device for each driver.
  370. */
  371. INIT_LIST_HEAD((struct list_head *)&gd->log_head);
  372. while (drv < end) {
  373. struct log_device *ldev;
  374. ldev = calloc(1, sizeof(*ldev));
  375. if (!ldev) {
  376. debug("%s: Cannot allocate memory\n", __func__);
  377. return -ENOMEM;
  378. }
  379. INIT_LIST_HEAD(&ldev->filter_head);
  380. ldev->drv = drv;
  381. ldev->flags = drv->flags;
  382. list_add_tail(&ldev->sibling_node,
  383. (struct list_head *)&gd->log_head);
  384. drv++;
  385. }
  386. gd->flags |= GD_FLG_LOG_READY;
  387. if (!gd->default_log_level)
  388. gd->default_log_level = CONFIG_LOG_DEFAULT_LEVEL;
  389. gd->log_fmt = log_get_default_format();
  390. gd->logc_prev = LOGC_NONE;
  391. gd->logl_prev = LOGL_INFO;
  392. return 0;
  393. }