trace.c 10.0 KB

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