1*b2e16a85SSimon Glass /* 2*b2e16a85SSimon Glass * Copyright (c) 2012 The Chromium OS Authors. 3*b2e16a85SSimon Glass * 4*b2e16a85SSimon Glass * This program is free software; you can redistribute it and/or 5*b2e16a85SSimon Glass * modify it under the terms of the GNU General Public License as 6*b2e16a85SSimon Glass * published by the Free Software Foundation; either version 2 of 7*b2e16a85SSimon Glass * the License, or (at your option) any later version. 8*b2e16a85SSimon Glass * 9*b2e16a85SSimon Glass * This program is distributed in the hope that it will be useful, 10*b2e16a85SSimon Glass * but WITHOUT ANY WARRANTY; without even the implied warranty of 11*b2e16a85SSimon Glass * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12*b2e16a85SSimon Glass * GNU General Public License for more details. 13*b2e16a85SSimon Glass * 14*b2e16a85SSimon Glass * You should have received a copy of the GNU General Public License 15*b2e16a85SSimon Glass * along with this program; if not, write to the Free Software 16*b2e16a85SSimon Glass * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 17*b2e16a85SSimon Glass * MA 02111-1307 USA 18*b2e16a85SSimon Glass */ 19*b2e16a85SSimon Glass 20*b2e16a85SSimon Glass #ifndef __TRACE_H 21*b2e16a85SSimon Glass #define __TRACE_H 22*b2e16a85SSimon Glass 23*b2e16a85SSimon Glass enum { 24*b2e16a85SSimon Glass /* 25*b2e16a85SSimon Glass * This affects the granularity of our trace. We can bin function 26*b2e16a85SSimon Glass * entry points into groups on the basis that functions typically 27*b2e16a85SSimon Glass * have a minimum size, so entry points can't appear any closer 28*b2e16a85SSimon Glass * than this to each other. 29*b2e16a85SSimon Glass * 30*b2e16a85SSimon Glass * The value here assumes a minimum instruction size of 4 bytes, 31*b2e16a85SSimon Glass * or that instructions are 2 bytes but there are at least 2 of 32*b2e16a85SSimon Glass * them in every function. 33*b2e16a85SSimon Glass * 34*b2e16a85SSimon Glass * Increasing this value reduces the number of functions we can 35*b2e16a85SSimon Glass * resolve, but reduces the size of the uintptr_t array used for 36*b2e16a85SSimon Glass * our function list, which is the length of the code divided by 37*b2e16a85SSimon Glass * this value. 38*b2e16a85SSimon Glass */ 39*b2e16a85SSimon Glass FUNC_SITE_SIZE = 4, /* distance between function sites */ 40*b2e16a85SSimon Glass }; 41*b2e16a85SSimon Glass 42*b2e16a85SSimon Glass enum trace_chunk_type { 43*b2e16a85SSimon Glass TRACE_CHUNK_FUNCS, 44*b2e16a85SSimon Glass TRACE_CHUNK_CALLS, 45*b2e16a85SSimon Glass }; 46*b2e16a85SSimon Glass 47*b2e16a85SSimon Glass /* A trace record for a function, as written to the profile output file */ 48*b2e16a85SSimon Glass struct trace_output_func { 49*b2e16a85SSimon Glass uint32_t offset; /* Function offset into code */ 50*b2e16a85SSimon Glass uint32_t call_count; /* Number of times called */ 51*b2e16a85SSimon Glass }; 52*b2e16a85SSimon Glass 53*b2e16a85SSimon Glass /* A header at the start of the trace output buffer */ 54*b2e16a85SSimon Glass struct trace_output_hdr { 55*b2e16a85SSimon Glass enum trace_chunk_type type; /* Record type */ 56*b2e16a85SSimon Glass uint32_t rec_count; /* Number of records */ 57*b2e16a85SSimon Glass }; 58*b2e16a85SSimon Glass 59*b2e16a85SSimon Glass /* Print statistics about traced function calls */ 60*b2e16a85SSimon Glass void trace_print_stats(void); 61*b2e16a85SSimon Glass 62*b2e16a85SSimon Glass /** 63*b2e16a85SSimon Glass * Dump a list of functions and call counts into a buffer 64*b2e16a85SSimon Glass * 65*b2e16a85SSimon Glass * Each record in the buffer is a struct trace_func_stats. The 'needed' 66*b2e16a85SSimon Glass * parameter returns the number of bytes needed to complete the operation, 67*b2e16a85SSimon Glass * which may be more than buff_size if your buffer is too small. 68*b2e16a85SSimon Glass * 69*b2e16a85SSimon Glass * @param buff Buffer in which to place data, or NULL to count size 70*b2e16a85SSimon Glass * @param buff_size Size of buffer 71*b2e16a85SSimon Glass * @param needed Returns number of bytes used / needed 72*b2e16a85SSimon Glass * @return 0 if ok, -1 on error (buffer exhausted) 73*b2e16a85SSimon Glass */ 74*b2e16a85SSimon Glass int trace_list_functions(void *buff, int buff_size, unsigned *needed); 75*b2e16a85SSimon Glass 76*b2e16a85SSimon Glass /* Flags for ftrace_record */ 77*b2e16a85SSimon Glass enum ftrace_flags { 78*b2e16a85SSimon Glass FUNCF_EXIT = 0UL << 30, 79*b2e16a85SSimon Glass FUNCF_ENTRY = 1UL << 30, 80*b2e16a85SSimon Glass FUNCF_TEXTBASE = 2UL << 30, 81*b2e16a85SSimon Glass 82*b2e16a85SSimon Glass FUNCF_TIMESTAMP_MASK = 0x3fffffff, 83*b2e16a85SSimon Glass }; 84*b2e16a85SSimon Glass 85*b2e16a85SSimon Glass #define TRACE_CALL_TYPE(call) ((call)->flags & 0xc0000000UL) 86*b2e16a85SSimon Glass 87*b2e16a85SSimon Glass /* Information about a single function entry/exit */ 88*b2e16a85SSimon Glass struct trace_call { 89*b2e16a85SSimon Glass uint32_t func; /* Function offset */ 90*b2e16a85SSimon Glass uint32_t caller; /* Caller function offset */ 91*b2e16a85SSimon Glass uint32_t flags; /* Flags and timestamp */ 92*b2e16a85SSimon Glass }; 93*b2e16a85SSimon Glass 94*b2e16a85SSimon Glass int trace_list_calls(void *buff, int buff_size, unsigned int *needed); 95*b2e16a85SSimon Glass 96*b2e16a85SSimon Glass /** 97*b2e16a85SSimon Glass * Turn function tracing on and off 98*b2e16a85SSimon Glass * 99*b2e16a85SSimon Glass * Don't enable trace if it has not been initialised. 100*b2e16a85SSimon Glass * 101*b2e16a85SSimon Glass * @param enabled 1 to enable trace, 0 to disable 102*b2e16a85SSimon Glass */ 103*b2e16a85SSimon Glass void trace_set_enabled(int enabled); 104*b2e16a85SSimon Glass 105*b2e16a85SSimon Glass #ifdef CONFIG_TRACE_EARLY 106*b2e16a85SSimon Glass int trace_early_init(void); 107*b2e16a85SSimon Glass #else 108*b2e16a85SSimon Glass static inline int trace_early_init(void) 109*b2e16a85SSimon Glass { 110*b2e16a85SSimon Glass return 0; 111*b2e16a85SSimon Glass } 112*b2e16a85SSimon Glass #endif 113*b2e16a85SSimon Glass 114*b2e16a85SSimon Glass /** 115*b2e16a85SSimon Glass * Init the trace system 116*b2e16a85SSimon Glass * 117*b2e16a85SSimon Glass * This should be called after relocation with a suitably large buffer 118*b2e16a85SSimon Glass * (typically as large as the U-Boot text area) 119*b2e16a85SSimon Glass * 120*b2e16a85SSimon Glass * @param buff Pointer to trace buffer 121*b2e16a85SSimon Glass * @param buff_size Size of trace buffer 122*b2e16a85SSimon Glass */ 123*b2e16a85SSimon Glass int trace_init(void *buff, size_t buff_size); 124*b2e16a85SSimon Glass 125*b2e16a85SSimon Glass #endif 126