blob: c7b427f30786777aa6eb9c88513fd1c8b1e25acf [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass9c628922013-06-11 11:14:43 -07002/*
Simon Glassdd8f19a2023-01-15 14:15:53 -07003 * Copyright 2023 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass9c628922013-06-11 11:14:43 -07005 */
6
Simon Glassdd8f19a2023-01-15 14:15:53 -07007/*
8 * Decode and dump U-Boot trace information into formats that can be used
Simon Glassd854ce42023-01-15 14:15:57 -07009 * by trace-cmd, kernelshark or flamegraph.pl
Simon Glassdd8f19a2023-01-15 14:15:53 -070010 *
11 * See doc/develop/trace.rst for more information
12 */
Simon Glass9c628922013-06-11 11:14:43 -070013
14#include <assert.h>
15#include <ctype.h>
16#include <limits.h>
17#include <regex.h>
18#include <stdarg.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include <sys/param.h>
Jörg Krause983487f2015-04-22 21:36:22 +020024#include <sys/types.h>
Simon Glass9c628922013-06-11 11:14:43 -070025
26#include <compiler.h>
27#include <trace.h>
Simon Glassdd8f19a2023-01-15 14:15:53 -070028#include <abuf.h>
Simon Glass9c628922013-06-11 11:14:43 -070029
Simon Glassd854ce42023-01-15 14:15:57 -070030#include <linux/list.h>
31
Simon Glassdd8f19a2023-01-15 14:15:53 -070032/* Set to 1 to emit version 7 file (currently this doesn't work) */
33#define VERSION7 0
34
35/* enable some debug features */
36#define _DEBUG 0
37
38/* from linux/kernel.h */
39#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
40#define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a) - 1)
Simon Glass9c628922013-06-11 11:14:43 -070041
Simon Glassd854ce42023-01-15 14:15:57 -070042/**
43 * container_of - cast a member of a structure out to the containing structure
44 * @ptr: the pointer to the member.
45 * @type: the type of the container struct this is embedded in.
46 * @member: the name of the member within the struct.
47 *
48 * (this is needed by list.h)
49 */
50#define container_of(ptr, type, member) ({ \
51 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
52 (type *)( (char *)__mptr - offsetof(type,member) );})
53
Simon Glass9c628922013-06-11 11:14:43 -070054enum {
55 FUNCF_TRACE = 1 << 0, /* Include this function in trace */
Simon Glassdd8f19a2023-01-15 14:15:53 -070056 TRACE_PAGE_SIZE = 4096, /* Assumed page size for trace */
57 TRACE_PID = 1, /* PID to use for U-Boot */
58 LEN_STACK_SIZE = 4, /* number of nested length fix-ups */
59 TRACE_PAGE_MASK = TRACE_PAGE_SIZE - 1,
60 MAX_STACK_DEPTH = 50, /* Max nested function calls */
61 MAX_LINE_LEN = 500, /* Max characters per line */
62};
63
Simon Glass31fb49a2023-01-15 14:15:56 -070064/**
65 * enum out_format_t - supported output formats
66 *
67 * @OUT_FMT_DEFAULT: Use the default for the output file
68 * @OUT_FMT_FUNCTION: Write ftrace 'function' records
69 * @OUT_FMT_FUNCGRAPH: Write ftrace funcgraph_entry and funcgraph_exit records
Simon Glass143058f2023-01-15 14:15:58 -070070 * @OUT_FMT_FLAMEGRAPH_CALLS: Write a file suitable for flamegraph.pl
71 * @OUT_FMT_FLAMEGRAPH_TIMING: Write a file suitable for flamegraph.pl with the
72 * counts set to the number of microseconds used by each function
Simon Glass31fb49a2023-01-15 14:15:56 -070073 */
74enum out_format_t {
75 OUT_FMT_DEFAULT,
76 OUT_FMT_FUNCTION,
77 OUT_FMT_FUNCGRAPH,
Simon Glass143058f2023-01-15 14:15:58 -070078 OUT_FMT_FLAMEGRAPH_CALLS,
79 OUT_FMT_FLAMEGRAPH_TIMING,
Simon Glass31fb49a2023-01-15 14:15:56 -070080};
81
Simon Glassdd8f19a2023-01-15 14:15:53 -070082/* Section types for v7 format (trace-cmd format) */
83enum {
84 SECTION_OPTIONS,
85};
86
87/* Option types (trace-cmd format) */
88enum {
89 OPTION_DONE,
90 OPTION_DATE,
91 OPTION_CPUSTAT,
92 OPTION_BUFFER,
93 OPTION_TRACECLOCK,
94 OPTION_UNAME,
95 OPTION_HOOK,
96 OPTION_OFFSET,
97 OPTION_CPUCOUNT,
98 OPTION_VERSION,
99 OPTION_PROCMAPS,
100 OPTION_TRACEID,
101 OPTION_TIME_SHIFT,
102 OPTION_GUEST,
103 OPTION_TSC2NSEC,
104};
105
106/* types of trace records (trace-cmd format) */
107enum trace_type {
108 __TRACE_FIRST_TYPE = 0,
109
110 TRACE_FN,
111 TRACE_CTX,
112 TRACE_WAKE,
113 TRACE_STACK,
114 TRACE_PRINT,
115 TRACE_BPRINT,
116 TRACE_MMIO_RW,
117 TRACE_MMIO_MAP,
118 TRACE_BRANCH,
119 TRACE_GRAPH_RET,
120 TRACE_GRAPH_ENT,
Simon Glass9c628922013-06-11 11:14:43 -0700121};
122
Simon Glass9e2afae2023-01-15 14:15:52 -0700123/**
Simon Glassd854ce42023-01-15 14:15:57 -0700124 * struct flame_node - a node in the call-stack tree
125 *
126 * Each stack frame detected in the trace is given a node corresponding to a
127 * function call in the call stack. Functions can appear multiple times when
128 * they are called by a different set of parent functions.
129 *
130 * @parent: Parent node (the call stack for the function that called this one)
131 * @child_head: List of children of this node (functions called from here)
132 * @sibling: Next node in the list of children
133 * @func: Function this node refers to (NULL for root node)
134 * @count: Number of times this call-stack occurred
Simon Glass143058f2023-01-15 14:15:58 -0700135 * @duration: Number of microseconds taken to run this function, excluding all
136 * of the functions it calls
Simon Glassd854ce42023-01-15 14:15:57 -0700137 */
138struct flame_node {
139 struct flame_node *parent;
140 struct list_head child_head;
141 struct list_head sibling_node;
142 struct func_info *func;
143 int count;
Simon Glass143058f2023-01-15 14:15:58 -0700144 ulong duration;
Simon Glassd854ce42023-01-15 14:15:57 -0700145};
146
147/**
148 * struct flame_state - state information for building the flame graph
149 *
150 * @node: Current node being processed (corresponds to a function call)
Simon Glass143058f2023-01-15 14:15:58 -0700151 * @stack: Stack of call-start time for this function as well as the
152 * accumulated total time of all child calls (so we can subtract them from the
153 * function's call time. This is an 'empty' stack, meaning that @stack_ptr
154 * points to the next available stack position
155 * @stack_ptr: points to first empty position in the stack
Simon Glassd854ce42023-01-15 14:15:57 -0700156 * @nodes: Number of nodes created (running count)
157 */
158struct flame_state {
159 struct flame_node *node;
Simon Glass143058f2023-01-15 14:15:58 -0700160 struct stack_info {
161 ulong timestamp;
162 ulong child_total;
163 } stack[MAX_STACK_DEPTH];
164 int stack_ptr;
Simon Glassd854ce42023-01-15 14:15:57 -0700165 int nodes;
166};
167
168/**
Simon Glass9e2afae2023-01-15 14:15:52 -0700169 * struct func_info - information recorded for each function
170 *
171 * @offset: Function offset in the image, measured from the text_base
172 * @name: Function name
173 * @code_size: Total code size of the function
174 * @flags: Either 0 or FUNCF_TRACE
Simon Glass9e2afae2023-01-15 14:15:52 -0700175 */
Simon Glass9c628922013-06-11 11:14:43 -0700176struct func_info {
177 unsigned long offset;
178 const char *name;
179 unsigned long code_size;
Simon Glass9c628922013-06-11 11:14:43 -0700180 unsigned flags;
Simon Glass9c628922013-06-11 11:14:43 -0700181};
182
Simon Glass9e2afae2023-01-15 14:15:52 -0700183/**
184 * enum trace_line_type - whether to include or exclude a function
185 *
186 * @TRACE_LINE_INCLUDE: Include the function
187 * @TRACE_LINE_EXCLUDE: Exclude the function
188 */
Simon Glass9c628922013-06-11 11:14:43 -0700189enum trace_line_type {
190 TRACE_LINE_INCLUDE,
191 TRACE_LINE_EXCLUDE,
192};
193
Simon Glass9e2afae2023-01-15 14:15:52 -0700194/**
195 * struct trace_configline_info - information about a config-file line
196 *
197 * @next: Next line
198 * @type: Line type
199 * @name: identifier name / wildcard
200 * @regex: Regex to use if name starts with '/'
201 */
Simon Glass9c628922013-06-11 11:14:43 -0700202struct trace_configline_info {
203 struct trace_configline_info *next;
204 enum trace_line_type type;
Simon Glass9e2afae2023-01-15 14:15:52 -0700205 const char *name;
206 regex_t regex;
Simon Glass9c628922013-06-11 11:14:43 -0700207};
208
Simon Glassdd8f19a2023-01-15 14:15:53 -0700209/**
210 * struct tw_len - holds information about a length value that need fix-ups
211 *
212 * This is used to record a placeholder for a u32 or u64 length which is written
213 * to the output file but needs to be updated once the length is actually known
214 *
215 * This allows us to write tw->ptr - @len_base to position @ptr in the file
216 *
217 * @ptr: Position of the length value in the file
218 * @base: Base position for the calculation
219 * @size: Size of the length value, in bytes (4 or 8)
220 */
221struct tw_len {
222 int ptr;
223 int base;
224 int size;
225};
226
227/**
228 * struct twriter - Writer for trace records
229 *
230 * Maintains state used when writing the output file in trace-cmd format
231 *
232 * @ptr: Current file position
233 * @len_stack: Stack of length values that need fixing up
234 * @len: Number of items on @len_stack
235 * @str_buf: Buffer of strings (for v7 format)
236 * @str_ptr: Current write-position in the buffer for strings
237 * @fout: Output file
238 */
239struct twriter {
240 int ptr;
241 struct tw_len len_stack[LEN_STACK_SIZE];
242 int len_count;
243 struct abuf str_buf;
244 int str_ptr;
245 FILE *fout;
246};
247
Simon Glass9c628922013-06-11 11:14:43 -0700248/* The contents of the trace config file */
249struct trace_configline_info *trace_config_head;
250
Simon Glass9e2afae2023-01-15 14:15:52 -0700251/* list of all functions in System.map file, sorted by offset in the image */
Simon Glass9c628922013-06-11 11:14:43 -0700252struct func_info *func_list;
Simon Glass9e2afae2023-01-15 14:15:52 -0700253
254int func_count; /* number of functions */
255struct trace_call *call_list; /* list of all calls in the input trace file */
256int call_count; /* number of calls */
Simon Glass9c628922013-06-11 11:14:43 -0700257int verbose; /* Verbosity level 0=none, 1=warn, 2=notice, 3=info, 4=debug */
Simon Glass9e2afae2023-01-15 14:15:52 -0700258ulong text_offset; /* text address of first function */
Simon Glass7ac0a502023-01-15 14:15:55 -0700259ulong text_base; /* CONFIG_TEXT_BASE from trace file */
Simon Glass9c628922013-06-11 11:14:43 -0700260
Simon Glass9e2afae2023-01-15 14:15:52 -0700261/* debugging helpers */
Simon Glass9c628922013-06-11 11:14:43 -0700262static void outf(int level, const char *fmt, ...)
263 __attribute__ ((format (__printf__, 2, 3)));
264#define error(fmt, b...) outf(0, fmt, ##b)
265#define warn(fmt, b...) outf(1, fmt, ##b)
266#define notice(fmt, b...) outf(2, fmt, ##b)
267#define info(fmt, b...) outf(3, fmt, ##b)
268#define debug(fmt, b...) outf(4, fmt, ##b)
269
Simon Glass9c628922013-06-11 11:14:43 -0700270static void outf(int level, const char *fmt, ...)
271{
272 if (verbose >= level) {
273 va_list args;
274
275 va_start(args, fmt);
276 vfprintf(stderr, fmt, args);
277 va_end(args);
278 }
279}
280
281static void usage(void)
282{
283 fprintf(stderr,
Simon Glassec86a632022-12-21 16:08:24 -0700284 "Usage: proftool [-cmtv] <cmd> <profdata>\n"
Simon Glass9c628922013-06-11 11:14:43 -0700285 "\n"
286 "Commands\n"
Simon Glass9e2afae2023-01-15 14:15:52 -0700287 " dump-ftrace\t\tDump out records in ftrace format for use by trace-cmd\n"
Simon Glassd854ce42023-01-15 14:15:57 -0700288 " dump-flamegraph\tWrite a file for use with flamegraph.pl\n"
Simon Glass9c628922013-06-11 11:14:43 -0700289 "\n"
290 "Options:\n"
Simon Glass9e2afae2023-01-15 14:15:52 -0700291 " -c <cfg>\tSpecify config file\n"
Simon Glass31fb49a2023-01-15 14:15:56 -0700292 " -f <subtype>\tSpecify output subtype\n"
Vincent Stehléaa3dc332024-04-02 13:29:16 +0200293 " -m <map>\tSpecify System.map file\n"
Simon Glass9e2afae2023-01-15 14:15:52 -0700294 " -o <fname>\tSpecify output file\n"
Simon Glassec86a632022-12-21 16:08:24 -0700295 " -t <fname>\tSpecify trace data file (from U-Boot 'trace calls')\n"
Simon Glass31fb49a2023-01-15 14:15:56 -0700296 " -v <0-4>\tSpecify verbosity\n"
297 "\n"
298 "Subtypes for dump-ftrace:\n"
299 " function - write function-call records (caller/callee)\n"
Simon Glass143058f2023-01-15 14:15:58 -0700300 " funcgraph - write function entry/exit records (graph)\n"
301 "\n"
302 "Subtypes for dump-flamegraph\n"
303 " calls - create a flamegraph of stack frames\n"
304 " timing - create a flamegraph of microseconds for each stack frame\n");
Simon Glass9c628922013-06-11 11:14:43 -0700305 exit(EXIT_FAILURE);
306}
307
Simon Glass9e2afae2023-01-15 14:15:52 -0700308/**
Vincent Stehléaa3dc332024-04-02 13:29:16 +0200309 * h_cmp_offset - bsearch() function to compare two functions by their offset
Simon Glass9e2afae2023-01-15 14:15:52 -0700310 *
311 * @v1: Pointer to first function (struct func_info)
312 * @v2: Pointer to second function (struct func_info)
313 * Returns: < 0 if v1 offset < v2 offset, 0 if equal, > 0 otherwise
314 */
Simon Glass9c628922013-06-11 11:14:43 -0700315static int h_cmp_offset(const void *v1, const void *v2)
316{
317 const struct func_info *f1 = v1, *f2 = v2;
318
319 return (f1->offset / FUNC_SITE_SIZE) - (f2->offset / FUNC_SITE_SIZE);
320}
321
Simon Glass9e2afae2023-01-15 14:15:52 -0700322/**
323 * read_system_map() - read the System.map file to create a list of functions
324 *
325 * This also reads the text_offset value, since we assume that the first text
326 * symbol is at that address
327 *
328 * @fin: File to read
329 * Returns: 0 if OK, non-zero on error
330 */
Simon Glass9c628922013-06-11 11:14:43 -0700331static int read_system_map(FILE *fin)
332{
333 unsigned long offset, start = 0;
334 struct func_info *func;
335 char buff[MAX_LINE_LEN];
336 char symtype;
337 char symname[MAX_LINE_LEN + 1];
338 int linenum;
339 int alloced;
340
341 for (linenum = 1, alloced = func_count = 0;; linenum++) {
342 int fields = 0;
343
344 if (fgets(buff, sizeof(buff), fin))
345 fields = sscanf(buff, "%lx %c %100s\n", &offset,
346 &symtype, symname);
347 if (fields == 2) {
348 continue;
349 } else if (feof(fin)) {
350 break;
351 } else if (fields < 2) {
352 error("Map file line %d: invalid format\n", linenum);
353 return 1;
354 }
355
356 /* Must be a text symbol */
357 symtype = tolower(symtype);
358 if (symtype != 't' && symtype != 'w')
359 continue;
360
361 if (func_count == alloced) {
362 alloced += 256;
363 func_list = realloc(func_list,
364 sizeof(struct func_info) * alloced);
365 assert(func_list);
366 }
367 if (!func_count)
368 start = offset;
369
370 func = &func_list[func_count++];
371 memset(func, '\0', sizeof(*func));
372 func->offset = offset - start;
373 func->name = strdup(symname);
374 func->flags = FUNCF_TRACE; /* trace by default */
375
376 /* Update previous function's code size */
377 if (func_count > 1)
378 func[-1].code_size = func->offset - func[-1].offset;
379 }
Simon Glass7ac0a502023-01-15 14:15:55 -0700380 notice("%d functions found in map file, start addr %lx\n", func_count,
381 start);
Simon Glass9c628922013-06-11 11:14:43 -0700382 text_offset = start;
Simon Glass9e2afae2023-01-15 14:15:52 -0700383
Simon Glass9c628922013-06-11 11:14:43 -0700384 return 0;
385}
386
387static int read_data(FILE *fin, void *buff, int size)
388{
389 int err;
390
391 err = fread(buff, 1, size, fin);
392 if (!err)
393 return 1;
394 if (err != size) {
Simon Glass9e2afae2023-01-15 14:15:52 -0700395 error("Cannot read trace file at pos %lx\n", ftell(fin));
Simon Glass9c628922013-06-11 11:14:43 -0700396 return -1;
397 }
398 return 0;
399}
400
Simon Glass9e2afae2023-01-15 14:15:52 -0700401/**
402 * find_func_by_offset() - Look up a function by its offset
403 *
404 * @offset: Offset to search for, from text_base
405 * Returns: function, if found, else NULL
406 *
407 * This does a fast search for a function given its offset from text_base
408 *
409 */
410static struct func_info *find_func_by_offset(uint offset)
Simon Glass9c628922013-06-11 11:14:43 -0700411{
412 struct func_info key, *found;
413
414 key.offset = offset;
415 found = bsearch(&key, func_list, func_count, sizeof(struct func_info),
416 h_cmp_offset);
417
418 return found;
419}
420
Simon Glass9e2afae2023-01-15 14:15:52 -0700421/**
422 * find_caller_by_offset() - finds the function which contains the given offset
423 *
424 * @offset: Offset to search for, from text_base
425 * Returns: function, if found, else NULL
426 *
427 * If the offset falls between two functions, then it is assumed to belong to
428 * the first function (with the lowest offset). This is a way of figuring out
429 * which function owns code at a particular offset
430 */
431static struct func_info *find_caller_by_offset(uint offset)
Simon Glass9c628922013-06-11 11:14:43 -0700432{
433 int low; /* least function that could be a match */
Vincent Stehléaa3dc332024-04-02 13:29:16 +0200434 int high; /* greatest function that could be a match */
Simon Glass9c628922013-06-11 11:14:43 -0700435 struct func_info key;
436
437 low = 0;
438 high = func_count - 1;
439 key.offset = offset;
440 while (high > low + 1) {
441 int mid = (low + high) / 2;
442 int result;
443
444 result = h_cmp_offset(&key, &func_list[mid]);
445 if (result > 0)
446 low = mid;
447 else if (result < 0)
448 high = mid;
449 else
450 return &func_list[mid];
451 }
452
453 return low >= 0 ? &func_list[low] : NULL;
454}
455
Simon Glass9e2afae2023-01-15 14:15:52 -0700456/**
457 * read_calls() - Read the list of calls from the trace data
458 *
459 * The calls are stored consecutively in the trace output produced by U-Boot
460 *
461 * @fin: File to read from
462 * @count: Number of calls to read
463 * Returns: 0 if OK, -1 on error
464 */
Heinrich Schuchardt14690902019-06-14 21:50:55 +0200465static int read_calls(FILE *fin, size_t count)
Simon Glass9c628922013-06-11 11:14:43 -0700466{
467 struct trace_call *call_data;
468 int i;
469
Heinrich Schuchardt14690902019-06-14 21:50:55 +0200470 notice("call count: %zu\n", count);
Simon Glass9c628922013-06-11 11:14:43 -0700471 call_list = (struct trace_call *)calloc(count, sizeof(*call_data));
472 if (!call_list) {
473 error("Cannot allocate call_list\n");
474 return -1;
475 }
476 call_count = count;
477
478 call_data = call_list;
479 for (i = 0; i < count; i++, call_data++) {
480 if (read_data(fin, call_data, sizeof(*call_data)))
Simon Glass9e2afae2023-01-15 14:15:52 -0700481 return -1;
Simon Glass9c628922013-06-11 11:14:43 -0700482 }
483 return 0;
484}
485
Simon Glass9e2afae2023-01-15 14:15:52 -0700486/**
487 * read_trace() - Read the U-Boot trace file
488 *
489 * Read in the calls from the trace file. The function list is ignored at
490 * present
491 *
492 * @fin: File to read
493 * Returns 0 if OK, non-zero on error
494 */
495static int read_trace(FILE *fin)
Simon Glass9c628922013-06-11 11:14:43 -0700496{
497 struct trace_output_hdr hdr;
498
Simon Glass9c628922013-06-11 11:14:43 -0700499 while (!feof(fin)) {
500 int err;
501
502 err = read_data(fin, &hdr, sizeof(hdr));
503 if (err == 1)
504 break; /* EOF */
505 else if (err)
506 return 1;
Simon Glass7ac0a502023-01-15 14:15:55 -0700507 text_base = hdr.text_base;
Simon Glass9c628922013-06-11 11:14:43 -0700508
509 switch (hdr.type) {
510 case TRACE_CHUNK_FUNCS:
511 /* Ignored at present */
512 break;
513
514 case TRACE_CHUNK_CALLS:
515 if (read_calls(fin, hdr.rec_count))
516 return 1;
517 break;
518 }
519 }
520 return 0;
521}
522
Simon Glass9e2afae2023-01-15 14:15:52 -0700523/**
524 * read_map_file() - Read the System.map file
525 *
526 * This reads the file into the func_list array
527 *
528 * @fname: Filename to read
529 * Returns 0 if OK, non-zero on error
530 */
Simon Glass9c628922013-06-11 11:14:43 -0700531static int read_map_file(const char *fname)
532{
533 FILE *fmap;
534 int err = 0;
535
536 fmap = fopen(fname, "r");
537 if (!fmap) {
538 error("Cannot open map file '%s'\n", fname);
539 return 1;
540 }
541 if (fmap) {
542 err = read_system_map(fmap);
543 fclose(fmap);
544 }
545 return err;
546}
547
Simon Glass9e2afae2023-01-15 14:15:52 -0700548/**
549 * read_trace_file() - Open and read the U-Boot trace file
550 *
551 * Read in the calls from the trace file. The function list is ignored at
552 * present
553 *
554 * @fin: File to read
555 * Returns 0 if OK, non-zero on error
556 */
557static int read_trace_file(const char *fname)
Simon Glass9c628922013-06-11 11:14:43 -0700558{
Simon Glass9c628922013-06-11 11:14:43 -0700559 FILE *fprof;
560 int err;
561
562 fprof = fopen(fname, "rb");
563 if (!fprof) {
Simon Glass9e2afae2023-01-15 14:15:52 -0700564 error("Cannot open trace data file '%s'\n",
Simon Glass9c628922013-06-11 11:14:43 -0700565 fname);
566 return 1;
567 } else {
Simon Glass9e2afae2023-01-15 14:15:52 -0700568 err = read_trace(fprof);
Simon Glass9c628922013-06-11 11:14:43 -0700569 fclose(fprof);
570 if (err)
571 return err;
Simon Glass9c628922013-06-11 11:14:43 -0700572 }
573 return 0;
574}
575
576static int regex_report_error(regex_t *regex, int err, const char *op,
577 const char *name)
578{
579 char buf[200];
580
581 regerror(err, regex, buf, sizeof(buf));
582 error("Regex error '%s' in %s '%s'\n", buf, op, name);
583 return -1;
584}
585
586static void check_trace_config_line(struct trace_configline_info *item)
587{
588 struct func_info *func, *end;
589 int err;
590
591 debug("Checking trace config line '%s'\n", item->name);
592 for (func = func_list, end = func + func_count; func < end; func++) {
593 err = regexec(&item->regex, func->name, 0, NULL, 0);
594 debug(" - regex '%s', string '%s': %d\n", item->name,
595 func->name, err);
596 if (err == REG_NOMATCH)
597 continue;
598
Andreas Bießmann8a0103f2013-07-02 08:37:36 +0200599 if (err) {
Simon Glass9c628922013-06-11 11:14:43 -0700600 regex_report_error(&item->regex, err, "match",
601 item->name);
602 break;
603 }
604
605 /* It matches, so perform the action */
606 switch (item->type) {
607 case TRACE_LINE_INCLUDE:
608 info(" include %s at %lx\n", func->name,
609 text_offset + func->offset);
610 func->flags |= FUNCF_TRACE;
611 break;
612
613 case TRACE_LINE_EXCLUDE:
614 info(" exclude %s at %lx\n", func->name,
615 text_offset + func->offset);
616 func->flags &= ~FUNCF_TRACE;
617 break;
618 }
619 }
620}
621
Simon Glass9e2afae2023-01-15 14:15:52 -0700622/** check_trace_config() - Check trace-config file, reporting any problems */
Simon Glass9c628922013-06-11 11:14:43 -0700623static void check_trace_config(void)
624{
625 struct trace_configline_info *line;
626
627 for (line = trace_config_head; line; line = line->next)
628 check_trace_config_line(line);
629}
630
631/**
Simon Glass9e2afae2023-01-15 14:15:52 -0700632 * read_trace_config() - read the trace-config file
633 *
634 * This file consists of lines like:
635 *
636 * include-func <regex>
637 * exclude-func <regex>
638 *
639 * where <regex> is a regular expression matched against function names. It
640 * allows some functions to be dropped from the trace when producing ftrace
641 * records
642 *
643 * @fin: File to process
644 * Returns: 0 if OK, -1 on error
645 */
Simon Glass9c628922013-06-11 11:14:43 -0700646static int read_trace_config(FILE *fin)
647{
648 char buff[200];
649 int linenum = 0;
650 struct trace_configline_info **tailp = &trace_config_head;
651
652 while (fgets(buff, sizeof(buff), fin)) {
653 int len = strlen(buff);
654 struct trace_configline_info *line;
655 char *saveptr;
656 char *s, *tok;
657 int err;
658
659 linenum++;
660 if (len && buff[len - 1] == '\n')
661 buff[len - 1] = '\0';
662
663 /* skip blank lines and comments */
664 for (s = buff; *s == ' ' || *s == '\t'; s++)
665 ;
666 if (!*s || *s == '#')
667 continue;
668
Simon Glass9e2afae2023-01-15 14:15:52 -0700669 line = (struct trace_configline_info *)calloc(1, sizeof(*line));
Simon Glass9c628922013-06-11 11:14:43 -0700670 if (!line) {
671 error("Cannot allocate config line\n");
672 return -1;
673 }
674
675 tok = strtok_r(s, " \t", &saveptr);
676 if (!tok) {
677 error("Invalid trace config data on line %d\n",
678 linenum);
Maks Mishin6e5b3582025-02-05 19:26:07 +0300679 free(line);
Simon Glass9c628922013-06-11 11:14:43 -0700680 return -1;
681 }
682 if (0 == strcmp(tok, "include-func")) {
683 line->type = TRACE_LINE_INCLUDE;
684 } else if (0 == strcmp(tok, "exclude-func")) {
685 line->type = TRACE_LINE_EXCLUDE;
686 } else {
687 error("Unknown command in trace config data line %d\n",
688 linenum);
Maks Mishin6e5b3582025-02-05 19:26:07 +0300689 free(line);
Simon Glass9c628922013-06-11 11:14:43 -0700690 return -1;
691 }
692
693 tok = strtok_r(NULL, " \t", &saveptr);
694 if (!tok) {
695 error("Missing pattern in trace config data line %d\n",
696 linenum);
Maks Mishin6e5b3582025-02-05 19:26:07 +0300697 free(line);
Simon Glass9c628922013-06-11 11:14:43 -0700698 return -1;
699 }
700
701 err = regcomp(&line->regex, tok, REG_NOSUB);
702 if (err) {
Vincent Stehlé451b1fc2015-10-07 15:48:48 +0200703 int r = regex_report_error(&line->regex, err,
704 "compile", tok);
Simon Glass9c628922013-06-11 11:14:43 -0700705 free(line);
Vincent Stehlé451b1fc2015-10-07 15:48:48 +0200706 return r;
Simon Glass9c628922013-06-11 11:14:43 -0700707 }
708
709 /* link this new one to the end of the list */
710 line->name = strdup(tok);
711 line->next = NULL;
712 *tailp = line;
713 tailp = &line->next;
714 }
715
716 if (!feof(fin)) {
717 error("Cannot read from trace config file at position %ld\n",
718 ftell(fin));
719 return -1;
720 }
721 return 0;
722}
723
724static int read_trace_config_file(const char *fname)
725{
726 FILE *fin;
727 int err;
728
729 fin = fopen(fname, "r");
730 if (!fin) {
731 error("Cannot open trace_config file '%s'\n", fname);
732 return -1;
733 }
734 err = read_trace_config(fin);
735 fclose(fin);
736 return err;
737}
738
Simon Glassdd8f19a2023-01-15 14:15:53 -0700739/**
740 * tputh() - Write a 16-bit little-endian value to a file
741 *
742 * @fout: File to write to
743 * @val: Value to write
744 * Returns: number of bytes written (2)
745 */
746static int tputh(FILE *fout, unsigned int val)
Simon Glass9c628922013-06-11 11:14:43 -0700747{
Simon Glassdd8f19a2023-01-15 14:15:53 -0700748 fputc(val, fout);
749 fputc(val >> 8, fout);
Simon Glass9c628922013-06-11 11:14:43 -0700750
Simon Glassdd8f19a2023-01-15 14:15:53 -0700751 return 2;
752}
Simon Glass9c628922013-06-11 11:14:43 -0700753
Simon Glassdd8f19a2023-01-15 14:15:53 -0700754/**
755 * tputl() - Write a 32-bit little-endian value to a file
756 *
757 * @fout: File to write to
758 * @val: Value to write
759 * Returns: number of bytes written (4)
760 */
761static int tputl(FILE *fout, ulong val)
762{
763 fputc(val, fout);
764 fputc(val >> 8, fout);
765 fputc(val >> 16, fout);
766 fputc(val >> 24, fout);
767
768 return 4;
Simon Glass9c628922013-06-11 11:14:43 -0700769}
770
Simon Glassdd8f19a2023-01-15 14:15:53 -0700771/**
772 * tputh() - Write a 64-bit little-endian value to a file
773 *
774 * @fout: File to write to
775 * @val: Value to write
776 * Returns: number of bytes written (8)
Simon Glass9c628922013-06-11 11:14:43 -0700777 */
Simon Glassdd8f19a2023-01-15 14:15:53 -0700778static int tputq(FILE *fout, unsigned long long val)
Simon Glass9c628922013-06-11 11:14:43 -0700779{
Simon Glassdd8f19a2023-01-15 14:15:53 -0700780 tputl(fout, val);
781 tputl(fout, val >> 32U);
782
783 return 8;
784}
785
786/**
787 * tputh() - Write a string to a file
788 *
789 * The string is written without its terminator
790 *
791 * @fout: File to write to
792 * @val: Value to write
793 * Returns: number of bytes written
794 */
795static int tputs(FILE *fout, const char *str)
796{
797 fputs(str, fout);
798
799 return strlen(str);
800}
801
802/**
803 * add_str() - add a name string to the string table
804 *
805 * This is used by the v7 format
806 *
807 * @tw: Writer context
808 * @name: String to write
809 * Returns: Updated value of string pointer, or -1 if out of memory
810 */
811static int add_str(struct twriter *tw, const char *name)
812{
813 int str_ptr;
814 int len;
815
816 len = strlen(name) + 1;
817 str_ptr = tw->str_ptr;
818 tw->str_ptr += len;
819
820 if (tw->str_ptr > abuf_size(&tw->str_buf)) {
821 int new_size;
822
823 new_size = ALIGN(tw->str_ptr, 4096);
824 if (!abuf_realloc(&tw->str_buf, new_size))
825 return -1;
826 }
827
828 return str_ptr;
829}
830
831/**
832 * push_len() - Push a new length request onto the stack
833 *
834 * @tw: Writer context
835 * @base: Base position of the length calculation
836 * @msg: Indicates the type of caller, for debugging
837 * @size: Size of the length value, either 4 bytes or 8
838 * Returns number of bytes written to the file (=@size on success), -ve on error
839 *
840 * This marks a place where a length must be written, covering data that is
841 * about to be written. It writes a placeholder value.
842 *
843 * Once the data is written, calling pop_len() will update the placeholder with
844 * the correct length based on how many bytes have been written
845 */
846static int push_len(struct twriter *tw, int base, const char *msg, int size)
847{
848 struct tw_len *lp;
849
850 if (tw->len_count >= LEN_STACK_SIZE) {
851 fprintf(stderr, "Length-stack overflow: %s\n", msg);
852 return -1;
853 }
854 if (size != 4 && size != 8) {
855 fprintf(stderr, "Length-stack invalid size %d: %s\n", size,
856 msg);
857 return -1;
858 }
859
860 lp = &tw->len_stack[tw->len_count++];
861 lp->base = base;
862 lp->ptr = tw->ptr;
863 lp->size = size;
864
865 return size == 8 ? tputq(tw->fout, 0) : tputl(tw->fout, 0);
866}
867
868/**
869 * pop_len() - Update a length value once the length is known
870 *
871 * Pops a value of the length stack and updates the file at that position with
872 * the number of bytes written between now and then. Once done, the file is
873 * seeked to the current (tw->ptr) position again, so writing can continue as
874 * normal.
875 *
876 * @tw: Writer context
877 * @msg: Indicates the type of caller, for debugging
878 * Returns 0 if OK, -1 on error
879 */
880static int pop_len(struct twriter *tw, const char *msg)
881{
882 struct tw_len *lp;
883 int len, ret;
884
885 if (!tw->len_count) {
886 fprintf(stderr, "Length-stack underflow: %s\n", msg);
887 return -1;
888 }
889
890 lp = &tw->len_stack[--tw->len_count];
891 if (fseek(tw->fout, lp->ptr, SEEK_SET))
892 return -1;
893 len = tw->ptr - lp->base;
894 ret = lp->size == 8 ? tputq(tw->fout, len) : tputl(tw->fout, len);
895 if (ret < 0)
896 return -1;
897 if (fseek(tw->fout, tw->ptr, SEEK_SET))
898 return -1;
899
900 return 0;
901}
902
903/**
904 * start_header() - Start a v7 section
905 *
906 * Writes a header in v7 format
907 *
908 * @tw: Writer context
909 * @id: ID of header to write (SECTION_...)
910 * @flags: Flags value to write
911 * @name: Name of section
912 * Returns: number of bytes written
913 */
914static int start_header(struct twriter *tw, int id, uint flags,
915 const char *name)
916{
917 int str_id;
918 int lptr;
919 int base;
920 int ret;
921
922 base = tw->ptr + 16;
923 lptr = 0;
924 lptr += tputh(tw->fout, id);
925 lptr += tputh(tw->fout, flags);
926 str_id = add_str(tw, name);
927 if (str_id < 0)
928 return -1;
929 lptr += tputl(tw->fout, str_id);
930
931 /* placeholder for size */
932 ret = push_len(tw, base, "v7 header", 8);
933 if (ret < 0)
934 return -1;
935 lptr += ret;
936
937 return lptr;
938}
939
940/**
941 * start_page() - Start a new page of output data
942 *
943 * The output is arranged in 4KB pages with a base timestamp at the start of
944 * each. This starts a new page, making sure it is aligned to 4KB in the output
945 * file.
946 *
947 * @tw: Writer context
948 * @timestamp: Base timestamp for the page
949 */
950static int start_page(struct twriter *tw, ulong timestamp)
951{
952 int start;
953 int ret;
954
955 /* move to start of next page */
956 start = ALIGN(tw->ptr, TRACE_PAGE_SIZE);
957 ret = fseek(tw->fout, start, SEEK_SET);
958 if (ret < 0) {
959 fprintf(stderr, "Cannot seek to page start\n");
960 return -1;
961 }
962 tw->ptr = start;
963
964 /* page header */
965 tw->ptr += tputq(tw->fout, timestamp);
966 ret = push_len(tw, start + 16, "page", 8);
967 if (ret < 0)
968 return ret;
969 tw->ptr += ret;
970
971 return 0;
972}
973
974/**
975 * finish_page() - finish a page
976 *
977 * Sets the lengths correctly and moves to the start of the next page
978 *
979 * @tw: Writer context
980 * Returns: 0 on success, -1 on error
981 */
982static int finish_page(struct twriter *tw)
983{
984 int ret, end;
985
986 ret = pop_len(tw, "page");
987 if (ret < 0)
988 return ret;
989 end = ALIGN(tw->ptr, TRACE_PAGE_SIZE);
990
991 /*
992 * Write a byte so that the data actually makes to the file, in the case
993 * that we never write any more pages
994 */
995 if (tw->ptr != end) {
996 if (fseek(tw->fout, end - 1, SEEK_SET)) {
997 fprintf(stderr, "cannot seek to start of next page\n");
998 return -1;
999 }
1000 fputc(0, tw->fout);
1001 tw->ptr = end;
1002 }
1003
1004 return 0;
1005}
1006
1007/**
1008 * output_headers() - Output v6 headers to the file
1009 *
1010 * Writes out the various formats so that trace-cmd and kernelshark can make
1011 * sense of the data
1012 *
1013 * This updates tw->ptr as it goes
1014 *
1015 * @tw: Writer context
1016 * Returns: 0 on success, -ve on error
1017 */
1018static int output_headers(struct twriter *tw)
1019{
1020 FILE *fout = tw->fout;
1021 char str[800];
1022 int len, ret;
1023
1024 tw->ptr += fprintf(fout, "%c%c%ctracing6%c%c%c", 0x17, 0x08, 0x44,
1025 0 /* terminator */, 0 /* little endian */,
1026 4 /* 32-bit long values */);
1027
1028 /* host-machine page size 4KB */
1029 tw->ptr += tputl(fout, 4 << 10);
1030
1031 tw->ptr += fprintf(fout, "header_page%c", 0);
1032
1033 snprintf(str, sizeof(str),
1034 "\tfield: u64 timestamp;\toffset:0;\tsize:8;\tsigned:0;\n"
1035 "\tfield: local_t commit;\toffset:8;\tsize:8;\tsigned:1;\n"
1036 "\tfield: int overwrite;\toffset:8;\tsize:1;\tsigned:1;\n"
1037 "\tfield: char data;\toffset:16;\tsize:4080;\tsigned:1;\n");
1038 len = strlen(str);
1039 tw->ptr += tputq(fout, len);
1040 tw->ptr += tputs(fout, str);
1041
1042 if (VERSION7) {
1043 /* no compression */
1044 tw->ptr += fprintf(fout, "none%cversion%c\n", 0, 0);
1045
1046 ret = start_header(tw, SECTION_OPTIONS, 0, "options");
1047 if (ret < 0) {
1048 fprintf(stderr, "Cannot start option header\n");
1049 return -1;
1050 }
1051 tw->ptr += ret;
1052 tw->ptr += tputh(fout, OPTION_DONE);
1053 tw->ptr += tputl(fout, 8);
1054 tw->ptr += tputl(fout, 0);
1055 ret = pop_len(tw, "t7 header");
1056 if (ret < 0) {
1057 fprintf(stderr, "Cannot finish option header\n");
1058 return -1;
1059 }
1060 }
1061
1062 tw->ptr += fprintf(fout, "header_event%c", 0);
1063 snprintf(str, sizeof(str),
1064 "# compressed entry header\n"
1065 "\ttype_len : 5 bits\n"
1066 "\ttime_delta : 27 bits\n"
1067 "\tarray : 32 bits\n"
1068 "\n"
1069 "\tpadding : type == 29\n"
1070 "\ttime_extend : type == 30\n"
1071 "\ttime_stamp : type == 31\n"
1072 "\tdata max type_len == 28\n");
1073 len = strlen(str);
1074 tw->ptr += tputq(fout, len);
1075 tw->ptr += tputs(fout, str);
1076
1077 /* number of ftrace-event-format files */
1078 tw->ptr += tputl(fout, 3);
1079
1080 snprintf(str, sizeof(str),
1081 "name: function\n"
1082 "ID: 1\n"
1083 "format:\n"
1084 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
1085 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
1086 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;signed:0;\n"
1087 "\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
1088 "\n"
1089 "\tfield:unsigned long ip;\toffset:8;\tsize:8;\tsigned:0;\n"
1090 "\tfield:unsigned long parent_ip;\toffset:16;\tsize:8;\tsigned:0;\n"
1091 "\n"
1092 "print fmt: \" %%ps <-- %%ps\", (void *)REC->ip, (void *)REC->parent_ip\n");
1093 len = strlen(str);
1094 tw->ptr += tputq(fout, len);
1095 tw->ptr += tputs(fout, str);
1096
1097 snprintf(str, sizeof(str),
1098 "name: funcgraph_entry\n"
1099 "ID: 11\n"
1100 "format:\n"
1101 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
1102 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
1103 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;signed:0;\n"
1104 "\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
1105 "\n"
1106 "\tfield:unsigned long func;\toffset:8;\tsize:8;\tsigned:0;\n"
1107 "\tfield:int depth;\toffset:16;\tsize:4;\tsigned:1;\n"
1108 "\n"
1109 "print fmt: \"--> %%ps (%%d)\", (void *)REC->func, REC->depth\n");
1110 len = strlen(str);
1111 tw->ptr += tputq(fout, len);
1112 tw->ptr += tputs(fout, str);
1113
1114 snprintf(str, sizeof(str),
1115 "name: funcgraph_exit\n"
1116 "ID: 10\n"
1117 "format:\n"
1118 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
1119 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
1120 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;signed:0;\n"
1121 "\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
1122 "\n"
1123 "\tfield:unsigned long func;\toffset:8;\tsize:8;\tsigned:0;\n"
1124 "\tfield:int depth;\toffset:16;\tsize:4;\tsigned:1;\n"
1125 "\tfield:unsigned int overrun;\toffset:20;\tsize:4;\tsigned:0;\n"
1126 "\tfield:unsigned long long calltime;\toffset:24;\tsize:8;\tsigned:0;\n"
1127 "\tfield:unsigned long long rettime;\toffset:32;\tsize:8;\tsigned:0;\n"
1128 "\n"
1129 "print fmt: \"<-- %%ps (%%d) (start: %%llx end: %%llx) over: %%d\", (void *)REC->func, REC->depth, REC->calltime, REC->rettime, REC->depth\n");
1130 len = strlen(str);
1131 tw->ptr += tputq(fout, len);
1132 tw->ptr += tputs(fout, str);
1133
1134 return 0;
1135}
1136
1137/**
1138 * write_symbols() - Write the symbols out
1139 *
1140 * Writes the symbol information in the following format to mimic the Linux
1141 * /proc/kallsyms file:
1142 *
1143 * <address> T <name>
1144 *
1145 * This updates tw->ptr as it goes
1146 *
1147 * @tw: Writer context
1148 * Returns: 0 on success, -ve on error
1149 */
1150static int write_symbols(struct twriter *tw)
1151{
1152 char str[200];
1153 int ret, i;
1154
1155 /* write symbols */
1156 ret = push_len(tw, tw->ptr + 4, "syms", 4);
1157 if (ret < 0)
1158 return -1;
1159 tw->ptr += ret;
1160 for (i = 0; i < func_count; i++) {
1161 struct func_info *func = &func_list[i];
1162
1163 snprintf(str, sizeof(str), "%016lx T %s\n",
1164 text_offset + func->offset, func->name);
1165 tw->ptr += tputs(tw->fout, str);
1166 }
1167 ret = pop_len(tw, "syms");
1168 if (ret < 0)
1169 return -1;
1170 tw->ptr += ret;
1171
1172 return 0;
1173}
1174
1175/**
1176 * write_options() - Write the options out
1177 *
1178 * Writes various options which are needed or useful. We use OPTION_TSC2NSEC
1179 * to indicates that values in the output need to be multiplied by 1000 since
1180 * U-Boot's trace values are in microseconds.
1181 *
1182 * This updates tw->ptr as it goes
1183 *
1184 * @tw: Writer context
1185 * Returns: 0 on success, -ve on error
1186 */
1187static int write_options(struct twriter *tw)
1188{
1189 FILE *fout = tw->fout;
1190 char str[200];
1191 int len;
1192
1193 /* trace_printk, 0 for now */
1194 tw->ptr += tputl(fout, 0);
1195
1196 /* processes */
1197 snprintf(str, sizeof(str), "%d u-boot\n", TRACE_PID);
1198 len = strlen(str);
1199 tw->ptr += tputq(fout, len);
1200 tw->ptr += tputs(fout, str);
1201
1202 /* number of CPUs */
1203 tw->ptr += tputl(fout, 1);
1204
1205 tw->ptr += fprintf(fout, "options %c", 0);
1206
1207 /* traceclock */
1208 tw->ptr += tputh(fout, OPTION_TRACECLOCK);
1209 tw->ptr += tputl(fout, 0);
1210
1211 /* uname */
1212 tw->ptr += tputh(fout, OPTION_UNAME);
1213 snprintf(str, sizeof(str), "U-Boot");
1214 len = strlen(str);
1215 tw->ptr += tputl(fout, len);
1216 tw->ptr += tputs(fout, str);
1217
1218 /* version */
1219 tw->ptr += tputh(fout, OPTION_VERSION);
1220 snprintf(str, sizeof(str), "unknown");
1221 len = strlen(str);
1222 tw->ptr += tputl(fout, len);
1223 tw->ptr += tputs(fout, str);
1224
1225 /* trace ID */
1226 tw->ptr += tputh(fout, OPTION_TRACEID);
1227 tw->ptr += tputl(fout, 8);
1228 tw->ptr += tputq(fout, 0x123456780abcdef0);
1229
1230 /* time conversion */
1231 tw->ptr += tputh(fout, OPTION_TSC2NSEC);
1232 tw->ptr += tputl(fout, 16);
1233 tw->ptr += tputl(fout, 1000); /* multiplier */
1234 tw->ptr += tputl(fout, 0); /* shift */
1235 tw->ptr += tputq(fout, 0); /* offset */
1236
1237 /* cpustat - bogus data for now, but at least it mentions the CPU */
1238 tw->ptr += tputh(fout, OPTION_CPUSTAT);
1239 snprintf(str, sizeof(str),
1240 "CPU: 0\n"
1241 "entries: 100\n"
1242 "overrun: 43565\n"
1243 "commit overrun: 0\n"
1244 "bytes: 3360\n"
1245 "oldest event ts: 963732.447752\n"
1246 "now ts: 963832.146824\n"
1247 "dropped events: 0\n"
1248 "read events: 42379\n");
1249 len = strlen(str);
1250 tw->ptr += tputl(fout, len);
1251 tw->ptr += tputs(fout, str);
1252
1253 tw->ptr += tputh(fout, OPTION_DONE);
1254
1255 return 0;
1256}
1257
1258/**
Simon Glass31fb49a2023-01-15 14:15:56 -07001259 * calc_min_depth() - Calculate the minimum call depth from the call list
1260 *
1261 * Starting with a depth of 0, this works through the call list, adding 1 for
1262 * each function call and subtracting 1 for each function return. Most likely
1263 * the value ends up being negative, since the trace does not start at the
1264 * very top of the call stack, e.g. main(), but some function called by that.
1265 *
1266 * This value can be used to calculate the depth value for the first call,
1267 * such that it never goes negative for subsequent returns.
1268 *
1269 * Returns: minimum call depth (e.g. -2)
1270 */
1271static int calc_min_depth(void)
1272{
1273 struct trace_call *call;
1274 int depth, min_depth, i;
1275
1276 /* Calculate minimum depth */
1277 depth = 0;
1278 min_depth = 0;
1279 for (i = 0, call = call_list; i < call_count; i++, call++) {
1280 switch (TRACE_CALL_TYPE(call)) {
1281 case FUNCF_ENTRY:
1282 depth++;
1283 break;
1284 case FUNCF_EXIT:
1285 depth--;
1286 if (depth < min_depth)
1287 min_depth = depth;
1288 break;
1289 }
1290 }
1291
1292 return min_depth;
1293}
1294
1295/**
Simon Glassdd8f19a2023-01-15 14:15:53 -07001296 * write_pages() - Write the pages of trace data
1297 *
1298 * This works through all the calls, writing out as many pages of data as are
1299 * needed.
1300 *
1301 * @tw: Writer context
Simon Glass31fb49a2023-01-15 14:15:56 -07001302 * @out_format: Output format to use
Simon Glassdd8f19a2023-01-15 14:15:53 -07001303 * @missing_countp: Returns number of missing functions (not found in function
1304 * list)
1305 * @skip_countp: Returns number of skipped functions (excluded from trace)
1306 *
1307 * Returns: 0 on success, -ve on error
1308 */
Simon Glass31fb49a2023-01-15 14:15:56 -07001309static int write_pages(struct twriter *tw, enum out_format_t out_format,
1310 int *missing_countp, int *skip_countp)
Simon Glassdd8f19a2023-01-15 14:15:53 -07001311{
Simon Glass31fb49a2023-01-15 14:15:56 -07001312 ulong func_stack[MAX_STACK_DEPTH];
Simon Glassdd8f19a2023-01-15 14:15:53 -07001313 int stack_ptr; /* next free position in stack */
Simon Glass31fb49a2023-01-15 14:15:56 -07001314 int upto, depth, page_upto, i;
Simon Glass9c628922013-06-11 11:14:43 -07001315 int missing_count = 0, skip_count = 0;
Simon Glassdd8f19a2023-01-15 14:15:53 -07001316 struct trace_call *call;
1317 ulong last_timestamp;
1318 FILE *fout = tw->fout;
1319 int last_delta = 0;
1320 int err_count;
1321 bool in_page;
Simon Glass9c628922013-06-11 11:14:43 -07001322
Simon Glassdd8f19a2023-01-15 14:15:53 -07001323 in_page = false;
1324 last_timestamp = 0;
1325 upto = 0;
1326 page_upto = 0;
1327 err_count = 0;
1328
1329 /* maintain a stack of start times for calling functions */
1330 stack_ptr = 0;
1331
Simon Glass31fb49a2023-01-15 14:15:56 -07001332 /*
1333 * The first thing in the trace may not be the top-level function, so
1334 * set the initial depth so that no function goes below depth 0
1335 */
1336 depth = -calc_min_depth();
Simon Glass9c628922013-06-11 11:14:43 -07001337 for (i = 0, call = call_list; i < call_count; i++, call++) {
Simon Glass31fb49a2023-01-15 14:15:56 -07001338 bool entry = TRACE_CALL_TYPE(call) == FUNCF_ENTRY;
Simon Glassdd8f19a2023-01-15 14:15:53 -07001339 struct func_info *func;
1340 ulong timestamp;
1341 uint rec_words;
1342 int delta;
Simon Glass9c628922013-06-11 11:14:43 -07001343
Simon Glassdd8f19a2023-01-15 14:15:53 -07001344 func = find_func_by_offset(call->func);
Simon Glass9c628922013-06-11 11:14:43 -07001345 if (!func) {
1346 warn("Cannot find function at %lx\n",
1347 text_offset + call->func);
1348 missing_count++;
Simon Glassdd8f19a2023-01-15 14:15:53 -07001349 if (missing_count > 20) {
1350 /* perhaps trace does not match System.map */
1351 fprintf(stderr, "Too many missing functions\n");
1352 return -1;
1353 }
Simon Glass9c628922013-06-11 11:14:43 -07001354 continue;
1355 }
1356
1357 if (!(func->flags & FUNCF_TRACE)) {
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001358 debug("Function '%s' is excluded from trace\n",
Simon Glass9c628922013-06-11 11:14:43 -07001359 func->name);
1360 skip_count++;
1361 continue;
1362 }
1363
Simon Glass31fb49a2023-01-15 14:15:56 -07001364 if (out_format == OUT_FMT_FUNCTION)
1365 rec_words = 6;
1366 else /* 2 header words and then 3 or 8 others */
1367 rec_words = 2 + (entry ? 3 : 8);
Simon Glassdd8f19a2023-01-15 14:15:53 -07001368
1369 /* convert timestamp from us to ns */
1370 timestamp = call->flags & FUNCF_TIMESTAMP_MASK;
1371 if (in_page) {
1372 if (page_upto + rec_words * 4 > TRACE_PAGE_SIZE) {
1373 if (finish_page(tw))
1374 return -1;
1375 in_page = false;
1376 }
1377 }
1378 if (!in_page) {
1379 if (start_page(tw, timestamp))
1380 return -1;
1381 in_page = true;
1382 last_timestamp = timestamp;
1383 last_delta = 0;
1384 page_upto = tw->ptr & TRACE_PAGE_MASK;
1385 if (_DEBUG) {
1386 fprintf(stderr,
1387 "new page, last_timestamp=%ld, upto=%d\n",
1388 last_timestamp, upto);
1389 }
1390 }
1391
1392 delta = timestamp - last_timestamp;
1393 if (delta < 0) {
1394 fprintf(stderr, "Time went backwards\n");
1395 err_count++;
1396 }
1397
1398 if (err_count > 20) {
1399 fprintf(stderr, "Too many errors, giving up\n");
1400 return -1;
1401 }
1402
1403 if (delta > 0x07fffff) {
1404 /*
1405 * hard to imagine how this could happen since it means
1406 * that no function calls were made for a long time
1407 */
1408 fprintf(stderr, "cannot represent time delta %x\n",
1409 delta);
1410 return -1;
1411 }
1412
Simon Glass31fb49a2023-01-15 14:15:56 -07001413 if (out_format == OUT_FMT_FUNCTION) {
1414 struct func_info *caller_func;
1415
1416 if (_DEBUG) {
1417 fprintf(stderr, "%d: delta=%d, stamp=%ld\n",
1418 upto, delta, timestamp);
1419 fprintf(stderr,
1420 " last_delta %x to %x: last_timestamp=%lx, "
1421 "timestamp=%lx, call->flags=%x, upto=%d\n",
1422 last_delta, delta, last_timestamp,
1423 timestamp, call->flags, upto);
1424 }
1425
1426 /* type_len is 6, meaning 4 * 6 = 24 bytes */
1427 tw->ptr += tputl(fout, rec_words | (uint)delta << 5);
1428 tw->ptr += tputh(fout, TRACE_FN);
1429 tw->ptr += tputh(fout, 0); /* flags */
1430 tw->ptr += tputl(fout, TRACE_PID); /* PID */
1431 /* function */
1432 tw->ptr += tputq(fout, text_offset + func->offset);
1433 caller_func = find_caller_by_offset(call->caller);
1434 /* caller */
1435 tw->ptr += tputq(fout,
1436 text_offset + caller_func->offset);
1437 } else {
1438 tw->ptr += tputl(fout, rec_words | delta << 5);
1439 tw->ptr += tputh(fout, entry ? TRACE_GRAPH_ENT
1440 : TRACE_GRAPH_RET);
1441 tw->ptr += tputh(fout, 0); /* flags */
1442 tw->ptr += tputl(fout, TRACE_PID); /* PID */
1443 /* function */
1444 tw->ptr += tputq(fout, text_offset + func->offset);
1445 tw->ptr += tputl(fout, depth); /* depth */
1446 if (entry) {
1447 depth++;
1448 if (stack_ptr < MAX_STACK_DEPTH)
1449 func_stack[stack_ptr] = timestamp;
1450 stack_ptr++;
1451 } else {
1452 ulong func_duration = 0;
Simon Glassdd8f19a2023-01-15 14:15:53 -07001453
Simon Glass31fb49a2023-01-15 14:15:56 -07001454 depth--;
1455 if (stack_ptr && stack_ptr <= MAX_STACK_DEPTH) {
1456 ulong start = func_stack[--stack_ptr];
1457
1458 func_duration = timestamp - start;
1459 }
1460 tw->ptr += tputl(fout, 0); /* overrun */
1461 tw->ptr += tputq(fout, 0); /* calltime */
Jerome Forissier4ff91c82024-12-06 11:11:31 +01001462 /* rettime (nanoseconds) */
1463 tw->ptr += tputq(fout, func_duration * 1000);
Simon Glass31fb49a2023-01-15 14:15:56 -07001464 }
1465 }
Simon Glassdd8f19a2023-01-15 14:15:53 -07001466
1467 last_delta = delta;
1468 last_timestamp = timestamp;
1469 page_upto += 4 + rec_words * 4;
1470 upto++;
1471 if (stack_ptr == MAX_STACK_DEPTH)
1472 break;
1473 }
1474 if (in_page && finish_page(tw))
1475 return -1;
1476 *missing_countp = missing_count;
1477 *skip_countp = skip_count;
1478
1479 return 0;
1480}
1481
1482/**
1483 * write_flyrecord() - Write the flyrecord information
1484 *
1485 * Writes the header and pages of data for the "flyrecord" section. It also
1486 * writes out the counter-type info, selecting "[local]"
1487 *
1488 * @tw: Writer context
Simon Glass31fb49a2023-01-15 14:15:56 -07001489 * @out_format: Output format to use
Simon Glassdd8f19a2023-01-15 14:15:53 -07001490 * @missing_countp: Returns number of missing functions (not found in function
1491 * list)
1492 * @skip_countp: Returns number of skipped functions (excluded from trace)
1493 *
1494 * Returns: 0 on success, -ve on error
1495 */
Simon Glass31fb49a2023-01-15 14:15:56 -07001496static int write_flyrecord(struct twriter *tw, enum out_format_t out_format,
1497 int *missing_countp, int *skip_countp)
Simon Glassdd8f19a2023-01-15 14:15:53 -07001498{
Michal Simekfb01d122023-09-15 14:12:05 +02001499 unsigned long long start, start_ofs, len;
Michal Simekdba37182023-09-15 14:12:03 +02001500 int ret;
Simon Glassdd8f19a2023-01-15 14:15:53 -07001501 FILE *fout = tw->fout;
1502 char str[200];
1503
Michal Simekfb01d122023-09-15 14:12:05 +02001504 /* Record start pointer */
1505 start_ofs = tw->ptr;
1506 debug("Start of flyrecord header at: 0x%llx\n", start_ofs);
1507
Simon Glassdd8f19a2023-01-15 14:15:53 -07001508 tw->ptr += fprintf(fout, "flyrecord%c", 0);
1509
Michal Simekfb01d122023-09-15 14:12:05 +02001510 /* flyrecord\0 - allocated 10 bytes */
1511 start_ofs += 10;
1512
1513 /*
1514 * 8 bytes that are a 64-bit word containing the offset into the file
1515 * that holds the data for the CPU.
1516 *
1517 * 8 bytes that are a 64-bit word containing the size of the CPU
1518 * data at that offset.
1519 */
1520 start_ofs += 16;
1521
Michal Simek94fa07e2023-09-15 14:12:04 +02001522 snprintf(str, sizeof(str),
1523 "[local] global counter uptime perf mono mono_raw boot x86-tsc\n");
1524 len = strlen(str);
1525
Michal Simekfb01d122023-09-15 14:12:05 +02001526 /* trace clock length - 8 bytes */
1527 start_ofs += 8;
1528 /* trace clock data */
1529 start_ofs += len;
1530
1531 debug("Calculated flyrecord header end at: 0x%llx, trace clock len: 0x%llx\n",
1532 start_ofs, len);
1533
Simon Glassdd8f19a2023-01-15 14:15:53 -07001534 /* trace data */
Michal Simekfb01d122023-09-15 14:12:05 +02001535 start = ALIGN(start_ofs, TRACE_PAGE_SIZE);
Simon Glassdd8f19a2023-01-15 14:15:53 -07001536 tw->ptr += tputq(fout, start);
1537
1538 /* use a placeholder for the size */
1539 ret = push_len(tw, start, "flyrecord", 8);
1540 if (ret < 0)
1541 return -1;
1542 tw->ptr += ret;
1543
Simon Glassdd8f19a2023-01-15 14:15:53 -07001544 tw->ptr += tputq(fout, len);
1545 tw->ptr += tputs(fout, str);
1546
Michal Simekfb01d122023-09-15 14:12:05 +02001547 debug("End of flyrecord header at: 0x%x, offset: 0x%llx\n",
1548 tw->ptr, start);
1549
Simon Glass7ac0a502023-01-15 14:15:55 -07001550 debug("trace text base %lx, map file %lx\n", text_base, text_offset);
1551
Simon Glass31fb49a2023-01-15 14:15:56 -07001552 ret = write_pages(tw, out_format, missing_countp, skip_countp);
Simon Glassdd8f19a2023-01-15 14:15:53 -07001553 if (ret < 0) {
1554 fprintf(stderr, "Cannot output pages\n");
1555 return -1;
1556 }
1557
1558 ret = pop_len(tw, "flyrecord");
1559 if (ret < 0) {
1560 fprintf(stderr, "Cannot finish flyrecord header\n");
1561 return -1;
1562 }
1563
1564 return 0;
1565}
1566
1567/**
1568 * make_ftrace() - Write out an ftrace file
1569 *
1570 * See here for format:
1571 *
1572 * https://github.com/rostedt/trace-cmd/blob/master/Documentation/trace-cmd/trace-cmd.dat.v7.5.txt
1573 *
1574 * @fout: Output file
Simon Glass31fb49a2023-01-15 14:15:56 -07001575 * @out_format: Output format to use
Simon Glassdd8f19a2023-01-15 14:15:53 -07001576 * Returns: 0 on success, -ve on error
1577 */
Simon Glass31fb49a2023-01-15 14:15:56 -07001578static int make_ftrace(FILE *fout, enum out_format_t out_format)
Simon Glassdd8f19a2023-01-15 14:15:53 -07001579{
1580 int missing_count, skip_count;
1581 struct twriter tws, *tw = &tws;
1582 int ret;
1583
1584 memset(tw, '\0', sizeof(*tw));
1585 abuf_init(&tw->str_buf);
1586 tw->fout = fout;
1587
1588 tw->ptr = 0;
1589 ret = output_headers(tw);
1590 if (ret < 0) {
1591 fprintf(stderr, "Cannot output headers\n");
1592 return -1;
1593 }
1594 /* number of event systems files */
1595 tw->ptr += tputl(fout, 0);
Simon Glass9c628922013-06-11 11:14:43 -07001596
Simon Glassdd8f19a2023-01-15 14:15:53 -07001597 ret = write_symbols(tw);
1598 if (ret < 0) {
1599 fprintf(stderr, "Cannot write symbols\n");
1600 return -1;
1601 }
1602
1603 ret = write_options(tw);
1604 if (ret < 0) {
1605 fprintf(stderr, "Cannot write options\n");
1606 return -1;
Simon Glass9c628922013-06-11 11:14:43 -07001607 }
Simon Glassdd8f19a2023-01-15 14:15:53 -07001608
Simon Glass31fb49a2023-01-15 14:15:56 -07001609 ret = write_flyrecord(tw, out_format, &missing_count, &skip_count);
Simon Glassdd8f19a2023-01-15 14:15:53 -07001610 if (ret < 0) {
1611 fprintf(stderr, "Cannot write flyrecord\n");
1612 return -1;
1613 }
1614
Simon Glass9c628922013-06-11 11:14:43 -07001615 info("ftrace: %d functions not found, %d excluded\n", missing_count,
1616 skip_count);
1617
1618 return 0;
1619}
1620
Simon Glass9e2afae2023-01-15 14:15:52 -07001621/**
Simon Glassd854ce42023-01-15 14:15:57 -07001622 * create_node() - Create a new node in the flamegraph tree
1623 *
1624 * @msg: Message to use for debugging if something goes wrong
1625 * Returns: Pointer to newly created node, or NULL on error
1626 */
1627static struct flame_node *create_node(const char *msg)
1628{
1629 struct flame_node *node;
1630
1631 node = calloc(1, sizeof(*node));
1632 if (!node) {
1633 fprintf(stderr, "Out of memory for %s\n", msg);
1634 return NULL;
1635 }
1636 INIT_LIST_HEAD(&node->child_head);
1637
1638 return node;
1639}
1640
1641/**
1642 * process_call(): Add a call to the flamegraph info
1643 *
1644 * For function calls, if this call stack has been seen before, this increments
1645 * the call count, creating a new node if needed.
1646 *
1647 * For function returns, it adds up the time spent in this call stack,
1648 * subtracting the time spent by child functions.
1649 *
1650 * @state: Current flamegraph state
1651 * @entry: true if this is a function entry, false if a function exit
1652 * @timestamp: Timestamp from the trace file (in microseconds)
1653 * @func: Function that was called/returned from
1654 *
1655 * Returns: 0 on success, -ve on error
1656 */
1657static int process_call(struct flame_state *state, bool entry, ulong timestamp,
1658 struct func_info *func)
1659{
1660 struct flame_node *node = state->node;
Simon Glass143058f2023-01-15 14:15:58 -07001661 int stack_ptr = state->stack_ptr;
Simon Glassd854ce42023-01-15 14:15:57 -07001662
1663 if (entry) {
1664 struct flame_node *child, *chd;
1665
1666 /* see if we have this as a child node already */
1667 child = NULL;
1668 list_for_each_entry(chd, &node->child_head, sibling_node) {
1669 if (chd->func == func) {
1670 child = chd;
1671 break;
1672 }
1673 }
1674 if (!child) {
1675 /* create a new node */
1676 child = create_node("child");
1677 if (!child)
1678 return -1;
1679 list_add_tail(&child->sibling_node, &node->child_head);
1680 child->func = func;
1681 child->parent = node;
1682 state->nodes++;
1683 }
1684 debug("entry %s: move from %s to %s\n", func->name,
1685 node->func ? node->func->name : "(root)",
1686 child->func->name);
1687 child->count++;
Simon Glass143058f2023-01-15 14:15:58 -07001688 if (stack_ptr < MAX_STACK_DEPTH) {
1689 state->stack[stack_ptr].timestamp = timestamp;
1690 state->stack[stack_ptr].child_total = 0;
1691 }
1692 debug("%d: %20s: entry at %ld\n", stack_ptr, func->name,
1693 timestamp);
1694 stack_ptr++;
Simon Glassd854ce42023-01-15 14:15:57 -07001695 node = child;
1696 } else if (node->parent) {
Simon Glass143058f2023-01-15 14:15:58 -07001697 ulong total_duration = 0, child_duration = 0;
1698 struct stack_info *stk;
1699
Simon Glassd854ce42023-01-15 14:15:57 -07001700 debug("exit %s: move from %s to %s\n", func->name,
1701 node->func->name, node->parent->func ?
1702 node->parent->func->name : "(root)");
Simon Glass143058f2023-01-15 14:15:58 -07001703 if (stack_ptr && stack_ptr <= MAX_STACK_DEPTH) {
1704 stk = &state->stack[--stack_ptr];
1705
1706 /*
1707 * get total duration of the function which just
1708 * exited
1709 */
1710 total_duration = timestamp - stk->timestamp;
1711 child_duration = stk->child_total;
1712
1713 if (stack_ptr)
1714 state->stack[stack_ptr - 1].child_total += total_duration;
1715
1716 debug("%d: %20s: exit at %ld, total %ld, child %ld, child_total=%ld\n",
1717 stack_ptr, func->name, timestamp,
1718 total_duration, child_duration,
1719 stk->child_total);
1720 }
1721 node->duration += total_duration - child_duration;
Simon Glassd854ce42023-01-15 14:15:57 -07001722 node = node->parent;
1723 }
1724
Simon Glass143058f2023-01-15 14:15:58 -07001725 state->stack_ptr = stack_ptr;
Simon Glassd854ce42023-01-15 14:15:57 -07001726 state->node = node;
1727
1728 return 0;
1729}
1730
1731/**
1732 * make_flame_tree() - Create a tree of stack traces
1733 *
1734 * Set up a tree, with the root node having the top-level functions as children
1735 * and the leaf nodes being leaf functions. Each node has a count of how many
1736 * times this function appears in the trace
1737 *
Simon Glass143058f2023-01-15 14:15:58 -07001738 * @out_format: Output format to use
Simon Glassd854ce42023-01-15 14:15:57 -07001739 * @treep: Returns the resulting flamegraph tree
1740 * Returns: 0 on success, -ve on error
1741 */
Simon Glass143058f2023-01-15 14:15:58 -07001742static int make_flame_tree(enum out_format_t out_format,
1743 struct flame_node **treep)
Simon Glassd854ce42023-01-15 14:15:57 -07001744{
1745 struct flame_state state;
1746 struct flame_node *tree;
1747 struct trace_call *call;
Tom Rinifd2de4f2023-02-27 17:08:38 -05001748 int i;
Simon Glassd854ce42023-01-15 14:15:57 -07001749
Simon Glass143058f2023-01-15 14:15:58 -07001750 /* maintain a stack of start times, etc. for 'calling' functions */
1751 state.stack_ptr = 0;
1752
Simon Glassd854ce42023-01-15 14:15:57 -07001753 tree = create_node("tree");
1754 if (!tree)
1755 return -1;
1756 state.node = tree;
1757 state.nodes = 0;
1758
1759 for (i = 0, call = call_list; i < call_count; i++, call++) {
1760 bool entry = TRACE_CALL_TYPE(call) == FUNCF_ENTRY;
1761 ulong timestamp = call->flags & FUNCF_TIMESTAMP_MASK;
1762 struct func_info *func;
1763
Simon Glassd854ce42023-01-15 14:15:57 -07001764 func = find_func_by_offset(call->func);
1765 if (!func) {
1766 warn("Cannot find function at %lx\n",
1767 text_offset + call->func);
Simon Glassd854ce42023-01-15 14:15:57 -07001768 continue;
1769 }
1770
1771 if (process_call(&state, entry, timestamp, func))
1772 return -1;
1773 }
1774 fprintf(stderr, "%d nodes\n", state.nodes);
1775 *treep = tree;
1776
1777 return 0;
1778}
1779
1780/**
1781 * output_tree() - Output a flamegraph tree
1782 *
1783 * Writes the tree out to a file in a format suitable for flamegraph.pl
1784 *
1785 * This works by maintaining a string shared across all recursive calls. The
1786 * function name for this node is added to the existing string, to make up the
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001787 * full call-stack description. For example, on entry, @str_buf->data might
1788 * contain:
Simon Glassd854ce42023-01-15 14:15:57 -07001789 *
1790 * "initf_bootstage;bootstage_mark_name"
1791 * ^ @base
1792 *
1793 * with @base pointing to the \0 at the end of the string. This function adds
1794 * a ';' following by the name of the current function, e.g. "timer_get_boot_us"
1795 * as well as the output value, to get the full line:
1796 *
1797 * initf_bootstage;bootstage_mark_name;timer_get_boot_us 123
1798 *
1799 * @fout: Output file
Simon Glass143058f2023-01-15 14:15:58 -07001800 * @out_format: Output format to use
Simon Glassd854ce42023-01-15 14:15:57 -07001801 * @node: Node to output (pass the whole tree at first)
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001802 * @str_buf: String buffer to use to build the output line
Simon Glassd854ce42023-01-15 14:15:57 -07001803 * @base: Current base position in the string
1804 * @treep: Returns the resulting flamegraph tree
1805 * Returns 0 if OK, -1 on error
1806 */
Simon Glass143058f2023-01-15 14:15:58 -07001807static int output_tree(FILE *fout, enum out_format_t out_format,
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001808 const struct flame_node *node, struct abuf *str_buf,
Simon Glass143058f2023-01-15 14:15:58 -07001809 int base)
Simon Glassd854ce42023-01-15 14:15:57 -07001810{
1811 const struct flame_node *child;
1812 int pos;
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001813 char *str = abuf_data(str_buf);
Simon Glassd854ce42023-01-15 14:15:57 -07001814
Simon Glass143058f2023-01-15 14:15:58 -07001815 if (node->count) {
1816 if (out_format == OUT_FMT_FLAMEGRAPH_CALLS) {
1817 fprintf(fout, "%s %d\n", str, node->count);
1818 } else {
1819 /*
1820 * Write out the number of microseconds used by this
1821 * call stack. Since the time taken by child calls is
1822 * subtracted from this total, it can reach 0, meaning
1823 * that this function took no time beyond what its
1824 * children used. For this case, write 1 rather than 0,
1825 * so that this call stack appears in the flamegraph.
1826 * This has the effect of inflating the timing slightly,
1827 * but only by at most 1 microsecond per function,
1828 * assuming that is the timestamp resolution
1829 */
1830 fprintf(fout, "%s %ld\n", str,
1831 node->duration ? node->duration : 1);
1832 }
1833 }
Simon Glassd854ce42023-01-15 14:15:57 -07001834
1835 pos = base;
1836 if (pos)
1837 str[pos++] = ';';
1838 list_for_each_entry(child, &node->child_head, sibling_node) {
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001839 int len, needed;
Simon Glassd854ce42023-01-15 14:15:57 -07001840
1841 len = strlen(child->func->name);
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001842 needed = pos + len + 1;
1843 if (needed > abuf_size(str_buf)) {
1844 /*
1845 * We need to re-allocate the string buffer; increase
1846 * its size by multiples of 500 characters.
1847 */
1848 needed = 500 * ((needed / 500) + 1);
1849 if (!abuf_realloc(str_buf, needed))
1850 return -1;
1851 str = abuf_data(str_buf);
1852 memset(str + pos, 0, abuf_size(str_buf) - pos);
Simon Glassd854ce42023-01-15 14:15:57 -07001853 }
1854 strcpy(str + pos, child->func->name);
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001855 if (output_tree(fout, out_format, child, str_buf, pos + len))
Simon Glassd854ce42023-01-15 14:15:57 -07001856 return -1;
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001857 /*
1858 * Update our pointer as the string buffer might have been
1859 * re-allocated.
1860 */
1861 str = abuf_data(str_buf);
Simon Glassd854ce42023-01-15 14:15:57 -07001862 }
1863
1864 return 0;
1865}
1866
1867/**
1868 * make_flamegraph() - Write out a flame graph
1869 *
1870 * @fout: Output file
Simon Glass143058f2023-01-15 14:15:58 -07001871 * @out_format: Output format to use, e.g. function counts or timing
Simon Glassd854ce42023-01-15 14:15:57 -07001872 * Returns 0 if OK, -1 on error
1873 */
Simon Glass143058f2023-01-15 14:15:58 -07001874static int make_flamegraph(FILE *fout, enum out_format_t out_format)
Simon Glassd854ce42023-01-15 14:15:57 -07001875{
1876 struct flame_node *tree;
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001877 struct abuf str_buf;
1878 char *str;
1879 int ret = 0;
Simon Glassd854ce42023-01-15 14:15:57 -07001880
Simon Glass143058f2023-01-15 14:15:58 -07001881 if (make_flame_tree(out_format, &tree))
Simon Glassd854ce42023-01-15 14:15:57 -07001882 return -1;
1883
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001884 abuf_init(&str_buf);
1885 if (!abuf_realloc(&str_buf, 500))
Simon Glassd854ce42023-01-15 14:15:57 -07001886 return -1;
1887
Vincent Stehléaa3dc332024-04-02 13:29:16 +02001888 str = abuf_data(&str_buf);
1889 memset(str, 0, abuf_size(&str_buf));
1890 if (output_tree(fout, out_format, tree, &str_buf, 0))
1891 ret = -1;
1892
1893 abuf_uninit(&str_buf);
1894 return ret;
Simon Glassd854ce42023-01-15 14:15:57 -07001895}
1896
1897/**
Simon Glass9e2afae2023-01-15 14:15:52 -07001898 * prof_tool() - Performs requested action
1899 *
1900 * @argc: Number of arguments (used to obtain the command
1901 * @argv: List of arguments
1902 * @trace_fname: Filename of input file (trace data from U-Boot)
1903 * @map_fname: Filename of map file (System.map from U-Boot)
1904 * @trace_config_fname: Trace-configuration file, or NULL if none
1905 * @out_fname: Output filename
1906 */
Simon Glassed38aef2020-05-10 11:40:03 -06001907static int prof_tool(int argc, char *const argv[],
Simon Glass9e2afae2023-01-15 14:15:52 -07001908 const char *trace_fname, const char *map_fname,
Simon Glass31fb49a2023-01-15 14:15:56 -07001909 const char *trace_config_fname, const char *out_fname,
1910 enum out_format_t out_format)
Simon Glass9c628922013-06-11 11:14:43 -07001911{
1912 int err = 0;
1913
1914 if (read_map_file(map_fname))
1915 return -1;
Simon Glass9e2afae2023-01-15 14:15:52 -07001916 if (trace_fname && read_trace_file(trace_fname))
Simon Glass9c628922013-06-11 11:14:43 -07001917 return -1;
1918 if (trace_config_fname && read_trace_config_file(trace_config_fname))
1919 return -1;
1920
Simon Glass0974d002023-01-15 14:15:54 -07001921 check_trace_config();
Simon Glass9c628922013-06-11 11:14:43 -07001922
1923 for (; argc; argc--, argv++) {
1924 const char *cmd = *argv;
1925
Simon Glassdd8f19a2023-01-15 14:15:53 -07001926 if (!strcmp(cmd, "dump-ftrace")) {
1927 FILE *fout;
1928
Simon Glass31fb49a2023-01-15 14:15:56 -07001929 if (out_format != OUT_FMT_FUNCTION &&
1930 out_format != OUT_FMT_FUNCGRAPH)
1931 out_format = OUT_FMT_FUNCTION;
Simon Glassdd8f19a2023-01-15 14:15:53 -07001932 fout = fopen(out_fname, "w");
1933 if (!fout) {
1934 fprintf(stderr, "Cannot write file '%s'\n",
1935 out_fname);
1936 return -1;
1937 }
Simon Glass31fb49a2023-01-15 14:15:56 -07001938 err = make_ftrace(fout, out_format);
Simon Glassdd8f19a2023-01-15 14:15:53 -07001939 fclose(fout);
Simon Glassd854ce42023-01-15 14:15:57 -07001940 } else if (!strcmp(cmd, "dump-flamegraph")) {
1941 FILE *fout;
1942
Simon Glass143058f2023-01-15 14:15:58 -07001943 if (out_format != OUT_FMT_FLAMEGRAPH_CALLS &&
1944 out_format != OUT_FMT_FLAMEGRAPH_TIMING)
1945 out_format = OUT_FMT_FLAMEGRAPH_CALLS;
Simon Glassd854ce42023-01-15 14:15:57 -07001946 fout = fopen(out_fname, "w");
1947 if (!fout) {
1948 fprintf(stderr, "Cannot write file '%s'\n",
1949 out_fname);
1950 return -1;
1951 }
Simon Glass143058f2023-01-15 14:15:58 -07001952 err = make_flamegraph(fout, out_format);
Simon Glassd854ce42023-01-15 14:15:57 -07001953 fclose(fout);
Simon Glassdd8f19a2023-01-15 14:15:53 -07001954 } else {
Simon Glass9c628922013-06-11 11:14:43 -07001955 warn("Unknown command '%s'\n", cmd);
Simon Glassdd8f19a2023-01-15 14:15:53 -07001956 }
Simon Glass9c628922013-06-11 11:14:43 -07001957 }
1958
1959 return err;
1960}
1961
1962int main(int argc, char *argv[])
1963{
Simon Glass31fb49a2023-01-15 14:15:56 -07001964 enum out_format_t out_format = OUT_FMT_DEFAULT;
Simon Glass9c628922013-06-11 11:14:43 -07001965 const char *map_fname = "System.map";
Simon Glassec86a632022-12-21 16:08:24 -07001966 const char *trace_fname = NULL;
1967 const char *config_fname = NULL;
Simon Glass9e2afae2023-01-15 14:15:52 -07001968 const char *out_fname = NULL;
Simon Glass9c628922013-06-11 11:14:43 -07001969 int opt;
1970
1971 verbose = 2;
Simon Glass31fb49a2023-01-15 14:15:56 -07001972 while ((opt = getopt(argc, argv, "c:f:m:o:t:v:")) != -1) {
Simon Glass9c628922013-06-11 11:14:43 -07001973 switch (opt) {
Simon Glassec86a632022-12-21 16:08:24 -07001974 case 'c':
1975 config_fname = optarg;
Simon Glass9c628922013-06-11 11:14:43 -07001976 break;
Simon Glass31fb49a2023-01-15 14:15:56 -07001977 case 'f':
1978 if (!strcmp("function", optarg)) {
1979 out_format = OUT_FMT_FUNCTION;
1980 } else if (!strcmp("funcgraph", optarg)) {
1981 out_format = OUT_FMT_FUNCGRAPH;
Simon Glass143058f2023-01-15 14:15:58 -07001982 } else if (!strcmp("calls", optarg)) {
1983 out_format = OUT_FMT_FLAMEGRAPH_CALLS;
1984 } else if (!strcmp("timing", optarg)) {
1985 out_format = OUT_FMT_FLAMEGRAPH_TIMING;
Simon Glass31fb49a2023-01-15 14:15:56 -07001986 } else {
1987 fprintf(stderr,
1988 "Invalid format: use function, funcgraph, calls, timing\n");
1989 exit(1);
1990 }
1991 break;
Simon Glassec86a632022-12-21 16:08:24 -07001992 case 'm':
1993 map_fname = optarg;
Simon Glass9c628922013-06-11 11:14:43 -07001994 break;
Simon Glass9e2afae2023-01-15 14:15:52 -07001995 case 'o':
1996 out_fname = optarg;
1997 break;
Simon Glass9c628922013-06-11 11:14:43 -07001998 case 't':
Simon Glassec86a632022-12-21 16:08:24 -07001999 trace_fname = optarg;
Simon Glass9c628922013-06-11 11:14:43 -07002000 break;
Simon Glass9c628922013-06-11 11:14:43 -07002001 case 'v':
2002 verbose = atoi(optarg);
2003 break;
Simon Glass9c628922013-06-11 11:14:43 -07002004 default:
2005 usage();
2006 }
2007 }
2008 argc -= optind; argv += optind;
2009 if (argc < 1)
2010 usage();
2011
Simon Glass9e2afae2023-01-15 14:15:52 -07002012 if (!out_fname || !map_fname || !trace_fname) {
2013 fprintf(stderr,
2014 "Must provide trace data, System.map file and output file\n");
2015 usage();
2016 }
2017
Simon Glass9c628922013-06-11 11:14:43 -07002018 debug("Debug enabled\n");
Simon Glass9e2afae2023-01-15 14:15:52 -07002019 return prof_tool(argc, argv, trace_fname, map_fname, config_fname,
Simon Glass31fb49a2023-01-15 14:15:56 -07002020 out_fname, out_format);
Simon Glass9c628922013-06-11 11:14:43 -07002021}