[9f79f7]: src / runtime / backtrace.c Maximize Restore History

Download this file

backtrace.c    282 lines (236 with data), 7.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* simple backtrace facility
*/
/*
* This software is part of the SBCL system. See the README file for
* more information.
*
* This software is derived from the CMU CL system, which was
* written at Carnegie Mellon University and released into the
* public domain. The software is in the public domain and is
* provided with absolutely no warranty. See the COPYING and CREDITS
* files for more information.
*/
#include <stdio.h>
#include <signal.h>
#include "sbcl.h"
#include "runtime.h"
#include "globals.h"
#include "os.h"
#include "interrupt.h"
#include "lispregs.h"
#ifdef LISP_FEATURE_GENCGC
#include "gencgc-alloc-region.h"
#endif
#include "genesis/static-symbols.h"
#include "genesis/primitive-objects.h"
#include "thread.h"
#if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
/* KLUDGE: Sigh ... I know what the call frame looks like and it had
* better not change. */
struct call_frame {
#ifndef alpha
struct call_frame *old_cont;
#else
u32 old_cont;
#endif
lispobj saved_lra;
lispobj code;
lispobj other_state[5];
};
struct call_info {
#ifndef alpha
struct call_frame *frame;
#else
u32 frame;
#endif
int interrupted;
#ifndef alpha
struct code *code;
#else
u32 code;
#endif
lispobj lra;
int pc; /* Note: this is the trace file offset, not the actual pc. */
};
#define HEADER_LENGTH(header) ((header)>>8)
static int previous_info(struct call_info *info);
static struct code *
code_pointer(lispobj object)
{
lispobj *headerp, header;
int type, len;
headerp = (lispobj *) native_pointer(object);
header = *headerp;
type = widetag_of(header);
switch (type) {
case CODE_HEADER_WIDETAG:
break;
case RETURN_PC_HEADER_WIDETAG:
case SIMPLE_FUN_HEADER_WIDETAG:
len = HEADER_LENGTH(header);
if (len == 0)
headerp = NULL;
else
headerp -= len;
break;
default:
headerp = NULL;
}
return (struct code *) headerp;
}
static boolean
cs_valid_pointer_p(struct call_frame *pointer)
{
struct thread *thread=arch_os_get_current_thread();
return (((char *) thread->control_stack_start <= (char *) pointer) &&
((char *) pointer < (char *) current_control_stack_pointer));
}
static void
call_info_from_lisp_state(struct call_info *info)
{
info->frame = (struct call_frame *)current_control_frame_pointer;
info->interrupted = 0;
info->code = NULL;
info->lra = 0;
info->pc = 0;
previous_info(info);
}
static void
call_info_from_context(struct call_info *info, os_context_t *context)
{
unsigned long pc;
info->interrupted = 1;
if (lowtag_of(*os_context_register_addr(context, reg_CODE))
== FUN_POINTER_LOWTAG) {
/* We tried to call a function, but crapped out before $CODE could
* be fixed up. Probably an undefined function. */
info->frame =
(struct call_frame *)(*os_context_register_addr(context,
reg_OCFP));
info->lra = (lispobj)(*os_context_register_addr(context, reg_LRA));
info->code = code_pointer(info->lra);
pc = (unsigned long)native_pointer(info->lra);
}
else {
info->frame =
(struct call_frame *)(*os_context_register_addr(context, reg_CFP));
info->code =
code_pointer(*os_context_register_addr(context, reg_CODE));
info->lra = NIL;
pc = *os_context_pc_addr(context);
}
if (info->code != NULL)
info->pc = pc - (unsigned long) info->code -
#ifndef alpha
(HEADER_LENGTH(info->code->header) * sizeof(lispobj));
#else
(HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
#endif
else
info->pc = 0;
}
static int
previous_info(struct call_info *info)
{
struct call_frame *this_frame;
struct thread *thread=arch_os_get_current_thread();
int free;
if (!cs_valid_pointer_p(info->frame)) {
printf("Bogus callee value (0x%08x).\n", (unsigned long)info->frame);
return 0;
}
this_frame = info->frame;
info->lra = this_frame->saved_lra;
info->frame = this_frame->old_cont;
info->interrupted = 0;
if (info->frame == NULL || info->frame == this_frame)
return 0;
if (info->lra == NIL) {
/* We were interrupted. Find the correct signal context. */
free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread)>>2;
while (free-- > 0) {
os_context_t *context =
thread->interrupt_contexts[free];
if ((struct call_frame *)(*os_context_register_addr(context,
reg_CFP))
== info->frame) {
call_info_from_context(info, context);
break;
}
}
}
else {
info->code = code_pointer(info->lra);
if (info->code != NULL)
info->pc = (unsigned long)native_pointer(info->lra) -
(unsigned long)info->code -
#ifndef alpha
(HEADER_LENGTH(info->code->header) * sizeof(lispobj));
#else
(HEADER_LENGTH(((struct code *)info->code)->header) * sizeof(lispobj));
#endif
else
info->pc = 0;
}
return 1;
}
void
backtrace(int nframes)
{
struct call_info info;
call_info_from_lisp_state(&info);
do {
printf("<Frame 0x%08x%s, ", (unsigned long) info.frame,
info.interrupted ? " [interrupted]" : "");
if (info.code != (struct code *) 0) {
lispobj function;
printf("CODE: 0x%08X, ", (unsigned long) info.code | OTHER_POINTER_LOWTAG);
#ifndef alpha
function = info.code->entry_points;
#else
function = ((struct code *)info.code)->entry_points;
#endif
while (function != NIL) {
struct simple_fun *header;
lispobj name;
header = (struct simple_fun *) native_pointer(function);
name = header->name;
if (lowtag_of(name) == OTHER_POINTER_LOWTAG) {
lispobj *object;
object = (lispobj *) native_pointer(name);
if (widetag_of(*object) == SYMBOL_HEADER_WIDETAG) {
struct symbol *symbol;
symbol = (struct symbol *) object;
object = (lispobj *) native_pointer(symbol->name);
}
if (widetag_of(*object) == SIMPLE_BASE_STRING_WIDETAG) {
struct vector *string;
string = (struct vector *) object;
printf("%s, ", (char *) string->data);
} else
printf("(Not simple string??\?), ");
} else
printf("(Not other pointer??\?), ");
function = header->next;
}
}
else
printf("CODE: ???, ");
if (info.lra != NIL)
printf("LRA: 0x%08x, ", (unsigned long)info.lra);
else
printf("<no LRA>, ");
if (info.pc)
printf("PC: 0x%x>\n", info.pc);
else
printf("PC: ??\?>\n");
} while (--nframes > 0 && previous_info(&info));
}
#else
void
backtrace(int nframes)
{
printf("Can't backtrace on this hardware platform.\n");
}
#endif