xref: /plan9/sys/src/cmd/qi/symbols.c (revision f19e7b749ec99577072cd8e44030fe810f42c7ad)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <mach.h>
5 #define Extern extern
6 #include "power.h"
7 
8 #define	STRINGSZ	128
9 
10 /*
11  *	print the value of dot as file:line
12  */
13 void
printsource(long dot)14 printsource(long dot)
15 {
16 	char str[STRINGSZ];
17 
18 	if (fileline(str, STRINGSZ, dot))
19 		Bprint(bioout, "%s", str);
20 }
21 
22 void
printlocals(Symbol * fn,ulong fp)23 printlocals(Symbol *fn, ulong fp)
24 {
25 	int i;
26 	Symbol s;
27 
28 	s = *fn;
29 	for (i = 0; localsym(&s, i); i++) {
30 		if (s.class != CAUTO)
31 			continue;
32 		Bprint(bioout, "\t%s=#%lux\n", s.name, getmem_4(fp-s.value));
33 	}
34 }
35 
36 void
printparams(Symbol * fn,ulong fp)37 printparams(Symbol *fn, ulong fp)
38 {
39 	int i;
40 	Symbol s;
41 	int first;
42 
43 	fp += mach->szreg;			/* skip saved pc */
44 	s = *fn;
45 	for (first = i = 0; localsym(&s, i); i++) {
46 		if (s.class != CPARAM)
47 			continue;
48 		if (first++)
49 			Bprint(bioout, ", ");
50 		Bprint(bioout, "%s=#%lux", s.name, getmem_4(fp+s.value));
51 	}
52 	Bprint(bioout, ") ");
53 }
54 
55 #define STARTSYM	"_main"
56 #define	FRAMENAME	".frame"
57 
58 void
stktrace(int modif)59 stktrace(int modif)
60 {
61 	ulong pc, sp;
62 	Symbol s, f;
63 	int i;
64 	char buf[512];
65 
66 	pc = reg.pc;
67 	sp = reg.r[1];
68 	i = 0;
69 	while (findsym(pc, CTEXT, &s)) {
70 		if(strcmp(STARTSYM, s.name) == 0) {
71 			Bprint(bioout, "%s() at #%llux\n", s.name, s.value);
72 			break;
73 		}
74 		if (pc == s.value)	/* at first instruction */
75 			f.value = 0;
76 		else if (findlocal(&s, FRAMENAME, &f) == 0)
77 			break;
78 		if (s.type == 'L' || s.type == 'l' || pc <= s.value+4)
79 			pc = reg.lr;
80 		else pc = getmem_4(sp);
81 		sp += f.value;
82 		Bprint(bioout, "%s(", s.name);
83 		printparams(&s, sp);
84 		printsource(s.value);
85 		Bprint(bioout, " called from ");
86 		symoff(buf, sizeof(buf), pc-4, CTEXT);
87 		Bprint(bioout, buf);
88 		printsource(pc-8);
89 		Bprint(bioout, "\n");
90 		if(modif == 'C')
91 			printlocals(&s, sp);
92 		if(++i > 40){
93 			Bprint(bioout, "(trace truncated)\n");
94 			break;
95 		}
96 	}
97 }
98