Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef __TRACE_H |
| 7 | #define __TRACE_H |
| 8 | |
Simon Glass | adefafe | 2023-01-15 14:15:46 -0700 | [diff] [blame] | 9 | /* this file is included from a tool so uses uint32_t instead of u32, etc. */ |
| 10 | |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 11 | enum { |
| 12 | /* |
| 13 | * This affects the granularity of our trace. We can bin function |
| 14 | * entry points into groups on the basis that functions typically |
| 15 | * have a minimum size, so entry points can't appear any closer |
| 16 | * than this to each other. |
| 17 | * |
| 18 | * The value here assumes a minimum instruction size of 4 bytes, |
| 19 | * or that instructions are 2 bytes but there are at least 2 of |
Simon Glass | ec26f89 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 20 | * them in every function. Given that each function needs a call to |
| 21 | * __cyg_profile_func_enter() and __cyg_profile_func_exit() as well, |
| 22 | * we cannot have functions smaller that 16 bytes. |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 23 | * |
| 24 | * Increasing this value reduces the number of functions we can |
| 25 | * resolve, but reduces the size of the uintptr_t array used for |
| 26 | * our function list, which is the length of the code divided by |
| 27 | * this value. |
| 28 | */ |
Simon Glass | ec26f89 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 29 | FUNC_SITE_SIZE = 16, /* distance between function sites */ |
Simon Glass | adefafe | 2023-01-15 14:15:46 -0700 | [diff] [blame] | 30 | |
| 31 | TRACE_VERSION = 1, |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | enum trace_chunk_type { |
| 35 | TRACE_CHUNK_FUNCS, |
| 36 | TRACE_CHUNK_CALLS, |
| 37 | }; |
| 38 | |
| 39 | /* A trace record for a function, as written to the profile output file */ |
| 40 | struct trace_output_func { |
| 41 | uint32_t offset; /* Function offset into code */ |
| 42 | uint32_t call_count; /* Number of times called */ |
| 43 | }; |
| 44 | |
| 45 | /* A header at the start of the trace output buffer */ |
| 46 | struct trace_output_hdr { |
| 47 | enum trace_chunk_type type; /* Record type */ |
Simon Glass | adefafe | 2023-01-15 14:15:46 -0700 | [diff] [blame] | 48 | uint32_t version; /* Version (TRACE_VERSION) */ |
| 49 | uint32_t rec_count; /* Number of records */ |
| 50 | uint32_t spare; /* 0 */ |
| 51 | uint64_t text_base; /* Value of CONFIG_TEXT_BASE */ |
| 52 | uint64_t spare2; /* 0 */ |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /* Print statistics about traced function calls */ |
| 56 | void trace_print_stats(void); |
| 57 | |
| 58 | /** |
| 59 | * Dump a list of functions and call counts into a buffer |
| 60 | * |
| 61 | * Each record in the buffer is a struct trace_func_stats. The 'needed' |
| 62 | * parameter returns the number of bytes needed to complete the operation, |
| 63 | * which may be more than buff_size if your buffer is too small. |
| 64 | * |
| 65 | * @param buff Buffer in which to place data, or NULL to count size |
| 66 | * @param buff_size Size of buffer |
| 67 | * @param needed Returns number of bytes used / needed |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 68 | * Return: 0 if ok, -1 on error (buffer exhausted) |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 69 | */ |
Heinrich Schuchardt | 1469090 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 70 | int trace_list_functions(void *buff, size_t buff_size, size_t *needed); |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 71 | |
| 72 | /* Flags for ftrace_record */ |
| 73 | enum ftrace_flags { |
| 74 | FUNCF_EXIT = 0UL << 30, |
| 75 | FUNCF_ENTRY = 1UL << 30, |
Simon Glass | adefafe | 2023-01-15 14:15:46 -0700 | [diff] [blame] | 76 | /* two more values are available */ |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 77 | |
| 78 | FUNCF_TIMESTAMP_MASK = 0x3fffffff, |
| 79 | }; |
| 80 | |
| 81 | #define TRACE_CALL_TYPE(call) ((call)->flags & 0xc0000000UL) |
| 82 | |
| 83 | /* Information about a single function entry/exit */ |
| 84 | struct trace_call { |
| 85 | uint32_t func; /* Function offset */ |
| 86 | uint32_t caller; /* Caller function offset */ |
| 87 | uint32_t flags; /* Flags and timestamp */ |
| 88 | }; |
| 89 | |
Heinrich Schuchardt | 1469090 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 90 | int trace_list_calls(void *buff, size_t buff_size, size_t *needed); |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Turn function tracing on and off |
| 94 | * |
| 95 | * Don't enable trace if it has not been initialised. |
| 96 | * |
| 97 | * @param enabled 1 to enable trace, 0 to disable |
| 98 | */ |
| 99 | void trace_set_enabled(int enabled); |
| 100 | |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 101 | int trace_early_init(void); |
Simon Glass | c44b800 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Init the trace system |
| 105 | * |
| 106 | * This should be called after relocation with a suitably large buffer |
| 107 | * (typically as large as the U-Boot text area) |
| 108 | * |
| 109 | * @param buff Pointer to trace buffer |
| 110 | * @param buff_size Size of trace buffer |
| 111 | */ |
| 112 | int trace_init(void *buff, size_t buff_size); |
| 113 | |
| 114 | #endif |