traceback_x86.c 7.55 KB
Newer Older
1 2 3 4
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Russ Cox's avatar
Russ Cox committed
5 6
// +build amd64 386

7
#include "runtime.h"
Russ Cox's avatar
Russ Cox committed
8
#include "arch_GOARCH.h"
Russ Cox's avatar
Russ Cox committed
9
#include "malloc.h"
10
#include "funcdata.h"
11

12 13
void runtime·deferproc(void);
void runtime·newproc(void);
14
void runtime·morestack(void);
15
void runtime·sigpanic(void);
16

Russ Cox's avatar
Russ Cox committed
17 18 19
// This code is also used for the 386 tracebacks.
// Use uintptr for an appropriate word-sized integer.

20 21
static String unknown = { (uint8*)"?", 1 };

22 23
// Generic traceback.  Handles runtime stack prints (pcbuf == nil),
// the runtime.Callers function (pcbuf != nil), as well as the garbage
24
// collector (callback != nil).  A little clunky to merge these, but avoids
25
// duplicating the code and all its subtlety.
Russ Cox's avatar
Russ Cox committed
26
int32
27
runtime·gentraceback(uintptr pc0, uintptr sp0, uintptr lr0, G *gp, int32 skip, uintptr *pcbuf, int32 max, void (*callback)(Stkframe*, void*), void *v, bool printall)
28
{
29
	int32 i, n, nprint, line;
30 31
	uintptr tracepc;
	bool waspanic, printing;
32
	Func *f, *flr;
33
	Stkframe frame;
Russ Cox's avatar
Russ Cox committed
34
	Stktop *stk;
35
	String file;
36

Russ Cox's avatar
Russ Cox committed
37
	USED(lr0);
38

39
	nprint = 0;
40 41 42
	runtime·memclr((byte*)&frame, sizeof frame);
	frame.pc = pc0;
	frame.sp = sp0;
43
	waspanic = false;
44
	printing = pcbuf==nil && callback==nil;
45
	
Russ Cox's avatar
Russ Cox committed
46 47
	// If the PC is zero, it's likely a nil function call.
	// Start in the caller's frame.
48 49 50
	if(frame.pc == 0) {
		frame.pc = *(uintptr*)frame.sp;
		frame.sp += sizeof(uintptr);
51
	}
52 53 54 55 56 57 58 59 60 61
	
	f = runtime·findfunc(frame.pc);
	if(f == nil) {
		if(callback != nil) {
			runtime·printf("runtime: unknown pc %p\n", frame.pc);
			runtime·throw("unknown pc");
		}
		return 0;
	}
	frame.fn = f;
62

Russ Cox's avatar
Russ Cox committed
63
	n = 0;
64
	stk = (Stktop*)gp->stackbase;
65
	while(n < max) {
66 67 68 69 70 71 72
		// Typically:
		//	pc is the PC of the running function.
		//	sp is the stack pointer at that program counter.
		//	fp is the frame pointer (caller's stack pointer) at that program counter, or nil if unknown.
		//	stk is the stack containing sp.
		//	The caller's program counter is lr, unless lr is zero, in which case it is *(uintptr*)sp.
	
73
		if(frame.pc == (uintptr)runtime·lessstack) {
Russ Cox's avatar
Russ Cox committed
74
			// Hit top of stack segment.  Unwind to next segment.
75 76 77 78
			frame.pc = stk->gobuf.pc;
			frame.sp = stk->gobuf.sp;
			frame.lr = 0;
			frame.fp = 0;
79
			frame.fn = nil;
80
			if(printing && runtime·showframe(nil, gp))
81
				runtime·printf("----- stack segment boundary -----\n");
82
			stk = (Stktop*)stk->stackbase;
83 84 85 86

			f = runtime·findfunc(frame.pc);
			if(f == nil) {
				runtime·printf("runtime: unknown pc %p after stack split\n", frame.pc);
87 88
				if(callback != nil)
					runtime·throw("unknown pc");
89
			}
90 91
			frame.fn = f;
			continue;
92
		}
93
		f = frame.fn;
94

95
		// Found an actual function.
96 97
		// Derive frame pointer and link register.
		if(frame.fp == 0) {
98 99
			frame.fp = frame.sp + runtime·funcspdelta(f, frame.pc);
			frame.fp += sizeof(uintptr); // caller PC
100
		}
101 102 103 104 105 106 107 108
		if(runtime·topofstack(f)) {
			frame.lr = 0;
			flr = nil;
		} else {
			if(frame.lr == 0)
				frame.lr = ((uintptr*)frame.fp)[-1];
			flr = runtime·findfunc(frame.lr);
			if(flr == nil) {
109 110 111
				runtime·printf("runtime: unexpected return pc for %s called from %p\n", runtime·funcname(f), frame.lr);
				if(callback != nil)
					runtime·throw("unknown caller pc");
112 113
			}
		}
114 115

		// Derive size of arguments.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		// Most functions have a fixed-size argument block,
		// so we can use metadata about the function f.
		// Not all, though: there are some variadic functions
		// in package runtime, and for those we use call-specific
		// metadata recorded by f's caller.
		if(callback != nil || printing) {
			frame.argp = (byte*)frame.fp;
			if(f->args != ArgsSizeUnknown)
				frame.arglen = f->args;
			else if(flr == nil)
				frame.arglen = 0;
			else if(frame.lr == (uintptr)runtime·lessstack)
				frame.arglen = stk->argsize;
			else if((i = runtime·funcarglen(flr, frame.lr)) >= 0)
				frame.arglen = i;
			else {
132 133
				runtime·printf("runtime: unknown argument frame size for %s called from %p [%s]\n",
					runtime·funcname(f), frame.lr, flr ? runtime·funcname(flr) : "?");
134
				if(callback != nil)
135 136 137
					runtime·throw("invalid stack");
				frame.arglen = 0;
			}
138 139 140
		}

		// Derive location and size of local variables.
Russ Cox's avatar
Russ Cox committed
141
		if(frame.fp == frame.sp + sizeof(uintptr)) {
142 143 144 145 146 147 148 149 150 151
			// Function has not created a frame for itself yet.
			frame.varp = nil;
			frame.varlen = 0;
		} else if(f->locals == 0) {
			// Assume no information, so use whole frame.
			// TODO: Distinguish local==0 from local==unknown.
			frame.varp = (byte*)frame.sp;
			frame.varlen = frame.fp - sizeof(uintptr) - frame.sp;
		} else {
			if(f->locals > frame.fp - sizeof(uintptr) - frame.sp) {
152
				runtime·printf("runtime: inconsistent locals=%p frame=%p fp=%p sp=%p for %s\n", (uintptr)f->locals, (uintptr)f->frame, frame.fp, frame.sp, runtime·funcname(f));
153 154
				if(callback != nil)
					runtime·throw("invalid stack");
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
			}
			frame.varp = (byte*)frame.fp - sizeof(uintptr) - f->locals;
			frame.varlen = f->locals;
		}

		if(skip > 0) {
			skip--;
			goto skipped;
		}

		if(pcbuf != nil)
			pcbuf[n] = frame.pc;
		if(callback != nil)
			callback(&frame, v);
		if(printing) {
170
			if(printall || runtime·showframe(f, gp)) {
Russ Cox's avatar
Russ Cox committed
171 172 173 174
				// Print during crash.
				//	main(0x1, 0x2, 0x3)
				//		/home/rsc/go/src/runtime/x.go:23 +0xf
				//		
175 176
				tracepc = frame.pc;	// back up to CALL instruction for funcline.
				if(n > 0 && frame.pc > f->entry && !waspanic)
Russ Cox's avatar
Russ Cox committed
177
					tracepc--;
178
				runtime·printf("%s(", runtime·funcname(f));
179 180
				for(i = 0; i < frame.arglen/sizeof(uintptr); i++) {
					if(i >= 5) {
Russ Cox's avatar
Russ Cox committed
181 182 183
						runtime·prints(", ...");
						break;
					}
184 185 186
					if(i != 0)
						runtime·prints(", ");
					runtime·printhex(((uintptr*)frame.argp)[i]);
Russ Cox's avatar
Russ Cox committed
187
				}
Russ Cox's avatar
Russ Cox committed
188
				runtime·prints(")\n");
189 190
				line = runtime·funcline(f, tracepc, &file);
				runtime·printf("\t%S:%d", file, line);
191 192
				if(frame.pc > f->entry)
					runtime·printf(" +%p", (uintptr)(frame.pc - f->entry));
193 194
				if(m->throwing && gp == m->curg)
					runtime·printf(" fp=%p", frame.fp);
Russ Cox's avatar
Russ Cox committed
195
				runtime·printf("\n");
196
				nprint++;
Russ Cox's avatar
Russ Cox committed
197
			}
198
		}
199 200 201
		n++;
	
	skipped:
202 203
		waspanic = f->entry == (uintptr)runtime·sigpanic;

204
		// Do not unwind past the bottom of the stack.
205
		if(flr == nil)
206 207
			break;

208
		// Unwind to next frame.
209
		frame.fn = flr;
210 211 212 213
		frame.pc = frame.lr;
		frame.lr = 0;
		frame.sp = frame.fp;
		frame.fp = 0;
214
	}
215
	
216 217 218 219 220 221 222 223 224
	if(pcbuf == nil && callback == nil)
		n = nprint;
	
	return n;
}

static void
printcreatedby(G *gp)
{
225 226
	int32 line;
	String file;
227 228 229
	uintptr pc, tracepc;
	Func *f;

Russ Cox's avatar
Russ Cox committed
230
	// Show what created goroutine, except main goroutine (goid 1).
231
	if((pc = gp->gopc) != 0 && (f = runtime·findfunc(pc)) != nil && gp->goid != 1) {
232
		runtime·printf("created by %s\n", runtime·funcname(f));
233 234
		tracepc = pc;	// back up to CALL instruction for funcline.
		if(pc > f->entry)
235
			tracepc--;
236 237
		line =  runtime·funcline(f, tracepc, &file);
		runtime·printf("\t%S:%d", file, line);
238 239
		if(pc > f->entry)
			runtime·printf(" +%p", (uintptr)(pc - f->entry));
Russ Cox's avatar
Russ Cox committed
240
		runtime·printf("\n");
241
	}
242
}
Russ Cox's avatar
Russ Cox committed
243 244

void
245
runtime·traceback(uintptr pc, uintptr sp, uintptr lr, G *gp)
Russ Cox's avatar
Russ Cox committed
246
{
247 248
	USED(lr);

Russ Cox's avatar
Russ Cox committed
249 250
	if(gp->status == Gsyscall) {
		// Override signal registers if blocked in system call.
251 252
		pc = gp->sched.pc;
		sp = gp->sched.sp;
Russ Cox's avatar
Russ Cox committed
253
	}
254 255 256 257 258 259
	
	// Print traceback. By default, omits runtime frames.
	// If that means we print nothing at all, repeat forcing all frames printed.
	if(runtime·gentraceback(pc, sp, 0, gp, 0, nil, 100, nil, nil, false) == 0)
		runtime·gentraceback(pc, sp, 0, gp, 0, nil, 100, nil, nil, true);
	printcreatedby(gp);
Russ Cox's avatar
Russ Cox committed
260
}
Russ Cox's avatar
Russ Cox committed
261

Russ Cox's avatar
Russ Cox committed
262
int32
263
runtime·callers(int32 skip, uintptr *pcbuf, int32 m)
Russ Cox's avatar
Russ Cox committed
264
{
265
	uintptr pc, sp;
Russ Cox's avatar
Russ Cox committed
266

267 268
	sp = runtime·getcallersp(&skip);
	pc = (uintptr)runtime·getcallerpc(&skip);
Russ Cox's avatar
Russ Cox committed
269

270
	return runtime·gentraceback(pc, sp, 0, g, skip, pcbuf, m, nil, nil, false);
Russ Cox's avatar
Russ Cox committed
271
}