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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 #include <ipxe/init.h>
30 #include <ipxe/uaccess.h>
35 * Function trace recorder for crash and hang debugging
39 /** Constant for identifying valid trace buffers */
40 #define FNREC_MAGIC ( 'f' << 24 | 'n' << 16 | 'r' << 8 | 'e' )
42 /** Number of trace buffer entries */
43 #define FNREC_NUM_ENTRIES 4096
45 /** Trace buffer physical address
49 #define FNREC_PHYS_ADDRESS ( 17 * 1024 * 1024 )
51 /** A trace buffer entry */
53 /** Called function address */
62 unsigned long checksum;
67 /** Constant for identifying valid trace buffers */
70 /** Next trace buffer entry to fill */
74 struct fnrec_entry data[FNREC_NUM_ENTRIES]
75 __attribute__ (( aligned ( 64 ) ));
78 /** The trace buffer */
79 static struct fnrec_buffer *fnrec_buffer;
82 * Test whether the trace buffer is valid
84 * @ret is_valid Buffer is valid
86 static int fnrec_is_valid ( void ) {
87 return ( fnrec_buffer && ( fnrec_buffer->magic == FNREC_MAGIC ) );
91 * Invalidate the trace buffer
94 static void fnrec_invalidate ( void ) {
95 fnrec_buffer->magic = 0;
99 * Reset the trace buffer and clear entries
101 static void fnrec_reset ( void ) {
102 memset ( fnrec_buffer, 0, sizeof ( *fnrec_buffer ) );
103 fnrec_buffer->magic = FNREC_MAGIC;
107 * Append an entry to the trace buffer
109 * @v called_fn Called function
110 * @v call_site Call site
111 * @ret entry Trace buffer entry
113 static struct fnrec_entry * fnrec_append ( void *called_fn, void *call_site ) {
114 struct fnrec_entry *entry;
116 /* Re-use existing entry, if possible */
117 entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
118 if ( ( entry->called_fn == called_fn ) &&
119 ( entry->call_site == call_site ) &&
120 ( entry->entry_count >= entry->exit_count ) ) {
124 /* Otherwise, create a new entry */
125 fnrec_buffer->idx = ( ( fnrec_buffer->idx + 1 ) % FNREC_NUM_ENTRIES );
126 entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
127 entry->called_fn = called_fn;
128 entry->call_site = call_site;
129 entry->entry_count = 0;
130 entry->exit_count = 0;
131 entry->checksum = ( ( ( unsigned long ) called_fn ) ^
132 ( ( unsigned long ) call_site ) );
137 * Print the contents of the trace buffer in chronological order
139 static void fnrec_dump ( void ) {
140 struct fnrec_entry *entry;
143 unsigned long checksum;
145 printf ( "fnrec buffer dump:\n" );
146 for ( i = 1 ; i <= FNREC_NUM_ENTRIES ; i++ ) {
147 idx = ( ( fnrec_buffer->idx + i ) % FNREC_NUM_ENTRIES );
148 entry = &fnrec_buffer->data[idx];
149 if ( ( entry->entry_count == 0 ) && ( entry->exit_count == 0 ) )
151 checksum = ( ( ( ( unsigned long ) entry->called_fn ) ^
152 ( ( unsigned long ) entry->call_site ) ) +
153 entry->entry_count + entry->exit_count );
154 printf ( "%p %p %d %d", entry->called_fn, entry->call_site,
155 entry->entry_count, entry->exit_count );
156 if ( entry->checksum != checksum ) {
157 printf ( " (checksum wrong at phys %08lx)",
158 virt_to_phys ( entry ) );
165 * Function tracer initialisation function
167 static void fnrec_init ( void ) {
169 fnrec_buffer = phys_to_virt ( FNREC_PHYS_ADDRESS );
170 if ( fnrec_is_valid() ) {
174 printf ( "fnrec buffer not found\n" );
179 struct init_fn fnrec_init_fn __init_fn ( INIT_NORMAL ) = {
180 .initialise = fnrec_init,
184 * These functions are called from every C function. The compiler inserts
185 * these calls when -finstrument-functions is used.
187 void __cyg_profile_func_enter ( void *called_fn, void *call_site ) {
188 struct fnrec_entry *entry;
190 if ( fnrec_is_valid() ) {
191 entry = fnrec_append ( called_fn, call_site );
192 entry->entry_count++;
198 void __cyg_profile_func_exit ( void *called_fn, void *call_site ) {
199 struct fnrec_entry *entry;
201 if ( fnrec_is_valid() ) {
202 entry = fnrec_append ( called_fn, call_site );