1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2023 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
8 * Decode and dump U-Boot trace information into formats that can be used
9 * by trace-cmd or kernelshark
11 * See doc/develop/trace.rst for more information
23 #include <sys/param.h>
24 #include <sys/types.h>
30 /* Set to 1 to emit version 7 file (currently this doesn't work) */
33 /* enable some debug features */
36 /* from linux/kernel.h */
37 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
38 #define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a) - 1)
41 FUNCF_TRACE = 1 << 0, /* Include this function in trace */
42 TRACE_PAGE_SIZE = 4096, /* Assumed page size for trace */
43 TRACE_PID = 1, /* PID to use for U-Boot */
44 LEN_STACK_SIZE = 4, /* number of nested length fix-ups */
45 TRACE_PAGE_MASK = TRACE_PAGE_SIZE - 1,
46 MAX_STACK_DEPTH = 50, /* Max nested function calls */
47 MAX_LINE_LEN = 500, /* Max characters per line */
51 * enum out_format_t - supported output formats
53 * @OUT_FMT_DEFAULT: Use the default for the output file
54 * @OUT_FMT_FUNCTION: Write ftrace 'function' records
55 * @OUT_FMT_FUNCGRAPH: Write ftrace funcgraph_entry and funcgraph_exit records
63 /* Section types for v7 format (trace-cmd format) */
68 /* Option types (trace-cmd format) */
87 /* types of trace records (trace-cmd format) */
89 __TRACE_FIRST_TYPE = 0,
105 * struct func_info - information recorded for each function
107 * @offset: Function offset in the image, measured from the text_base
108 * @name: Function name
109 * @code_size: Total code size of the function
110 * @flags: Either 0 or FUNCF_TRACE
113 unsigned long offset;
115 unsigned long code_size;
120 * enum trace_line_type - whether to include or exclude a function
122 * @TRACE_LINE_INCLUDE: Include the function
123 * @TRACE_LINE_EXCLUDE: Exclude the function
125 enum trace_line_type {
131 * struct trace_configline_info - information about a config-file line
135 * @name: identifier name / wildcard
136 * @regex: Regex to use if name starts with '/'
138 struct trace_configline_info {
139 struct trace_configline_info *next;
140 enum trace_line_type type;
146 * struct tw_len - holds information about a length value that need fix-ups
148 * This is used to record a placeholder for a u32 or u64 length which is written
149 * to the output file but needs to be updated once the length is actually known
151 * This allows us to write tw->ptr - @len_base to position @ptr in the file
153 * @ptr: Position of the length value in the file
154 * @base: Base position for the calculation
155 * @size: Size of the length value, in bytes (4 or 8)
164 * struct twriter - Writer for trace records
166 * Maintains state used when writing the output file in trace-cmd format
168 * @ptr: Current file position
169 * @len_stack: Stack of length values that need fixing up
170 * @len: Number of items on @len_stack
171 * @str_buf: Buffer of strings (for v7 format)
172 * @str_ptr: Current write-position in the buffer for strings
177 struct tw_len len_stack[LEN_STACK_SIZE];
184 /* The contents of the trace config file */
185 struct trace_configline_info *trace_config_head;
187 /* list of all functions in System.map file, sorted by offset in the image */
188 struct func_info *func_list;
190 int func_count; /* number of functions */
191 struct trace_call *call_list; /* list of all calls in the input trace file */
192 int call_count; /* number of calls */
193 int verbose; /* Verbosity level 0=none, 1=warn, 2=notice, 3=info, 4=debug */
194 ulong text_offset; /* text address of first function */
195 ulong text_base; /* CONFIG_TEXT_BASE from trace file */
197 /* debugging helpers */
198 static void outf(int level, const char *fmt, ...)
199 __attribute__ ((format (__printf__, 2, 3)));
200 #define error(fmt, b...) outf(0, fmt, ##b)
201 #define warn(fmt, b...) outf(1, fmt, ##b)
202 #define notice(fmt, b...) outf(2, fmt, ##b)
203 #define info(fmt, b...) outf(3, fmt, ##b)
204 #define debug(fmt, b...) outf(4, fmt, ##b)
206 static void outf(int level, const char *fmt, ...)
208 if (verbose >= level) {
212 vfprintf(stderr, fmt, args);
217 static void usage(void)
220 "Usage: proftool [-cmtv] <cmd> <profdata>\n"
223 " dump-ftrace\t\tDump out records in ftrace format for use by trace-cmd\n"
226 " -c <cfg>\tSpecify config file\n"
227 " -f <subtype>\tSpecify output subtype\n"
228 " -m <map>\tSpecify Systen.map file\n"
229 " -o <fname>\tSpecify output file\n"
230 " -t <fname>\tSpecify trace data file (from U-Boot 'trace calls')\n"
231 " -v <0-4>\tSpecify verbosity\n"
233 "Subtypes for dump-ftrace:\n"
234 " function - write function-call records (caller/callee)\n"
235 " funcgraph - write function entry/exit records (graph)\n");
240 * h_cmp_offset - bsearch() function to compare two functions bny their offset
242 * @v1: Pointer to first function (struct func_info)
243 * @v2: Pointer to second function (struct func_info)
244 * Returns: < 0 if v1 offset < v2 offset, 0 if equal, > 0 otherwise
246 static int h_cmp_offset(const void *v1, const void *v2)
248 const struct func_info *f1 = v1, *f2 = v2;
250 return (f1->offset / FUNC_SITE_SIZE) - (f2->offset / FUNC_SITE_SIZE);
254 * read_system_map() - read the System.map file to create a list of functions
256 * This also reads the text_offset value, since we assume that the first text
257 * symbol is at that address
260 * Returns: 0 if OK, non-zero on error
262 static int read_system_map(FILE *fin)
264 unsigned long offset, start = 0;
265 struct func_info *func;
266 char buff[MAX_LINE_LEN];
268 char symname[MAX_LINE_LEN + 1];
272 for (linenum = 1, alloced = func_count = 0;; linenum++) {
275 if (fgets(buff, sizeof(buff), fin))
276 fields = sscanf(buff, "%lx %c %100s\n", &offset,
280 } else if (feof(fin)) {
282 } else if (fields < 2) {
283 error("Map file line %d: invalid format\n", linenum);
287 /* Must be a text symbol */
288 symtype = tolower(symtype);
289 if (symtype != 't' && symtype != 'w')
292 if (func_count == alloced) {
294 func_list = realloc(func_list,
295 sizeof(struct func_info) * alloced);
301 func = &func_list[func_count++];
302 memset(func, '\0', sizeof(*func));
303 func->offset = offset - start;
304 func->name = strdup(symname);
305 func->flags = FUNCF_TRACE; /* trace by default */
307 /* Update previous function's code size */
309 func[-1].code_size = func->offset - func[-1].offset;
311 notice("%d functions found in map file, start addr %lx\n", func_count,
318 static int read_data(FILE *fin, void *buff, int size)
322 err = fread(buff, 1, size, fin);
326 error("Cannot read trace file at pos %lx\n", ftell(fin));
333 * find_func_by_offset() - Look up a function by its offset
335 * @offset: Offset to search for, from text_base
336 * Returns: function, if found, else NULL
338 * This does a fast search for a function given its offset from text_base
341 static struct func_info *find_func_by_offset(uint offset)
343 struct func_info key, *found;
346 found = bsearch(&key, func_list, func_count, sizeof(struct func_info),
353 * find_caller_by_offset() - finds the function which contains the given offset
355 * @offset: Offset to search for, from text_base
356 * Returns: function, if found, else NULL
358 * If the offset falls between two functions, then it is assumed to belong to
359 * the first function (with the lowest offset). This is a way of figuring out
360 * which function owns code at a particular offset
362 static struct func_info *find_caller_by_offset(uint offset)
364 int low; /* least function that could be a match */
365 int high; /* greated function that could be a match */
366 struct func_info key;
369 high = func_count - 1;
371 while (high > low + 1) {
372 int mid = (low + high) / 2;
375 result = h_cmp_offset(&key, &func_list[mid]);
381 return &func_list[mid];
384 return low >= 0 ? &func_list[low] : NULL;
388 * read_calls() - Read the list of calls from the trace data
390 * The calls are stored consecutively in the trace output produced by U-Boot
392 * @fin: File to read from
393 * @count: Number of calls to read
394 * Returns: 0 if OK, -1 on error
396 static int read_calls(FILE *fin, size_t count)
398 struct trace_call *call_data;
401 notice("call count: %zu\n", count);
402 call_list = (struct trace_call *)calloc(count, sizeof(*call_data));
404 error("Cannot allocate call_list\n");
409 call_data = call_list;
410 for (i = 0; i < count; i++, call_data++) {
411 if (read_data(fin, call_data, sizeof(*call_data)))
418 * read_trace() - Read the U-Boot trace file
420 * Read in the calls from the trace file. The function list is ignored at
424 * Returns 0 if OK, non-zero on error
426 static int read_trace(FILE *fin)
428 struct trace_output_hdr hdr;
433 err = read_data(fin, &hdr, sizeof(hdr));
438 text_base = hdr.text_base;
441 case TRACE_CHUNK_FUNCS:
442 /* Ignored at present */
445 case TRACE_CHUNK_CALLS:
446 if (read_calls(fin, hdr.rec_count))
455 * read_map_file() - Read the System.map file
457 * This reads the file into the func_list array
459 * @fname: Filename to read
460 * Returns 0 if OK, non-zero on error
462 static int read_map_file(const char *fname)
467 fmap = fopen(fname, "r");
469 error("Cannot open map file '%s'\n", fname);
473 err = read_system_map(fmap);
480 * read_trace_file() - Open and read the U-Boot trace file
482 * Read in the calls from the trace file. The function list is ignored at
486 * Returns 0 if OK, non-zero on error
488 static int read_trace_file(const char *fname)
493 fprof = fopen(fname, "rb");
495 error("Cannot open trace data file '%s'\n",
499 err = read_trace(fprof);
507 static int regex_report_error(regex_t *regex, int err, const char *op,
512 regerror(err, regex, buf, sizeof(buf));
513 error("Regex error '%s' in %s '%s'\n", buf, op, name);
517 static void check_trace_config_line(struct trace_configline_info *item)
519 struct func_info *func, *end;
522 debug("Checking trace config line '%s'\n", item->name);
523 for (func = func_list, end = func + func_count; func < end; func++) {
524 err = regexec(&item->regex, func->name, 0, NULL, 0);
525 debug(" - regex '%s', string '%s': %d\n", item->name,
527 if (err == REG_NOMATCH)
531 regex_report_error(&item->regex, err, "match",
536 /* It matches, so perform the action */
537 switch (item->type) {
538 case TRACE_LINE_INCLUDE:
539 info(" include %s at %lx\n", func->name,
540 text_offset + func->offset);
541 func->flags |= FUNCF_TRACE;
544 case TRACE_LINE_EXCLUDE:
545 info(" exclude %s at %lx\n", func->name,
546 text_offset + func->offset);
547 func->flags &= ~FUNCF_TRACE;
553 /** check_trace_config() - Check trace-config file, reporting any problems */
554 static void check_trace_config(void)
556 struct trace_configline_info *line;
558 for (line = trace_config_head; line; line = line->next)
559 check_trace_config_line(line);
563 * read_trace_config() - read the trace-config file
565 * This file consists of lines like:
567 * include-func <regex>
568 * exclude-func <regex>
570 * where <regex> is a regular expression matched against function names. It
571 * allows some functions to be dropped from the trace when producing ftrace
574 * @fin: File to process
575 * Returns: 0 if OK, -1 on error
577 static int read_trace_config(FILE *fin)
581 struct trace_configline_info **tailp = &trace_config_head;
583 while (fgets(buff, sizeof(buff), fin)) {
584 int len = strlen(buff);
585 struct trace_configline_info *line;
591 if (len && buff[len - 1] == '\n')
592 buff[len - 1] = '\0';
594 /* skip blank lines and comments */
595 for (s = buff; *s == ' ' || *s == '\t'; s++)
597 if (!*s || *s == '#')
600 line = (struct trace_configline_info *)calloc(1, sizeof(*line));
602 error("Cannot allocate config line\n");
606 tok = strtok_r(s, " \t", &saveptr);
608 error("Invalid trace config data on line %d\n",
612 if (0 == strcmp(tok, "include-func")) {
613 line->type = TRACE_LINE_INCLUDE;
614 } else if (0 == strcmp(tok, "exclude-func")) {
615 line->type = TRACE_LINE_EXCLUDE;
617 error("Unknown command in trace config data line %d\n",
622 tok = strtok_r(NULL, " \t", &saveptr);
624 error("Missing pattern in trace config data line %d\n",
629 err = regcomp(&line->regex, tok, REG_NOSUB);
631 int r = regex_report_error(&line->regex, err,
637 /* link this new one to the end of the list */
638 line->name = strdup(tok);
645 error("Cannot read from trace config file at position %ld\n",
652 static int read_trace_config_file(const char *fname)
657 fin = fopen(fname, "r");
659 error("Cannot open trace_config file '%s'\n", fname);
662 err = read_trace_config(fin);
668 * tputh() - Write a 16-bit little-endian value to a file
670 * @fout: File to write to
671 * @val: Value to write
672 * Returns: number of bytes written (2)
674 static int tputh(FILE *fout, unsigned int val)
677 fputc(val >> 8, fout);
683 * tputl() - Write a 32-bit little-endian value to a file
685 * @fout: File to write to
686 * @val: Value to write
687 * Returns: number of bytes written (4)
689 static int tputl(FILE *fout, ulong val)
692 fputc(val >> 8, fout);
693 fputc(val >> 16, fout);
694 fputc(val >> 24, fout);
700 * tputh() - Write a 64-bit little-endian value to a file
702 * @fout: File to write to
703 * @val: Value to write
704 * Returns: number of bytes written (8)
706 static int tputq(FILE *fout, unsigned long long val)
709 tputl(fout, val >> 32U);
715 * tputh() - Write a string to a file
717 * The string is written without its terminator
719 * @fout: File to write to
720 * @val: Value to write
721 * Returns: number of bytes written
723 static int tputs(FILE *fout, const char *str)
731 * add_str() - add a name string to the string table
733 * This is used by the v7 format
735 * @tw: Writer context
736 * @name: String to write
737 * Returns: Updated value of string pointer, or -1 if out of memory
739 static int add_str(struct twriter *tw, const char *name)
744 len = strlen(name) + 1;
745 str_ptr = tw->str_ptr;
748 if (tw->str_ptr > abuf_size(&tw->str_buf)) {
751 new_size = ALIGN(tw->str_ptr, 4096);
752 if (!abuf_realloc(&tw->str_buf, new_size))
760 * push_len() - Push a new length request onto the stack
762 * @tw: Writer context
763 * @base: Base position of the length calculation
764 * @msg: Indicates the type of caller, for debugging
765 * @size: Size of the length value, either 4 bytes or 8
766 * Returns number of bytes written to the file (=@size on success), -ve on error
768 * This marks a place where a length must be written, covering data that is
769 * about to be written. It writes a placeholder value.
771 * Once the data is written, calling pop_len() will update the placeholder with
772 * the correct length based on how many bytes have been written
774 static int push_len(struct twriter *tw, int base, const char *msg, int size)
778 if (tw->len_count >= LEN_STACK_SIZE) {
779 fprintf(stderr, "Length-stack overflow: %s\n", msg);
782 if (size != 4 && size != 8) {
783 fprintf(stderr, "Length-stack invalid size %d: %s\n", size,
788 lp = &tw->len_stack[tw->len_count++];
793 return size == 8 ? tputq(tw->fout, 0) : tputl(tw->fout, 0);
797 * pop_len() - Update a length value once the length is known
799 * Pops a value of the length stack and updates the file at that position with
800 * the number of bytes written between now and then. Once done, the file is
801 * seeked to the current (tw->ptr) position again, so writing can continue as
804 * @tw: Writer context
805 * @msg: Indicates the type of caller, for debugging
806 * Returns 0 if OK, -1 on error
808 static int pop_len(struct twriter *tw, const char *msg)
813 if (!tw->len_count) {
814 fprintf(stderr, "Length-stack underflow: %s\n", msg);
818 lp = &tw->len_stack[--tw->len_count];
819 if (fseek(tw->fout, lp->ptr, SEEK_SET))
821 len = tw->ptr - lp->base;
822 ret = lp->size == 8 ? tputq(tw->fout, len) : tputl(tw->fout, len);
825 if (fseek(tw->fout, tw->ptr, SEEK_SET))
832 * start_header() - Start a v7 section
834 * Writes a header in v7 format
836 * @tw: Writer context
837 * @id: ID of header to write (SECTION_...)
838 * @flags: Flags value to write
839 * @name: Name of section
840 * Returns: number of bytes written
842 static int start_header(struct twriter *tw, int id, uint flags,
852 lptr += tputh(tw->fout, id);
853 lptr += tputh(tw->fout, flags);
854 str_id = add_str(tw, name);
857 lptr += tputl(tw->fout, str_id);
859 /* placeholder for size */
860 ret = push_len(tw, base, "v7 header", 8);
869 * start_page() - Start a new page of output data
871 * The output is arranged in 4KB pages with a base timestamp at the start of
872 * each. This starts a new page, making sure it is aligned to 4KB in the output
875 * @tw: Writer context
876 * @timestamp: Base timestamp for the page
878 static int start_page(struct twriter *tw, ulong timestamp)
883 /* move to start of next page */
884 start = ALIGN(tw->ptr, TRACE_PAGE_SIZE);
885 ret = fseek(tw->fout, start, SEEK_SET);
887 fprintf(stderr, "Cannot seek to page start\n");
893 tw->ptr += tputq(tw->fout, timestamp);
894 ret = push_len(tw, start + 16, "page", 8);
903 * finish_page() - finish a page
905 * Sets the lengths correctly and moves to the start of the next page
907 * @tw: Writer context
908 * Returns: 0 on success, -1 on error
910 static int finish_page(struct twriter *tw)
914 ret = pop_len(tw, "page");
917 end = ALIGN(tw->ptr, TRACE_PAGE_SIZE);
920 * Write a byte so that the data actually makes to the file, in the case
921 * that we never write any more pages
923 if (tw->ptr != end) {
924 if (fseek(tw->fout, end - 1, SEEK_SET)) {
925 fprintf(stderr, "cannot seek to start of next page\n");
936 * output_headers() - Output v6 headers to the file
938 * Writes out the various formats so that trace-cmd and kernelshark can make
941 * This updates tw->ptr as it goes
943 * @tw: Writer context
944 * Returns: 0 on success, -ve on error
946 static int output_headers(struct twriter *tw)
948 FILE *fout = tw->fout;
952 tw->ptr += fprintf(fout, "%c%c%ctracing6%c%c%c", 0x17, 0x08, 0x44,
953 0 /* terminator */, 0 /* little endian */,
954 4 /* 32-bit long values */);
956 /* host-machine page size 4KB */
957 tw->ptr += tputl(fout, 4 << 10);
959 tw->ptr += fprintf(fout, "header_page%c", 0);
961 snprintf(str, sizeof(str),
962 "\tfield: u64 timestamp;\toffset:0;\tsize:8;\tsigned:0;\n"
963 "\tfield: local_t commit;\toffset:8;\tsize:8;\tsigned:1;\n"
964 "\tfield: int overwrite;\toffset:8;\tsize:1;\tsigned:1;\n"
965 "\tfield: char data;\toffset:16;\tsize:4080;\tsigned:1;\n");
967 tw->ptr += tputq(fout, len);
968 tw->ptr += tputs(fout, str);
972 tw->ptr += fprintf(fout, "none%cversion%c\n", 0, 0);
974 ret = start_header(tw, SECTION_OPTIONS, 0, "options");
976 fprintf(stderr, "Cannot start option header\n");
980 tw->ptr += tputh(fout, OPTION_DONE);
981 tw->ptr += tputl(fout, 8);
982 tw->ptr += tputl(fout, 0);
983 ret = pop_len(tw, "t7 header");
985 fprintf(stderr, "Cannot finish option header\n");
990 tw->ptr += fprintf(fout, "header_event%c", 0);
991 snprintf(str, sizeof(str),
992 "# compressed entry header\n"
993 "\ttype_len : 5 bits\n"
994 "\ttime_delta : 27 bits\n"
995 "\tarray : 32 bits\n"
997 "\tpadding : type == 29\n"
998 "\ttime_extend : type == 30\n"
999 "\ttime_stamp : type == 31\n"
1000 "\tdata max type_len == 28\n");
1002 tw->ptr += tputq(fout, len);
1003 tw->ptr += tputs(fout, str);
1005 /* number of ftrace-event-format files */
1006 tw->ptr += tputl(fout, 3);
1008 snprintf(str, sizeof(str),
1012 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
1013 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
1014 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;signed:0;\n"
1015 "\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
1017 "\tfield:unsigned long ip;\toffset:8;\tsize:8;\tsigned:0;\n"
1018 "\tfield:unsigned long parent_ip;\toffset:16;\tsize:8;\tsigned:0;\n"
1020 "print fmt: \" %%ps <-- %%ps\", (void *)REC->ip, (void *)REC->parent_ip\n");
1022 tw->ptr += tputq(fout, len);
1023 tw->ptr += tputs(fout, str);
1025 snprintf(str, sizeof(str),
1026 "name: funcgraph_entry\n"
1029 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
1030 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
1031 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;signed:0;\n"
1032 "\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
1034 "\tfield:unsigned long func;\toffset:8;\tsize:8;\tsigned:0;\n"
1035 "\tfield:int depth;\toffset:16;\tsize:4;\tsigned:1;\n"
1037 "print fmt: \"--> %%ps (%%d)\", (void *)REC->func, REC->depth\n");
1039 tw->ptr += tputq(fout, len);
1040 tw->ptr += tputs(fout, str);
1042 snprintf(str, sizeof(str),
1043 "name: funcgraph_exit\n"
1046 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
1047 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
1048 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;signed:0;\n"
1049 "\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
1051 "\tfield:unsigned long func;\toffset:8;\tsize:8;\tsigned:0;\n"
1052 "\tfield:int depth;\toffset:16;\tsize:4;\tsigned:1;\n"
1053 "\tfield:unsigned int overrun;\toffset:20;\tsize:4;\tsigned:0;\n"
1054 "\tfield:unsigned long long calltime;\toffset:24;\tsize:8;\tsigned:0;\n"
1055 "\tfield:unsigned long long rettime;\toffset:32;\tsize:8;\tsigned:0;\n"
1057 "print fmt: \"<-- %%ps (%%d) (start: %%llx end: %%llx) over: %%d\", (void *)REC->func, REC->depth, REC->calltime, REC->rettime, REC->depth\n");
1059 tw->ptr += tputq(fout, len);
1060 tw->ptr += tputs(fout, str);
1066 * write_symbols() - Write the symbols out
1068 * Writes the symbol information in the following format to mimic the Linux
1069 * /proc/kallsyms file:
1071 * <address> T <name>
1073 * This updates tw->ptr as it goes
1075 * @tw: Writer context
1076 * Returns: 0 on success, -ve on error
1078 static int write_symbols(struct twriter *tw)
1084 ret = push_len(tw, tw->ptr + 4, "syms", 4);
1088 for (i = 0; i < func_count; i++) {
1089 struct func_info *func = &func_list[i];
1091 snprintf(str, sizeof(str), "%016lx T %s\n",
1092 text_offset + func->offset, func->name);
1093 tw->ptr += tputs(tw->fout, str);
1095 ret = pop_len(tw, "syms");
1104 * write_options() - Write the options out
1106 * Writes various options which are needed or useful. We use OPTION_TSC2NSEC
1107 * to indicates that values in the output need to be multiplied by 1000 since
1108 * U-Boot's trace values are in microseconds.
1110 * This updates tw->ptr as it goes
1112 * @tw: Writer context
1113 * Returns: 0 on success, -ve on error
1115 static int write_options(struct twriter *tw)
1117 FILE *fout = tw->fout;
1121 /* trace_printk, 0 for now */
1122 tw->ptr += tputl(fout, 0);
1125 snprintf(str, sizeof(str), "%d u-boot\n", TRACE_PID);
1127 tw->ptr += tputq(fout, len);
1128 tw->ptr += tputs(fout, str);
1130 /* number of CPUs */
1131 tw->ptr += tputl(fout, 1);
1133 tw->ptr += fprintf(fout, "options %c", 0);
1136 tw->ptr += tputh(fout, OPTION_TRACECLOCK);
1137 tw->ptr += tputl(fout, 0);
1140 tw->ptr += tputh(fout, OPTION_UNAME);
1141 snprintf(str, sizeof(str), "U-Boot");
1143 tw->ptr += tputl(fout, len);
1144 tw->ptr += tputs(fout, str);
1147 tw->ptr += tputh(fout, OPTION_VERSION);
1148 snprintf(str, sizeof(str), "unknown");
1150 tw->ptr += tputl(fout, len);
1151 tw->ptr += tputs(fout, str);
1154 tw->ptr += tputh(fout, OPTION_TRACEID);
1155 tw->ptr += tputl(fout, 8);
1156 tw->ptr += tputq(fout, 0x123456780abcdef0);
1158 /* time conversion */
1159 tw->ptr += tputh(fout, OPTION_TSC2NSEC);
1160 tw->ptr += tputl(fout, 16);
1161 tw->ptr += tputl(fout, 1000); /* multiplier */
1162 tw->ptr += tputl(fout, 0); /* shift */
1163 tw->ptr += tputq(fout, 0); /* offset */
1165 /* cpustat - bogus data for now, but at least it mentions the CPU */
1166 tw->ptr += tputh(fout, OPTION_CPUSTAT);
1167 snprintf(str, sizeof(str),
1171 "commit overrun: 0\n"
1173 "oldest event ts: 963732.447752\n"
1174 "now ts: 963832.146824\n"
1175 "dropped events: 0\n"
1176 "read events: 42379\n");
1178 tw->ptr += tputl(fout, len);
1179 tw->ptr += tputs(fout, str);
1181 tw->ptr += tputh(fout, OPTION_DONE);
1187 * calc_min_depth() - Calculate the minimum call depth from the call list
1189 * Starting with a depth of 0, this works through the call list, adding 1 for
1190 * each function call and subtracting 1 for each function return. Most likely
1191 * the value ends up being negative, since the trace does not start at the
1192 * very top of the call stack, e.g. main(), but some function called by that.
1194 * This value can be used to calculate the depth value for the first call,
1195 * such that it never goes negative for subsequent returns.
1197 * Returns: minimum call depth (e.g. -2)
1199 static int calc_min_depth(void)
1201 struct trace_call *call;
1202 int depth, min_depth, i;
1204 /* Calculate minimum depth */
1207 for (i = 0, call = call_list; i < call_count; i++, call++) {
1208 switch (TRACE_CALL_TYPE(call)) {
1214 if (depth < min_depth)
1224 * write_pages() - Write the pages of trace data
1226 * This works through all the calls, writing out as many pages of data as are
1229 * @tw: Writer context
1230 * @out_format: Output format to use
1231 * @missing_countp: Returns number of missing functions (not found in function
1233 * @skip_countp: Returns number of skipped functions (excluded from trace)
1235 * Returns: 0 on success, -ve on error
1237 static int write_pages(struct twriter *tw, enum out_format_t out_format,
1238 int *missing_countp, int *skip_countp)
1240 ulong func_stack[MAX_STACK_DEPTH];
1241 int stack_ptr; /* next free position in stack */
1242 int upto, depth, page_upto, i;
1243 int missing_count = 0, skip_count = 0;
1244 struct trace_call *call;
1245 ulong last_timestamp;
1246 FILE *fout = tw->fout;
1257 /* maintain a stack of start times for calling functions */
1261 * The first thing in the trace may not be the top-level function, so
1262 * set the initial depth so that no function goes below depth 0
1264 depth = -calc_min_depth();
1265 for (i = 0, call = call_list; i < call_count; i++, call++) {
1266 bool entry = TRACE_CALL_TYPE(call) == FUNCF_ENTRY;
1267 struct func_info *func;
1272 func = find_func_by_offset(call->func);
1274 warn("Cannot find function at %lx\n",
1275 text_offset + call->func);
1277 if (missing_count > 20) {
1278 /* perhaps trace does not match System.map */
1279 fprintf(stderr, "Too many missing functions\n");
1285 if (!(func->flags & FUNCF_TRACE)) {
1286 debug("Funcion '%s' is excluded from trace\n",
1292 if (out_format == OUT_FMT_FUNCTION)
1294 else /* 2 header words and then 3 or 8 others */
1295 rec_words = 2 + (entry ? 3 : 8);
1297 /* convert timestamp from us to ns */
1298 timestamp = call->flags & FUNCF_TIMESTAMP_MASK;
1300 if (page_upto + rec_words * 4 > TRACE_PAGE_SIZE) {
1301 if (finish_page(tw))
1307 if (start_page(tw, timestamp))
1310 last_timestamp = timestamp;
1312 page_upto = tw->ptr & TRACE_PAGE_MASK;
1315 "new page, last_timestamp=%ld, upto=%d\n",
1316 last_timestamp, upto);
1320 delta = timestamp - last_timestamp;
1322 fprintf(stderr, "Time went backwards\n");
1326 if (err_count > 20) {
1327 fprintf(stderr, "Too many errors, giving up\n");
1331 if (delta > 0x07fffff) {
1333 * hard to imagine how this could happen since it means
1334 * that no function calls were made for a long time
1336 fprintf(stderr, "cannot represent time delta %x\n",
1341 if (out_format == OUT_FMT_FUNCTION) {
1342 struct func_info *caller_func;
1345 fprintf(stderr, "%d: delta=%d, stamp=%ld\n",
1346 upto, delta, timestamp);
1348 " last_delta %x to %x: last_timestamp=%lx, "
1349 "timestamp=%lx, call->flags=%x, upto=%d\n",
1350 last_delta, delta, last_timestamp,
1351 timestamp, call->flags, upto);
1354 /* type_len is 6, meaning 4 * 6 = 24 bytes */
1355 tw->ptr += tputl(fout, rec_words | (uint)delta << 5);
1356 tw->ptr += tputh(fout, TRACE_FN);
1357 tw->ptr += tputh(fout, 0); /* flags */
1358 tw->ptr += tputl(fout, TRACE_PID); /* PID */
1360 tw->ptr += tputq(fout, text_offset + func->offset);
1361 caller_func = find_caller_by_offset(call->caller);
1363 tw->ptr += tputq(fout,
1364 text_offset + caller_func->offset);
1366 tw->ptr += tputl(fout, rec_words | delta << 5);
1367 tw->ptr += tputh(fout, entry ? TRACE_GRAPH_ENT
1369 tw->ptr += tputh(fout, 0); /* flags */
1370 tw->ptr += tputl(fout, TRACE_PID); /* PID */
1372 tw->ptr += tputq(fout, text_offset + func->offset);
1373 tw->ptr += tputl(fout, depth); /* depth */
1376 if (stack_ptr < MAX_STACK_DEPTH)
1377 func_stack[stack_ptr] = timestamp;
1380 ulong func_duration = 0;
1383 if (stack_ptr && stack_ptr <= MAX_STACK_DEPTH) {
1384 ulong start = func_stack[--stack_ptr];
1386 func_duration = timestamp - start;
1388 tw->ptr += tputl(fout, 0); /* overrun */
1389 tw->ptr += tputq(fout, 0); /* calltime */
1391 tw->ptr += tputq(fout, func_duration);
1396 last_timestamp = timestamp;
1397 page_upto += 4 + rec_words * 4;
1399 if (stack_ptr == MAX_STACK_DEPTH)
1402 if (in_page && finish_page(tw))
1404 *missing_countp = missing_count;
1405 *skip_countp = skip_count;
1411 * write_flyrecord() - Write the flyrecord information
1413 * Writes the header and pages of data for the "flyrecord" section. It also
1414 * writes out the counter-type info, selecting "[local]"
1416 * @tw: Writer context
1417 * @out_format: Output format to use
1418 * @missing_countp: Returns number of missing functions (not found in function
1420 * @skip_countp: Returns number of skipped functions (excluded from trace)
1422 * Returns: 0 on success, -ve on error
1424 static int write_flyrecord(struct twriter *tw, enum out_format_t out_format,
1425 int *missing_countp, int *skip_countp)
1427 int start, ret, len;
1428 FILE *fout = tw->fout;
1431 tw->ptr += fprintf(fout, "flyrecord%c", 0);
1434 start = ALIGN(tw->ptr + 16, TRACE_PAGE_SIZE);
1435 tw->ptr += tputq(fout, start);
1437 /* use a placeholder for the size */
1438 ret = push_len(tw, start, "flyrecord", 8);
1443 snprintf(str, sizeof(str),
1444 "[local] global counter uptime perf mono mono_raw boot x86-tsc\n");
1446 tw->ptr += tputq(fout, len);
1447 tw->ptr += tputs(fout, str);
1449 debug("trace text base %lx, map file %lx\n", text_base, text_offset);
1451 ret = write_pages(tw, out_format, missing_countp, skip_countp);
1453 fprintf(stderr, "Cannot output pages\n");
1457 ret = pop_len(tw, "flyrecord");
1459 fprintf(stderr, "Cannot finish flyrecord header\n");
1467 * make_ftrace() - Write out an ftrace file
1469 * See here for format:
1471 * https://github.com/rostedt/trace-cmd/blob/master/Documentation/trace-cmd/trace-cmd.dat.v7.5.txt
1473 * @fout: Output file
1474 * @out_format: Output format to use
1475 * Returns: 0 on success, -ve on error
1477 static int make_ftrace(FILE *fout, enum out_format_t out_format)
1479 int missing_count, skip_count;
1480 struct twriter tws, *tw = &tws;
1483 memset(tw, '\0', sizeof(*tw));
1484 abuf_init(&tw->str_buf);
1488 ret = output_headers(tw);
1490 fprintf(stderr, "Cannot output headers\n");
1493 /* number of event systems files */
1494 tw->ptr += tputl(fout, 0);
1496 ret = write_symbols(tw);
1498 fprintf(stderr, "Cannot write symbols\n");
1502 ret = write_options(tw);
1504 fprintf(stderr, "Cannot write options\n");
1508 ret = write_flyrecord(tw, out_format, &missing_count, &skip_count);
1510 fprintf(stderr, "Cannot write flyrecord\n");
1514 info("ftrace: %d functions not found, %d excluded\n", missing_count,
1521 * prof_tool() - Performs requested action
1523 * @argc: Number of arguments (used to obtain the command
1524 * @argv: List of arguments
1525 * @trace_fname: Filename of input file (trace data from U-Boot)
1526 * @map_fname: Filename of map file (System.map from U-Boot)
1527 * @trace_config_fname: Trace-configuration file, or NULL if none
1528 * @out_fname: Output filename
1530 static int prof_tool(int argc, char *const argv[],
1531 const char *trace_fname, const char *map_fname,
1532 const char *trace_config_fname, const char *out_fname,
1533 enum out_format_t out_format)
1537 if (read_map_file(map_fname))
1539 if (trace_fname && read_trace_file(trace_fname))
1541 if (trace_config_fname && read_trace_config_file(trace_config_fname))
1544 check_trace_config();
1546 for (; argc; argc--, argv++) {
1547 const char *cmd = *argv;
1549 if (!strcmp(cmd, "dump-ftrace")) {
1552 if (out_format != OUT_FMT_FUNCTION &&
1553 out_format != OUT_FMT_FUNCGRAPH)
1554 out_format = OUT_FMT_FUNCTION;
1555 fout = fopen(out_fname, "w");
1557 fprintf(stderr, "Cannot write file '%s'\n",
1561 err = make_ftrace(fout, out_format);
1564 warn("Unknown command '%s'\n", cmd);
1571 int main(int argc, char *argv[])
1573 enum out_format_t out_format = OUT_FMT_DEFAULT;
1574 const char *map_fname = "System.map";
1575 const char *trace_fname = NULL;
1576 const char *config_fname = NULL;
1577 const char *out_fname = NULL;
1581 while ((opt = getopt(argc, argv, "c:f:m:o:t:v:")) != -1) {
1584 config_fname = optarg;
1587 if (!strcmp("function", optarg)) {
1588 out_format = OUT_FMT_FUNCTION;
1589 } else if (!strcmp("funcgraph", optarg)) {
1590 out_format = OUT_FMT_FUNCGRAPH;
1593 "Invalid format: use function, funcgraph, calls, timing\n");
1604 trace_fname = optarg;
1607 verbose = atoi(optarg);
1613 argc -= optind; argv += optind;
1617 if (!out_fname || !map_fname || !trace_fname) {
1619 "Must provide trace data, System.map file and output file\n");
1623 debug("Debug enabled\n");
1624 return prof_tool(argc, argv, trace_fname, map_fname, config_fname,
1625 out_fname, out_format);