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 #ifdef CONFIG_EFI_LOADER
62 * trace_gd - the value of the gd register
64 static volatile void *trace_gd
;
67 * trace_save_gd() - save the value of the gd register
69 static void __attribute__((no_instrument_function
)) trace_save_gd(void)
75 * trace_swap_gd() - swap between U-Boot and application gd register value
77 * An UEFI application may change the value of the register that gd lives in.
78 * But some of our functions like get_ticks() access this register. So we
79 * have to set the gd register to the U-Boot value when entering a trace
80 * point and set it back to the application value when exiting the trace point.
82 static void __attribute__((no_instrument_function
)) trace_swap_gd(void)
84 volatile void *temp_gd
= trace_gd
;
92 static void __attribute__((no_instrument_function
)) trace_save_gd(void)
96 static void __attribute__((no_instrument_function
)) trace_swap_gd(void)
102 static void __attribute__((no_instrument_function
)) add_ftrace(void *func_ptr
,
103 void *caller
, ulong flags
)
105 if (hdr
->depth
> hdr
->depth_limit
) {
106 hdr
->ftrace_too_deep_count
++;
109 if (hdr
->ftrace_count
< hdr
->ftrace_size
) {
110 struct trace_call
*rec
= &hdr
->ftrace
[hdr
->ftrace_count
];
112 rec
->func
= func_ptr_to_num(func_ptr
);
113 rec
->caller
= func_ptr_to_num(caller
);
114 rec
->flags
= flags
| (timer_get_us() & FUNCF_TIMESTAMP_MASK
);
119 static void __attribute__((no_instrument_function
)) add_textbase(void)
121 if (hdr
->ftrace_count
< hdr
->ftrace_size
) {
122 struct trace_call
*rec
= &hdr
->ftrace
[hdr
->ftrace_count
];
124 rec
->func
= CONFIG_SYS_TEXT_BASE
;
126 rec
->flags
= FUNCF_TEXTBASE
;
132 * This is called on every function entry
134 * We add to our tally for this function and add to the list of called
137 * @param func_ptr Pointer to function being entered
138 * @param caller Pointer to function which called this function
140 void __attribute__((no_instrument_function
)) __cyg_profile_func_enter(
141 void *func_ptr
, void *caller
)
147 add_ftrace(func_ptr
, caller
, FUNCF_ENTRY
);
148 func
= func_ptr_to_num(func_ptr
);
149 if (func
< hdr
->func_count
) {
150 hdr
->call_accum
[func
]++;
153 hdr
->untracked_count
++;
156 if (hdr
->depth
> hdr
->depth_limit
)
157 hdr
->max_depth
= hdr
->depth
;
163 * This is called on every function exit
165 * We do nothing here.
167 * @param func_ptr Pointer to function being entered
168 * @param caller Pointer to function which called this function
170 void __attribute__((no_instrument_function
)) __cyg_profile_func_exit(
171 void *func_ptr
, void *caller
)
175 add_ftrace(func_ptr
, caller
, FUNCF_EXIT
);
182 * Produce a list of called functions
184 * The information is written into the supplied buffer - a header followed
185 * by a list of function records.
187 * @param buff Buffer to place list into
188 * @param buff_size Size of buffer
189 * @param needed Returns size of buffer needed, which may be
190 * greater than buff_size if we ran out of space.
191 * @return 0 if ok, -1 if space was exhausted
193 int trace_list_functions(void *buff
, size_t buff_size
, size_t *needed
)
195 struct trace_output_hdr
*output_hdr
= NULL
;
196 void *end
, *ptr
= buff
;
200 end
= buff
? buff
+ buff_size
: NULL
;
202 /* Place some header information */
203 if (ptr
+ sizeof(struct trace_output_hdr
) < end
)
205 ptr
+= sizeof(struct trace_output_hdr
);
207 /* Add information about each function */
208 for (func
= upto
= 0; func
< hdr
->func_count
; func
++) {
209 size_t calls
= hdr
->call_accum
[func
];
214 if (ptr
+ sizeof(struct trace_output_func
) < end
) {
215 struct trace_output_func
*stats
= ptr
;
217 stats
->offset
= func
* FUNC_SITE_SIZE
;
218 stats
->call_count
= calls
;
221 ptr
+= sizeof(struct trace_output_func
);
224 /* Update the header */
226 output_hdr
->rec_count
= upto
;
227 output_hdr
->type
= TRACE_CHUNK_FUNCS
;
230 /* Work out how must of the buffer we used */
231 *needed
= ptr
- buff
;
238 int trace_list_calls(void *buff
, size_t buff_size
, size_t *needed
)
240 struct trace_output_hdr
*output_hdr
= NULL
;
241 void *end
, *ptr
= buff
;
245 end
= buff
? buff
+ buff_size
: NULL
;
247 /* Place some header information */
248 if (ptr
+ sizeof(struct trace_output_hdr
) < end
)
250 ptr
+= sizeof(struct trace_output_hdr
);
252 /* Add information about each call */
253 count
= hdr
->ftrace_count
;
254 if (count
> hdr
->ftrace_size
)
255 count
= hdr
->ftrace_size
;
256 for (rec
= upto
= 0; rec
< count
; rec
++) {
257 if (ptr
+ sizeof(struct trace_call
) < end
) {
258 struct trace_call
*call
= &hdr
->ftrace
[rec
];
259 struct trace_call
*out
= ptr
;
261 out
->func
= call
->func
* FUNC_SITE_SIZE
;
262 out
->caller
= call
->caller
* FUNC_SITE_SIZE
;
263 out
->flags
= call
->flags
;
266 ptr
+= sizeof(struct trace_call
);
269 /* Update the header */
271 output_hdr
->rec_count
= upto
;
272 output_hdr
->type
= TRACE_CHUNK_CALLS
;
275 /* Work out how must of the buffer we used */
276 *needed
= ptr
- buff
;
283 /* Print basic information about tracing */
284 void trace_print_stats(void)
289 puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
290 puts("You will likely get zeroed data here\n");
293 printf("Trace is disabled\n");
296 print_grouped_ull(hdr
->func_count
, 10);
297 puts(" function sites\n");
298 print_grouped_ull(hdr
->call_count
, 10);
299 puts(" function calls\n");
300 print_grouped_ull(hdr
->untracked_count
, 10);
301 puts(" untracked function calls\n");
302 count
= min(hdr
->ftrace_count
, hdr
->ftrace_size
);
303 print_grouped_ull(count
, 10);
304 puts(" traced function calls");
305 if (hdr
->ftrace_count
> hdr
->ftrace_size
) {
306 printf(" (%lu dropped due to overflow)",
307 hdr
->ftrace_count
- hdr
->ftrace_size
);
310 printf("%15d maximum observed call depth\n", hdr
->max_depth
);
311 printf("%15d call depth limit\n", hdr
->depth_limit
);
312 print_grouped_ull(hdr
->ftrace_too_deep_count
, 10);
313 puts(" calls not traced due to depth\n");
316 void __attribute__((no_instrument_function
)) trace_set_enabled(int enabled
)
318 trace_enabled
= enabled
!= 0;
322 * Init the tracing system ready for used, and enable it
324 * @param buff Pointer to trace buffer
325 * @param buff_size Size of trace buffer
327 int __attribute__((no_instrument_function
)) trace_init(void *buff
,
330 ulong func_count
= gd
->mon_len
/ FUNC_SITE_SIZE
;
332 int was_disabled
= !trace_enabled
;
337 #ifdef CONFIG_TRACE_EARLY
342 * Copy over the early trace data if we have it. Disable
343 * tracing while we are doing this.
346 hdr
= map_sysmem(CONFIG_TRACE_EARLY_ADDR
,
347 CONFIG_TRACE_EARLY_SIZE
);
348 end
= (char *)&hdr
->ftrace
[min(hdr
->ftrace_count
,
350 used
= end
- (char *)hdr
;
351 printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
352 used
, CONFIG_TRACE_EARLY_ADDR
,
353 (ulong
)map_to_sysmem(buff
));
354 memcpy(buff
, hdr
, used
);
356 puts("trace: already enabled\n");
360 hdr
= (struct trace_hdr
*)buff
;
361 needed
= sizeof(*hdr
) + func_count
* sizeof(uintptr_t);
362 if (needed
> buff_size
) {
363 printf("trace: buffer size %zd bytes: at least %zd needed\n",
369 memset(hdr
, '\0', needed
);
370 hdr
->func_count
= func_count
;
371 hdr
->call_accum
= (uintptr_t *)(hdr
+ 1);
373 /* Use any remaining space for the timed function trace */
374 hdr
->ftrace
= (struct trace_call
*)(buff
+ needed
);
375 hdr
->ftrace_size
= (buff_size
- needed
) / sizeof(*hdr
->ftrace
);
378 puts("trace: enabled\n");
379 hdr
->depth_limit
= CONFIG_TRACE_CALL_DEPTH_LIMIT
;
386 #ifdef CONFIG_TRACE_EARLY
387 int __attribute__((no_instrument_function
)) trace_early_init(void)
389 ulong func_count
= gd
->mon_len
/ FUNC_SITE_SIZE
;
390 size_t buff_size
= CONFIG_TRACE_EARLY_SIZE
;
393 /* We can ignore additional calls to this function */
397 hdr
= map_sysmem(CONFIG_TRACE_EARLY_ADDR
, CONFIG_TRACE_EARLY_SIZE
);
398 needed
= sizeof(*hdr
) + func_count
* sizeof(uintptr_t);
399 if (needed
> buff_size
) {
400 printf("trace: buffer size is %zd bytes, at least %zd needed\n",
405 memset(hdr
, '\0', needed
);
406 hdr
->call_accum
= (uintptr_t *)(hdr
+ 1);
407 hdr
->func_count
= func_count
;
409 /* Use any remaining space for the timed function trace */
410 hdr
->ftrace
= (struct trace_call
*)((char *)hdr
+ needed
);
411 hdr
->ftrace_size
= (buff_size
- needed
) / sizeof(*hdr
->ftrace
);
413 hdr
->depth_limit
= CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT
;
414 printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR
);