blob: feeef55c1b98aa7061b3e13b15194f40fbb26509 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6c887b22013-06-11 11:14:43 -07002/*
Simon Glassbe16fc82023-01-15 14:15:53 -07003 * Copyright 2023 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass6c887b22013-06-11 11:14:43 -07005 */
6
Simon Glassbe16fc82023-01-15 14:15:53 -07007/*
8 * Decode and dump U-Boot trace information into formats that can be used
9 * by trace-cmd or kernelshark
10 *
11 * See doc/develop/trace.rst for more information
12 */
Simon Glass6c887b22013-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 Krause26e355d2015-04-22 21:36:22 +020024#include <sys/types.h>
Simon Glass6c887b22013-06-11 11:14:43 -070025
26#include <compiler.h>
27#include <trace.h>
Simon Glassbe16fc82023-01-15 14:15:53 -070028#include <abuf.h>
Simon Glass6c887b22013-06-11 11:14:43 -070029
Simon Glassbe16fc82023-01-15 14:15:53 -070030/* Set to 1 to emit version 7 file (currently this doesn't work) */
31#define VERSION7 0
32
33/* enable some debug features */
34#define _DEBUG 0
35
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)
Simon Glass6c887b22013-06-11 11:14:43 -070039
40enum {
41 FUNCF_TRACE = 1 << 0, /* Include this function in trace */
Simon Glassbe16fc82023-01-15 14:15:53 -070042 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 */
48};
49
50/* Section types for v7 format (trace-cmd format) */
51enum {
52 SECTION_OPTIONS,
53};
54
55/* Option types (trace-cmd format) */
56enum {
57 OPTION_DONE,
58 OPTION_DATE,
59 OPTION_CPUSTAT,
60 OPTION_BUFFER,
61 OPTION_TRACECLOCK,
62 OPTION_UNAME,
63 OPTION_HOOK,
64 OPTION_OFFSET,
65 OPTION_CPUCOUNT,
66 OPTION_VERSION,
67 OPTION_PROCMAPS,
68 OPTION_TRACEID,
69 OPTION_TIME_SHIFT,
70 OPTION_GUEST,
71 OPTION_TSC2NSEC,
72};
73
74/* types of trace records (trace-cmd format) */
75enum trace_type {
76 __TRACE_FIRST_TYPE = 0,
77
78 TRACE_FN,
79 TRACE_CTX,
80 TRACE_WAKE,
81 TRACE_STACK,
82 TRACE_PRINT,
83 TRACE_BPRINT,
84 TRACE_MMIO_RW,
85 TRACE_MMIO_MAP,
86 TRACE_BRANCH,
87 TRACE_GRAPH_RET,
88 TRACE_GRAPH_ENT,
Simon Glass6c887b22013-06-11 11:14:43 -070089};
90
Simon Glass9efc0b22023-01-15 14:15:52 -070091/**
92 * struct func_info - information recorded for each function
93 *
94 * @offset: Function offset in the image, measured from the text_base
95 * @name: Function name
96 * @code_size: Total code size of the function
97 * @flags: Either 0 or FUNCF_TRACE
98 * @objsection: the section this function is in
99 */
Simon Glass6c887b22013-06-11 11:14:43 -0700100struct func_info {
101 unsigned long offset;
102 const char *name;
103 unsigned long code_size;
Simon Glass6c887b22013-06-11 11:14:43 -0700104 unsigned flags;
Simon Glass6c887b22013-06-11 11:14:43 -0700105 struct objsection_info *objsection;
106};
107
Simon Glass9efc0b22023-01-15 14:15:52 -0700108/**
109 * enum trace_line_type - whether to include or exclude a function
110 *
111 * @TRACE_LINE_INCLUDE: Include the function
112 * @TRACE_LINE_EXCLUDE: Exclude the function
113 */
Simon Glass6c887b22013-06-11 11:14:43 -0700114enum trace_line_type {
115 TRACE_LINE_INCLUDE,
116 TRACE_LINE_EXCLUDE,
117};
118
Simon Glass9efc0b22023-01-15 14:15:52 -0700119/**
120 * struct trace_configline_info - information about a config-file line
121 *
122 * @next: Next line
123 * @type: Line type
124 * @name: identifier name / wildcard
125 * @regex: Regex to use if name starts with '/'
126 */
Simon Glass6c887b22013-06-11 11:14:43 -0700127struct trace_configline_info {
128 struct trace_configline_info *next;
129 enum trace_line_type type;
Simon Glass9efc0b22023-01-15 14:15:52 -0700130 const char *name;
131 regex_t regex;
Simon Glass6c887b22013-06-11 11:14:43 -0700132};
133
Simon Glassbe16fc82023-01-15 14:15:53 -0700134/**
135 * struct tw_len - holds information about a length value that need fix-ups
136 *
137 * This is used to record a placeholder for a u32 or u64 length which is written
138 * to the output file but needs to be updated once the length is actually known
139 *
140 * This allows us to write tw->ptr - @len_base to position @ptr in the file
141 *
142 * @ptr: Position of the length value in the file
143 * @base: Base position for the calculation
144 * @size: Size of the length value, in bytes (4 or 8)
145 */
146struct tw_len {
147 int ptr;
148 int base;
149 int size;
150};
151
152/**
153 * struct twriter - Writer for trace records
154 *
155 * Maintains state used when writing the output file in trace-cmd format
156 *
157 * @ptr: Current file position
158 * @len_stack: Stack of length values that need fixing up
159 * @len: Number of items on @len_stack
160 * @str_buf: Buffer of strings (for v7 format)
161 * @str_ptr: Current write-position in the buffer for strings
162 * @fout: Output file
163 */
164struct twriter {
165 int ptr;
166 struct tw_len len_stack[LEN_STACK_SIZE];
167 int len_count;
168 struct abuf str_buf;
169 int str_ptr;
170 FILE *fout;
171};
172
Simon Glass6c887b22013-06-11 11:14:43 -0700173/* The contents of the trace config file */
174struct trace_configline_info *trace_config_head;
175
Simon Glass9efc0b22023-01-15 14:15:52 -0700176/* list of all functions in System.map file, sorted by offset in the image */
Simon Glass6c887b22013-06-11 11:14:43 -0700177struct func_info *func_list;
Simon Glass6c887b22013-06-11 11:14:43 -0700178
Simon Glass9efc0b22023-01-15 14:15:52 -0700179int func_count; /* number of functions */
180struct trace_call *call_list; /* list of all calls in the input trace file */
181int call_count; /* number of calls */
182int verbose; /* Verbosity level 0=none, 1=warn, 2=notice, 3=info, 4=debug */
183ulong text_offset; /* text address of first function */
184
185/* debugging helpers */
Simon Glass6c887b22013-06-11 11:14:43 -0700186static void outf(int level, const char *fmt, ...)
187 __attribute__ ((format (__printf__, 2, 3)));
188#define error(fmt, b...) outf(0, fmt, ##b)
189#define warn(fmt, b...) outf(1, fmt, ##b)
190#define notice(fmt, b...) outf(2, fmt, ##b)
191#define info(fmt, b...) outf(3, fmt, ##b)
192#define debug(fmt, b...) outf(4, fmt, ##b)
193
Simon Glass6c887b22013-06-11 11:14:43 -0700194static void outf(int level, const char *fmt, ...)
195{
196 if (verbose >= level) {
197 va_list args;
198
199 va_start(args, fmt);
200 vfprintf(stderr, fmt, args);
201 va_end(args);
202 }
203}
204
205static void usage(void)
206{
207 fprintf(stderr,
Simon Glassb87f0812022-12-21 16:08:24 -0700208 "Usage: proftool [-cmtv] <cmd> <profdata>\n"
Simon Glass6c887b22013-06-11 11:14:43 -0700209 "\n"
210 "Commands\n"
Simon Glass9efc0b22023-01-15 14:15:52 -0700211 " dump-ftrace\t\tDump out records in ftrace format for use by trace-cmd\n"
Simon Glass6c887b22013-06-11 11:14:43 -0700212 "\n"
213 "Options:\n"
Simon Glass9efc0b22023-01-15 14:15:52 -0700214 " -c <cfg>\tSpecify config file\n"
Simon Glass6c887b22013-06-11 11:14:43 -0700215 " -m <map>\tSpecify Systen.map file\n"
Simon Glass9efc0b22023-01-15 14:15:52 -0700216 " -o <fname>\tSpecify output file\n"
Simon Glassb87f0812022-12-21 16:08:24 -0700217 " -t <fname>\tSpecify trace data file (from U-Boot 'trace calls')\n"
Simon Glass6c887b22013-06-11 11:14:43 -0700218 " -v <0-4>\tSpecify verbosity\n");
219 exit(EXIT_FAILURE);
220}
221
Simon Glass9efc0b22023-01-15 14:15:52 -0700222/**
223 * h_cmp_offset - bsearch() function to compare two functions bny their offset
224 *
225 * @v1: Pointer to first function (struct func_info)
226 * @v2: Pointer to second function (struct func_info)
227 * Returns: < 0 if v1 offset < v2 offset, 0 if equal, > 0 otherwise
228 */
Simon Glass6c887b22013-06-11 11:14:43 -0700229static int h_cmp_offset(const void *v1, const void *v2)
230{
231 const struct func_info *f1 = v1, *f2 = v2;
232
233 return (f1->offset / FUNC_SITE_SIZE) - (f2->offset / FUNC_SITE_SIZE);
234}
235
Simon Glass9efc0b22023-01-15 14:15:52 -0700236/**
237 * read_system_map() - read the System.map file to create a list of functions
238 *
239 * This also reads the text_offset value, since we assume that the first text
240 * symbol is at that address
241 *
242 * @fin: File to read
243 * Returns: 0 if OK, non-zero on error
244 */
Simon Glass6c887b22013-06-11 11:14:43 -0700245static int read_system_map(FILE *fin)
246{
247 unsigned long offset, start = 0;
248 struct func_info *func;
249 char buff[MAX_LINE_LEN];
250 char symtype;
251 char symname[MAX_LINE_LEN + 1];
252 int linenum;
253 int alloced;
254
255 for (linenum = 1, alloced = func_count = 0;; linenum++) {
256 int fields = 0;
257
258 if (fgets(buff, sizeof(buff), fin))
259 fields = sscanf(buff, "%lx %c %100s\n", &offset,
260 &symtype, symname);
261 if (fields == 2) {
262 continue;
263 } else if (feof(fin)) {
264 break;
265 } else if (fields < 2) {
266 error("Map file line %d: invalid format\n", linenum);
267 return 1;
268 }
269
270 /* Must be a text symbol */
271 symtype = tolower(symtype);
272 if (symtype != 't' && symtype != 'w')
273 continue;
274
275 if (func_count == alloced) {
276 alloced += 256;
277 func_list = realloc(func_list,
278 sizeof(struct func_info) * alloced);
279 assert(func_list);
280 }
281 if (!func_count)
282 start = offset;
283
284 func = &func_list[func_count++];
285 memset(func, '\0', sizeof(*func));
286 func->offset = offset - start;
287 func->name = strdup(symname);
288 func->flags = FUNCF_TRACE; /* trace by default */
289
290 /* Update previous function's code size */
291 if (func_count > 1)
292 func[-1].code_size = func->offset - func[-1].offset;
293 }
294 notice("%d functions found in map file\n", func_count);
295 text_offset = start;
Simon Glass9efc0b22023-01-15 14:15:52 -0700296
Simon Glass6c887b22013-06-11 11:14:43 -0700297 return 0;
298}
299
300static int read_data(FILE *fin, void *buff, int size)
301{
302 int err;
303
304 err = fread(buff, 1, size, fin);
305 if (!err)
306 return 1;
307 if (err != size) {
Simon Glass9efc0b22023-01-15 14:15:52 -0700308 error("Cannot read trace file at pos %lx\n", ftell(fin));
Simon Glass6c887b22013-06-11 11:14:43 -0700309 return -1;
310 }
311 return 0;
312}
313
Simon Glass9efc0b22023-01-15 14:15:52 -0700314/**
315 * find_func_by_offset() - Look up a function by its offset
316 *
317 * @offset: Offset to search for, from text_base
318 * Returns: function, if found, else NULL
319 *
320 * This does a fast search for a function given its offset from text_base
321 *
322 */
323static struct func_info *find_func_by_offset(uint offset)
Simon Glass6c887b22013-06-11 11:14:43 -0700324{
325 struct func_info key, *found;
326
327 key.offset = offset;
328 found = bsearch(&key, func_list, func_count, sizeof(struct func_info),
329 h_cmp_offset);
330
331 return found;
332}
333
Simon Glass9efc0b22023-01-15 14:15:52 -0700334/**
335 * find_caller_by_offset() - finds the function which contains the given offset
336 *
337 * @offset: Offset to search for, from text_base
338 * Returns: function, if found, else NULL
339 *
340 * If the offset falls between two functions, then it is assumed to belong to
341 * the first function (with the lowest offset). This is a way of figuring out
342 * which function owns code at a particular offset
343 */
344static struct func_info *find_caller_by_offset(uint offset)
Simon Glass6c887b22013-06-11 11:14:43 -0700345{
346 int low; /* least function that could be a match */
347 int high; /* greated function that could be a match */
348 struct func_info key;
349
350 low = 0;
351 high = func_count - 1;
352 key.offset = offset;
353 while (high > low + 1) {
354 int mid = (low + high) / 2;
355 int result;
356
357 result = h_cmp_offset(&key, &func_list[mid]);
358 if (result > 0)
359 low = mid;
360 else if (result < 0)
361 high = mid;
362 else
363 return &func_list[mid];
364 }
365
366 return low >= 0 ? &func_list[low] : NULL;
367}
368
Simon Glass9efc0b22023-01-15 14:15:52 -0700369/**
370 * read_calls() - Read the list of calls from the trace data
371 *
372 * The calls are stored consecutively in the trace output produced by U-Boot
373 *
374 * @fin: File to read from
375 * @count: Number of calls to read
376 * Returns: 0 if OK, -1 on error
377 */
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200378static int read_calls(FILE *fin, size_t count)
Simon Glass6c887b22013-06-11 11:14:43 -0700379{
380 struct trace_call *call_data;
381 int i;
382
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200383 notice("call count: %zu\n", count);
Simon Glass6c887b22013-06-11 11:14:43 -0700384 call_list = (struct trace_call *)calloc(count, sizeof(*call_data));
385 if (!call_list) {
386 error("Cannot allocate call_list\n");
387 return -1;
388 }
389 call_count = count;
390
391 call_data = call_list;
392 for (i = 0; i < count; i++, call_data++) {
393 if (read_data(fin, call_data, sizeof(*call_data)))
Simon Glass9efc0b22023-01-15 14:15:52 -0700394 return -1;
Simon Glass6c887b22013-06-11 11:14:43 -0700395 }
396 return 0;
397}
398
Simon Glass9efc0b22023-01-15 14:15:52 -0700399/**
400 * read_trace() - Read the U-Boot trace file
401 *
402 * Read in the calls from the trace file. The function list is ignored at
403 * present
404 *
405 * @fin: File to read
406 * Returns 0 if OK, non-zero on error
407 */
408static int read_trace(FILE *fin)
Simon Glass6c887b22013-06-11 11:14:43 -0700409{
410 struct trace_output_hdr hdr;
411
Simon Glass6c887b22013-06-11 11:14:43 -0700412 while (!feof(fin)) {
413 int err;
414
415 err = read_data(fin, &hdr, sizeof(hdr));
416 if (err == 1)
417 break; /* EOF */
418 else if (err)
419 return 1;
420
421 switch (hdr.type) {
422 case TRACE_CHUNK_FUNCS:
423 /* Ignored at present */
424 break;
425
426 case TRACE_CHUNK_CALLS:
427 if (read_calls(fin, hdr.rec_count))
428 return 1;
429 break;
430 }
431 }
432 return 0;
433}
434
Simon Glass9efc0b22023-01-15 14:15:52 -0700435/**
436 * read_map_file() - Read the System.map file
437 *
438 * This reads the file into the func_list array
439 *
440 * @fname: Filename to read
441 * Returns 0 if OK, non-zero on error
442 */
Simon Glass6c887b22013-06-11 11:14:43 -0700443static int read_map_file(const char *fname)
444{
445 FILE *fmap;
446 int err = 0;
447
448 fmap = fopen(fname, "r");
449 if (!fmap) {
450 error("Cannot open map file '%s'\n", fname);
451 return 1;
452 }
453 if (fmap) {
454 err = read_system_map(fmap);
455 fclose(fmap);
456 }
457 return err;
458}
459
Simon Glass9efc0b22023-01-15 14:15:52 -0700460/**
461 * read_trace_file() - Open and read the U-Boot trace file
462 *
463 * Read in the calls from the trace file. The function list is ignored at
464 * present
465 *
466 * @fin: File to read
467 * Returns 0 if OK, non-zero on error
468 */
469static int read_trace_file(const char *fname)
Simon Glass6c887b22013-06-11 11:14:43 -0700470{
Simon Glass6c887b22013-06-11 11:14:43 -0700471 FILE *fprof;
472 int err;
473
474 fprof = fopen(fname, "rb");
475 if (!fprof) {
Simon Glass9efc0b22023-01-15 14:15:52 -0700476 error("Cannot open trace data file '%s'\n",
Simon Glass6c887b22013-06-11 11:14:43 -0700477 fname);
478 return 1;
479 } else {
Simon Glass9efc0b22023-01-15 14:15:52 -0700480 err = read_trace(fprof);
Simon Glass6c887b22013-06-11 11:14:43 -0700481 fclose(fprof);
482 if (err)
483 return err;
Simon Glass6c887b22013-06-11 11:14:43 -0700484 }
485 return 0;
486}
487
488static int regex_report_error(regex_t *regex, int err, const char *op,
489 const char *name)
490{
491 char buf[200];
492
493 regerror(err, regex, buf, sizeof(buf));
494 error("Regex error '%s' in %s '%s'\n", buf, op, name);
495 return -1;
496}
497
498static void check_trace_config_line(struct trace_configline_info *item)
499{
500 struct func_info *func, *end;
501 int err;
502
503 debug("Checking trace config line '%s'\n", item->name);
504 for (func = func_list, end = func + func_count; func < end; func++) {
505 err = regexec(&item->regex, func->name, 0, NULL, 0);
506 debug(" - regex '%s', string '%s': %d\n", item->name,
507 func->name, err);
508 if (err == REG_NOMATCH)
509 continue;
510
Andreas Bießmannd4125eb2013-07-02 08:37:36 +0200511 if (err) {
Simon Glass6c887b22013-06-11 11:14:43 -0700512 regex_report_error(&item->regex, err, "match",
513 item->name);
514 break;
515 }
516
517 /* It matches, so perform the action */
518 switch (item->type) {
519 case TRACE_LINE_INCLUDE:
520 info(" include %s at %lx\n", func->name,
521 text_offset + func->offset);
522 func->flags |= FUNCF_TRACE;
523 break;
524
525 case TRACE_LINE_EXCLUDE:
526 info(" exclude %s at %lx\n", func->name,
527 text_offset + func->offset);
528 func->flags &= ~FUNCF_TRACE;
529 break;
530 }
531 }
532}
533
Simon Glass9efc0b22023-01-15 14:15:52 -0700534/** check_trace_config() - Check trace-config file, reporting any problems */
Simon Glass6c887b22013-06-11 11:14:43 -0700535static void check_trace_config(void)
536{
537 struct trace_configline_info *line;
538
539 for (line = trace_config_head; line; line = line->next)
540 check_trace_config_line(line);
541}
542
543/**
544 * Check the functions to see if they each have an objsection. If not, then
545 * the linker must have eliminated them.
546 */
547static void check_functions(void)
548{
549 struct func_info *func, *end;
550 unsigned long removed_code_size = 0;
551 int not_found = 0;
552
553 /* Look for missing functions */
554 for (func = func_list, end = func + func_count; func < end; func++) {
555 if (!func->objsection) {
556 removed_code_size += func->code_size;
557 not_found++;
558 }
559 }
560
561 /* Figure out what functions we want to trace */
562 check_trace_config();
563
564 warn("%d functions removed by linker, %ld code size\n",
565 not_found, removed_code_size);
566}
567
Simon Glass9efc0b22023-01-15 14:15:52 -0700568/**
569 * read_trace_config() - read the trace-config file
570 *
571 * This file consists of lines like:
572 *
573 * include-func <regex>
574 * exclude-func <regex>
575 *
576 * where <regex> is a regular expression matched against function names. It
577 * allows some functions to be dropped from the trace when producing ftrace
578 * records
579 *
580 * @fin: File to process
581 * Returns: 0 if OK, -1 on error
582 */
Simon Glass6c887b22013-06-11 11:14:43 -0700583static int read_trace_config(FILE *fin)
584{
585 char buff[200];
586 int linenum = 0;
587 struct trace_configline_info **tailp = &trace_config_head;
588
589 while (fgets(buff, sizeof(buff), fin)) {
590 int len = strlen(buff);
591 struct trace_configline_info *line;
592 char *saveptr;
593 char *s, *tok;
594 int err;
595
596 linenum++;
597 if (len && buff[len - 1] == '\n')
598 buff[len - 1] = '\0';
599
600 /* skip blank lines and comments */
601 for (s = buff; *s == ' ' || *s == '\t'; s++)
602 ;
603 if (!*s || *s == '#')
604 continue;
605
Simon Glass9efc0b22023-01-15 14:15:52 -0700606 line = (struct trace_configline_info *)calloc(1, sizeof(*line));
Simon Glass6c887b22013-06-11 11:14:43 -0700607 if (!line) {
608 error("Cannot allocate config line\n");
609 return -1;
610 }
611
612 tok = strtok_r(s, " \t", &saveptr);
613 if (!tok) {
614 error("Invalid trace config data on line %d\n",
615 linenum);
616 return -1;
617 }
618 if (0 == strcmp(tok, "include-func")) {
619 line->type = TRACE_LINE_INCLUDE;
620 } else if (0 == strcmp(tok, "exclude-func")) {
621 line->type = TRACE_LINE_EXCLUDE;
622 } else {
623 error("Unknown command in trace config data line %d\n",
624 linenum);
625 return -1;
626 }
627
628 tok = strtok_r(NULL, " \t", &saveptr);
629 if (!tok) {
630 error("Missing pattern in trace config data line %d\n",
631 linenum);
632 return -1;
633 }
634
635 err = regcomp(&line->regex, tok, REG_NOSUB);
636 if (err) {
Vincent Stehlé1ca8f882015-10-07 15:48:48 +0200637 int r = regex_report_error(&line->regex, err,
638 "compile", tok);
Simon Glass6c887b22013-06-11 11:14:43 -0700639 free(line);
Vincent Stehlé1ca8f882015-10-07 15:48:48 +0200640 return r;
Simon Glass6c887b22013-06-11 11:14:43 -0700641 }
642
643 /* link this new one to the end of the list */
644 line->name = strdup(tok);
645 line->next = NULL;
646 *tailp = line;
647 tailp = &line->next;
648 }
649
650 if (!feof(fin)) {
651 error("Cannot read from trace config file at position %ld\n",
652 ftell(fin));
653 return -1;
654 }
655 return 0;
656}
657
658static int read_trace_config_file(const char *fname)
659{
660 FILE *fin;
661 int err;
662
663 fin = fopen(fname, "r");
664 if (!fin) {
665 error("Cannot open trace_config file '%s'\n", fname);
666 return -1;
667 }
668 err = read_trace_config(fin);
669 fclose(fin);
670 return err;
671}
672
Simon Glassbe16fc82023-01-15 14:15:53 -0700673/**
674 * tputh() - Write a 16-bit little-endian value to a file
675 *
676 * @fout: File to write to
677 * @val: Value to write
678 * Returns: number of bytes written (2)
679 */
680static int tputh(FILE *fout, unsigned int val)
Simon Glass6c887b22013-06-11 11:14:43 -0700681{
Simon Glassbe16fc82023-01-15 14:15:53 -0700682 fputc(val, fout);
683 fputc(val >> 8, fout);
Simon Glass6c887b22013-06-11 11:14:43 -0700684
Simon Glassbe16fc82023-01-15 14:15:53 -0700685 return 2;
Simon Glass6c887b22013-06-11 11:14:43 -0700686}
687
Simon Glassbe16fc82023-01-15 14:15:53 -0700688/**
689 * tputl() - Write a 32-bit little-endian value to a file
690 *
691 * @fout: File to write to
692 * @val: Value to write
693 * Returns: number of bytes written (4)
Simon Glass6c887b22013-06-11 11:14:43 -0700694 */
Simon Glassbe16fc82023-01-15 14:15:53 -0700695static int tputl(FILE *fout, ulong val)
Simon Glass6c887b22013-06-11 11:14:43 -0700696{
Simon Glassbe16fc82023-01-15 14:15:53 -0700697 fputc(val, fout);
698 fputc(val >> 8, fout);
699 fputc(val >> 16, fout);
700 fputc(val >> 24, fout);
701
702 return 4;
703}
704
705/**
706 * tputh() - Write a 64-bit little-endian value to a file
707 *
708 * @fout: File to write to
709 * @val: Value to write
710 * Returns: number of bytes written (8)
711 */
712static int tputq(FILE *fout, unsigned long long val)
713{
714 tputl(fout, val);
715 tputl(fout, val >> 32U);
716
717 return 8;
718}
719
720/**
721 * tputh() - Write a string to a file
722 *
723 * The string is written without its terminator
724 *
725 * @fout: File to write to
726 * @val: Value to write
727 * Returns: number of bytes written
728 */
729static int tputs(FILE *fout, const char *str)
730{
731 fputs(str, fout);
732
733 return strlen(str);
734}
735
736/**
737 * add_str() - add a name string to the string table
738 *
739 * This is used by the v7 format
740 *
741 * @tw: Writer context
742 * @name: String to write
743 * Returns: Updated value of string pointer, or -1 if out of memory
744 */
745static int add_str(struct twriter *tw, const char *name)
746{
747 int str_ptr;
748 int len;
749
750 len = strlen(name) + 1;
751 str_ptr = tw->str_ptr;
752 tw->str_ptr += len;
753
754 if (tw->str_ptr > abuf_size(&tw->str_buf)) {
755 int new_size;
756
757 new_size = ALIGN(tw->str_ptr, 4096);
758 if (!abuf_realloc(&tw->str_buf, new_size))
759 return -1;
760 }
761
762 return str_ptr;
763}
764
765/**
766 * push_len() - Push a new length request onto the stack
767 *
768 * @tw: Writer context
769 * @base: Base position of the length calculation
770 * @msg: Indicates the type of caller, for debugging
771 * @size: Size of the length value, either 4 bytes or 8
772 * Returns number of bytes written to the file (=@size on success), -ve on error
773 *
774 * This marks a place where a length must be written, covering data that is
775 * about to be written. It writes a placeholder value.
776 *
777 * Once the data is written, calling pop_len() will update the placeholder with
778 * the correct length based on how many bytes have been written
779 */
780static int push_len(struct twriter *tw, int base, const char *msg, int size)
781{
782 struct tw_len *lp;
783
784 if (tw->len_count >= LEN_STACK_SIZE) {
785 fprintf(stderr, "Length-stack overflow: %s\n", msg);
786 return -1;
787 }
788 if (size != 4 && size != 8) {
789 fprintf(stderr, "Length-stack invalid size %d: %s\n", size,
790 msg);
791 return -1;
792 }
793
794 lp = &tw->len_stack[tw->len_count++];
795 lp->base = base;
796 lp->ptr = tw->ptr;
797 lp->size = size;
798
799 return size == 8 ? tputq(tw->fout, 0) : tputl(tw->fout, 0);
800}
801
802/**
803 * pop_len() - Update a length value once the length is known
804 *
805 * Pops a value of the length stack and updates the file at that position with
806 * the number of bytes written between now and then. Once done, the file is
807 * seeked to the current (tw->ptr) position again, so writing can continue as
808 * normal.
809 *
810 * @tw: Writer context
811 * @msg: Indicates the type of caller, for debugging
812 * Returns 0 if OK, -1 on error
813 */
814static int pop_len(struct twriter *tw, const char *msg)
815{
816 struct tw_len *lp;
817 int len, ret;
818
819 if (!tw->len_count) {
820 fprintf(stderr, "Length-stack underflow: %s\n", msg);
821 return -1;
822 }
823
824 lp = &tw->len_stack[--tw->len_count];
825 if (fseek(tw->fout, lp->ptr, SEEK_SET))
826 return -1;
827 len = tw->ptr - lp->base;
828 ret = lp->size == 8 ? tputq(tw->fout, len) : tputl(tw->fout, len);
829 if (ret < 0)
830 return -1;
831 if (fseek(tw->fout, tw->ptr, SEEK_SET))
832 return -1;
833
834 return 0;
835}
836
837/**
838 * start_header() - Start a v7 section
839 *
840 * Writes a header in v7 format
841 *
842 * @tw: Writer context
843 * @id: ID of header to write (SECTION_...)
844 * @flags: Flags value to write
845 * @name: Name of section
846 * Returns: number of bytes written
847 */
848static int start_header(struct twriter *tw, int id, uint flags,
849 const char *name)
850{
851 int str_id;
852 int lptr;
853 int base;
854 int ret;
855
856 base = tw->ptr + 16;
857 lptr = 0;
858 lptr += tputh(tw->fout, id);
859 lptr += tputh(tw->fout, flags);
860 str_id = add_str(tw, name);
861 if (str_id < 0)
862 return -1;
863 lptr += tputl(tw->fout, str_id);
864
865 /* placeholder for size */
866 ret = push_len(tw, base, "v7 header", 8);
867 if (ret < 0)
868 return -1;
869 lptr += ret;
870
871 return lptr;
872}
873
874/**
875 * start_page() - Start a new page of output data
876 *
877 * The output is arranged in 4KB pages with a base timestamp at the start of
878 * each. This starts a new page, making sure it is aligned to 4KB in the output
879 * file.
880 *
881 * @tw: Writer context
882 * @timestamp: Base timestamp for the page
883 */
884static int start_page(struct twriter *tw, ulong timestamp)
885{
886 int start;
887 int ret;
888
889 /* move to start of next page */
890 start = ALIGN(tw->ptr, TRACE_PAGE_SIZE);
891 ret = fseek(tw->fout, start, SEEK_SET);
892 if (ret < 0) {
893 fprintf(stderr, "Cannot seek to page start\n");
894 return -1;
895 }
896 tw->ptr = start;
897
898 /* page header */
899 tw->ptr += tputq(tw->fout, timestamp);
900 ret = push_len(tw, start + 16, "page", 8);
901 if (ret < 0)
902 return ret;
903 tw->ptr += ret;
904
905 return 0;
906}
907
908/**
909 * finish_page() - finish a page
910 *
911 * Sets the lengths correctly and moves to the start of the next page
912 *
913 * @tw: Writer context
914 * Returns: 0 on success, -1 on error
915 */
916static int finish_page(struct twriter *tw)
917{
918 int ret, end;
919
920 ret = pop_len(tw, "page");
921 if (ret < 0)
922 return ret;
923 end = ALIGN(tw->ptr, TRACE_PAGE_SIZE);
924
925 /*
926 * Write a byte so that the data actually makes to the file, in the case
927 * that we never write any more pages
928 */
929 if (tw->ptr != end) {
930 if (fseek(tw->fout, end - 1, SEEK_SET)) {
931 fprintf(stderr, "cannot seek to start of next page\n");
932 return -1;
933 }
934 fputc(0, tw->fout);
935 tw->ptr = end;
936 }
937
938 return 0;
939}
940
941/**
942 * output_headers() - Output v6 headers to the file
943 *
944 * Writes out the various formats so that trace-cmd and kernelshark can make
945 * sense of the data
946 *
947 * This updates tw->ptr as it goes
948 *
949 * @tw: Writer context
950 * Returns: 0 on success, -ve on error
951 */
952static int output_headers(struct twriter *tw)
953{
954 FILE *fout = tw->fout;
955 char str[800];
956 int len, ret;
957
958 tw->ptr += fprintf(fout, "%c%c%ctracing6%c%c%c", 0x17, 0x08, 0x44,
959 0 /* terminator */, 0 /* little endian */,
960 4 /* 32-bit long values */);
961
962 /* host-machine page size 4KB */
963 tw->ptr += tputl(fout, 4 << 10);
964
965 tw->ptr += fprintf(fout, "header_page%c", 0);
966
967 snprintf(str, sizeof(str),
968 "\tfield: u64 timestamp;\toffset:0;\tsize:8;\tsigned:0;\n"
969 "\tfield: local_t commit;\toffset:8;\tsize:8;\tsigned:1;\n"
970 "\tfield: int overwrite;\toffset:8;\tsize:1;\tsigned:1;\n"
971 "\tfield: char data;\toffset:16;\tsize:4080;\tsigned:1;\n");
972 len = strlen(str);
973 tw->ptr += tputq(fout, len);
974 tw->ptr += tputs(fout, str);
975
976 if (VERSION7) {
977 /* no compression */
978 tw->ptr += fprintf(fout, "none%cversion%c\n", 0, 0);
979
980 ret = start_header(tw, SECTION_OPTIONS, 0, "options");
981 if (ret < 0) {
982 fprintf(stderr, "Cannot start option header\n");
983 return -1;
984 }
985 tw->ptr += ret;
986 tw->ptr += tputh(fout, OPTION_DONE);
987 tw->ptr += tputl(fout, 8);
988 tw->ptr += tputl(fout, 0);
989 ret = pop_len(tw, "t7 header");
990 if (ret < 0) {
991 fprintf(stderr, "Cannot finish option header\n");
992 return -1;
993 }
994 }
995
996 tw->ptr += fprintf(fout, "header_event%c", 0);
997 snprintf(str, sizeof(str),
998 "# compressed entry header\n"
999 "\ttype_len : 5 bits\n"
1000 "\ttime_delta : 27 bits\n"
1001 "\tarray : 32 bits\n"
1002 "\n"
1003 "\tpadding : type == 29\n"
1004 "\ttime_extend : type == 30\n"
1005 "\ttime_stamp : type == 31\n"
1006 "\tdata max type_len == 28\n");
1007 len = strlen(str);
1008 tw->ptr += tputq(fout, len);
1009 tw->ptr += tputs(fout, str);
1010
1011 /* number of ftrace-event-format files */
1012 tw->ptr += tputl(fout, 3);
1013
1014 snprintf(str, sizeof(str),
1015 "name: function\n"
1016 "ID: 1\n"
1017 "format:\n"
1018 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
1019 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
1020 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;signed:0;\n"
1021 "\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
1022 "\n"
1023 "\tfield:unsigned long ip;\toffset:8;\tsize:8;\tsigned:0;\n"
1024 "\tfield:unsigned long parent_ip;\toffset:16;\tsize:8;\tsigned:0;\n"
1025 "\n"
1026 "print fmt: \" %%ps <-- %%ps\", (void *)REC->ip, (void *)REC->parent_ip\n");
1027 len = strlen(str);
1028 tw->ptr += tputq(fout, len);
1029 tw->ptr += tputs(fout, str);
1030
1031 snprintf(str, sizeof(str),
1032 "name: funcgraph_entry\n"
1033 "ID: 11\n"
1034 "format:\n"
1035 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
1036 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
1037 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;signed:0;\n"
1038 "\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
1039 "\n"
1040 "\tfield:unsigned long func;\toffset:8;\tsize:8;\tsigned:0;\n"
1041 "\tfield:int depth;\toffset:16;\tsize:4;\tsigned:1;\n"
1042 "\n"
1043 "print fmt: \"--> %%ps (%%d)\", (void *)REC->func, REC->depth\n");
1044 len = strlen(str);
1045 tw->ptr += tputq(fout, len);
1046 tw->ptr += tputs(fout, str);
1047
1048 snprintf(str, sizeof(str),
1049 "name: funcgraph_exit\n"
1050 "ID: 10\n"
1051 "format:\n"
1052 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
1053 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
1054 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;signed:0;\n"
1055 "\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
1056 "\n"
1057 "\tfield:unsigned long func;\toffset:8;\tsize:8;\tsigned:0;\n"
1058 "\tfield:int depth;\toffset:16;\tsize:4;\tsigned:1;\n"
1059 "\tfield:unsigned int overrun;\toffset:20;\tsize:4;\tsigned:0;\n"
1060 "\tfield:unsigned long long calltime;\toffset:24;\tsize:8;\tsigned:0;\n"
1061 "\tfield:unsigned long long rettime;\toffset:32;\tsize:8;\tsigned:0;\n"
1062 "\n"
1063 "print fmt: \"<-- %%ps (%%d) (start: %%llx end: %%llx) over: %%d\", (void *)REC->func, REC->depth, REC->calltime, REC->rettime, REC->depth\n");
1064 len = strlen(str);
1065 tw->ptr += tputq(fout, len);
1066 tw->ptr += tputs(fout, str);
1067
1068 return 0;
1069}
1070
1071/**
1072 * write_symbols() - Write the symbols out
1073 *
1074 * Writes the symbol information in the following format to mimic the Linux
1075 * /proc/kallsyms file:
1076 *
1077 * <address> T <name>
1078 *
1079 * This updates tw->ptr as it goes
1080 *
1081 * @tw: Writer context
1082 * Returns: 0 on success, -ve on error
1083 */
1084static int write_symbols(struct twriter *tw)
1085{
1086 char str[200];
1087 int ret, i;
1088
1089 /* write symbols */
1090 ret = push_len(tw, tw->ptr + 4, "syms", 4);
1091 if (ret < 0)
1092 return -1;
1093 tw->ptr += ret;
1094 for (i = 0; i < func_count; i++) {
1095 struct func_info *func = &func_list[i];
1096
1097 snprintf(str, sizeof(str), "%016lx T %s\n",
1098 text_offset + func->offset, func->name);
1099 tw->ptr += tputs(tw->fout, str);
1100 }
1101 ret = pop_len(tw, "syms");
1102 if (ret < 0)
1103 return -1;
1104 tw->ptr += ret;
1105
1106 return 0;
1107}
1108
1109/**
1110 * write_options() - Write the options out
1111 *
1112 * Writes various options which are needed or useful. We use OPTION_TSC2NSEC
1113 * to indicates that values in the output need to be multiplied by 1000 since
1114 * U-Boot's trace values are in microseconds.
1115 *
1116 * This updates tw->ptr as it goes
1117 *
1118 * @tw: Writer context
1119 * Returns: 0 on success, -ve on error
1120 */
1121static int write_options(struct twriter *tw)
1122{
1123 FILE *fout = tw->fout;
1124 char str[200];
1125 int len;
1126
1127 /* trace_printk, 0 for now */
1128 tw->ptr += tputl(fout, 0);
1129
1130 /* processes */
1131 snprintf(str, sizeof(str), "%d u-boot\n", TRACE_PID);
1132 len = strlen(str);
1133 tw->ptr += tputq(fout, len);
1134 tw->ptr += tputs(fout, str);
1135
1136 /* number of CPUs */
1137 tw->ptr += tputl(fout, 1);
1138
1139 tw->ptr += fprintf(fout, "options %c", 0);
1140
1141 /* traceclock */
1142 tw->ptr += tputh(fout, OPTION_TRACECLOCK);
1143 tw->ptr += tputl(fout, 0);
1144
1145 /* uname */
1146 tw->ptr += tputh(fout, OPTION_UNAME);
1147 snprintf(str, sizeof(str), "U-Boot");
1148 len = strlen(str);
1149 tw->ptr += tputl(fout, len);
1150 tw->ptr += tputs(fout, str);
1151
1152 /* version */
1153 tw->ptr += tputh(fout, OPTION_VERSION);
1154 snprintf(str, sizeof(str), "unknown");
1155 len = strlen(str);
1156 tw->ptr += tputl(fout, len);
1157 tw->ptr += tputs(fout, str);
1158
1159 /* trace ID */
1160 tw->ptr += tputh(fout, OPTION_TRACEID);
1161 tw->ptr += tputl(fout, 8);
1162 tw->ptr += tputq(fout, 0x123456780abcdef0);
1163
1164 /* time conversion */
1165 tw->ptr += tputh(fout, OPTION_TSC2NSEC);
1166 tw->ptr += tputl(fout, 16);
1167 tw->ptr += tputl(fout, 1000); /* multiplier */
1168 tw->ptr += tputl(fout, 0); /* shift */
1169 tw->ptr += tputq(fout, 0); /* offset */
1170
1171 /* cpustat - bogus data for now, but at least it mentions the CPU */
1172 tw->ptr += tputh(fout, OPTION_CPUSTAT);
1173 snprintf(str, sizeof(str),
1174 "CPU: 0\n"
1175 "entries: 100\n"
1176 "overrun: 43565\n"
1177 "commit overrun: 0\n"
1178 "bytes: 3360\n"
1179 "oldest event ts: 963732.447752\n"
1180 "now ts: 963832.146824\n"
1181 "dropped events: 0\n"
1182 "read events: 42379\n");
1183 len = strlen(str);
1184 tw->ptr += tputl(fout, len);
1185 tw->ptr += tputs(fout, str);
1186
1187 tw->ptr += tputh(fout, OPTION_DONE);
1188
1189 return 0;
1190}
1191
1192/**
1193 * write_pages() - Write the pages of trace data
1194 *
1195 * This works through all the calls, writing out as many pages of data as are
1196 * needed.
1197 *
1198 * @tw: Writer context
1199 * @missing_countp: Returns number of missing functions (not found in function
1200 * list)
1201 * @skip_countp: Returns number of skipped functions (excluded from trace)
1202 *
1203 * Returns: 0 on success, -ve on error
1204 */
1205static int write_pages(struct twriter *tw, int *missing_countp,
1206 int *skip_countp)
1207{
1208 int stack_ptr; /* next free position in stack */
1209 int upto, page_upto, i;
Simon Glass6c887b22013-06-11 11:14:43 -07001210 int missing_count = 0, skip_count = 0;
Simon Glassbe16fc82023-01-15 14:15:53 -07001211 struct trace_call *call;
1212 ulong last_timestamp;
1213 FILE *fout = tw->fout;
1214 int last_delta = 0;
1215 int err_count;
1216 bool in_page;
Simon Glass6c887b22013-06-11 11:14:43 -07001217
Simon Glassbe16fc82023-01-15 14:15:53 -07001218 in_page = false;
1219 last_timestamp = 0;
1220 upto = 0;
1221 page_upto = 0;
1222 err_count = 0;
1223
1224 /* maintain a stack of start times for calling functions */
1225 stack_ptr = 0;
1226
Simon Glass6c887b22013-06-11 11:14:43 -07001227 for (i = 0, call = call_list; i < call_count; i++, call++) {
Simon Glassbe16fc82023-01-15 14:15:53 -07001228 struct func_info *caller_func;
1229 struct func_info *func;
1230 ulong timestamp;
1231 uint rec_words;
1232 int delta;
Simon Glass6c887b22013-06-11 11:14:43 -07001233
Simon Glassbe16fc82023-01-15 14:15:53 -07001234 func = find_func_by_offset(call->func);
Simon Glass6c887b22013-06-11 11:14:43 -07001235 if (!func) {
1236 warn("Cannot find function at %lx\n",
1237 text_offset + call->func);
1238 missing_count++;
Simon Glassbe16fc82023-01-15 14:15:53 -07001239 if (missing_count > 20) {
1240 /* perhaps trace does not match System.map */
1241 fprintf(stderr, "Too many missing functions\n");
1242 return -1;
1243 }
Simon Glass6c887b22013-06-11 11:14:43 -07001244 continue;
1245 }
1246
1247 if (!(func->flags & FUNCF_TRACE)) {
1248 debug("Funcion '%s' is excluded from trace\n",
1249 func->name);
1250 skip_count++;
1251 continue;
1252 }
1253
Simon Glassbe16fc82023-01-15 14:15:53 -07001254 rec_words = 6;
Simon Glass6c887b22013-06-11 11:14:43 -07001255
Simon Glassbe16fc82023-01-15 14:15:53 -07001256 /* convert timestamp from us to ns */
1257 timestamp = call->flags & FUNCF_TIMESTAMP_MASK;
1258 if (in_page) {
1259 if (page_upto + rec_words * 4 > TRACE_PAGE_SIZE) {
1260 if (finish_page(tw))
1261 return -1;
1262 in_page = false;
1263 }
1264 }
1265 if (!in_page) {
1266 if (start_page(tw, timestamp))
1267 return -1;
1268 in_page = true;
1269 last_timestamp = timestamp;
1270 last_delta = 0;
1271 page_upto = tw->ptr & TRACE_PAGE_MASK;
1272 if (_DEBUG) {
1273 fprintf(stderr,
1274 "new page, last_timestamp=%ld, upto=%d\n",
1275 last_timestamp, upto);
1276 }
1277 }
1278
1279 delta = timestamp - last_timestamp;
1280 if (delta < 0) {
1281 fprintf(stderr, "Time went backwards\n");
1282 err_count++;
1283 }
1284
1285 if (err_count > 20) {
1286 fprintf(stderr, "Too many errors, giving up\n");
1287 return -1;
1288 }
1289
1290 if (delta > 0x07fffff) {
1291 /*
1292 * hard to imagine how this could happen since it means
1293 * that no function calls were made for a long time
1294 */
1295 fprintf(stderr, "cannot represent time delta %x\n",
1296 delta);
1297 return -1;
1298 }
1299
1300 if (_DEBUG) {
1301 fprintf(stderr, "%d: delta=%d, stamp=%ld\n",
1302 upto, delta, timestamp);
1303 fprintf(stderr,
1304 " last_delta %x to %x: last_timestamp=%lx, timestamp=%lx, call->flags=%x, upto=%d\n",
1305 last_delta, delta, last_timestamp, timestamp, call->flags, upto);
1306 }
1307
1308 /* type_len is 6, meaning 4 * 6 = 24 bytes */
1309 tw->ptr += tputl(fout, rec_words | (uint)delta << 5);
1310 tw->ptr += tputh(fout, TRACE_FN);
1311 tw->ptr += tputh(fout, 0); /* flags */
1312 tw->ptr += tputl(fout, TRACE_PID); /* PID */
1313 /* function */
1314 tw->ptr += tputq(fout, text_offset + func->offset);
1315 caller_func = find_caller_by_offset(call->caller);
1316 /* caller */
1317 tw->ptr += tputq(fout, text_offset + caller_func->offset);
1318
1319 last_delta = delta;
1320 last_timestamp = timestamp;
1321 page_upto += 4 + rec_words * 4;
1322 upto++;
1323 if (stack_ptr == MAX_STACK_DEPTH)
1324 break;
Simon Glass6c887b22013-06-11 11:14:43 -07001325 }
Simon Glassbe16fc82023-01-15 14:15:53 -07001326 if (in_page && finish_page(tw))
1327 return -1;
1328 *missing_countp = missing_count;
1329 *skip_countp = skip_count;
1330
1331 return 0;
1332}
1333
1334/**
1335 * write_flyrecord() - Write the flyrecord information
1336 *
1337 * Writes the header and pages of data for the "flyrecord" section. It also
1338 * writes out the counter-type info, selecting "[local]"
1339 *
1340 * @tw: Writer context
1341 * @missing_countp: Returns number of missing functions (not found in function
1342 * list)
1343 * @skip_countp: Returns number of skipped functions (excluded from trace)
1344 *
1345 * Returns: 0 on success, -ve on error
1346 */
1347static int write_flyrecord(struct twriter *tw, int *missing_countp,
1348 int *skip_countp)
1349{
1350 int start, ret, len;
1351 FILE *fout = tw->fout;
1352 char str[200];
1353
1354 tw->ptr += fprintf(fout, "flyrecord%c", 0);
1355
1356 /* trace data */
1357 start = ALIGN(tw->ptr + 16, TRACE_PAGE_SIZE);
1358 tw->ptr += tputq(fout, start);
1359
1360 /* use a placeholder for the size */
1361 ret = push_len(tw, start, "flyrecord", 8);
1362 if (ret < 0)
1363 return -1;
1364 tw->ptr += ret;
1365
1366 snprintf(str, sizeof(str),
1367 "[local] global counter uptime perf mono mono_raw boot x86-tsc\n");
1368 len = strlen(str);
1369 tw->ptr += tputq(fout, len);
1370 tw->ptr += tputs(fout, str);
1371
1372 ret = write_pages(tw, missing_countp, skip_countp);
1373 if (ret < 0) {
1374 fprintf(stderr, "Cannot output pages\n");
1375 return -1;
1376 }
1377
1378 ret = pop_len(tw, "flyrecord");
1379 if (ret < 0) {
1380 fprintf(stderr, "Cannot finish flyrecord header\n");
1381 return -1;
1382 }
1383
1384 return 0;
1385}
1386
1387/**
1388 * make_ftrace() - Write out an ftrace file
1389 *
1390 * See here for format:
1391 *
1392 * https://github.com/rostedt/trace-cmd/blob/master/Documentation/trace-cmd/trace-cmd.dat.v7.5.txt
1393 *
1394 * @fout: Output file
1395 * Returns: 0 on success, -ve on error
1396 */
1397static int make_ftrace(FILE *fout)
1398{
1399 int missing_count, skip_count;
1400 struct twriter tws, *tw = &tws;
1401 int ret;
1402
1403 memset(tw, '\0', sizeof(*tw));
1404 abuf_init(&tw->str_buf);
1405 tw->fout = fout;
1406
1407 tw->ptr = 0;
1408 ret = output_headers(tw);
1409 if (ret < 0) {
1410 fprintf(stderr, "Cannot output headers\n");
1411 return -1;
1412 }
1413 /* number of event systems files */
1414 tw->ptr += tputl(fout, 0);
1415
1416 ret = write_symbols(tw);
1417 if (ret < 0) {
1418 fprintf(stderr, "Cannot write symbols\n");
1419 return -1;
1420 }
1421
1422 ret = write_options(tw);
1423 if (ret < 0) {
1424 fprintf(stderr, "Cannot write options\n");
1425 return -1;
1426 }
1427
1428 ret = write_flyrecord(tw, &missing_count, &skip_count);
1429 if (ret < 0) {
1430 fprintf(stderr, "Cannot write flyrecord\n");
1431 return -1;
1432 }
1433
Simon Glass6c887b22013-06-11 11:14:43 -07001434 info("ftrace: %d functions not found, %d excluded\n", missing_count,
1435 skip_count);
1436
1437 return 0;
1438}
1439
Simon Glass9efc0b22023-01-15 14:15:52 -07001440/**
1441 * prof_tool() - Performs requested action
1442 *
1443 * @argc: Number of arguments (used to obtain the command
1444 * @argv: List of arguments
1445 * @trace_fname: Filename of input file (trace data from U-Boot)
1446 * @map_fname: Filename of map file (System.map from U-Boot)
1447 * @trace_config_fname: Trace-configuration file, or NULL if none
1448 * @out_fname: Output filename
1449 */
Simon Glass09140112020-05-10 11:40:03 -06001450static int prof_tool(int argc, char *const argv[],
Simon Glass9efc0b22023-01-15 14:15:52 -07001451 const char *trace_fname, const char *map_fname,
1452 const char *trace_config_fname, const char *out_fname)
Simon Glass6c887b22013-06-11 11:14:43 -07001453{
1454 int err = 0;
1455
1456 if (read_map_file(map_fname))
1457 return -1;
Simon Glass9efc0b22023-01-15 14:15:52 -07001458 if (trace_fname && read_trace_file(trace_fname))
Simon Glass6c887b22013-06-11 11:14:43 -07001459 return -1;
1460 if (trace_config_fname && read_trace_config_file(trace_config_fname))
1461 return -1;
1462
1463 check_functions();
1464
1465 for (; argc; argc--, argv++) {
1466 const char *cmd = *argv;
1467
Simon Glassbe16fc82023-01-15 14:15:53 -07001468 if (!strcmp(cmd, "dump-ftrace")) {
1469 FILE *fout;
1470
1471 fout = fopen(out_fname, "w");
1472 if (!fout) {
1473 fprintf(stderr, "Cannot write file '%s'\n",
1474 out_fname);
1475 return -1;
1476 }
1477 err = make_ftrace(fout);
1478 fclose(fout);
1479 } else {
Simon Glass6c887b22013-06-11 11:14:43 -07001480 warn("Unknown command '%s'\n", cmd);
Simon Glassbe16fc82023-01-15 14:15:53 -07001481 }
Simon Glass6c887b22013-06-11 11:14:43 -07001482 }
1483
1484 return err;
1485}
1486
1487int main(int argc, char *argv[])
1488{
1489 const char *map_fname = "System.map";
Simon Glassb87f0812022-12-21 16:08:24 -07001490 const char *trace_fname = NULL;
1491 const char *config_fname = NULL;
Simon Glass9efc0b22023-01-15 14:15:52 -07001492 const char *out_fname = NULL;
Simon Glass6c887b22013-06-11 11:14:43 -07001493 int opt;
1494
1495 verbose = 2;
Simon Glass9efc0b22023-01-15 14:15:52 -07001496 while ((opt = getopt(argc, argv, "c:m:o:t:v:")) != -1) {
Simon Glass6c887b22013-06-11 11:14:43 -07001497 switch (opt) {
Simon Glassb87f0812022-12-21 16:08:24 -07001498 case 'c':
1499 config_fname = optarg;
1500 break;
Simon Glass6c887b22013-06-11 11:14:43 -07001501 case 'm':
1502 map_fname = optarg;
1503 break;
Simon Glass9efc0b22023-01-15 14:15:52 -07001504 case 'o':
1505 out_fname = optarg;
1506 break;
Simon Glass6c887b22013-06-11 11:14:43 -07001507 case 't':
Simon Glassb87f0812022-12-21 16:08:24 -07001508 trace_fname = optarg;
Simon Glass6c887b22013-06-11 11:14:43 -07001509 break;
Simon Glass6c887b22013-06-11 11:14:43 -07001510 case 'v':
1511 verbose = atoi(optarg);
1512 break;
Simon Glass6c887b22013-06-11 11:14:43 -07001513 default:
1514 usage();
1515 }
1516 }
1517 argc -= optind; argv += optind;
1518 if (argc < 1)
1519 usage();
1520
Simon Glass9efc0b22023-01-15 14:15:52 -07001521 if (!out_fname || !map_fname || !trace_fname) {
1522 fprintf(stderr,
1523 "Must provide trace data, System.map file and output file\n");
1524 usage();
1525 }
1526
Simon Glass6c887b22013-06-11 11:14:43 -07001527 debug("Debug enabled\n");
Simon Glass9efc0b22023-01-15 14:15:52 -07001528 return prof_tool(argc, argv, trace_fname, map_fname, config_fname,
1529 out_fname);
Simon Glass6c887b22013-06-11 11:14:43 -07001530}