trace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2012 The Chromium OS Authors.
  4. */
  5. #include <common.h>
  6. #include <mapmem.h>
  7. #include <time.h>
  8. #include <trace.h>
  9. #include <asm/io.h>
  10. #include <asm/sections.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. static char trace_enabled __attribute__((section(".data")));
  13. static char trace_inited __attribute__((section(".data")));
  14. /* The header block at the start of the trace memory area */
  15. struct trace_hdr {
  16. int func_count; /* Total number of function call sites */
  17. u64 call_count; /* Total number of tracked function calls */
  18. u64 untracked_count; /* Total number of untracked function calls */
  19. int funcs_used; /* Total number of functions used */
  20. /*
  21. * Call count for each function. This is indexed by the word offset
  22. * of the function from gd->relocaddr
  23. */
  24. uintptr_t *call_accum;
  25. /* Function trace list */
  26. struct trace_call *ftrace; /* The function call records */
  27. ulong ftrace_size; /* Num. of ftrace records we have space for */
  28. ulong ftrace_count; /* Num. of ftrace records written */
  29. ulong ftrace_too_deep_count; /* Functions that were too deep */
  30. int depth;
  31. int depth_limit;
  32. int max_depth;
  33. };
  34. static struct trace_hdr *hdr; /* Pointer to start of trace buffer */
  35. static inline uintptr_t __attribute__((no_instrument_function))
  36. func_ptr_to_num(void *func_ptr)
  37. {
  38. uintptr_t offset = (uintptr_t)func_ptr;
  39. #ifdef CONFIG_SANDBOX
  40. offset -= (uintptr_t)&_init;
  41. #else
  42. if (gd->flags & GD_FLG_RELOC)
  43. offset -= gd->relocaddr;
  44. else
  45. offset -= CONFIG_SYS_TEXT_BASE;
  46. #endif
  47. return offset / FUNC_SITE_SIZE;
  48. }
  49. #if defined(CONFIG_EFI_LOADER) && defined(CONFIG_ARM)
  50. /**
  51. * trace_gd - the value of the gd register
  52. */
  53. static volatile gd_t *trace_gd;
  54. /**
  55. * trace_save_gd() - save the value of the gd register
  56. */
  57. static void __attribute__((no_instrument_function)) trace_save_gd(void)
  58. {
  59. trace_gd = gd;
  60. }
  61. /**
  62. * trace_swap_gd() - swap between U-Boot and application gd register value
  63. *
  64. * An UEFI application may change the value of the register that gd lives in.
  65. * But some of our functions like get_ticks() access this register. So we
  66. * have to set the gd register to the U-Boot value when entering a trace
  67. * point and set it back to the application value when exiting the trace point.
  68. */
  69. static void __attribute__((no_instrument_function)) trace_swap_gd(void)
  70. {
  71. volatile gd_t *temp_gd = trace_gd;
  72. trace_gd = gd;
  73. set_gd(temp_gd);
  74. }
  75. #else
  76. static void __attribute__((no_instrument_function)) trace_save_gd(void)
  77. {
  78. }
  79. static void __attribute__((no_instrument_function)) trace_swap_gd(void)
  80. {
  81. }
  82. #endif
  83. static void __attribute__((no_instrument_function)) add_ftrace(void *func_ptr,
  84. void *caller, ulong flags)
  85. {
  86. if (hdr->depth > hdr->depth_limit) {
  87. hdr->ftrace_too_deep_count++;
  88. return;
  89. }
  90. if (hdr->ftrace_count < hdr->ftrace_size) {
  91. struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
  92. rec->func = func_ptr_to_num(func_ptr);
  93. rec->caller = func_ptr_to_num(caller);
  94. rec->flags = flags | (timer_get_us() & FUNCF_TIMESTAMP_MASK);
  95. }
  96. hdr->ftrace_count++;
  97. }
  98. static void __attribute__((no_instrument_function)) add_textbase(void)
  99. {
  100. if (hdr->ftrace_count < hdr->ftrace_size) {
  101. struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
  102. rec->func = CONFIG_SYS_TEXT_BASE;
  103. rec->caller = 0;
  104. rec->flags = FUNCF_TEXTBASE;
  105. }
  106. hdr->ftrace_count++;
  107. }
  108. /**
  109. * __cyg_profile_func_enter() - record function entry
  110. *
  111. * We add to our tally for this function and add to the list of called
  112. * functions.
  113. *
  114. * @func_ptr: pointer to function being entered
  115. * @caller: pointer to function which called this function
  116. */
  117. void __attribute__((no_instrument_function)) __cyg_profile_func_enter(
  118. void *func_ptr, void *caller)
  119. {
  120. if (trace_enabled) {
  121. int func;
  122. trace_swap_gd();
  123. add_ftrace(func_ptr, caller, FUNCF_ENTRY);
  124. func = func_ptr_to_num(func_ptr);
  125. if (func < hdr->func_count) {
  126. hdr->call_accum[func]++;
  127. hdr->call_count++;
  128. } else {
  129. hdr->untracked_count++;
  130. }
  131. hdr->depth++;
  132. if (hdr->depth > hdr->depth_limit)
  133. hdr->max_depth = hdr->depth;
  134. trace_swap_gd();
  135. }
  136. }
  137. /**
  138. * __cyg_profile_func_exit() - record function exit
  139. *
  140. * @func_ptr: pointer to function being entered
  141. * @caller: pointer to function which called this function
  142. */
  143. void __attribute__((no_instrument_function)) __cyg_profile_func_exit(
  144. void *func_ptr, void *caller)
  145. {
  146. if (trace_enabled) {
  147. trace_swap_gd();
  148. add_ftrace(func_ptr, caller, FUNCF_EXIT);
  149. hdr->depth--;
  150. trace_swap_gd();
  151. }
  152. }
  153. /**
  154. * trace_list_functions() - produce a list of called functions
  155. *
  156. * The information is written into the supplied buffer - a header followed
  157. * by a list of function records.
  158. *
  159. * @buff: buffer to place list into
  160. * @buff_size: size of buffer
  161. * @needed: returns size of buffer needed, which may be
  162. * greater than buff_size if we ran out of space.
  163. * Return: 0 if ok, -ENOSPC if space was exhausted
  164. */
  165. int trace_list_functions(void *buff, size_t buff_size, size_t *needed)
  166. {
  167. struct trace_output_hdr *output_hdr = NULL;
  168. void *end, *ptr = buff;
  169. size_t func;
  170. size_t upto;
  171. end = buff ? buff + buff_size : NULL;
  172. /* Place some header information */
  173. if (ptr + sizeof(struct trace_output_hdr) < end)
  174. output_hdr = ptr;
  175. ptr += sizeof(struct trace_output_hdr);
  176. /* Add information about each function */
  177. for (func = upto = 0; func < hdr->func_count; func++) {
  178. size_t calls = hdr->call_accum[func];
  179. if (!calls)
  180. continue;
  181. if (ptr + sizeof(struct trace_output_func) < end) {
  182. struct trace_output_func *stats = ptr;
  183. stats->offset = func * FUNC_SITE_SIZE;
  184. stats->call_count = calls;
  185. upto++;
  186. }
  187. ptr += sizeof(struct trace_output_func);
  188. }
  189. /* Update the header */
  190. if (output_hdr) {
  191. output_hdr->rec_count = upto;
  192. output_hdr->type = TRACE_CHUNK_FUNCS;
  193. }
  194. /* Work out how must of the buffer we used */
  195. *needed = ptr - buff;
  196. if (ptr > end)
  197. return -ENOSPC;
  198. return 0;
  199. }
  200. /**
  201. * trace_list_functions() - produce a list of function calls
  202. *
  203. * The information is written into the supplied buffer - a header followed
  204. * by a list of function records.
  205. *
  206. * @buff: buffer to place list into
  207. * @buff_size: size of buffer
  208. * @needed: returns size of buffer needed, which may be
  209. * greater than buff_size if we ran out of space.
  210. * Return: 0 if ok, -ENOSPC if space was exhausted
  211. */
  212. int trace_list_calls(void *buff, size_t buff_size, size_t *needed)
  213. {
  214. struct trace_output_hdr *output_hdr = NULL;
  215. void *end, *ptr = buff;
  216. size_t rec, upto;
  217. size_t count;
  218. end = buff ? buff + buff_size : NULL;
  219. /* Place some header information */
  220. if (ptr + sizeof(struct trace_output_hdr) < end)
  221. output_hdr = ptr;
  222. ptr += sizeof(struct trace_output_hdr);
  223. /* Add information about each call */
  224. count = hdr->ftrace_count;
  225. if (count > hdr->ftrace_size)
  226. count = hdr->ftrace_size;
  227. for (rec = upto = 0; rec < count; rec++) {
  228. if (ptr + sizeof(struct trace_call) < end) {
  229. struct trace_call *call = &hdr->ftrace[rec];
  230. struct trace_call *out = ptr;
  231. out->func = call->func * FUNC_SITE_SIZE;
  232. out->caller = call->caller * FUNC_SITE_SIZE;
  233. out->flags = call->flags;
  234. upto++;
  235. }
  236. ptr += sizeof(struct trace_call);
  237. }
  238. /* Update the header */
  239. if (output_hdr) {
  240. output_hdr->rec_count = upto;
  241. output_hdr->type = TRACE_CHUNK_CALLS;
  242. }
  243. /* Work out how must of the buffer we used */
  244. *needed = ptr - buff;
  245. if (ptr > end)
  246. return -ENOSPC;
  247. return 0;
  248. }
  249. /**
  250. * trace_print_stats() - print basic information about tracing
  251. */
  252. void trace_print_stats(void)
  253. {
  254. ulong count;
  255. #ifndef FTRACE
  256. puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
  257. puts("You will likely get zeroed data here\n");
  258. #endif
  259. if (!trace_inited) {
  260. printf("Trace is disabled\n");
  261. return;
  262. }
  263. print_grouped_ull(hdr->func_count, 10);
  264. puts(" function sites\n");
  265. print_grouped_ull(hdr->call_count, 10);
  266. puts(" function calls\n");
  267. print_grouped_ull(hdr->untracked_count, 10);
  268. puts(" untracked function calls\n");
  269. count = min(hdr->ftrace_count, hdr->ftrace_size);
  270. print_grouped_ull(count, 10);
  271. puts(" traced function calls");
  272. if (hdr->ftrace_count > hdr->ftrace_size) {
  273. printf(" (%lu dropped due to overflow)",
  274. hdr->ftrace_count - hdr->ftrace_size);
  275. }
  276. puts("\n");
  277. printf("%15d maximum observed call depth\n", hdr->max_depth);
  278. printf("%15d call depth limit\n", hdr->depth_limit);
  279. print_grouped_ull(hdr->ftrace_too_deep_count, 10);
  280. puts(" calls not traced due to depth\n");
  281. }
  282. void __attribute__((no_instrument_function)) trace_set_enabled(int enabled)
  283. {
  284. trace_enabled = enabled != 0;
  285. }
  286. /**
  287. * trace_init() - initialize the tracing system and enable it
  288. *
  289. * @buff: Pointer to trace buffer
  290. * @buff_size: Size of trace buffer
  291. * Return: 0 if ok
  292. */
  293. int __attribute__((no_instrument_function)) trace_init(void *buff,
  294. size_t buff_size)
  295. {
  296. ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
  297. size_t needed;
  298. int was_disabled = !trace_enabled;
  299. trace_save_gd();
  300. if (!was_disabled) {
  301. #ifdef CONFIG_TRACE_EARLY
  302. char *end;
  303. ulong used;
  304. /*
  305. * Copy over the early trace data if we have it. Disable
  306. * tracing while we are doing this.
  307. */
  308. trace_enabled = 0;
  309. hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR,
  310. CONFIG_TRACE_EARLY_SIZE);
  311. end = (char *)&hdr->ftrace[min(hdr->ftrace_count,
  312. hdr->ftrace_size)];
  313. used = end - (char *)hdr;
  314. printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
  315. used, CONFIG_TRACE_EARLY_ADDR,
  316. (ulong)map_to_sysmem(buff));
  317. memcpy(buff, hdr, used);
  318. #else
  319. puts("trace: already enabled\n");
  320. return -EALREADY;
  321. #endif
  322. }
  323. hdr = (struct trace_hdr *)buff;
  324. needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
  325. if (needed > buff_size) {
  326. printf("trace: buffer size %zd bytes: at least %zd needed\n",
  327. buff_size, needed);
  328. return -ENOSPC;
  329. }
  330. if (was_disabled)
  331. memset(hdr, '\0', needed);
  332. hdr->func_count = func_count;
  333. hdr->call_accum = (uintptr_t *)(hdr + 1);
  334. /* Use any remaining space for the timed function trace */
  335. hdr->ftrace = (struct trace_call *)(buff + needed);
  336. hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
  337. add_textbase();
  338. puts("trace: enabled\n");
  339. hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
  340. trace_enabled = 1;
  341. trace_inited = 1;
  342. return 0;
  343. }
  344. #ifdef CONFIG_TRACE_EARLY
  345. /**
  346. * trace_early_init() - initialize the tracing system for early tracing
  347. *
  348. * Return: 0 if ok, -ENOSPC if not enough memory is available
  349. */
  350. int __attribute__((no_instrument_function)) trace_early_init(void)
  351. {
  352. ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
  353. size_t buff_size = CONFIG_TRACE_EARLY_SIZE;
  354. size_t needed;
  355. /* We can ignore additional calls to this function */
  356. if (trace_enabled)
  357. return 0;
  358. hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE);
  359. needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
  360. if (needed > buff_size) {
  361. printf("trace: buffer size is %zd bytes, at least %zd needed\n",
  362. buff_size, needed);
  363. return -ENOSPC;
  364. }
  365. memset(hdr, '\0', needed);
  366. hdr->call_accum = (uintptr_t *)(hdr + 1);
  367. hdr->func_count = func_count;
  368. /* Use any remaining space for the timed function trace */
  369. hdr->ftrace = (struct trace_call *)((char *)hdr + needed);
  370. hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
  371. add_textbase();
  372. hdr->depth_limit = CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT;
  373. printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR);
  374. trace_enabled = 1;
  375. return 0;
  376. }
  377. #endif