2 * Copyright (c) 2012 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/sections.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 static char trace_enabled __attribute__((section(".data")));
16 static char trace_inited __attribute__((section(".data")));
18 /* The header block at the start of the trace memory area */
20 int func_count; /* Total number of function call sites */
21 u64 call_count; /* Total number of tracked function calls */
22 u64 untracked_count; /* Total number of untracked function calls */
23 int funcs_used; /* Total number of functions used */
26 * Call count for each function. This is indexed by the word offset
27 * of the function from gd->relocaddr
29 uintptr_t *call_accum;
31 /* Function trace list */
32 struct trace_call *ftrace; /* The function call records */
33 ulong ftrace_size; /* Num. of ftrace records we have space for */
34 ulong ftrace_count; /* Num. of ftrace records written */
35 ulong ftrace_too_deep_count; /* Functions that were too deep */
42 static struct trace_hdr *hdr; /* Pointer to start of trace buffer */
44 static inline uintptr_t __attribute__((no_instrument_function))
45 func_ptr_to_num(void *func_ptr)
47 uintptr_t offset = (uintptr_t)func_ptr;
50 offset -= (uintptr_t)&_init;
52 if (gd->flags & GD_FLG_RELOC)
53 offset -= gd->relocaddr;
55 offset -= CONFIG_SYS_TEXT_BASE;
57 return offset / FUNC_SITE_SIZE;
60 static void __attribute__((no_instrument_function)) add_ftrace(void *func_ptr,
61 void *caller, ulong flags)
63 if (hdr->depth > hdr->depth_limit) {
64 hdr->ftrace_too_deep_count++;
67 if (hdr->ftrace_count < hdr->ftrace_size) {
68 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);
77 static void __attribute__((no_instrument_function)) add_textbase(void)
79 if (hdr->ftrace_count < hdr->ftrace_size) {
80 struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
82 rec->func = CONFIG_SYS_TEXT_BASE;
84 rec->flags = FUNCF_TEXTBASE;
90 * This is called on every function entry
92 * We add to our tally for this function and add to the list of called
95 * @param func_ptr Pointer to function being entered
96 * @param caller Pointer to function which called this function
98 void __attribute__((no_instrument_function)) __cyg_profile_func_enter(
99 void *func_ptr, void *caller)
104 add_ftrace(func_ptr, caller, FUNCF_ENTRY);
105 func = func_ptr_to_num(func_ptr);
106 if (func < hdr->func_count) {
107 hdr->call_accum[func]++;
110 hdr->untracked_count++;
113 if (hdr->depth > hdr->depth_limit)
114 hdr->max_depth = hdr->depth;
119 * This is called on every function exit
121 * We do nothing here.
123 * @param func_ptr Pointer to function being entered
124 * @param caller Pointer to function which called this function
126 void __attribute__((no_instrument_function)) __cyg_profile_func_exit(
127 void *func_ptr, void *caller)
130 add_ftrace(func_ptr, caller, FUNCF_EXIT);
136 * Produce a list of called functions
138 * The information is written into the supplied buffer - a header followed
139 * by a list of function records.
141 * @param buff Buffer to place list into
142 * @param buff_size Size of buffer
143 * @param needed Returns size of buffer needed, which may be
144 * greater than buff_size if we ran out of space.
145 * @return 0 if ok, -1 if space was exhausted
147 int trace_list_functions(void *buff, int buff_size, unsigned int *needed)
149 struct trace_output_hdr *output_hdr = NULL;
150 void *end, *ptr = buff;
154 end = buff ? buff + buff_size : NULL;
156 /* Place some header information */
157 if (ptr + sizeof(struct trace_output_hdr) < end)
159 ptr += sizeof(struct trace_output_hdr);
161 /* Add information about each function */
162 for (func = upto = 0; func < hdr->func_count; func++) {
163 int calls = hdr->call_accum[func];
168 if (ptr + sizeof(struct trace_output_func) < end) {
169 struct trace_output_func *stats = ptr;
171 stats->offset = func * FUNC_SITE_SIZE;
172 stats->call_count = calls;
175 ptr += sizeof(struct trace_output_func);
178 /* Update the header */
180 output_hdr->rec_count = upto;
181 output_hdr->type = TRACE_CHUNK_FUNCS;
184 /* Work out how must of the buffer we used */
185 *needed = ptr - buff;
191 int trace_list_calls(void *buff, int buff_size, unsigned *needed)
193 struct trace_output_hdr *output_hdr = NULL;
194 void *end, *ptr = buff;
198 end = buff ? buff + buff_size : NULL;
200 /* Place some header information */
201 if (ptr + sizeof(struct trace_output_hdr) < end)
203 ptr += sizeof(struct trace_output_hdr);
205 /* Add information about each call */
206 count = hdr->ftrace_count;
207 if (count > hdr->ftrace_size)
208 count = hdr->ftrace_size;
209 for (rec = upto = 0; rec < count; rec++) {
210 if (ptr + sizeof(struct trace_call) < end) {
211 struct trace_call *call = &hdr->ftrace[rec];
212 struct trace_call *out = ptr;
214 out->func = call->func * FUNC_SITE_SIZE;
215 out->caller = call->caller * FUNC_SITE_SIZE;
216 out->flags = call->flags;
219 ptr += sizeof(struct trace_call);
222 /* Update the header */
224 output_hdr->rec_count = upto;
225 output_hdr->type = TRACE_CHUNK_CALLS;
228 /* Work out how must of the buffer we used */
229 *needed = ptr - buff;
235 /* Print basic information about tracing */
236 void trace_print_stats(void)
241 puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
242 puts("You will likely get zeroed data here\n");
245 printf("Trace is disabled\n");
248 print_grouped_ull(hdr->func_count, 10);
249 puts(" function sites\n");
250 print_grouped_ull(hdr->call_count, 10);
251 puts(" function calls\n");
252 print_grouped_ull(hdr->untracked_count, 10);
253 puts(" untracked function calls\n");
254 count = min(hdr->ftrace_count, hdr->ftrace_size);
255 print_grouped_ull(count, 10);
256 puts(" traced function calls");
257 if (hdr->ftrace_count > hdr->ftrace_size) {
258 printf(" (%lu dropped due to overflow)",
259 hdr->ftrace_count - hdr->ftrace_size);
262 printf("%15d maximum observed call depth\n", hdr->max_depth);
263 printf("%15d call depth limit\n", hdr->depth_limit);
264 print_grouped_ull(hdr->ftrace_too_deep_count, 10);
265 puts(" calls not traced due to depth\n");
268 void __attribute__((no_instrument_function)) trace_set_enabled(int enabled)
270 trace_enabled = enabled != 0;
274 * Init the tracing system ready for used, and enable it
276 * @param buff Pointer to trace buffer
277 * @param buff_size Size of trace buffer
279 int __attribute__((no_instrument_function)) trace_init(void *buff,
282 ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
284 int was_disabled = !trace_enabled;
287 #ifdef CONFIG_TRACE_EARLY
292 * Copy over the early trace data if we have it. Disable
293 * tracing while we are doing this.
296 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR,
297 CONFIG_TRACE_EARLY_SIZE);
298 end = (char *)&hdr->ftrace[hdr->ftrace_count];
299 used = end - (char *)hdr;
300 printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
301 used, CONFIG_TRACE_EARLY_ADDR,
302 (ulong)map_to_sysmem(buff));
303 memcpy(buff, hdr, used);
305 puts("trace: already enabled\n");
309 hdr = (struct trace_hdr *)buff;
310 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
311 if (needed > buff_size) {
312 printf("trace: buffer size %zd bytes: at least %zd needed\n",
318 memset(hdr, '\0', needed);
319 hdr->func_count = func_count;
320 hdr->call_accum = (uintptr_t *)(hdr + 1);
322 /* Use any remaining space for the timed function trace */
323 hdr->ftrace = (struct trace_call *)(buff + needed);
324 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
327 puts("trace: enabled\n");
328 hdr->depth_limit = 15;
334 #ifdef CONFIG_TRACE_EARLY
335 int __attribute__((no_instrument_function)) trace_early_init(void)
337 ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
338 size_t buff_size = CONFIG_TRACE_EARLY_SIZE;
341 /* We can ignore additional calls to this function */
345 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE);
346 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
347 if (needed > buff_size) {
348 printf("trace: buffer size is %zd bytes, at least %zd needed\n",
353 memset(hdr, '\0', needed);
354 hdr->call_accum = (uintptr_t *)(hdr + 1);
355 hdr->func_count = func_count;
357 /* Use any remaining space for the timed function trace */
358 hdr->ftrace = (struct trace_call *)((char *)hdr + needed);
359 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
361 hdr->depth_limit = 200;
362 printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR);