trace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. #ifdef CONFIG_EFI_LOADER
  50. /**
  51. * trace_gd - the value of the gd register
  52. */
  53. static volatile void *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 void *temp_gd = trace_gd;
  72. trace_gd = gd;
  73. 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. * This is called on every function entry
  110. *
  111. * We add to our tally for this function and add to the list of called
  112. * functions.
  113. *
  114. * @param func_ptr Pointer to function being entered
  115. * @param 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. * This is called on every function exit
  139. *
  140. * We do nothing here.
  141. *
  142. * @param func_ptr Pointer to function being entered
  143. * @param caller Pointer to function which called this function
  144. */
  145. void __attribute__((no_instrument_function)) __cyg_profile_func_exit(
  146. void *func_ptr, void *caller)
  147. {
  148. if (trace_enabled) {
  149. trace_swap_gd();
  150. add_ftrace(func_ptr, caller, FUNCF_EXIT);
  151. hdr->depth--;
  152. trace_swap_gd();
  153. }
  154. }
  155. /**
  156. * Produce a list of called functions
  157. *
  158. * The information is written into the supplied buffer - a header followed
  159. * by a list of function records.
  160. *
  161. * @param buff Buffer to place list into
  162. * @param buff_size Size of buffer
  163. * @param needed Returns size of buffer needed, which may be
  164. * greater than buff_size if we ran out of space.
  165. * @return 0 if ok, -1 if space was exhausted
  166. */
  167. int trace_list_functions(void *buff, size_t buff_size, size_t *needed)
  168. {
  169. struct trace_output_hdr *output_hdr = NULL;
  170. void *end, *ptr = buff;
  171. size_t func;
  172. size_t upto;
  173. end = buff ? buff + buff_size : NULL;
  174. /* Place some header information */
  175. if (ptr + sizeof(struct trace_output_hdr) < end)
  176. output_hdr = ptr;
  177. ptr += sizeof(struct trace_output_hdr);
  178. /* Add information about each function */
  179. for (func = upto = 0; func < hdr->func_count; func++) {
  180. size_t calls = hdr->call_accum[func];
  181. if (!calls)
  182. continue;
  183. if (ptr + sizeof(struct trace_output_func) < end) {
  184. struct trace_output_func *stats = ptr;
  185. stats->offset = func * FUNC_SITE_SIZE;
  186. stats->call_count = calls;
  187. upto++;
  188. }
  189. ptr += sizeof(struct trace_output_func);
  190. }
  191. /* Update the header */
  192. if (output_hdr) {
  193. output_hdr->rec_count = upto;
  194. output_hdr->type = TRACE_CHUNK_FUNCS;
  195. }
  196. /* Work out how must of the buffer we used */
  197. *needed = ptr - buff;
  198. if (ptr > end)
  199. return -ENOSPC;
  200. return 0;
  201. }
  202. int trace_list_calls(void *buff, size_t buff_size, size_t *needed)
  203. {
  204. struct trace_output_hdr *output_hdr = NULL;
  205. void *end, *ptr = buff;
  206. size_t rec, upto;
  207. size_t count;
  208. end = buff ? buff + buff_size : NULL;
  209. /* Place some header information */
  210. if (ptr + sizeof(struct trace_output_hdr) < end)
  211. output_hdr = ptr;
  212. ptr += sizeof(struct trace_output_hdr);
  213. /* Add information about each call */
  214. count = hdr->ftrace_count;
  215. if (count > hdr->ftrace_size)
  216. count = hdr->ftrace_size;
  217. for (rec = upto = 0; rec < count; rec++) {
  218. if (ptr + sizeof(struct trace_call) < end) {
  219. struct trace_call *call = &hdr->ftrace[rec];
  220. struct trace_call *out = ptr;
  221. out->func = call->func * FUNC_SITE_SIZE;
  222. out->caller = call->caller * FUNC_SITE_SIZE;
  223. out->flags = call->flags;
  224. upto++;
  225. }
  226. ptr += sizeof(struct trace_call);
  227. }
  228. /* Update the header */
  229. if (output_hdr) {
  230. output_hdr->rec_count = upto;
  231. output_hdr->type = TRACE_CHUNK_CALLS;
  232. }
  233. /* Work out how must of the buffer we used */
  234. *needed = ptr - buff;
  235. if (ptr > end)
  236. return -ENOSPC;
  237. return 0;
  238. }
  239. /* Print basic information about tracing */
  240. void trace_print_stats(void)
  241. {
  242. ulong count;
  243. #ifndef FTRACE
  244. puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
  245. puts("You will likely get zeroed data here\n");
  246. #endif
  247. if (!trace_inited) {
  248. printf("Trace is disabled\n");
  249. return;
  250. }
  251. print_grouped_ull(hdr->func_count, 10);
  252. puts(" function sites\n");
  253. print_grouped_ull(hdr->call_count, 10);
  254. puts(" function calls\n");
  255. print_grouped_ull(hdr->untracked_count, 10);
  256. puts(" untracked function calls\n");
  257. count = min(hdr->ftrace_count, hdr->ftrace_size);
  258. print_grouped_ull(count, 10);
  259. puts(" traced function calls");
  260. if (hdr->ftrace_count > hdr->ftrace_size) {
  261. printf(" (%lu dropped due to overflow)",
  262. hdr->ftrace_count - hdr->ftrace_size);
  263. }
  264. puts("\n");
  265. printf("%15d maximum observed call depth\n", hdr->max_depth);
  266. printf("%15d call depth limit\n", hdr->depth_limit);
  267. print_grouped_ull(hdr->ftrace_too_deep_count, 10);
  268. puts(" calls not traced due to depth\n");
  269. }
  270. void __attribute__((no_instrument_function)) trace_set_enabled(int enabled)
  271. {
  272. trace_enabled = enabled != 0;
  273. }
  274. /**
  275. * Init the tracing system ready for used, and enable it
  276. *
  277. * @param buff Pointer to trace buffer
  278. * @param buff_size Size of trace buffer
  279. */
  280. int __attribute__((no_instrument_function)) trace_init(void *buff,
  281. size_t buff_size)
  282. {
  283. ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
  284. size_t needed;
  285. int was_disabled = !trace_enabled;
  286. trace_save_gd();
  287. if (!was_disabled) {
  288. #ifdef CONFIG_TRACE_EARLY
  289. char *end;
  290. ulong used;
  291. /*
  292. * Copy over the early trace data if we have it. Disable
  293. * tracing while we are doing this.
  294. */
  295. trace_enabled = 0;
  296. hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR,
  297. CONFIG_TRACE_EARLY_SIZE);
  298. end = (char *)&hdr->ftrace[min(hdr->ftrace_count,
  299. hdr->ftrace_size)];
  300. used = end - (char *)hdr;
  301. printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
  302. used, CONFIG_TRACE_EARLY_ADDR,
  303. (ulong)map_to_sysmem(buff));
  304. memcpy(buff, hdr, used);
  305. #else
  306. puts("trace: already enabled\n");
  307. return -EALREADY;
  308. #endif
  309. }
  310. hdr = (struct trace_hdr *)buff;
  311. needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
  312. if (needed > buff_size) {
  313. printf("trace: buffer size %zd bytes: at least %zd needed\n",
  314. buff_size, needed);
  315. return -ENOSPC;
  316. }
  317. if (was_disabled)
  318. memset(hdr, '\0', needed);
  319. hdr->func_count = func_count;
  320. hdr->call_accum = (uintptr_t *)(hdr + 1);
  321. /* Use any remaining space for the timed function trace */
  322. hdr->ftrace = (struct trace_call *)(buff + needed);
  323. hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
  324. add_textbase();
  325. puts("trace: enabled\n");
  326. hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
  327. trace_enabled = 1;
  328. trace_inited = 1;
  329. return 0;
  330. }
  331. #ifdef CONFIG_TRACE_EARLY
  332. int __attribute__((no_instrument_function)) trace_early_init(void)
  333. {
  334. ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
  335. size_t buff_size = CONFIG_TRACE_EARLY_SIZE;
  336. size_t needed;
  337. /* We can ignore additional calls to this function */
  338. if (trace_enabled)
  339. return 0;
  340. hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE);
  341. needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
  342. if (needed > buff_size) {
  343. printf("trace: buffer size is %zd bytes, at least %zd needed\n",
  344. buff_size, needed);
  345. return -ENOSPC;
  346. }
  347. memset(hdr, '\0', needed);
  348. hdr->call_accum = (uintptr_t *)(hdr + 1);
  349. hdr->func_count = func_count;
  350. /* Use any remaining space for the timed function trace */
  351. hdr->ftrace = (struct trace_call *)((char *)hdr + needed);
  352. hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
  353. add_textbase();
  354. hdr->depth_limit = CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT;
  355. printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR);
  356. trace_enabled = 1;
  357. return 0;
  358. }
  359. #endif