log.c 9.8 KB

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