summaryrefslogtreecommitdiff
path: root/src/runtime/rt2_amd64.c
blob: 5a388bfe0262a53a268aa4213b30028e82a2027b (plain)
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
// 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.

#include "runtime.h"

extern int32	debug;

extern uint8 end;

void
traceback(uint8 *pc, uint8 *sp, void* r15)
{
	int32 spoff;
	int8* spp;
	uint8* callpc;
	int32 counter;
	int32 i;
	string name;
	Func *f;
	G g;
	Stktop *stktop;

	// store local copy of per-process data block that we can write as we unwind
	mcpy((byte*)&g, (byte*)r15, sizeof(G));

	// if the PC is zero, it's probably due to a nil function pointer.
	// pop the failed frame.
	if(pc == nil) {
		pc = ((uint8**)sp)[0];
		sp += 8;
	}

	counter = 0;
	name = gostring((byte*)"panic");
	for(;;){
		callpc = pc;
		if((uint8*)retfromnewstack == pc) {
			// call site is retfromnewstack(); pop to earlier stack block to get true caller
			stktop = (Stktop*)g.stackbase;
			g.stackbase = stktop->oldbase;
			g.stackguard = stktop->oldguard;
			sp = stktop->oldsp;
			pc = ((uint8**)sp)[1];
			sp += 16;  // two irrelevant calls on stack - morestack, plus the call morestack made
			continue;
		}
		f = findfunc((uint64)callpc);
		if(f == nil)
			return;
		name = f->name;
		sp += f->frame;
		if(counter++ > 100){
			prints("stack trace terminated\n");
			break;
		}
		if((pc = ((uint8**)sp)[-1]) <= (uint8*)0x1000)
			break;

		/* print this frame */
		prints("0x");
		sys·printpointer(callpc  - 1);	// -1 to get to CALL instr.
		prints("?zi\n");
		prints("\t");
		sys·printstring(name);
		prints("(");
		for(i = 0; i < 3; i++){
			if(i != 0)
				prints(", ");
			sys·printint(((uint32*)sp)[i]);
		}
		prints(", ...)\n");
		prints("\t");
		sys·printstring(name);
		prints("(");
		for(i = 0; i < 3; i++){
			if(i != 0)
				prints(", ");
			prints("0x");
			sys·printpointer(((void**)sp)[i]);
		}
		prints(", ...)\n");
	}
}