iPXE
fnrec.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Stefan Hajnoczi <stefanha@gmail.com>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <ipxe/init.h>
30 #include <ipxe/uaccess.h>
31 #include <ipxe/io.h>
32 
33 /** @file
34  *
35  * Function trace recorder for crash and hang debugging
36  *
37  */
38 
39 /** Constant for identifying valid trace buffers */
40 #define FNREC_MAGIC ( 'f' << 24 | 'n' << 16 | 'r' << 8 | 'e' )
41 
42 /** Number of trace buffer entries */
43 #define FNREC_NUM_ENTRIES 4096
44 
45 /** Trace buffer physical address
46  *
47  * Fixed at 17MB
48  */
49 #define FNREC_PHYS_ADDRESS ( 17 * 1024 * 1024 )
50 
51 /** A trace buffer entry */
52 struct fnrec_entry {
53  /** Called function address */
54  void *called_fn;
55  /** Call site */
56  void *call_site;
57  /** Entry count */
59  /** Exit count */
61  /** Checksum */
62  unsigned long checksum;
63 };
64 
65 /** A trace buffer */
66 struct fnrec_buffer {
67  /** Constant for identifying valid trace buffers */
69 
70  /** Next trace buffer entry to fill */
71  unsigned int idx;
72 
73  /** Trace buffer */
75  __attribute__ (( aligned ( 64 ) ));
76 };
77 
78 /** The trace buffer */
79 static struct fnrec_buffer *fnrec_buffer;
80 
81 /**
82  * Test whether the trace buffer is valid
83  *
84  * @ret is_valid Buffer is valid
85  */
86 static int fnrec_is_valid ( void ) {
87  return ( fnrec_buffer && ( fnrec_buffer->magic == FNREC_MAGIC ) );
88 }
89 
90 /**
91  * Invalidate the trace buffer
92  *
93  */
94 static void fnrec_invalidate ( void ) {
95  fnrec_buffer->magic = 0;
96 }
97 
98 /**
99  * Reset the trace buffer and clear entries
100  */
101 static void fnrec_reset ( void ) {
102  memset ( fnrec_buffer, 0, sizeof ( *fnrec_buffer ) );
104 }
105 
106 /**
107  * Append an entry to the trace buffer
108  *
109  * @v called_fn Called function
110  * @v call_site Call site
111  * @ret entry Trace buffer entry
112  */
113 static struct fnrec_entry * fnrec_append ( void *called_fn, void *call_site ) {
114  struct fnrec_entry *entry;
115 
116  /* Re-use existing entry, if possible */
118  if ( ( entry->called_fn == called_fn ) &&
119  ( entry->call_site == call_site ) &&
120  ( entry->entry_count >= entry->exit_count ) ) {
121  return entry;
122  }
123 
124  /* Otherwise, create a new entry */
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 ) );
133  return entry;
134 }
135 
136 /**
137  * Print the contents of the trace buffer in chronological order
138  */
139 static void fnrec_dump ( void ) {
140  struct fnrec_entry *entry;
141  unsigned int i;
142  unsigned int idx;
143  unsigned long checksum;
144 
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 ) )
150  continue;
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 ) );
159  }
160  printf ( "\n");
161  }
162 }
163 
164 /**
165  * Function tracer initialisation function
166  */
167 static void fnrec_init ( void ) {
168 
170  if ( fnrec_is_valid() ) {
172  fnrec_dump();
173  } else {
174  printf ( "fnrec buffer not found\n" );
175  }
176  fnrec_reset();
177 }
178 
179 struct init_fn fnrec_init_fn __init_fn ( INIT_NORMAL ) = {
181 };
182 
183 /*
184  * These functions are called from every C function. The compiler inserts
185  * these calls when -finstrument-functions is used.
186  */
187 void __cyg_profile_func_enter ( void *called_fn, void *call_site ) {
188  struct fnrec_entry *entry;
189 
190  if ( fnrec_is_valid() ) {
192  entry->entry_count++;
193  entry->checksum++;
194  mb();
195  }
196 }
197 
199  struct fnrec_entry *entry;
200 
201  if ( fnrec_is_valid() ) {
203  entry->exit_count++;
204  entry->checksum++;
205  mb();
206  }
207 }
static struct fnrec_entry * fnrec_append(void *called_fn, void *call_site)
Append an entry to the trace buffer.
Definition: fnrec.c:113
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
iPXE I/O API
unsigned short uint16_t
Definition: stdint.h:11
static int fnrec_is_valid(void)
Test whether the trace buffer is valid.
Definition: fnrec.c:86
uint8_t checksum
Checksum.
Definition: pnpbios.c:37
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
void(* initialise)(void)
Definition: init.h:15
struct init_fn fnrec_init_fn __init_fn(INIT_NORMAL)
uint16_t entry_count
Entry count.
Definition: fnrec.c:58
#define FNREC_NUM_ENTRIES
Number of trace buffer entries.
Definition: fnrec.c:43
static void fnrec_dump(void)
Print the contents of the trace buffer in chronological order.
Definition: fnrec.c:139
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
Access to external ("user") memory.
void __cyg_profile_func_enter(void *called_fn, void *call_site)
Definition: fnrec.c:187
A trace buffer entry.
Definition: fnrec.c:52
A trace buffer.
Definition: fnrec.c:66
static __always_inline void * phys_to_virt(unsigned long phys_addr)
Convert physical address to a virtual address.
Definition: uaccess.h:299
#define FNREC_PHYS_ADDRESS
Trace buffer physical address.
Definition: fnrec.c:49
#define INIT_NORMAL
Normal initialisation.
Definition: init.h:30
An initialisation function.
Definition: init.h:14
void * call_site
Call site.
Definition: fnrec.c:56
A 16-bit general register.
Definition: registers.h:24
union aes_table_entry entry[256]
Table entries, indexed by S(N)
Definition: aes.c:26
static struct fnrec_buffer * fnrec_buffer
The trace buffer.
Definition: fnrec.c:79
uint16_t exit_count
Exit count.
Definition: fnrec.c:60
static void fnrec_init(void)
Function tracer initialisation function.
Definition: fnrec.c:167
struct fnrec_entry data[FNREC_NUM_ENTRIES]
Trace buffer.
Definition: fnrec.c:74
unsigned int uint32_t
Definition: stdint.h:12
static void fnrec_invalidate(void)
Invalidate the trace buffer.
Definition: fnrec.c:94
void * called_fn
Called function address.
Definition: fnrec.c:54
void __cyg_profile_func_exit(void *called_fn, void *call_site)
Definition: fnrec.c:198
unsigned int idx
Next trace buffer entry to fill.
Definition: fnrec.c:71
#define FNREC_MAGIC
Constant for identifying valid trace buffers.
Definition: fnrec.c:40
void mb(void)
Memory barrier.
unsigned long checksum
Checksum.
Definition: fnrec.c:62
uint32_t magic
Constant for identifying valid trace buffers.
Definition: fnrec.c:68
static void fnrec_reset(void)
Reset the trace buffer and clear entries.
Definition: fnrec.c:101
String functions.
void * memset(void *dest, int character, size_t len) __nonnull