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