trace.c 9.3 KB

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