2 * Copyright (C) 2010 Stefan Hajnoczi <stefanha@gmail.com>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/init.h>
26 #include <ipxe/uaccess.h>
31 * Function trace recorder for crash and hang debugging
35 /** Constant for identifying valid trace buffers */
36 #define FNREC_MAGIC ( 'f' << 24 | 'n' << 16 | 'r' << 8 | 'e' )
38 /** Number of trace buffer entries */
39 #define FNREC_NUM_ENTRIES 4096
41 /** Trace buffer physical address
45 #define FNREC_PHYS_ADDRESS ( 17 * 1024 * 1024 )
47 /** A trace buffer entry */
49 /** Called function address */
58 unsigned long checksum;
63 /** Constant for identifying valid trace buffers */
66 /** Next trace buffer entry to fill */
70 struct fnrec_entry data[FNREC_NUM_ENTRIES]
71 __attribute__ (( aligned ( 64 ) ));
74 /** The trace buffer */
75 static struct fnrec_buffer *fnrec_buffer;
78 * Test whether the trace buffer is valid
80 * @ret is_valid Buffer is valid
82 static int fnrec_is_valid ( void ) {
83 return ( fnrec_buffer && ( fnrec_buffer->magic == FNREC_MAGIC ) );
87 * Invalidate the trace buffer
90 static void fnrec_invalidate ( void ) {
91 fnrec_buffer->magic = 0;
95 * Reset the trace buffer and clear entries
97 static void fnrec_reset ( void ) {
98 memset ( fnrec_buffer, 0, sizeof ( *fnrec_buffer ) );
99 fnrec_buffer->magic = FNREC_MAGIC;
103 * Append an entry to the trace buffer
105 * @v called_fn Called function
106 * @v call_site Call site
107 * @ret entry Trace buffer entry
109 static struct fnrec_entry * fnrec_append ( void *called_fn, void *call_site ) {
110 struct fnrec_entry *entry;
112 /* Re-use existing entry, if possible */
113 entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
114 if ( ( entry->called_fn == called_fn ) &&
115 ( entry->call_site == call_site ) &&
116 ( entry->entry_count >= entry->exit_count ) ) {
120 /* Otherwise, create a new entry */
121 fnrec_buffer->idx = ( ( fnrec_buffer->idx + 1 ) % FNREC_NUM_ENTRIES );
122 entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
123 entry->called_fn = called_fn;
124 entry->call_site = call_site;
125 entry->entry_count = 0;
126 entry->exit_count = 0;
127 entry->checksum = ( ( ( unsigned long ) called_fn ) ^
128 ( ( unsigned long ) call_site ) );
133 * Print the contents of the trace buffer in chronological order
135 static void fnrec_dump ( void ) {
136 struct fnrec_entry *entry;
139 unsigned long checksum;
141 printf ( "fnrec buffer dump:\n" );
142 for ( i = 1 ; i <= FNREC_NUM_ENTRIES ; i++ ) {
143 idx = ( ( fnrec_buffer->idx + i ) % FNREC_NUM_ENTRIES );
144 entry = &fnrec_buffer->data[idx];
145 if ( ( entry->entry_count == 0 ) && ( entry->exit_count == 0 ) )
147 checksum = ( ( ( ( unsigned long ) entry->called_fn ) ^
148 ( ( unsigned long ) entry->call_site ) ) +
149 entry->entry_count + entry->exit_count );
150 printf ( "%p %p %d %d", entry->called_fn, entry->call_site,
151 entry->entry_count, entry->exit_count );
152 if ( entry->checksum != checksum ) {
153 printf ( " (checksum wrong at phys %08lx)",
154 virt_to_phys ( entry ) );
161 * Function tracer initialisation function
163 static void fnrec_init ( void ) {
165 fnrec_buffer = phys_to_virt ( FNREC_PHYS_ADDRESS );
166 if ( fnrec_is_valid() ) {
170 printf ( "fnrec buffer not found\n" );
175 struct init_fn fnrec_init_fn __init_fn ( INIT_NORMAL ) = {
176 .initialise = fnrec_init,
180 * These functions are called from every C function. The compiler inserts
181 * these calls when -finstrument-functions is used.
183 void __cyg_profile_func_enter ( void *called_fn, void *call_site ) {
184 struct fnrec_entry *entry;
186 if ( fnrec_is_valid() ) {
187 entry = fnrec_append ( called_fn, call_site );
188 entry->entry_count++;
194 void __cyg_profile_func_exit ( void *called_fn, void *call_site ) {
195 struct fnrec_entry *entry;
197 if ( fnrec_is_valid() ) {
198 entry = fnrec_append ( called_fn, call_site );