summaryrefslogtreecommitdiff
path: root/appl/cmd/stack.b
blob: 189870d35b209af7a619c857eb218289af4db9ac (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
implement Command;

include "sys.m";
	sys: Sys;
	print, fprint, FD: import sys;
	stderr: ref FD;

include "draw.m";

include "debug.m";
	debug: Debug;
	Prog, Module, Exp: import debug;

include "arg.m";
include "bufio.m";
	bufio: Bufio;
	Iobuf: import bufio;

include "env.m";
	env: Env;

include "string.m";
	str: String;

include "dis.m";
	dism: Dis;

Command: module
{
	init: fn(ctxt: ref Draw->Context, argv: list of string);
};

usage()
{
	sys->fprint(stderr, "usage: stack [-v] pid\n");
	raise "fail:usage";
}

badmodule(p: string)
{
	sys->fprint(stderr, "stack: cannot load %s: %r\n", p);
	raise "fail:bad module";
}

sbldirs: list of (string, string);

init(nil: ref Draw->Context, argv: list of string)
{

	sys = load Sys Sys->PATH;
	stderr = sys->fildes(2);
	arg := load Arg Arg->PATH;
	if (arg == nil)
		badmodule(Arg->PATH);
	bufio = load Bufio Bufio->PATH;
	if (bufio == nil)
		badmodule(Bufio->PATH);
	debug = load Debug Debug->PATH;
	if(debug == nil)
		badmodule(Debug->PATH);
	env = load Env Env->PATH;
	if (env != nil) {
		str = load String String->PATH;
		if (str == nil)
			badmodule(String->PATH);
	}
	bout := bufio->fopen(sys->fildes(1), Sys->OWRITE);

	arg->init(argv);
	verbose := 0;
	while ((opt := arg->opt()) != 0) {
		case opt {
		'v' =>
			verbose = 1;
		'p' =>
			dispath := arg->arg();
			sblpath := arg->arg();
			if (dispath == nil || sblpath == nil)
				usage();
			sbldirs = (addslash(dispath), addslash(sblpath)) :: sbldirs;
		* =>
			usage();
		}
	}
	if (env != nil && (pathl := env->getenv("sblpath")) != nil) {
		toks := str->unquoted(pathl);
		for (; toks != nil && tl toks != nil; toks = tl tl toks)
			sbldirs = (addslash(hd toks), addslash(hd tl toks)) :: sbldirs;
	}
	t: list of (string, string);
	for (; sbldirs != nil; sbldirs = tl sbldirs)
		t = hd sbldirs :: t;
	sbldirs = t;

	argv = arg->argv();
	if(argv == nil)
		usage();

	debug->init();

	(p, err) := debug->prog(int hd argv);
	if(err != nil){
		fprint(stderr, "stack: %s\n", err);
		return;
	}
	stk: array of ref Exp;
	(stk, err) = p.stack();

	if(err != nil){
		fprint(stderr, "stack: %s\n", err);
		return;
	}

	for(i := 0; i < len stk; i++){
		stdsym(stk[i].m);
		stk[i].m.stdsym();
		stk[i].findsym();
		bout.puts(stk[i].name + "(");
		vs := stk[i].expand();
		if(verbose && vs != nil){
			for(j := 0; j < len vs; j++){
				if(vs[j].name == "args"){
					d := vs[j].expand();
					s := "";
					for(j = 0; j < len d; j++) {
						bout.puts(sys->sprint("%s%s=%s", s, d[j].name, d[j].val().t0));
						s = ", ";
					}
					break;
				}
			}
		}
		bout.puts(sys->sprint(") %s\n", stk[i].srcstr()));
		if(verbose && vs != nil){
			for(j := 0; j < len vs; j++){
				if(vs[j].name == "locals"){
					d := vs[j].expand();
					for(j = 0; j < len d; j++)
						bout.puts("\t" + d[j].name + "=" + d[j].val().t0 + "\n");
					break;
				}
			}
		}
	}
	bout.flush();
}

stdsym(m: ref Module)
{
	dis := m.dis();
	if(dism == nil){
		dism = load Dis Dis->PATH;
		if(dism != nil)
			dism->init();
	}
	if(dism != nil && (sp := dism->src(dis)) != nil){
		sp = sp[0: len sp - 1] + "sbl";
		(sym, nil) := debug->sym(sp);
		if (sym != nil) {
			m.addsym(sym);
			return;
		}
	}
	for (sbl := sbldirs; sbl != nil; sbl = tl sbl) {
		(dispath, sblpath) := hd sbl;
		if (len dis > len dispath && dis[0:len dispath] == dispath) {
			sblpath = sblpath + dis[len dispath:];
			if (len sblpath > 4 && sblpath[len sblpath - 4:] == ".dis")
				sblpath = sblpath[0:len sblpath - 4] + ".sbl";
			(sym, nil) := debug->sym(sblpath);
			if (sym != nil) {
				m.addsym(sym);
				return;
			}
		}
	}
}
			
addslash(p: string): string
{
	if (p != nil && p[len p - 1] != '/')
		p[len p] = '/';
	return p;
}