xref: /plan9-contrib/sys/src/cmd/vi/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 "mips.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 #define STARTSYM	"_main"
55 #define	FRAMENAME	".frame"
56 
57 void
stktrace(int modif)58 stktrace(int modif)
59 {
60 	ulong pc, sp;
61 	Symbol s, f;
62 	int i;
63 	char buf[512];
64 
65 	pc = reg.pc;
66 	sp = reg.r[29];
67 	i = 0;
68 	while (findsym(pc, CTEXT, &s)) {
69 		if(strcmp(STARTSYM, s.name) == 0) {
70 			Bprint(bioout, "%s() at #%llux\n", s.name, s.value);
71 			break;
72 		}
73 		if (pc == s.value)	/* at first instruction */
74 			f.value = 0;
75 		else if (findlocal(&s, FRAMENAME, &f) == 0)
76 			break;
77 		if (s.type == 'L' || s.type == 'l' || pc <= s.value+4)
78 			pc = reg.r[31];
79 		else pc = getmem_4(sp);
80 		sp += f.value;
81 		Bprint(bioout, "%s(", s.name);
82 		printparams(&s, sp);
83 		printsource(s.value);
84 		Bprint(bioout, " called from ");
85 		symoff(buf, sizeof(buf), pc-8, CTEXT);
86 		Bprint(bioout, buf);
87 		printsource(pc-8);
88 		Bprint(bioout, "\n");
89 		if(modif == 'C')
90 			printlocals(&s, sp);
91 		if(++i > 40){
92 			Bprint(bioout, "(trace truncated)\n");
93 			break;
94 		}
95 	}
96 }
97