1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
6 /* Decode and dump U-Boot profiling information */
17 #include <sys/param.h>
18 #include <sys/types.h>
23 #define MAX_LINE_LEN 500
26 FUNCF_TRACE
= 1 << 0, /* Include this function in trace */
32 unsigned long code_size
;
33 unsigned long call_count
;
35 /* the section this function is in */
36 struct objsection_info
*objsection
;
39 enum trace_line_type
{
44 struct trace_configline_info
{
45 struct trace_configline_info
*next
;
46 enum trace_line_type type
;
47 const char *name
; /* identifier name / wildcard */
48 regex_t regex
; /* Regex to use if name starts with / */
51 /* The contents of the trace config file */
52 struct trace_configline_info
*trace_config_head
;
54 struct func_info
*func_list
;
56 struct trace_call
*call_list
;
58 int verbose
; /* Verbosity level 0=none, 1=warn, 2=notice, 3=info, 4=debug */
59 unsigned long text_offset
; /* text address of first function */
61 static void outf(int level
, const char *fmt
, ...)
62 __attribute__ ((format (__printf__
, 2, 3)));
63 #define error(fmt, b...) outf(0, fmt, ##b)
64 #define warn(fmt, b...) outf(1, fmt, ##b)
65 #define notice(fmt, b...) outf(2, fmt, ##b)
66 #define info(fmt, b...) outf(3, fmt, ##b)
67 #define debug(fmt, b...) outf(4, fmt, ##b)
70 static void outf(int level
, const char *fmt
, ...)
72 if (verbose
>= level
) {
76 vfprintf(stderr
, fmt
, args
);
81 static void usage(void)
84 "Usage: proftool -cds -v3 <cmd> <profdata>\n"
87 " dump-ftrace\t\tDump out textual data in ftrace format\n"
90 " -m <map>\tSpecify Systen.map file\n"
91 " -t <trace>\tSpecific trace data file (from U-Boot)\n"
92 " -v <0-4>\tSpecify verbosity\n");
96 static int h_cmp_offset(const void *v1
, const void *v2
)
98 const struct func_info
*f1
= v1
, *f2
= v2
;
100 return (f1
->offset
/ FUNC_SITE_SIZE
) - (f2
->offset
/ FUNC_SITE_SIZE
);
103 static int read_system_map(FILE *fin
)
105 unsigned long offset
, start
= 0;
106 struct func_info
*func
;
107 char buff
[MAX_LINE_LEN
];
109 char symname
[MAX_LINE_LEN
+ 1];
113 for (linenum
= 1, alloced
= func_count
= 0;; linenum
++) {
116 if (fgets(buff
, sizeof(buff
), fin
))
117 fields
= sscanf(buff
, "%lx %c %100s\n", &offset
,
121 } else if (feof(fin
)) {
123 } else if (fields
< 2) {
124 error("Map file line %d: invalid format\n", linenum
);
128 /* Must be a text symbol */
129 symtype
= tolower(symtype
);
130 if (symtype
!= 't' && symtype
!= 'w')
133 if (func_count
== alloced
) {
135 func_list
= realloc(func_list
,
136 sizeof(struct func_info
) * alloced
);
142 func
= &func_list
[func_count
++];
143 memset(func
, '\0', sizeof(*func
));
144 func
->offset
= offset
- start
;
145 func
->name
= strdup(symname
);
146 func
->flags
= FUNCF_TRACE
; /* trace by default */
148 /* Update previous function's code size */
150 func
[-1].code_size
= func
->offset
- func
[-1].offset
;
152 notice("%d functions found in map file\n", func_count
);
157 static int read_data(FILE *fin
, void *buff
, int size
)
161 err
= fread(buff
, 1, size
, fin
);
165 error("Cannot read profile file at pos %ld\n", ftell(fin
));
171 static struct func_info
*find_func_by_offset(uint32_t offset
)
173 struct func_info key
, *found
;
176 found
= bsearch(&key
, func_list
, func_count
, sizeof(struct func_info
),
182 /* This finds the function which contains the given offset */
183 static struct func_info
*find_caller_by_offset(uint32_t offset
)
185 int low
; /* least function that could be a match */
186 int high
; /* greated function that could be a match */
187 struct func_info key
;
190 high
= func_count
- 1;
192 while (high
> low
+ 1) {
193 int mid
= (low
+ high
) / 2;
196 result
= h_cmp_offset(&key
, &func_list
[mid
]);
202 return &func_list
[mid
];
205 return low
>= 0 ? &func_list
[low
] : NULL
;
208 static int read_calls(FILE *fin
, int count
)
210 struct trace_call
*call_data
;
213 notice("call count: %d\n", count
);
214 call_list
= (struct trace_call
*)calloc(count
, sizeof(*call_data
));
216 error("Cannot allocate call_list\n");
221 call_data
= call_list
;
222 for (i
= 0; i
< count
; i
++, call_data
++) {
223 if (read_data(fin
, call_data
, sizeof(*call_data
)))
229 static int read_profile(FILE *fin
, int *not_found
)
231 struct trace_output_hdr hdr
;
237 err
= read_data(fin
, &hdr
, sizeof(hdr
));
244 case TRACE_CHUNK_FUNCS
:
245 /* Ignored at present */
248 case TRACE_CHUNK_CALLS
:
249 if (read_calls(fin
, hdr
.rec_count
))
257 static int read_map_file(const char *fname
)
262 fmap
= fopen(fname
, "r");
264 error("Cannot open map file '%s'\n", fname
);
268 err
= read_system_map(fmap
);
274 static int read_profile_file(const char *fname
)
276 int not_found
= INT_MAX
;
280 fprof
= fopen(fname
, "rb");
282 error("Cannot open profile data file '%s'\n",
286 err
= read_profile(fprof
, ¬_found
);
292 warn("%d profile functions could not be found in the map file - are you sure that your profile data and map file correspond?\n",
300 static int regex_report_error(regex_t
*regex
, int err
, const char *op
,
305 regerror(err
, regex
, buf
, sizeof(buf
));
306 error("Regex error '%s' in %s '%s'\n", buf
, op
, name
);
310 static void check_trace_config_line(struct trace_configline_info
*item
)
312 struct func_info
*func
, *end
;
315 debug("Checking trace config line '%s'\n", item
->name
);
316 for (func
= func_list
, end
= func
+ func_count
; func
< end
; func
++) {
317 err
= regexec(&item
->regex
, func
->name
, 0, NULL
, 0);
318 debug(" - regex '%s', string '%s': %d\n", item
->name
,
320 if (err
== REG_NOMATCH
)
324 regex_report_error(&item
->regex
, err
, "match",
329 /* It matches, so perform the action */
330 switch (item
->type
) {
331 case TRACE_LINE_INCLUDE
:
332 info(" include %s at %lx\n", func
->name
,
333 text_offset
+ func
->offset
);
334 func
->flags
|= FUNCF_TRACE
;
337 case TRACE_LINE_EXCLUDE
:
338 info(" exclude %s at %lx\n", func
->name
,
339 text_offset
+ func
->offset
);
340 func
->flags
&= ~FUNCF_TRACE
;
346 static void check_trace_config(void)
348 struct trace_configline_info
*line
;
350 for (line
= trace_config_head
; line
; line
= line
->next
)
351 check_trace_config_line(line
);
355 * Check the functions to see if they each have an objsection. If not, then
356 * the linker must have eliminated them.
358 static void check_functions(void)
360 struct func_info
*func
, *end
;
361 unsigned long removed_code_size
= 0;
364 /* Look for missing functions */
365 for (func
= func_list
, end
= func
+ func_count
; func
< end
; func
++) {
366 if (!func
->objsection
) {
367 removed_code_size
+= func
->code_size
;
372 /* Figure out what functions we want to trace */
373 check_trace_config();
375 warn("%d functions removed by linker, %ld code size\n",
376 not_found
, removed_code_size
);
379 static int read_trace_config(FILE *fin
)
383 struct trace_configline_info
**tailp
= &trace_config_head
;
385 while (fgets(buff
, sizeof(buff
), fin
)) {
386 int len
= strlen(buff
);
387 struct trace_configline_info
*line
;
393 if (len
&& buff
[len
- 1] == '\n')
394 buff
[len
- 1] = '\0';
396 /* skip blank lines and comments */
397 for (s
= buff
; *s
== ' ' || *s
== '\t'; s
++)
399 if (!*s
|| *s
== '#')
402 line
= (struct trace_configline_info
*)calloc(1,
405 error("Cannot allocate config line\n");
409 tok
= strtok_r(s
, " \t", &saveptr
);
411 error("Invalid trace config data on line %d\n",
415 if (0 == strcmp(tok
, "include-func")) {
416 line
->type
= TRACE_LINE_INCLUDE
;
417 } else if (0 == strcmp(tok
, "exclude-func")) {
418 line
->type
= TRACE_LINE_EXCLUDE
;
420 error("Unknown command in trace config data line %d\n",
425 tok
= strtok_r(NULL
, " \t", &saveptr
);
427 error("Missing pattern in trace config data line %d\n",
432 err
= regcomp(&line
->regex
, tok
, REG_NOSUB
);
434 int r
= regex_report_error(&line
->regex
, err
,
440 /* link this new one to the end of the list */
441 line
->name
= strdup(tok
);
448 error("Cannot read from trace config file at position %ld\n",
455 static int read_trace_config_file(const char *fname
)
460 fin
= fopen(fname
, "r");
462 error("Cannot open trace_config file '%s'\n", fname
);
465 err
= read_trace_config(fin
);
470 static void out_func(ulong func_offset
, int is_caller
, const char *suffix
)
472 struct func_info
*func
;
474 func
= (is_caller
? find_caller_by_offset
: find_func_by_offset
)
478 printf("%s%s", func
->name
, suffix
);
480 printf("%lx%s", func_offset
, suffix
);
486 * # TASK-PID CPU# TIMESTAMP FUNCTION
488 * # bash-4251 [01] 10152.583854: path_put <-path_walk
489 * # bash-4251 [01] 10152.583855: dput <-path_put
490 * # bash-4251 [01] 10152.583855: _atomic_dec_and_lock <-dput
492 static int make_ftrace(void)
494 struct trace_call
*call
;
495 int missing_count
= 0, skip_count
= 0;
498 printf("# tracer: ftrace\n"
500 "# TASK-PID CPU# TIMESTAMP FUNCTION\n"
502 for (i
= 0, call
= call_list
; i
< call_count
; i
++, call
++) {
503 struct func_info
*func
= find_func_by_offset(call
->func
);
504 ulong time
= call
->flags
& FUNCF_TIMESTAMP_MASK
;
506 if (TRACE_CALL_TYPE(call
) != FUNCF_ENTRY
&&
507 TRACE_CALL_TYPE(call
) != FUNCF_EXIT
)
510 warn("Cannot find function at %lx\n",
511 text_offset
+ call
->func
);
516 if (!(func
->flags
& FUNCF_TRACE
)) {
517 debug("Funcion '%s' is excluded from trace\n",
523 printf("%16s-%-5d [01] %lu.%06lu: ", "uboot", 1,
524 time
/ 1000000, time
% 1000000);
526 out_func(call
->func
, 0, " <- ");
527 out_func(call
->caller
, 1, "\n");
529 info("ftrace: %d functions not found, %d excluded\n", missing_count
,
535 static int prof_tool(int argc
, char * const argv
[],
536 const char *prof_fname
, const char *map_fname
,
537 const char *trace_config_fname
)
541 if (read_map_file(map_fname
))
543 if (prof_fname
&& read_profile_file(prof_fname
))
545 if (trace_config_fname
&& read_trace_config_file(trace_config_fname
))
550 for (; argc
; argc
--, argv
++) {
551 const char *cmd
= *argv
;
553 if (0 == strcmp(cmd
, "dump-ftrace"))
556 warn("Unknown command '%s'\n", cmd
);
562 int main(int argc
, char *argv
[])
564 const char *map_fname
= "System.map";
565 const char *prof_fname
= NULL
;
566 const char *trace_config_fname
= NULL
;
570 while ((opt
= getopt(argc
, argv
, "m:p:t:v:")) != -1) {
581 trace_config_fname
= optarg
;
585 verbose
= atoi(optarg
);
592 argc
-= optind
; argv
+= optind
;
596 debug("Debug enabled\n");
597 return prof_tool(argc
, argv
, prof_fname
, map_fname
,