|
@@ -23,6 +23,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
23
|
23
|
#include <string.h>
|
24
|
24
|
#include <ipxe/init.h>
|
25
|
25
|
#include <ipxe/uaccess.h>
|
|
26
|
+#include <ipxe/io.h>
|
26
|
27
|
|
27
|
28
|
/** @file
|
28
|
29
|
*
|
|
@@ -30,12 +31,30 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
30
|
31
|
*
|
31
|
32
|
*/
|
32
|
33
|
|
33
|
|
-enum {
|
34
|
|
- /** Constant for identifying valid trace buffers */
|
35
|
|
- fnrec_magic = 'f' << 24 | 'n' << 16 | 'r' << 8 | 'e',
|
|
34
|
+/** Constant for identifying valid trace buffers */
|
|
35
|
+#define FNREC_MAGIC ( 'f' << 24 | 'n' << 16 | 'r' << 8 | 'e' )
|
|
36
|
+
|
|
37
|
+/** Number of trace buffer entries */
|
|
38
|
+#define FNREC_NUM_ENTRIES 4096
|
36
|
39
|
|
37
|
|
- /** Trace buffer length */
|
38
|
|
- fnrec_buffer_length = 4096 / sizeof ( unsigned long ),
|
|
40
|
+/** Trace buffer physical address
|
|
41
|
+ *
|
|
42
|
+ * Fixed at 17MB
|
|
43
|
+ */
|
|
44
|
+#define FNREC_PHYS_ADDRESS ( 17 * 1024 * 1024 )
|
|
45
|
+
|
|
46
|
+/** A trace buffer entry */
|
|
47
|
+struct fnrec_entry {
|
|
48
|
+ /** Called function address */
|
|
49
|
+ void *called_fn;
|
|
50
|
+ /** Call site */
|
|
51
|
+ void *call_site;
|
|
52
|
+ /** Entry count */
|
|
53
|
+ uint16_t entry_count;
|
|
54
|
+ /** Exit count */
|
|
55
|
+ uint16_t exit_count;
|
|
56
|
+ /** Checksum */
|
|
57
|
+ unsigned long checksum;
|
39
|
58
|
};
|
40
|
59
|
|
41
|
60
|
/** A trace buffer */
|
|
@@ -44,10 +63,11 @@ struct fnrec_buffer {
|
44
|
63
|
uint32_t magic;
|
45
|
64
|
|
46
|
65
|
/** Next trace buffer entry to fill */
|
47
|
|
- uint32_t idx;
|
|
66
|
+ unsigned int idx;
|
48
|
67
|
|
49
|
|
- /** Function address trace buffer */
|
50
|
|
- unsigned long data[fnrec_buffer_length];
|
|
68
|
+ /** Trace buffer */
|
|
69
|
+ struct fnrec_entry data[FNREC_NUM_ENTRIES]
|
|
70
|
+ __attribute__ (( aligned ( 64 ) ));
|
51
|
71
|
};
|
52
|
72
|
|
53
|
73
|
/** The trace buffer */
|
|
@@ -59,7 +79,15 @@ static struct fnrec_buffer *fnrec_buffer;
|
59
|
79
|
* @ret is_valid Buffer is valid
|
60
|
80
|
*/
|
61
|
81
|
static int fnrec_is_valid ( void ) {
|
62
|
|
- return fnrec_buffer && fnrec_buffer->magic == fnrec_magic;
|
|
82
|
+ return ( fnrec_buffer && ( fnrec_buffer->magic == FNREC_MAGIC ) );
|
|
83
|
+}
|
|
84
|
+
|
|
85
|
+/**
|
|
86
|
+ * Invalidate the trace buffer
|
|
87
|
+ *
|
|
88
|
+ */
|
|
89
|
+static void fnrec_invalidate ( void ) {
|
|
90
|
+ fnrec_buffer->magic = 0;
|
63
|
91
|
}
|
64
|
92
|
|
65
|
93
|
/**
|
|
@@ -67,43 +95,64 @@ static int fnrec_is_valid ( void ) {
|
67
|
95
|
*/
|
68
|
96
|
static void fnrec_reset ( void ) {
|
69
|
97
|
memset ( fnrec_buffer, 0, sizeof ( *fnrec_buffer ) );
|
70
|
|
- fnrec_buffer->magic = fnrec_magic;
|
|
98
|
+ fnrec_buffer->magic = FNREC_MAGIC;
|
71
|
99
|
}
|
72
|
100
|
|
73
|
101
|
/**
|
74
|
|
- * Write a value to the end of the buffer if it is not a repetition
|
|
102
|
+ * Append an entry to the trace buffer
|
75
|
103
|
*
|
76
|
|
- * @v l Value to append
|
|
104
|
+ * @v called_fn Called function
|
|
105
|
+ * @v call_site Call site
|
|
106
|
+ * @ret entry Trace buffer entry
|
77
|
107
|
*/
|
78
|
|
-static void fnrec_append_unique ( unsigned long l ) {
|
79
|
|
- static unsigned long lastval;
|
80
|
|
- uint32_t idx = fnrec_buffer->idx;
|
81
|
|
-
|
82
|
|
- /* Avoid recording the same value repeatedly */
|
83
|
|
- if ( l == lastval )
|
84
|
|
- return;
|
|
108
|
+static struct fnrec_entry * fnrec_append ( void *called_fn, void *call_site ) {
|
|
109
|
+ struct fnrec_entry *entry;
|
|
110
|
+
|
|
111
|
+ /* Re-use existing entry, if possible */
|
|
112
|
+ entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
|
|
113
|
+ if ( ( entry->called_fn == called_fn ) &&
|
|
114
|
+ ( entry->call_site == call_site ) &&
|
|
115
|
+ ( entry->entry_count >= entry->exit_count ) ) {
|
|
116
|
+ return entry;
|
|
117
|
+ }
|
85
|
118
|
|
86
|
|
- fnrec_buffer->data[idx] = l;
|
87
|
|
- fnrec_buffer->idx = ( idx + 1 ) % fnrec_buffer_length;
|
88
|
|
- lastval = l;
|
|
119
|
+ /* Otherwise, create a new entry */
|
|
120
|
+ fnrec_buffer->idx = ( ( fnrec_buffer->idx + 1 ) % FNREC_NUM_ENTRIES );
|
|
121
|
+ entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
|
|
122
|
+ entry->called_fn = called_fn;
|
|
123
|
+ entry->call_site = call_site;
|
|
124
|
+ entry->entry_count = 0;
|
|
125
|
+ entry->exit_count = 0;
|
|
126
|
+ entry->checksum = ( ( ( unsigned long ) called_fn ) ^
|
|
127
|
+ ( ( unsigned long ) call_site ) );
|
|
128
|
+ return entry;
|
89
|
129
|
}
|
90
|
130
|
|
91
|
131
|
/**
|
92
|
132
|
* Print the contents of the trace buffer in chronological order
|
93
|
133
|
*/
|
94
|
134
|
static void fnrec_dump ( void ) {
|
95
|
|
- size_t i;
|
96
|
|
-
|
97
|
|
- if ( !fnrec_is_valid() ) {
|
98
|
|
- printf ( "fnrec buffer not found\n" );
|
99
|
|
- return;
|
100
|
|
- }
|
|
135
|
+ struct fnrec_entry *entry;
|
|
136
|
+ unsigned int i;
|
|
137
|
+ unsigned int idx;
|
|
138
|
+ unsigned long checksum;
|
101
|
139
|
|
102
|
140
|
printf ( "fnrec buffer dump:\n" );
|
103
|
|
- for ( i = 0; i < fnrec_buffer_length; i++ ) {
|
104
|
|
- unsigned long l = fnrec_buffer->data[
|
105
|
|
- ( fnrec_buffer->idx + i ) % fnrec_buffer_length];
|
106
|
|
- printf ( "%08lx%c", l, i % 8 == 7 ? '\n' : ' ' );
|
|
141
|
+ for ( i = 1 ; i <= FNREC_NUM_ENTRIES ; i++ ) {
|
|
142
|
+ idx = ( ( fnrec_buffer->idx + i ) % FNREC_NUM_ENTRIES );
|
|
143
|
+ entry = &fnrec_buffer->data[idx];
|
|
144
|
+ if ( ( entry->entry_count == 0 ) && ( entry->exit_count == 0 ) )
|
|
145
|
+ continue;
|
|
146
|
+ checksum = ( ( ( ( unsigned long ) entry->called_fn ) ^
|
|
147
|
+ ( ( unsigned long ) entry->call_site ) ) +
|
|
148
|
+ entry->entry_count + entry->exit_count );
|
|
149
|
+ printf ( "%p %p %d %d", entry->called_fn, entry->call_site,
|
|
150
|
+ entry->entry_count, entry->exit_count );
|
|
151
|
+ if ( entry->checksum != checksum ) {
|
|
152
|
+ printf ( " (checksum wrong at phys %08lx)",
|
|
153
|
+ virt_to_phys ( entry ) );
|
|
154
|
+ }
|
|
155
|
+ printf ( "\n");
|
107
|
156
|
}
|
108
|
157
|
}
|
109
|
158
|
|
|
@@ -111,9 +160,14 @@ static void fnrec_dump ( void ) {
|
111
|
160
|
* Function tracer initialisation function
|
112
|
161
|
*/
|
113
|
162
|
static void fnrec_init ( void ) {
|
114
|
|
- /* Hardcoded to 17 MB */
|
115
|
|
- fnrec_buffer = phys_to_virt ( 17 * 1024 * 1024 );
|
116
|
|
- fnrec_dump();
|
|
163
|
+
|
|
164
|
+ fnrec_buffer = phys_to_virt ( FNREC_PHYS_ADDRESS );
|
|
165
|
+ if ( fnrec_is_valid() ) {
|
|
166
|
+ fnrec_invalidate();
|
|
167
|
+ fnrec_dump();
|
|
168
|
+ } else {
|
|
169
|
+ printf ( "fnrec buffer not found\n" );
|
|
170
|
+ }
|
117
|
171
|
fnrec_reset();
|
118
|
172
|
}
|
119
|
173
|
|
|
@@ -125,10 +179,24 @@ struct init_fn fnrec_init_fn __init_fn ( INIT_NORMAL ) = {
|
125
|
179
|
* These functions are called from every C function. The compiler inserts
|
126
|
180
|
* these calls when -finstrument-functions is used.
|
127
|
181
|
*/
|
128
|
|
-void __cyg_profile_func_enter ( void *called_fn, void *call_site __unused ) {
|
129
|
|
- if ( fnrec_is_valid() )
|
130
|
|
- fnrec_append_unique ( ( unsigned long ) called_fn );
|
|
182
|
+void __cyg_profile_func_enter ( void *called_fn, void *call_site ) {
|
|
183
|
+ struct fnrec_entry *entry;
|
|
184
|
+
|
|
185
|
+ if ( fnrec_is_valid() ) {
|
|
186
|
+ entry = fnrec_append ( called_fn, call_site );
|
|
187
|
+ entry->entry_count++;
|
|
188
|
+ entry->checksum++;
|
|
189
|
+ mb();
|
|
190
|
+ }
|
131
|
191
|
}
|
132
|
192
|
|
133
|
|
-void __cyg_profile_func_exit ( void *called_fn __unused, void *call_site __unused ) {
|
|
193
|
+void __cyg_profile_func_exit ( void *called_fn, void *call_site ) {
|
|
194
|
+ struct fnrec_entry *entry;
|
|
195
|
+
|
|
196
|
+ if ( fnrec_is_valid() ) {
|
|
197
|
+ entry = fnrec_append ( called_fn, call_site );
|
|
198
|
+ entry->exit_count++;
|
|
199
|
+ entry->checksum++;
|
|
200
|
+ mb();
|
|
201
|
+ }
|
134
|
202
|
}
|