]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - tools/proftool.c
2 * Copyright (c) 2013 Google, Inc
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,
20 /* Decode and dump U-Boot profiling information */
31 #include <sys/param.h>
36 #define MAX_LINE_LEN 500
39 FUNCF_TRACE
= 1 << 0, /* Include this function in trace */
45 unsigned long code_size
;
46 unsigned long call_count
;
48 /* the section this function is in */
49 struct objsection_info
*objsection
;
52 enum trace_line_type
{
57 struct trace_configline_info
{
58 struct trace_configline_info
*next
;
59 enum trace_line_type type
;
60 const char *name
; /* identifier name / wildcard */
61 regex_t regex
; /* Regex to use if name starts with / */
64 /* The contents of the trace config file */
65 struct trace_configline_info
*trace_config_head
;
67 struct func_info
*func_list
;
69 struct trace_call
*call_list
;
71 int verbose
; /* Verbosity level 0=none, 1=warn, 2=notice, 3=info, 4=debug */
72 unsigned long text_offset
; /* text address of first function */
74 static void outf(int level
, const char *fmt
, ...)
75 __attribute__ ((format (__printf__
, 2, 3)));
76 #define error(fmt, b...) outf(0, fmt, ##b)
77 #define warn(fmt, b...) outf(1, fmt, ##b)
78 #define notice(fmt, b...) outf(2, fmt, ##b)
79 #define info(fmt, b...) outf(3, fmt, ##b)
80 #define debug(fmt, b...) outf(4, fmt, ##b)
83 static void outf(int level
, const char *fmt
, ...)
85 if (verbose
>= level
) {
89 vfprintf(stderr
, fmt
, args
);
94 static void usage(void)
97 "Usage: proftool -cds -v3 <cmd> <profdata>\n"
100 " dump-ftrace\t\tDump out textual data in ftrace format\n"
103 " -m <map>\tSpecify Systen.map file\n"
104 " -t <trace>\tSpecific trace data file (from U-Boot)\n"
105 " -v <0-4>\tSpecify verbosity\n");
109 static int h_cmp_offset(const void *v1
, const void *v2
)
111 const struct func_info
*f1
= v1
, *f2
= v2
;
113 return (f1
->offset
/ FUNC_SITE_SIZE
) - (f2
->offset
/ FUNC_SITE_SIZE
);
116 static int read_system_map(FILE *fin
)
118 unsigned long offset
, start
= 0;
119 struct func_info
*func
;
120 char buff
[MAX_LINE_LEN
];
122 char symname
[MAX_LINE_LEN
+ 1];
126 for (linenum
= 1, alloced
= func_count
= 0;; linenum
++) {
129 if (fgets(buff
, sizeof(buff
), fin
))
130 fields
= sscanf(buff
, "%lx %c %100s\n", &offset
,
134 } else if (feof(fin
)) {
136 } else if (fields
< 2) {
137 error("Map file line %d: invalid format\n", linenum
);
141 /* Must be a text symbol */
142 symtype
= tolower(symtype
);
143 if (symtype
!= 't' && symtype
!= 'w')
146 if (func_count
== alloced
) {
148 func_list
= realloc(func_list
,
149 sizeof(struct func_info
) * alloced
);
155 func
= &func_list
[func_count
++];
156 memset(func
, '\0', sizeof(*func
));
157 func
->offset
= offset
- start
;
158 func
->name
= strdup(symname
);
159 func
->flags
= FUNCF_TRACE
; /* trace by default */
161 /* Update previous function's code size */
163 func
[-1].code_size
= func
->offset
- func
[-1].offset
;
165 notice("%d functions found in map file\n", func_count
);
170 static int read_data(FILE *fin
, void *buff
, int size
)
174 err
= fread(buff
, 1, size
, fin
);
178 error("Cannot read profile file at pos %ld\n", ftell(fin
));
184 static struct func_info
*find_func_by_offset(uint32_t offset
)
186 struct func_info key
, *found
;
189 found
= bsearch(&key
, func_list
, func_count
, sizeof(struct func_info
),
195 /* This finds the function which contains the given offset */
196 static struct func_info
*find_caller_by_offset(uint32_t offset
)
198 int low
; /* least function that could be a match */
199 int high
; /* greated function that could be a match */
200 struct func_info key
;
203 high
= func_count
- 1;
205 while (high
> low
+ 1) {
206 int mid
= (low
+ high
) / 2;
209 result
= h_cmp_offset(&key
, &func_list
[mid
]);
215 return &func_list
[mid
];
218 return low
>= 0 ? &func_list
[low
] : NULL
;
221 static int read_calls(FILE *fin
, int count
)
223 struct trace_call
*call_data
;
226 notice("call count: %d\n", count
);
227 call_list
= (struct trace_call
*)calloc(count
, sizeof(*call_data
));
229 error("Cannot allocate call_list\n");
234 call_data
= call_list
;
235 for (i
= 0; i
< count
; i
++, call_data
++) {
236 if (read_data(fin
, call_data
, sizeof(*call_data
)))
242 static int read_profile(FILE *fin
, int *not_found
)
244 struct trace_output_hdr hdr
;
250 err
= read_data(fin
, &hdr
, sizeof(hdr
));
257 case TRACE_CHUNK_FUNCS
:
258 /* Ignored at present */
261 case TRACE_CHUNK_CALLS
:
262 if (read_calls(fin
, hdr
.rec_count
))
270 static int read_map_file(const char *fname
)
275 fmap
= fopen(fname
, "r");
277 error("Cannot open map file '%s'\n", fname
);
281 err
= read_system_map(fmap
);
287 static int read_profile_file(const char *fname
)
289 int not_found
= INT_MAX
;
293 fprof
= fopen(fname
, "rb");
295 error("Cannot open profile data file '%s'\n",
299 err
= read_profile(fprof
, ¬_found
);
305 warn("%d profile functions could not be found in the map file - are you sure that your profile data and map file correspond?\n",
313 static int regex_report_error(regex_t
*regex
, int err
, const char *op
,
318 regerror(err
, regex
, buf
, sizeof(buf
));
319 error("Regex error '%s' in %s '%s'\n", buf
, op
, name
);
323 static void check_trace_config_line(struct trace_configline_info
*item
)
325 struct func_info
*func
, *end
;
328 debug("Checking trace config line '%s'\n", item
->name
);
329 for (func
= func_list
, end
= func
+ func_count
; func
< end
; func
++) {
330 err
= regexec(&item
->regex
, func
->name
, 0, NULL
, 0);
331 debug(" - regex '%s', string '%s': %d\n", item
->name
,
333 if (err
== REG_NOMATCH
)
336 if (err
!= REG_NOERROR
) {
337 regex_report_error(&item
->regex
, err
, "match",
342 /* It matches, so perform the action */
343 switch (item
->type
) {
344 case TRACE_LINE_INCLUDE
:
345 info(" include %s at %lx\n", func
->name
,
346 text_offset
+ func
->offset
);
347 func
->flags
|= FUNCF_TRACE
;
350 case TRACE_LINE_EXCLUDE
:
351 info(" exclude %s at %lx\n", func
->name
,
352 text_offset
+ func
->offset
);
353 func
->flags
&= ~FUNCF_TRACE
;
359 static void check_trace_config(void)
361 struct trace_configline_info
*line
;
363 for (line
= trace_config_head
; line
; line
= line
->next
)
364 check_trace_config_line(line
);
368 * Check the functions to see if they each have an objsection. If not, then
369 * the linker must have eliminated them.
371 static void check_functions(void)
373 struct func_info
*func
, *end
;
374 unsigned long removed_code_size
= 0;
377 /* Look for missing functions */
378 for (func
= func_list
, end
= func
+ func_count
; func
< end
; func
++) {
379 if (!func
->objsection
) {
380 removed_code_size
+= func
->code_size
;
385 /* Figure out what functions we want to trace */
386 check_trace_config();
388 warn("%d functions removed by linker, %ld code size\n",
389 not_found
, removed_code_size
);
392 static int read_trace_config(FILE *fin
)
396 struct trace_configline_info
**tailp
= &trace_config_head
;
398 while (fgets(buff
, sizeof(buff
), fin
)) {
399 int len
= strlen(buff
);
400 struct trace_configline_info
*line
;
406 if (len
&& buff
[len
- 1] == '\n')
407 buff
[len
- 1] = '\0';
409 /* skip blank lines and comments */
410 for (s
= buff
; *s
== ' ' || *s
== '\t'; s
++)
412 if (!*s
|| *s
== '#')
415 line
= (struct trace_configline_info
*)calloc(1,
418 error("Cannot allocate config line\n");
422 tok
= strtok_r(s
, " \t", &saveptr
);
424 error("Invalid trace config data on line %d\n",
428 if (0 == strcmp(tok
, "include-func")) {
429 line
->type
= TRACE_LINE_INCLUDE
;
430 } else if (0 == strcmp(tok
, "exclude-func")) {
431 line
->type
= TRACE_LINE_EXCLUDE
;
433 error("Unknown command in trace config data line %d\n",
438 tok
= strtok_r(NULL
, " \t", &saveptr
);
440 error("Missing pattern in trace config data line %d\n",
445 err
= regcomp(&line
->regex
, tok
, REG_NOSUB
);
448 return regex_report_error(&line
->regex
, err
, "compile",
452 /* link this new one to the end of the list */
453 line
->name
= strdup(tok
);
460 error("Cannot read from trace config file at position %ld\n",
467 static int read_trace_config_file(const char *fname
)
472 fin
= fopen(fname
, "r");
474 error("Cannot open trace_config file '%s'\n", fname
);
477 err
= read_trace_config(fin
);
482 static void out_func(ulong func_offset
, int is_caller
, const char *suffix
)
484 struct func_info
*func
;
486 func
= (is_caller
? find_caller_by_offset
: find_func_by_offset
)
490 printf("%s%s", func
->name
, suffix
);
492 printf("%lx%s", func_offset
, suffix
);
498 * # TASK-PID CPU# TIMESTAMP FUNCTION
500 * # bash-4251 [01] 10152.583854: path_put <-path_walk
501 * # bash-4251 [01] 10152.583855: dput <-path_put
502 * # bash-4251 [01] 10152.583855: _atomic_dec_and_lock <-dput
504 static int make_ftrace(void)
506 struct trace_call
*call
;
507 int missing_count
= 0, skip_count
= 0;
510 printf("# tracer: ftrace\n"
512 "# TASK-PID CPU# TIMESTAMP FUNCTION\n"
514 for (i
= 0, call
= call_list
; i
< call_count
; i
++, call
++) {
515 struct func_info
*func
= find_func_by_offset(call
->func
);
516 ulong time
= call
->flags
& FUNCF_TIMESTAMP_MASK
;
518 if (TRACE_CALL_TYPE(call
) != FUNCF_ENTRY
&&
519 TRACE_CALL_TYPE(call
) != FUNCF_EXIT
)
522 warn("Cannot find function at %lx\n",
523 text_offset
+ call
->func
);
528 if (!(func
->flags
& FUNCF_TRACE
)) {
529 debug("Funcion '%s' is excluded from trace\n",
535 printf("%16s-%-5d [01] %lu.%06lu: ", "uboot", 1,
536 time
/ 1000000, time
% 1000000);
538 out_func(call
->func
, 0, " <- ");
539 out_func(call
->caller
, 1, "\n");
541 info("ftrace: %d functions not found, %d excluded\n", missing_count
,
547 static int prof_tool(int argc
, char * const argv
[],
548 const char *prof_fname
, const char *map_fname
,
549 const char *trace_config_fname
)
553 if (read_map_file(map_fname
))
555 if (prof_fname
&& read_profile_file(prof_fname
))
557 if (trace_config_fname
&& read_trace_config_file(trace_config_fname
))
562 for (; argc
; argc
--, argv
++) {
563 const char *cmd
= *argv
;
565 if (0 == strcmp(cmd
, "dump-ftrace"))
568 warn("Unknown command '%s'\n", cmd
);
574 int main(int argc
, char *argv
[])
576 const char *map_fname
= "System.map";
577 const char *prof_fname
= NULL
;
578 const char *trace_config_fname
= NULL
;
582 while ((opt
= getopt(argc
, argv
, "m:p:t:v:")) != -1) {
593 trace_config_fname
= optarg
;
597 verbose
= atoi(optarg
);
604 argc
-= optind
; argv
+= optind
;
608 debug("Debug enabled\n");
609 return prof_tool(argc
, argv
, prof_fname
, map_fname
,