iPXE
fnrec.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Stefan Hajnoczi <stefanha@gmail.com>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <ipxe/init.h>
00030 #include <ipxe/uaccess.h>
00031 #include <ipxe/io.h>
00032 
00033 /** @file
00034  *
00035  * Function trace recorder for crash and hang debugging
00036  *
00037  */
00038 
00039 /** Constant for identifying valid trace buffers */
00040 #define FNREC_MAGIC ( 'f' << 24 | 'n' << 16 | 'r' << 8 | 'e' )
00041 
00042 /** Number of trace buffer entries */
00043 #define FNREC_NUM_ENTRIES 4096
00044 
00045 /** Trace buffer physical address
00046  *
00047  * Fixed at 17MB
00048  */
00049 #define FNREC_PHYS_ADDRESS ( 17 * 1024 * 1024 )
00050 
00051 /** A trace buffer entry */
00052 struct fnrec_entry {
00053         /** Called function address */
00054         void *called_fn;
00055         /** Call site */
00056         void *call_site;
00057         /** Entry count */
00058         uint16_t entry_count;
00059         /** Exit count */
00060         uint16_t exit_count;
00061         /** Checksum */
00062         unsigned long checksum;
00063 };
00064 
00065 /** A trace buffer */
00066 struct fnrec_buffer {
00067         /** Constant for identifying valid trace buffers */
00068         uint32_t magic;
00069 
00070         /** Next trace buffer entry to fill */
00071         unsigned int idx;
00072 
00073         /** Trace buffer */
00074         struct fnrec_entry data[FNREC_NUM_ENTRIES]
00075                 __attribute__ (( aligned ( 64 ) ));
00076 };
00077 
00078 /** The trace buffer */
00079 static struct fnrec_buffer *fnrec_buffer;
00080 
00081 /**
00082  * Test whether the trace buffer is valid
00083  *
00084  * @ret is_valid        Buffer is valid
00085  */
00086 static int fnrec_is_valid ( void ) {
00087         return ( fnrec_buffer && ( fnrec_buffer->magic == FNREC_MAGIC ) );
00088 }
00089 
00090 /**
00091  * Invalidate the trace buffer
00092  *
00093  */
00094 static void fnrec_invalidate ( void ) {
00095         fnrec_buffer->magic = 0;
00096 }
00097 
00098 /**
00099  * Reset the trace buffer and clear entries
00100  */
00101 static void fnrec_reset ( void ) {
00102         memset ( fnrec_buffer, 0, sizeof ( *fnrec_buffer ) );
00103         fnrec_buffer->magic = FNREC_MAGIC;
00104 }
00105 
00106 /**
00107  * Append an entry to the trace buffer
00108  *
00109  * @v called_fn         Called function
00110  * @v call_site         Call site
00111  * @ret entry           Trace buffer entry
00112  */
00113 static struct fnrec_entry * fnrec_append ( void *called_fn, void *call_site ) {
00114         struct fnrec_entry *entry;
00115 
00116         /* Re-use existing entry, if possible */
00117         entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
00118         if ( ( entry->called_fn == called_fn ) &&
00119              ( entry->call_site == call_site ) &&
00120              ( entry->entry_count >= entry->exit_count ) ) {
00121                 return entry;
00122         }
00123 
00124         /* Otherwise, create a new entry */
00125         fnrec_buffer->idx = ( ( fnrec_buffer->idx + 1 ) % FNREC_NUM_ENTRIES );
00126         entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
00127         entry->called_fn = called_fn;
00128         entry->call_site = call_site;
00129         entry->entry_count = 0;
00130         entry->exit_count = 0;
00131         entry->checksum = ( ( ( unsigned long ) called_fn ) ^
00132                             ( ( unsigned long ) call_site ) );
00133         return entry;
00134 }
00135 
00136 /**
00137  * Print the contents of the trace buffer in chronological order
00138  */
00139 static void fnrec_dump ( void ) {
00140         struct fnrec_entry *entry;
00141         unsigned int i;
00142         unsigned int idx;
00143         unsigned long checksum;
00144 
00145         printf ( "fnrec buffer dump:\n" );
00146         for ( i = 1 ; i <= FNREC_NUM_ENTRIES ; i++ ) {
00147                 idx = ( ( fnrec_buffer->idx + i ) % FNREC_NUM_ENTRIES );
00148                 entry = &fnrec_buffer->data[idx];
00149                 if ( ( entry->entry_count == 0 ) && ( entry->exit_count == 0 ) )
00150                         continue;
00151                 checksum = ( ( ( ( unsigned long ) entry->called_fn ) ^
00152                                ( ( unsigned long ) entry->call_site ) ) +
00153                              entry->entry_count + entry->exit_count );
00154                 printf ( "%p %p %d %d", entry->called_fn, entry->call_site,
00155                          entry->entry_count, entry->exit_count );
00156                 if ( entry->checksum != checksum ) {
00157                         printf ( " (checksum wrong at phys %08lx)",
00158                                  virt_to_phys ( entry ) );
00159                 }
00160                 printf ( "\n");
00161         }
00162 }
00163 
00164 /**
00165  * Function tracer initialisation function
00166  */
00167 static void fnrec_init ( void ) {
00168 
00169         fnrec_buffer = phys_to_virt ( FNREC_PHYS_ADDRESS );
00170         if ( fnrec_is_valid() ) {
00171                 fnrec_invalidate();
00172                 fnrec_dump();
00173         } else {
00174                 printf ( "fnrec buffer not found\n" );
00175         }
00176         fnrec_reset();
00177 }
00178 
00179 struct init_fn fnrec_init_fn __init_fn ( INIT_NORMAL ) = {
00180         .initialise = fnrec_init,
00181 };
00182 
00183 /*
00184  * These functions are called from every C function.  The compiler inserts
00185  * these calls when -finstrument-functions is used.
00186  */
00187 void __cyg_profile_func_enter ( void *called_fn, void *call_site ) {
00188         struct fnrec_entry *entry;
00189 
00190         if ( fnrec_is_valid() ) {
00191                 entry = fnrec_append ( called_fn, call_site );
00192                 entry->entry_count++;
00193                 entry->checksum++;
00194                 mb();
00195         }
00196 }
00197 
00198 void __cyg_profile_func_exit ( void *called_fn, void *call_site ) {
00199         struct fnrec_entry *entry;
00200 
00201         if ( fnrec_is_valid() ) {
00202                 entry = fnrec_append ( called_fn, call_site );
00203                 entry->exit_count++;
00204                 entry->checksum++;
00205                 mb();
00206         }
00207 }