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
24FILE_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 */
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 */
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 */
80
81/**
82 * Test whether the trace buffer is valid
83 *
84 * @ret is_valid Buffer is valid
85 */
86static int fnrec_is_valid ( void ) {
87 return ( fnrec_buffer && ( fnrec_buffer->magic == FNREC_MAGIC ) );
88}
89
90/**
91 * Invalidate the trace buffer
92 *
93 */
94static void fnrec_invalidate ( void ) {
96}
97
98/**
99 * Reset the trace buffer and clear entries
100 */
101static 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 */
113static struct fnrec_entry * fnrec_append ( void *called_fn, void *call_site ) {
114 struct fnrec_entry *entry;
115
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 ) ) {
121 return entry;
122 }
123
124 /* Otherwise, create a new entry */
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 ) );
133 return entry;
134}
135
136/**
137 * Print the contents of the trace buffer in chronological order
138 */
139static 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 */
167static void fnrec_init ( void ) {
168
169 fnrec_buffer = phys_to_virt ( FNREC_PHYS_ADDRESS );
170 if ( fnrec_is_valid() ) {
172 fnrec_dump();
173 } else {
174 printf ( "fnrec buffer not found\n" );
175 }
176 fnrec_reset();
177}
178
179struct init_fn fnrec_init_fn __init_fn ( INIT_NORMAL ) = {
180 .name = "fnrec",
181 .initialise = fnrec_init,
182};
183
184/*
185 * These functions are called from every C function. The compiler inserts
186 * these calls when -finstrument-functions is used.
187 */
188void __cyg_profile_func_enter ( void *called_fn, void *call_site ) {
189 struct fnrec_entry *entry;
190
191 if ( fnrec_is_valid() ) {
192 entry = fnrec_append ( called_fn, call_site );
193 entry->entry_count++;
194 entry->checksum++;
195 mb();
196 }
197}
198
200 struct fnrec_entry *entry;
201
202 if ( fnrec_is_valid() ) {
203 entry = fnrec_append ( called_fn, call_site );
204 entry->exit_count++;
205 entry->checksum++;
206 mb();
207 }
208}
unsigned short uint16_t
Definition stdint.h:11
unsigned int uint32_t
Definition stdint.h:12
static void fnrec_init(void)
Function tracer initialisation function.
Definition fnrec.c:167
#define FNREC_NUM_ENTRIES
Number of trace buffer entries.
Definition fnrec.c:43
static struct fnrec_entry * fnrec_append(void *called_fn, void *call_site)
Append an entry to the trace buffer.
Definition fnrec.c:113
#define FNREC_PHYS_ADDRESS
Trace buffer physical address.
Definition fnrec.c:49
static int fnrec_is_valid(void)
Test whether the trace buffer is valid.
Definition fnrec.c:86
static void fnrec_dump(void)
Print the contents of the trace buffer in chronological order.
Definition fnrec.c:139
static void fnrec_invalidate(void)
Invalidate the trace buffer.
Definition fnrec.c:94
void __cyg_profile_func_enter(void *called_fn, void *call_site)
Definition fnrec.c:188
static void fnrec_reset(void)
Reset the trace buffer and clear entries.
Definition fnrec.c:101
#define FNREC_MAGIC
Constant for identifying valid trace buffers.
Definition fnrec.c:40
void __cyg_profile_func_exit(void *called_fn, void *call_site)
Definition fnrec.c:199
#define INIT_NORMAL
Normal initialisation.
Definition init.h:32
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define __attribute__(x)
Definition compiler.h:10
iPXE I/O API
void mb(void)
Memory barrier.
String functions.
void * memset(void *dest, int character, size_t len) __nonnull
#define __init_fn(init_order)
Declare an initialisation functon.
Definition init.h:24
Access to external ("user") memory.
uint8_t checksum
Checksum.
Definition pnpbios.c:12
A trace buffer.
Definition fnrec.c:66
uint32_t magic
Constant for identifying valid trace buffers.
Definition fnrec.c:68
unsigned int idx
Next trace buffer entry to fill.
Definition fnrec.c:71
struct fnrec_entry data[FNREC_NUM_ENTRIES]
Trace buffer.
Definition fnrec.c:74
A trace buffer entry.
Definition fnrec.c:52
void * called_fn
Called function address.
Definition fnrec.c:54
void * call_site
Call site.
Definition fnrec.c:56
uint16_t entry_count
Entry count.
Definition fnrec.c:58
unsigned long checksum
Checksum.
Definition fnrec.c:62
uint16_t exit_count
Exit count.
Definition fnrec.c:60
An initialisation function.
Definition init.h:15
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:465