2 * Copyright (c) 2012 The Chromium OS Authors.
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.
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.
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,
23 #include <asm/sections.h>
25 DECLARE_GLOBAL_DATA_PTR
;
27 static char trace_enabled
__attribute__((section(".data")));
28 static char trace_inited
__attribute__((section(".data")));
30 /* The header block at the start of the trace memory area */
32 int func_count
; /* Total number of function call sites */
33 u64 call_count
; /* Total number of tracked function calls */
34 u64 untracked_count
; /* Total number of untracked function calls */
35 int funcs_used
; /* Total number of functions used */
38 * Call count for each function. This is indexed by the word offset
39 * of the function from gd->relocaddr
41 uintptr_t *call_accum
;
43 /* Function trace list */
44 struct trace_call
*ftrace
; /* The function call records */
45 ulong ftrace_size
; /* Num. of ftrace records we have space for */
46 ulong ftrace_count
; /* Num. of ftrace records written */
47 ulong ftrace_too_deep_count
; /* Functions that were too deep */
54 static struct trace_hdr
*hdr
; /* Pointer to start of trace buffer */
56 static inline uintptr_t __attribute__((no_instrument_function
))
57 func_ptr_to_num(void *func_ptr
)
59 uintptr_t offset
= (uintptr_t)func_ptr
;
62 offset
-= (uintptr_t)&_init
;
64 if (gd
->flags
& GD_FLG_RELOC
)
65 offset
-= gd
->relocaddr
;
67 offset
-= CONFIG_SYS_TEXT_BASE
;
69 return offset
/ FUNC_SITE_SIZE
;
72 static void __attribute__((no_instrument_function
)) add_ftrace(void *func_ptr
,
73 void *caller
, ulong flags
)
75 if (hdr
->depth
> hdr
->depth_limit
) {
76 hdr
->ftrace_too_deep_count
++;
79 if (hdr
->ftrace_count
< hdr
->ftrace_size
) {
80 struct trace_call
*rec
= &hdr
->ftrace
[hdr
->ftrace_count
];
82 rec
->func
= func_ptr_to_num(func_ptr
);
83 rec
->caller
= func_ptr_to_num(caller
);
84 rec
->flags
= flags
| (timer_get_us() & FUNCF_TIMESTAMP_MASK
);
89 static void __attribute__((no_instrument_function
)) add_textbase(void)
91 if (hdr
->ftrace_count
< hdr
->ftrace_size
) {
92 struct trace_call
*rec
= &hdr
->ftrace
[hdr
->ftrace_count
];
94 rec
->func
= CONFIG_SYS_TEXT_BASE
;
96 rec
->flags
= FUNCF_TEXTBASE
;
102 * This is called on every function entry
104 * We add to our tally for this function and add to the list of called
107 * @param func_ptr Pointer to function being entered
108 * @param caller Pointer to function which called this function
110 void __attribute__((no_instrument_function
)) __cyg_profile_func_enter(
111 void *func_ptr
, void *caller
)
116 add_ftrace(func_ptr
, caller
, FUNCF_ENTRY
);
117 func
= func_ptr_to_num(func_ptr
);
118 if (func
< hdr
->func_count
) {
119 hdr
->call_accum
[func
]++;
122 hdr
->untracked_count
++;
125 if (hdr
->depth
> hdr
->depth_limit
)
126 hdr
->max_depth
= hdr
->depth
;
131 * This is called on every function exit
133 * We do nothing here.
135 * @param func_ptr Pointer to function being entered
136 * @param caller Pointer to function which called this function
138 void __attribute__((no_instrument_function
)) __cyg_profile_func_exit(
139 void *func_ptr
, void *caller
)
142 add_ftrace(func_ptr
, caller
, FUNCF_EXIT
);
148 * Produce a list of called functions
150 * The information is written into the supplied buffer - a header followed
151 * by a list of function records.
153 * @param buff Buffer to place list into
154 * @param buff_size Size of buffer
155 * @param needed Returns size of buffer needed, which may be
156 * greater than buff_size if we ran out of space.
157 * @return 0 if ok, -1 if space was exhausted
159 int trace_list_functions(void *buff
, int buff_size
, unsigned int *needed
)
161 struct trace_output_hdr
*output_hdr
= NULL
;
162 void *end
, *ptr
= buff
;
166 end
= buff
? buff
+ buff_size
: NULL
;
168 /* Place some header information */
169 if (ptr
+ sizeof(struct trace_output_hdr
) < end
)
171 ptr
+= sizeof(struct trace_output_hdr
);
173 /* Add information about each function */
174 for (func
= upto
= 0; func
< hdr
->func_count
; func
++) {
175 int calls
= hdr
->call_accum
[func
];
180 if (ptr
+ sizeof(struct trace_output_func
) < end
) {
181 struct trace_output_func
*stats
= ptr
;
183 stats
->offset
= func
* FUNC_SITE_SIZE
;
184 stats
->call_count
= calls
;
187 ptr
+= sizeof(struct trace_output_func
);
190 /* Update the header */
192 output_hdr
->rec_count
= upto
;
193 output_hdr
->type
= TRACE_CHUNK_FUNCS
;
196 /* Work out how must of the buffer we used */
197 *needed
= ptr
- buff
;
203 int trace_list_calls(void *buff
, int buff_size
, unsigned *needed
)
205 struct trace_output_hdr
*output_hdr
= NULL
;
206 void *end
, *ptr
= buff
;
210 end
= buff
? buff
+ buff_size
: NULL
;
212 /* Place some header information */
213 if (ptr
+ sizeof(struct trace_output_hdr
) < end
)
215 ptr
+= sizeof(struct trace_output_hdr
);
217 /* Add information about each call */
218 count
= hdr
->ftrace_count
;
219 if (count
> hdr
->ftrace_size
)
220 count
= hdr
->ftrace_size
;
221 for (rec
= upto
= 0; rec
< count
; rec
++) {
222 if (ptr
+ sizeof(struct trace_call
) < end
) {
223 struct trace_call
*call
= &hdr
->ftrace
[rec
];
224 struct trace_call
*out
= ptr
;
226 out
->func
= call
->func
* FUNC_SITE_SIZE
;
227 out
->caller
= call
->caller
* FUNC_SITE_SIZE
;
228 out
->flags
= call
->flags
;
231 ptr
+= sizeof(struct trace_call
);
234 /* Update the header */
236 output_hdr
->rec_count
= upto
;
237 output_hdr
->type
= TRACE_CHUNK_CALLS
;
240 /* Work out how must of the buffer we used */
241 *needed
= ptr
- buff
;
247 /* Print basic information about tracing */
248 void trace_print_stats(void)
253 puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
254 puts("You will likely get zeroed data here\n");
257 printf("Trace is disabled\n");
260 print_grouped_ull(hdr
->func_count
, 10);
261 puts(" function sites\n");
262 print_grouped_ull(hdr
->call_count
, 10);
263 puts(" function calls\n");
264 print_grouped_ull(hdr
->untracked_count
, 10);
265 puts(" untracked function calls\n");
266 count
= min(hdr
->ftrace_count
, hdr
->ftrace_size
);
267 print_grouped_ull(count
, 10);
268 puts(" traced function calls");
269 if (hdr
->ftrace_count
> hdr
->ftrace_size
) {
270 printf(" (%lu dropped due to overflow)",
271 hdr
->ftrace_count
- hdr
->ftrace_size
);
274 printf("%15d maximum observed call depth\n", hdr
->max_depth
);
275 printf("%15d call depth limit\n", hdr
->depth_limit
);
276 print_grouped_ull(hdr
->ftrace_too_deep_count
, 10);
277 puts(" calls not traced due to depth\n");
280 void __attribute__((no_instrument_function
)) trace_set_enabled(int enabled
)
282 trace_enabled
= enabled
!= 0;
286 * Init the tracing system ready for used, and enable it
288 * @param buff Pointer to trace buffer
289 * @param buff_size Size of trace buffer
291 int __attribute__((no_instrument_function
)) trace_init(void *buff
,
294 ulong func_count
= gd
->mon_len
/ FUNC_SITE_SIZE
;
296 int was_disabled
= !trace_enabled
;
299 #ifdef CONFIG_TRACE_EARLY
304 * Copy over the early trace data if we have it. Disable
305 * tracing while we are doing this.
308 hdr
= map_sysmem(CONFIG_TRACE_EARLY_ADDR
,
309 CONFIG_TRACE_EARLY_SIZE
);
310 end
= (char *)&hdr
->ftrace
[hdr
->ftrace_count
];
311 used
= end
- (char *)hdr
;
312 printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
313 used
, CONFIG_TRACE_EARLY_ADDR
,
314 (ulong
)map_to_sysmem(buff
));
315 memcpy(buff
, hdr
, used
);
317 puts("trace: already enabled\n");
321 hdr
= (struct trace_hdr
*)buff
;
322 needed
= sizeof(*hdr
) + func_count
* sizeof(uintptr_t);
323 if (needed
> buff_size
) {
324 printf("trace: buffer size %zd bytes: at least %zd needed\n",
330 memset(hdr
, '\0', needed
);
331 hdr
->func_count
= func_count
;
332 hdr
->call_accum
= (uintptr_t *)(hdr
+ 1);
334 /* Use any remaining space for the timed function trace */
335 hdr
->ftrace
= (struct trace_call
*)(buff
+ needed
);
336 hdr
->ftrace_size
= (buff_size
- needed
) / sizeof(*hdr
->ftrace
);
339 puts("trace: enabled\n");
340 hdr
->depth_limit
= 15;
346 #ifdef CONFIG_TRACE_EARLY
347 int __attribute__((no_instrument_function
)) trace_early_init(void)
349 ulong func_count
= gd
->mon_len
/ FUNC_SITE_SIZE
;
350 size_t buff_size
= CONFIG_TRACE_EARLY_SIZE
;
353 /* We can ignore additional calls to this function */
357 hdr
= map_sysmem(CONFIG_TRACE_EARLY_ADDR
, CONFIG_TRACE_EARLY_SIZE
);
358 needed
= sizeof(*hdr
) + func_count
* sizeof(uintptr_t);
359 if (needed
> buff_size
) {
360 printf("trace: buffer size is %zd bytes, at least %zd needed\n",
365 memset(hdr
, '\0', needed
);
366 hdr
->call_accum
= (uintptr_t *)(hdr
+ 1);
367 hdr
->func_count
= func_count
;
369 /* Use any remaining space for the timed function trace */
370 hdr
->ftrace
= (struct trace_call
*)((char *)hdr
+ needed
);
371 hdr
->ftrace_size
= (buff_size
- needed
) / sizeof(*hdr
->ftrace
);
373 hdr
->depth_limit
= 200;
374 printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR
);