trace.c 9.3 KB

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