blob: 763d6d1255a94479663adea10a3eda02e085488d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassc44b8002013-06-11 11:14:39 -07002/*
3 * Copyright (c) 2012 The Chromium OS Authors.
Simon Glassc44b8002013-06-11 11:14:39 -07004 */
5
6#ifndef __TRACE_H
7#define __TRACE_H
8
Simon Glassadefafe2023-01-15 14:15:46 -07009/* this file is included from a tool so uses uint32_t instead of u32, etc. */
10
Simon Glassc44b8002013-06-11 11:14:39 -070011enum {
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 Glassec26f892023-01-15 14:15:47 -070020 * 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 Glassc44b8002013-06-11 11:14:39 -070023 *
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 Glassec26f892023-01-15 14:15:47 -070029 FUNC_SITE_SIZE = 16, /* distance between function sites */
Simon Glassadefafe2023-01-15 14:15:46 -070030
31 TRACE_VERSION = 1,
Simon Glassc44b8002013-06-11 11:14:39 -070032};
33
34enum 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 */
40struct 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 */
46struct trace_output_hdr {
47 enum trace_chunk_type type; /* Record type */
Simon Glassadefafe2023-01-15 14:15:46 -070048 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 Glassc44b8002013-06-11 11:14:39 -070053};
54
55/* Print statistics about traced function calls */
56void 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 Schuchardt47b4c022022-01-19 18:05:50 +010068 * Return: 0 if ok, -1 on error (buffer exhausted)
Simon Glassc44b8002013-06-11 11:14:39 -070069 */
Heinrich Schuchardt14690902019-06-14 21:50:55 +020070int trace_list_functions(void *buff, size_t buff_size, size_t *needed);
Simon Glassc44b8002013-06-11 11:14:39 -070071
72/* Flags for ftrace_record */
73enum ftrace_flags {
74 FUNCF_EXIT = 0UL << 30,
75 FUNCF_ENTRY = 1UL << 30,
Simon Glassadefafe2023-01-15 14:15:46 -070076 /* two more values are available */
Simon Glassc44b8002013-06-11 11:14:39 -070077
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 */
84struct 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 Schuchardt14690902019-06-14 21:50:55 +020090int trace_list_calls(void *buff, size_t buff_size, size_t *needed);
Simon Glassc44b8002013-06-11 11:14:39 -070091
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 */
99void trace_set_enabled(int enabled);
100
Simon Glassc44b8002013-06-11 11:14:39 -0700101int trace_early_init(void);
Simon Glassc44b8002013-06-11 11:14:39 -0700102
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 */
112int trace_init(void *buff, size_t buff_size);
113
114#endif