1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012 The Chromium OS Authors.
10 #include <asm/sections.h>
12 DECLARE_GLOBAL_DATA_PTR;
14 static char trace_enabled __attribute__((section(".data")));
15 static char trace_inited __attribute__((section(".data")));
17 /* The header block at the start of the trace memory area */
19 int func_count; /* Total number of function call sites */
20 u64 call_count; /* Total number of tracked function calls */
21 u64 untracked_count; /* Total number of untracked function calls */
22 int funcs_used; /* Total number of functions used */
25 * Call count for each function. This is indexed by the word offset
26 * of the function from gd->relocaddr
28 uintptr_t *call_accum;
30 /* Function trace list */
31 struct trace_call *ftrace; /* The function call records */
32 ulong ftrace_size; /* Num. of ftrace records we have space for */
33 ulong ftrace_count; /* Num. of ftrace records written */
34 ulong ftrace_too_deep_count; /* Functions that were too deep */
41 static struct trace_hdr *hdr; /* Pointer to start of trace buffer */
43 static inline uintptr_t __attribute__((no_instrument_function))
44 func_ptr_to_num(void *func_ptr)
46 uintptr_t offset = (uintptr_t)func_ptr;
49 offset -= (uintptr_t)&_init;
51 if (gd->flags & GD_FLG_RELOC)
52 offset -= gd->relocaddr;
54 offset -= CONFIG_SYS_TEXT_BASE;
56 return offset / FUNC_SITE_SIZE;
59 static void __attribute__((no_instrument_function)) add_ftrace(void *func_ptr,
60 void *caller, ulong flags)
62 if (hdr->depth > hdr->depth_limit) {
63 hdr->ftrace_too_deep_count++;
66 if (hdr->ftrace_count < hdr->ftrace_size) {
67 struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
69 rec->func = func_ptr_to_num(func_ptr);
70 rec->caller = func_ptr_to_num(caller);
71 rec->flags = flags | (timer_get_us() & FUNCF_TIMESTAMP_MASK);
76 static void __attribute__((no_instrument_function)) add_textbase(void)
78 if (hdr->ftrace_count < hdr->ftrace_size) {
79 struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
81 rec->func = CONFIG_SYS_TEXT_BASE;
83 rec->flags = FUNCF_TEXTBASE;
89 * This is called on every function entry
91 * We add to our tally for this function and add to the list of called
94 * @param func_ptr Pointer to function being entered
95 * @param caller Pointer to function which called this function
97 void __attribute__((no_instrument_function)) __cyg_profile_func_enter(
98 void *func_ptr, void *caller)
103 add_ftrace(func_ptr, caller, FUNCF_ENTRY);
104 func = func_ptr_to_num(func_ptr);
105 if (func < hdr->func_count) {
106 hdr->call_accum[func]++;
109 hdr->untracked_count++;
112 if (hdr->depth > hdr->depth_limit)
113 hdr->max_depth = hdr->depth;
118 * This is called on every function exit
120 * We do nothing here.
122 * @param func_ptr Pointer to function being entered
123 * @param caller Pointer to function which called this function
125 void __attribute__((no_instrument_function)) __cyg_profile_func_exit(
126 void *func_ptr, void *caller)
129 add_ftrace(func_ptr, caller, FUNCF_EXIT);
135 * Produce a list of called functions
137 * The information is written into the supplied buffer - a header followed
138 * by a list of function records.
140 * @param buff Buffer to place list into
141 * @param buff_size Size of buffer
142 * @param needed Returns size of buffer needed, which may be
143 * greater than buff_size if we ran out of space.
144 * @return 0 if ok, -1 if space was exhausted
146 int trace_list_functions(void *buff, int buff_size, unsigned int *needed)
148 struct trace_output_hdr *output_hdr = NULL;
149 void *end, *ptr = buff;
153 end = buff ? buff + buff_size : NULL;
155 /* Place some header information */
156 if (ptr + sizeof(struct trace_output_hdr) < end)
158 ptr += sizeof(struct trace_output_hdr);
160 /* Add information about each function */
161 for (func = upto = 0; func < hdr->func_count; func++) {
162 int calls = hdr->call_accum[func];
167 if (ptr + sizeof(struct trace_output_func) < end) {
168 struct trace_output_func *stats = ptr;
170 stats->offset = func * FUNC_SITE_SIZE;
171 stats->call_count = calls;
174 ptr += sizeof(struct trace_output_func);
177 /* Update the header */
179 output_hdr->rec_count = upto;
180 output_hdr->type = TRACE_CHUNK_FUNCS;
183 /* Work out how must of the buffer we used */
184 *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;
236 /* Print basic information about tracing */
237 void trace_print_stats(void)
242 puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
243 puts("You will likely get zeroed data here\n");
246 printf("Trace is disabled\n");
249 print_grouped_ull(hdr->func_count, 10);
250 puts(" function sites\n");
251 print_grouped_ull(hdr->call_count, 10);
252 puts(" function calls\n");
253 print_grouped_ull(hdr->untracked_count, 10);
254 puts(" untracked function calls\n");
255 count = min(hdr->ftrace_count, hdr->ftrace_size);
256 print_grouped_ull(count, 10);
257 puts(" traced function calls");
258 if (hdr->ftrace_count > hdr->ftrace_size) {
259 printf(" (%lu dropped due to overflow)",
260 hdr->ftrace_count - hdr->ftrace_size);
263 printf("%15d maximum observed call depth\n", hdr->max_depth);
264 printf("%15d call depth limit\n", hdr->depth_limit);
265 print_grouped_ull(hdr->ftrace_too_deep_count, 10);
266 puts(" calls not traced due to depth\n");
269 void __attribute__((no_instrument_function)) trace_set_enabled(int enabled)
271 trace_enabled = enabled != 0;
275 * Init the tracing system ready for used, and enable it
277 * @param buff Pointer to trace buffer
278 * @param buff_size Size of trace buffer
280 int __attribute__((no_instrument_function)) trace_init(void *buff,
283 ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
285 int was_disabled = !trace_enabled;
288 #ifdef CONFIG_TRACE_EARLY
293 * Copy over the early trace data if we have it. Disable
294 * tracing while we are doing this.
297 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR,
298 CONFIG_TRACE_EARLY_SIZE);
299 end = (char *)&hdr->ftrace[min(hdr->ftrace_count,
301 used = end - (char *)hdr;
302 printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
303 used, CONFIG_TRACE_EARLY_ADDR,
304 (ulong)map_to_sysmem(buff));
305 memcpy(buff, hdr, used);
307 puts("trace: already enabled\n");
311 hdr = (struct trace_hdr *)buff;
312 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
313 if (needed > buff_size) {
314 printf("trace: buffer size %zd bytes: at least %zd needed\n",
320 memset(hdr, '\0', needed);
321 hdr->func_count = func_count;
322 hdr->call_accum = (uintptr_t *)(hdr + 1);
324 /* Use any remaining space for the timed function trace */
325 hdr->ftrace = (struct trace_call *)(buff + needed);
326 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
329 puts("trace: enabled\n");
330 hdr->depth_limit = 15;
337 #ifdef CONFIG_TRACE_EARLY
338 int __attribute__((no_instrument_function)) trace_early_init(void)
340 ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
341 size_t buff_size = CONFIG_TRACE_EARLY_SIZE;
344 /* We can ignore additional calls to this function */
348 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE);
349 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
350 if (needed > buff_size) {
351 printf("trace: buffer size is %zd bytes, at least %zd needed\n",
356 memset(hdr, '\0', needed);
357 hdr->call_accum = (uintptr_t *)(hdr + 1);
358 hdr->func_count = func_count;
360 /* Use any remaining space for the timed function trace */
361 hdr->ftrace = (struct trace_call *)((char *)hdr + needed);
362 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
364 hdr->depth_limit = 200;
365 printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR);