aboutsummaryrefslogtreecommitdiff
path: root/acid/truss
blob: 82dd1df39b1033d91747291225ba9a849120ec41 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// poor emulation of SVR5 truss command - traces system calls

include("syscall");

_stoprunning = 0;

defn stopped(pid) {
	local l;
	local pc;
	pc = *PC;
	if notes then {
		if (notes[0]!="sys: breakpoint") then
		{
			print(pid,": ",trapreason(),"\t");
			print(fmt(pc,97),"\t",fmt(pc,105),"\n");
			print("Notes pending:\n");
			l = notes;
			while l do
			{
				print("\t",head l,"\n");
				l = tail l;
			}
			_stoprunning = 1;
		}
	}
}

defn _addressof(pattern) {
	local s, l;
	l = symbols;
	pattern = "^\\$*"+pattern+"$";
	while l do
	{
		s = head l;
		if regexp(pattern, s[0]) && ((s[1] == 'T') || (s[1] == 'L')) then
			return s[2];
		l = tail l;
	}
	return 0;
}

stopPC = {};
readPC = {};
fd2pathPC = {};
errstrPC = {};
awaitPC = {};
_waitPC = {};
_errstrPC = {};
trusscalls = {
		"sysr1",
		"_errstr",
		"bind",
		"chdir",
		"close",
		"dup",
		"alarm",
		"exec",
		"_exits",
		"_fsession",
		"fauth",
		"_fstat",
		"segbrk",
		"_mount",
		"open",
		"_read",
		"oseek",
		"sleep",
		"_stat",
		"rfork",
		"_write",
		"pipe",
		"create",
		"fd2path",
		"brk_",
		"remove",
		"_wstat",
		"_fwstat",
		"notify",
		"noted",
		"segattach",
		"segdetach",
		"segfree",
		"segflush",
		"rendezvous",
		"unmount",
		"_wait",
		"seek",
		"fversion",
		"errstr",
		"stat",
		"fstat",
		"wstat",
		"fwstat",
		"mount",
		"await",
		"pread",
		"pwrite",
	};

trussapecalls = {
		"_SYSR1",
		"__ERRSTR",
		"_BIND",
		"_CHDIR",
		"_CLOSE",
		"_DUP",
		"_ALARM",
		"_EXEC",
		"_EXITS",
		"__FSESSION",
		"_FAUTH",
		"__FSTAT",
		"_SEGBRK",
		"__MOUNT",
		"_OPEN",
		"__READ",
		"_OSEEK",
		"_SLEEP",
		"__STAT",
		"_RFORK",
		"__WRITE",
		"_PIPE",
		"_CREATE",
		"_FD2PATH",
		"_BRK_",
		"_REMOVE",
		"__WSTAT",
		"__FWSTAT",
		"_NOTIFY",
		"_NOTED",
		"_SEGATTACH",
		"_SEGDETACH",
		"_SEGFREE",
		"_SEGFLUSH",
		"_RENDEZVOUS",
		"_UNMOUNT",
		"__WAIT",
		"_SEEK",
		"__NFVERSION",
		"__NERRSTR",
		"_STAT",
		"__NFSTAT",
		"__NWSTAT",
		"__NFWSTAT",
		"__NMOUNT",
		"__NAWAIT",
		"_PREAD",
		"_PWRITE",
	};

defn addressof(pattern) {
	// translate to ape system calls if we have an ape binary
	if _addressof("_EXITS") == 0 then
		return _addressof(pattern);
	return _addressof(trussapecalls[match(pattern, trusscalls)]);
}

defn setuptruss() {
	local lst, offset, name, addr;

	trussbpt = {};
	offset = trapoffset();
	lst = trusscalls;
	while lst do
	{
		name = head lst;
		lst = tail lst;
		addr = addressof(name);
		if addr then
		{
			bpset(addr+offset);
			trussbpt = append trussbpt, (addr+offset);
			// sometimes _exits is renamed $_exits
			if(regexp("exits|exec", name)) then stopPC = append stopPC, (addr+offset);
			if(regexp("read", name)) then readPC = append readPC, (addr+offset);
			if(regexp("fd2path", name)) then fd2pathPC = append fd2pathPC, (addr+offset);
			if(regexp("^\\$*await", name)) then awaitPC = append awaitPC, (addr+offset);
			if(regexp("^\\$*errstr", name)) then errstrPC = append errstrPC, (addr+offset);
			// compatibility hacks for old kernel
			if(regexp("_wait", name)) then _waitPC = append _waitPC, (addr+offset);
			if(regexp("_errstr", name)) then _errstrPC = append _errstrPC, (addr+offset);
		}
	}
}

defn trussflush() {
	stop(pid);		// already stopped, but flushes output
}

defn new() {
	bplist = {};
	newproc(progargs);
	bpset(follow(main)[0]);
	cont();
	bpdel(*PC);
	// clear the hang bit, which is left set by newproc, so programs we fork/exec don't hang
	printto("/proc/"+itoa(pid)+"/ctl", "nohang");
}

defn truss() {
	local pc, lst, offset, prevpc, pcspret, ret;

	offset = trapoffset();

	stop(pid);
	_stoprunning = 0;
	setuptruss();
	pcspret = UPCSPRET();

	while !_stoprunning do {
		cont();
		if notes[0]!="sys: breakpoint" then {
			cleantruss();
			return {};
		}
		pc = *PC;
		if match(*PC, stopPC)>=0 then {
			print(pid,": ",trapreason(),"\t");
			print(fmt(pc,'a'),"\t",fmt(pc,'i'),"\n");
			cleantruss();
			return {};
		}
		if match(*PC, trussbpt)>=0 then {
			usyscall();
			trussflush();
			prevpc = *PC;
			step();
			ret = eval pcspret[2];
			print("\treturn value: ", ret\D, "\n");
			if (ret>=0) && (match(prevpc, readPC)>=0) then {
				print("\tdata: ");
				printtextordata(*((eval pcspret[1])+4), ret);
				print("\n");
			}
			if (ret>=0) && (match(prevpc, fd2pathPC)>=0) then {
				print("\tdata: \"", *(*((eval pcspret[1])+4)\s), "\"\n");
			}
			if (ret>=0) && (match(prevpc, errstrPC)>=0) then {
				print("\tdata: \"", *(*(eval pcspret[1])\s), "\"\n");
			}
			if (ret>=0) && (match(prevpc, awaitPC)>=0) then {
				print("\tdata: ");
				printtextordata(*(eval pcspret[1]), ret);
				print("\n");
			}
			// compatibility hacks for old kernel:
			if (ret>=0) && (match(prevpc, _waitPC)>=0) then {
				print("\tdata: ");
				printtextordata(*(eval pcspret[1]), 12+3*12+64);
				print("\n");
			}
			if (ret>=0) && (match(prevpc, _errstrPC)>=0) then {
				print("\tdata: ");
				printtextordata(*(eval pcspret[1]), 64);
				print("\n");
			}
		}
		trussflush();
	}
}

defn cleantruss() {
	local lst, offset, addr;

	stop(pid);
	offset = trapoffset();
	lst = trussbpt;
	while lst do
	{
		addr = head lst;
		lst = tail lst;
		bpdel(addr);
	}
	trussbpt = {};
	**PC = @*PC;	// repair current instruction
}

defn untruss() {
	cleantruss();
	start(pid);
}

print(acidfile);