aboutsummaryrefslogtreecommitdiff
path: root/acid/port
blob: c8e195fc1b3b0fd41b04280e1cfb7a32eb63bc7c (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
// portable acid for all architectures

defn pfl(addr)
{
	print(pcfile(addr), ":", pcline(addr), "\n");
}

defn
notestk(addr)
{
	local pc, sp;
	complex Ureg addr;

	pc = addr.pc\X;
	sp = addr.sp\X;

	print("Note pc:", pc, " sp:", sp, " ", fmt(pc, 'a'), " ");
	pfl(pc);
	_stk({"PC", pc, "SP", sp, linkreg(addr)}, 1);
}

defn
notelstk(addr)
{
	local pc, sp;
	complex Ureg addr;

	pc = addr.pc\X;
	sp = addr.sp\X;

	print("Note pc:", pc, " sp:", sp, " ", fmt(pc, 'a'), " ");
	pfl(pc);
	_stk({"PC", pc, "SP", sp, linkreg(addr)}, 1);
}

defn params(param)
{
	while param do {
		sym = head param;
		print(sym[0], "=", itoa(sym[1], "%#ux"));
		param = tail param;
		if param then
			print (",");
	}	
}

stkprefix = "";
stkignore = {};
stkend = 0;

defn locals(l)
{
	local sym;

	while l do {
		sym = head l;
		print(stkprefix, "\t", sym[0], "=", itoa(sym[1], "%#ux"), "\n");
		l = tail l;
	}	
}

defn _stkign(frame)
{
	local file;

	file = pcfile(frame[0]);
	s = stkignore;
	while s do {
		if regexp(head s, file) then
			return 1;
		s = tail s;
	}
	return 0;
}

// print a stack trace
//
// in a run of leading frames in files matched by regexps in stkignore,
// only print the last one.
defn _stk(regs, dolocals)
{
	local stk, frame, pc, fn, done, callerpc, paramlist, locallist;

	stk = strace(regs);
	if stkignore then {
		while stk && tail stk && _stkign(head tail stk) do
			stk = tail stk;
	}

	callerpc = 0;
	done = 0;
	while stk && !done do {
		frame = head stk;
		stk = tail stk;
		fn = frame[0];
		pc = frame[1];
		callerpc = frame[2];
		paramlist = frame[3];
		locallist = frame[4];

		print(stkprefix, fmt(fn, 'a'), "(");
		params(paramlist);
		print(")");
		if pc != fn then
			print("+", itoa(pc-fn, "%#ux"));
		print(" ");
		pfl(pc);
		if dolocals then
			locals(locallist);
		if fn == var("threadmain") || fn == var("p9main") then
			done=1;
		if fn == var("threadstart") || fn == var("scheduler") then
			done=1;
		if callerpc == 0 then
			done=1;
	}
	if callerpc && !done then {
		print(stkprefix, fmt(callerpc, 'a'), " ");
		pfl(callerpc);
	}
}

defn findsrc(file)
{
	local lst, src;

	if file[0] == '/' then {
		src = file(file);
		if src != {} then {
			srcfiles = append srcfiles, file;
			srctext = append srctext, src;
			return src;
		}
		return {};
	}

	lst = srcpath;
	while head lst do {
		src = file(head lst+file);
		if src != {} then {
			srcfiles = append srcfiles, file;
			srctext = append srctext, src;
			return src;
		}
		lst = tail lst;
	}
}

defn line(addr)
{
	local src, file;

	file = pcfile(addr);
	src = match(file, srcfiles);

	if src >= 0 then
		src = srctext[src];
	else
		src = findsrc(file);

	if src == {} then {
		print("no source for ", file, "\n");
		return {};
	}
	line = pcline(addr)-1;
	print(file, ":", src[line], "\n");
}

defn addsrcdir(dir)
{
	dir = dir+"/";

	if match(dir, srcpath) >= 0 then {
		print("already in srcpath\n");
		return {};
	}

	srcpath = {dir}+srcpath;
}

defn source()
{
	local l;

	l = srcpath;
	while l do {
		print(head l, "\n");
		l = tail l;
	}
	l = srcfiles;

	while l do {
		print("\t", head l, "\n");
		l = tail l;
	}
}

defn Bsrc(addr)
{
	local lst;

	lst = srcpath;
	file = pcfile(addr);
	if file[0] == '/' && access(file) then {
		rc("B "+file+":"+itoa(pcline(addr)));
		return {};
	}
	while head lst do {
		name = head lst+file;
		if access(name) then {
			rc("B "+name+":"+itoa(pcline(addr)));
			return {};
		}
		lst = tail lst;
	}
	print("no source for ", file, "\n");
}

defn srcline(addr)
{
	local text, cline, line, file, src;
	file = pcfile(addr);
	src = match(file,srcfiles);
	if (src>=0) then
		src = srctext[src];
	else
		src = findsrc(file);
	if (src=={}) then
	{
		return "(no source)";
	}
	return src[pcline(addr)-1];
}

defn src(addr)
{
	local src, file, line, cline, text;

	file = pcfile(addr);
	src = match(file, srcfiles);

	if src >= 0 then
		src = srctext[src];
	else
		src = findsrc(file);

	if src == {} then {
		print("no source for ", file, "\n");
		return {};
	}

	cline = pcline(addr)-1;
	print(file, ":", cline+1, "\n");
	line = cline-5;
	loop 0,10 do {
		if line >= 0 then {
			if line == cline then
				print(">");
			else
				print(" ");
			text = src[line];
			if text == {} then
				return {};
			print(line+1, "\t", text, "\n");
		}
		line = line+1;
	}	
}

defn step()					// single step the process
{
	local lst, lpl, addr, bput;

	bput = 0;
	if match(*PC, bplist) >= 0 then {	// Sitting on a breakpoint
		bput = fmt(*PC, bpfmt);
		*bput = @bput;
	}

	lst = follow(*PC);

	lpl = lst;
	while lpl do {				// place break points
		*(head lpl) = bpinst;
		lpl = tail lpl;
	}

	startstop(pid);				// do the step

	while lst do {				// remove the breakpoints
		addr = fmt(head lst, bpfmt);
		*addr = @addr;
		lst = tail lst;
	}
	if bput != 0 then
		*bput = bpinst;
}

defn bpset(addr)				// set a breakpoint
{
	if status(pid) != "Stopped" then {
		print("Waiting...\n");
		stop(pid);
	}
	if match(addr, bplist) >= 0 then
		print("breakpoint already set at ", fmt(addr, 'a'), "\n");
	else {
		*fmt(addr, bpfmt) = bpinst;
		bplist = append bplist, addr;
	}
}

defn bptab()					// print a table of breakpoints
{
	local lst, addr;

	lst = bplist;
	while lst do {
		addr = head lst;
		print("\t", fmt(addr, 'X'), " ", fmt(addr, 'a'), "  ", fmt(addr, 'i'), "\n");
		lst = tail lst;
	}
}

defn bpdel(addr)				// delete a breakpoint
{
	local n, pc, nbplist;

	if addr == 0 then {
		while bplist do {
			pc = head bplist;
			pc = fmt(pc, bpfmt);
			*pc = @pc;
			bplist = tail bplist;
		}
		return {};
	}

	n = match(addr, bplist);
	if n < 0  then {
		print("no breakpoint at ", fmt(addr, 'a'), "\n");
		return {};
	}

	addr = fmt(addr, bpfmt);
	*addr = @addr;

	nbplist = {};				// delete from list
	while bplist do {
		pc = head bplist;
		if pc != addr then
			nbplist = append nbplist, pc;
		bplist = tail bplist;
	}
	bplist = nbplist;			// delete from memory
}

defn cont()					// continue execution
{
	local addr;

	addr = fmt(*PC, bpfmt);
	if match(addr, bplist) >= 0 then {	// Sitting on a breakpoint
		*addr = @addr;
		step();				// Step over
		*addr = bpinst;
	}
	startstop(pid);				// Run
}

defn stopped(pid)		// called from acid when a process changes state
{
	pfixstop(pid);
	pstop(pid);		// stub so this is easy to replace
}

defn procs()			// print status of processes
{
	local c, lst, cpid;

	cpid = pid;
	lst = proclist;
	while lst do {
		np = head lst;
		setproc(np);
		if np == cpid then
			c = '>';
		else
			c = ' ';
		print(fmt(c, 'c'), np, ": ", status(np), " at ", fmt(*PC, 'a'), " setproc(", np, ")\n");
		lst = tail lst;
	}
	pid = cpid;
	if pid != 0 then
		setproc(pid);
}

_asmlines = 30;

defn asm(addr)
{
	local bound;

	bound = fnbound(addr);

	addr = fmt(addr, 'i');
	loop 1,_asmlines do {
		print(fmt(addr, 'a'), " ", fmt(addr, 'X'));
		print("\t", @addr++, "\n");
		if bound != {} && addr > bound[1] then {
			lasmaddr = addr;
			return {};
		}
	}
	lasmaddr = addr;
}

defn casm()
{
	asm(lasmaddr);
}

defn xasm(addr)
{
	local bound;

	bound = fnbound(addr);

	addr = fmt(addr, 'i');
	loop 1,_asmlines do {
		print(fmt(addr, 'a'), " ", fmt(addr, 'X'));
		print("\t", *addr++, "\n");
		if bound != {} && addr > bound[1] then {
			lasmaddr = addr;
			return {};
		}
	}
	lasmaddr = addr;
}

defn xcasm()
{
	xasm(lasmaddr);
}

defn win()
{
	local npid, estr;

	bplist = {};
	notes = {};

	estr = "/sys/lib/acid/window '0 0 600 400' "+textfile;
	if progargs != "" then
		estr = estr+" "+progargs;

	npid = rc(estr);
	npid = atoi(npid);
	if npid == 0 then
		error("win failed to create process");

	setproc(npid);
	stopped(npid);
}

defn win2()
{
	local npid, estr;

	bplist = {};
	notes = {};

	estr = "/sys/lib/acid/transcript '0 0 600 400' '100 100 700 500' "+textfile;
	if progargs != "" then
		estr = estr+" "+progargs;

	npid = rc(estr);
	npid = atoi(npid);
	if npid == 0 then
		error("win failed to create process");

	setproc(npid);
	stopped(npid);
}

printstopped = 1;
defn new()
{
	local a;
	
	bplist = {};
	newproc(progargs);
	a = var("p9main");
	if a == {} then
		a = var("main");
	if a == {} then
		return {};
	bpset(a);
	while *PC != a do
		cont();
	bpdel(a);
}

defn stmnt()			// step one statement
{
	local line;

	line = pcline(*PC);
	while 1 do {
		step();
		if line != pcline(*PC) then {
			src(*PC);
			return {};
		}
	}
}

defn func()			// step until we leave the current function
{
	local bound, end, start, pc;

	bound = fnbound(*PC);
	if bound == {} then {
		print("cannot locate text symbol\n");
		return {};
	}

	pc = *PC;
	start = bound[0];
	end = bound[1];
	while pc >= start && pc < end do {
		step();
		pc = *PC;
	}
}

defn next()
{
	local sp, bound, pc;

	sp = *SP;
	bound = fnbound(*PC);
	if bound == {} then {
		print("cannot locate text symbol\n");
		return {};
	}
	stmnt();
	pc = *PC;
	if pc >= bound[0] && pc < bound[1] then
		return {};

	while (pc < bound[0] || pc > bound[1]) && sp >= *SP do {
		step();
		pc = *PC;
	}
	src(*PC);
}

defn maps()
{
	local m, mm;

	m = map();
	while m != {} do {
		mm = head m;
		m = tail m;
		print(mm[2]\X, " ", mm[3]\X, " ", mm[4]\X, " ", mm[0], " ", mm[1], "\n");
	}
}

defn dump(addr, n, fmt)
{
	loop 0, n do {
		print(fmt(addr, 'X'), ": ");
		addr = mem(addr, fmt);
	}
}

defn mem(addr, fmt)
{

	local i, c, n;

	i = 0;
	while fmt[i] != 0 do {
		c = fmt[i];
		n = 0;
		while '0' <= fmt[i] && fmt[i] <= '9' do {
			n = 10*n + fmt[i]-'0';
			i = i+1;
		}
		if n <= 0 then n = 1;
		addr = fmt(addr, fmt[i]);
		while n > 0 do {
			print(*addr++, " ");
			n = n-1;
		}
		i = i+1;
	}
	print("\n");
	return addr;
}

defn symbols(pattern)
{
	local l, s, name;
	
	l = symbols;
	while l do {
		s = head l;
		if regexp(pattern, s[0]) then {
			name = s[4];
			if name == {} then
				name = "";
			print(s[0], "\t", s[1], "\t", s[2], "\t", s[3], "\t", name, "\n");
		}
		l = tail l;
	}
}

defn havesymbol(name)
{
	local l, s;

	l = symbols;
	while l do {
		s = head l;
		l = tail l;
		if s[0] == name then
			return 1;
	}
	return 0;
}

defn spsrch(len)
{
	local addr, a, s, e;

	addr = *SP;
	s = origin & 0x7fffffff;
	e = etext & 0x7fffffff;
	loop 1, len do {
		a = *addr++;
		c = a & 0x7fffffff;
		if c > s && c < e then {
			print("src(", a, ")\n");
			pfl(a);
		}			
	}
}

defn acidtypes()
{
	local syms;
	local l;

	l = textfile();
	if l != {} then {
		syms = "acidtypes";
		while l != {} do {
			syms = syms + " " + ((head l)[0]);
			l = tail l;
		}
		includepipe(syms);
	}
}

defn getregs()
{
	local regs, l;

	regs = {};
	l = registers;
	while l != {} do {
		regs = append regs, var(l[0]);
		l = tail l;
	}
	return regs;
}

defn setregs(regs)
{
	local l;

	l = registers;
	while l != {} do {
		var(l[0]) = regs[0];
		l = tail l;
		regs = tail regs;
	}
	return regs;
}

defn resetregs()
{
	local l;

	l = registers;
	while l != {} do {
		var(l[0]) = register(l[0]);
		l = tail l;
	}
}

defn clearregs()
{
	local l;

	l = registers;
	while l != {} do {
		var(l[0]) = refconst(~0);
		l = tail l;
	}
}

progargs="";
print(acidfile);