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
;
190 int trace_list_calls(void *buff
, int buff_size
, unsigned *needed
)
192 struct trace_output_hdr
*output_hdr
= NULL
;
193 void *end
, *ptr
= buff
;
197 end
= buff
? buff
+ buff_size
: NULL
;
199 /* Place some header information */
200 if (ptr
+ sizeof(struct trace_output_hdr
) < end
)
202 ptr
+= sizeof(struct trace_output_hdr
);
204 /* Add information about each call */
205 count
= hdr
->ftrace_count
;
206 if (count
> hdr
->ftrace_size
)
207 count
= hdr
->ftrace_size
;
208 for (rec
= upto
= 0; rec
< count
; rec
++) {
209 if (ptr
+ sizeof(struct trace_call
) < end
) {
210 struct trace_call
*call
= &hdr
->ftrace
[rec
];
211 struct trace_call
*out
= ptr
;
213 out
->func
= call
->func
* FUNC_SITE_SIZE
;
214 out
->caller
= call
->caller
* FUNC_SITE_SIZE
;
215 out
->flags
= call
->flags
;
218 ptr
+= sizeof(struct trace_call
);
221 /* Update the header */
223 output_hdr
->rec_count
= upto
;
224 output_hdr
->type
= TRACE_CHUNK_CALLS
;
227 /* Work out how must of the buffer we used */
228 *needed
= ptr
- buff
;
234 /* Print basic information about tracing */
235 void trace_print_stats(void)
240 puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
241 puts("You will likely get zeroed data here\n");
244 printf("Trace is disabled\n");
247 print_grouped_ull(hdr
->func_count
, 10);
248 puts(" function sites\n");
249 print_grouped_ull(hdr
->call_count
, 10);
250 puts(" function calls\n");
251 print_grouped_ull(hdr
->untracked_count
, 10);
252 puts(" untracked function calls\n");
253 count
= min(hdr
->ftrace_count
, hdr
->ftrace_size
);
254 print_grouped_ull(count
, 10);
255 puts(" traced function calls");
256 if (hdr
->ftrace_count
> hdr
->ftrace_size
) {
257 printf(" (%lu dropped due to overflow)",
258 hdr
->ftrace_count
- hdr
->ftrace_size
);
261 printf("%15d maximum observed call depth\n", hdr
->max_depth
);
262 printf("%15d call depth limit\n", hdr
->depth_limit
);
263 print_grouped_ull(hdr
->ftrace_too_deep_count
, 10);
264 puts(" calls not traced due to depth\n");
267 void __attribute__((no_instrument_function
)) trace_set_enabled(int enabled
)
269 trace_enabled
= enabled
!= 0;
273 * Init the tracing system ready for used, and enable it
275 * @param buff Pointer to trace buffer
276 * @param buff_size Size of trace buffer
278 int __attribute__((no_instrument_function
)) trace_init(void *buff
,
281 ulong func_count
= gd
->mon_len
/ FUNC_SITE_SIZE
;
283 int was_disabled
= !trace_enabled
;
286 #ifdef CONFIG_TRACE_EARLY
291 * Copy over the early trace data if we have it. Disable
292 * tracing while we are doing this.
295 hdr
= map_sysmem(CONFIG_TRACE_EARLY_ADDR
,
296 CONFIG_TRACE_EARLY_SIZE
);
297 end
= (char *)&hdr
->ftrace
[hdr
->ftrace_count
];
298 used
= end
- (char *)hdr
;
299 printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
300 used
, CONFIG_TRACE_EARLY_ADDR
,
301 (ulong
)map_to_sysmem(buff
));
302 memcpy(buff
, hdr
, used
);
304 puts("trace: already enabled\n");
308 hdr
= (struct trace_hdr
*)buff
;
309 needed
= sizeof(*hdr
) + func_count
* sizeof(uintptr_t);
310 if (needed
> buff_size
) {
311 printf("trace: buffer size %zd bytes: at least %zd needed\n",
317 memset(hdr
, '\0', needed
);
318 hdr
->func_count
= func_count
;
319 hdr
->call_accum
= (uintptr_t *)(hdr
+ 1);
321 /* Use any remaining space for the timed function trace */
322 hdr
->ftrace
= (struct trace_call
*)(buff
+ needed
);
323 hdr
->ftrace_size
= (buff_size
- needed
) / sizeof(*hdr
->ftrace
);
326 puts("trace: enabled\n");
327 hdr
->depth_limit
= 15;
333 #ifdef CONFIG_TRACE_EARLY
334 int __attribute__((no_instrument_function
)) trace_early_init(void)
336 ulong func_count
= gd
->mon_len
/ FUNC_SITE_SIZE
;
337 size_t buff_size
= CONFIG_TRACE_EARLY_SIZE
;
340 /* We can ignore additional calls to this function */
344 hdr
= map_sysmem(CONFIG_TRACE_EARLY_ADDR
, CONFIG_TRACE_EARLY_SIZE
);
345 needed
= sizeof(*hdr
) + func_count
* sizeof(uintptr_t);
346 if (needed
> buff_size
) {
347 printf("trace: buffer size is %zd bytes, at least %zd needed\n",
352 memset(hdr
, '\0', needed
);
353 hdr
->call_accum
= (uintptr_t *)(hdr
+ 1);
354 hdr
->func_count
= func_count
;
356 /* Use any remaining space for the timed function trace */
357 hdr
->ftrace
= (struct trace_call
*)((char *)hdr
+ needed
);
358 hdr
->ftrace_size
= (buff_size
- needed
) / sizeof(*hdr
->ftrace
);
360 hdr
->depth_limit
= 200;
361 printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR
);