won't build with newvm system.
[unix-history] / usr / src / old / adb / adb.vax / runpcs.c
CommitLineData
a1dada70 1#ifndef lint
d26d88c4 2static char sccsid[] = "@(#)runpcs.c 4.6 %G%";
a1dada70 3#endif
a27df539
BJ
4/*
5 *
6 * UNIX debugger
7 *
8 */
9
10#include "defs.h"
a27df539
BJ
11
12extern MAP txtmap;
13
14MSG NOFORK;
15MSG ENDPCS;
16MSG BADWAIT;
17
18CHAR *lp;
19ADDR sigint;
20ADDR sigqit;
21
22/* breakpoints */
23BKPTR bkpthead;
24
25REGLIST reglist[];
26
27CHAR lastc;
28
29INT fcor;
30INT fsym;
31STRING errflg;
32INT errno;
33INT signo;
34INT sigcode;
35
36L_INT dot;
37STRING symfil;
38INT wtflag;
39L_INT pid;
40L_INT expv;
41INT adrflg;
42L_INT loopcnt;
43
44
45
46
47
48/* service routines for sub process control */
49
50getsig(sig)
51{ return(expr(0) ? expv : sig);
52}
53
54ADDR userpc = 1;
55
56runpcs(runmode,execsig)
57{
58 INT rc;
59 REG BKPTR bkpt;
60 IF adrflg THEN userpc=dot; FI
61 printf("%s: running\n", symfil);
62
63 WHILE --loopcnt>=0
64 DO
65#ifdef DEBUG
66 printf("\ncontinue %x %d\n",userpc,execsig);
67#endif
68 IF runmode==SINGLE
69 THEN delbp(); /* hardware handles single-stepping */
70 ELSE /* continuing from a breakpoint is hard */
71 IF bkpt=scanbkpt(userpc)
72 THEN execbkpt(bkpt,execsig); execsig=0;
73 FI
74 setbp();
75 FI
76 ptrace(runmode,pid,userpc,execsig);
77 bpwait(); chkerr(); execsig=0; delbp(); readregs();
78
79 IF (signo==0) ANDF (bkpt=scanbkpt(userpc))
80 THEN /* stopped by BPT instruction */
81#ifdef DEBUG
82 printf("\n BPT code; '%s'%o'%o'%d",
83 bkpt->comm,bkpt->comm[0],EOR,bkpt->flag);
84#endif
85 dot=bkpt->loc;
86 IF bkpt->flag==BKPTEXEC
87 ORF ((bkpt->flag=BKPTEXEC)
88 ANDF bkpt->comm[0]!=EOR
89 ANDF command(bkpt->comm,':')
90 ANDF --bkpt->count)
91 THEN execbkpt(bkpt,execsig); execsig=0; loopcnt++;
92 ELSE bkpt->count=bkpt->initcnt; rc=1;
93 FI
94 ELSE execsig=signo; rc=0;
95 FI
96 OD
97 return(rc);
98}
99
100#define BPOUT 0
101#define BPIN 1
102INT bpstate = BPOUT;
103
104endpcs()
105{
106 REG BKPTR bkptr;
107 IF pid
ecc0ec9e 108 THEN ptrace(PT_KILL,pid,0,0); pid=0; userpc=1;
a27df539
BJ
109 FOR bkptr=bkpthead; bkptr; bkptr=bkptr->nxtbkpt
110 DO IF bkptr->flag
111 THEN bkptr->flag=BKPTSET;
112 FI
113 OD
114 FI
115 bpstate=BPOUT;
116}
117
118#ifdef VFORK
119nullsig()
120{
121
122}
123#endif
124
125setup()
126{
127 close(fsym); fsym = -1;
128#ifndef VFORK
129 IF (pid = fork()) == 0
130#else
131 IF (pid = vfork()) == 0
132#endif
ecc0ec9e 133 THEN ptrace(PT_TRACE_ME,0,0,0);
a27df539
BJ
134#ifdef VFORK
135 signal(SIGTRAP,nullsig);
136#endif
137 signal(SIGINT,sigint); signal(SIGQUIT,sigqit);
138 doexec(); exit(0);
139 ELIF pid == -1
140 THEN error(NOFORK);
141 ELSE bpwait(); readregs(); lp[0]=EOR; lp[1]=0;
142 fsym=open(symfil,wtflag);
143 IF errflg
144 THEN printf("%s: cannot execute\n",symfil);
145 endpcs(); error(0);
146 FI
147 FI
148 bpstate=BPOUT;
149}
150
151execbkpt(bkptr,execsig)
152BKPTR bkptr;
153{
154#ifdef DEBUG
155 printf("exbkpt: %d\n",bkptr->count);
156#endif
157 delbp();
ecc0ec9e 158 ptrace(PT_STEP,pid,bkptr->loc,execsig);
a27df539
BJ
159 bkptr->flag=BKPTSET;
160 bpwait(); chkerr(); readregs();
161}
162
163
164doexec()
165{
166 STRING argl[MAXARG];
167 CHAR args[LINSIZ];
168 STRING p, *ap, filnam;
169 extern STRING environ;
170 ap=argl; p=args;
171 *ap++=symfil;
172 REP IF rdc()==EOR THEN break; FI
173 *ap = p;
8a7b24df
RH
174 /*
175 * First thing is to look for direction characters
176 * and get filename. Do not use up the args for filenames.
177 * Then get rid of spaces before next args.
178 */
179 IF lastc=='<'
180 THEN REP readchar(); PER lastc==SP ORF lastc==TB DONE
181 filnam = p;
182 WHILE lastc!=EOR ANDF lastc!=SP ANDF lastc!=TB ANDF lastc!='>'
183 DO *p++=lastc; readchar(); OD
184 *p = 0;
185 close(0);
a27df539
BJ
186 IF open(filnam,0)<0
187 THEN printf("%s: cannot open\n",filnam); _exit(0);
188 FI
8a7b24df
RH
189 p = *ap;
190 ELIF lastc=='>'
191 THEN REP readchar(); PER lastc==SP ORF lastc==TB DONE
192 filnam = p;
193 WHILE lastc!=EOR ANDF lastc!=SP ANDF lastc!=TB ANDF lastc!='<'
194 DO *p++=lastc; readchar(); OD
195 *p = '\0';
196 close(1);
a27df539
BJ
197 IF creat(filnam,0666)<0
198 THEN printf("%s: cannot create\n",filnam); _exit(0);
199 FI
8a7b24df
RH
200 p = *ap;
201 ELSE
202 WHILE lastc!=EOR ANDF lastc!=SP ANDF lastc!=TB ANDF lastc!='>' ANDF lastc!='<'
203 DO *p++=lastc; readchar(); OD
204 *p++ = '\0';
205 ap++;
a27df539
BJ
206 FI
207 PER lastc!=EOR DONE
208 *ap++=0;
209 exect(symfil, argl, environ);
210 perror(symfil);
211}
212
213BKPTR scanbkpt(adr)
214ADDR adr;
215{
216 REG BKPTR bkptr;
217 FOR bkptr=bkpthead; bkptr; bkptr=bkptr->nxtbkpt
218 DO IF bkptr->flag ANDF bkptr->loc==adr
219 THEN break;
220 FI
221 OD
222 return(bkptr);
223}
224
225delbp()
226{
227 REG ADDR a;
228 REG BKPTR bkptr;
229 IF bpstate!=BPOUT
230 THEN
231 FOR bkptr=bkpthead; bkptr; bkptr=bkptr->nxtbkpt
232 DO IF bkptr->flag
233 THEN a=bkptr->loc;
234 IF a < txtmap.e1 THEN
d26d88c4 235 ptrace(PT_WRITE_I,pid,a,bkptr->ins);
a27df539 236 ELSE
d26d88c4 237 ptrace(PT_WRITE_D,pid,a,bkptr->ins);
a27df539
BJ
238 FI
239 FI
240 OD
241 bpstate=BPOUT;
242 FI
243}
244
d26d88c4
SL
245#ifdef vax
246#define SETBP(ins) (BPT | ((ins) &~ 0xFF))
247#endif
248
a27df539
BJ
249setbp()
250{
251 REG ADDR a;
252 REG BKPTR bkptr;
253
254 IF bpstate!=BPIN
255 THEN
256 FOR bkptr=bkpthead; bkptr; bkptr=bkptr->nxtbkpt
257 DO IF bkptr->flag
258 THEN a = bkptr->loc;
259 IF a < txtmap.e1 THEN
ecc0ec9e 260 bkptr->ins = ptrace(PT_READ_I, pid, a, 0);
d26d88c4 261 ptrace(PT_WRITE_I, pid, a, SETBP(bkptr->ins));
a27df539 262 ELSE
ecc0ec9e 263 bkptr->ins = ptrace(PT_READ_D, pid, a, 0);
d26d88c4 264 ptrace(PT_WRITE_D, pid, a, SETBP(bkptr->ins));
a27df539
BJ
265 FI
266 IF errno
267 THEN prints("cannot set breakpoint: ");
268 psymoff(bkptr->loc,ISYM,"\n");
269 FI
270 FI
271 OD
272 bpstate=BPIN;
273 FI
274}
275
276bpwait()
277{
278 REG ADDR w;
279 ADDR stat;
280
281 signal(SIGINT, 1);
282 WHILE (w = wait(&stat))!=pid ANDF w != -1 DONE
283 signal(SIGINT,sigint);
284 IF w == -1
285 THEN pid=0;
286 errflg=BADWAIT;
287 ELIF (stat & 0177) != 0177
288 THEN sigcode = 0;
289 IF signo = stat&0177
290 THEN sigprint();
291 FI
292 IF stat&0200
293 THEN prints(" - core dumped");
294 close(fcor);
295 setcor();
296 FI
297 pid=0;
298 errflg=ENDPCS;
299 ELSE signo = stat>>8;
ecc0ec9e 300 sigcode = ptrace(PT_READ_U, pid, &((struct user *)0)->u_code, 0);
a27df539
BJ
301 IF signo!=SIGTRAP
302 THEN sigprint();
303 ELSE signo=0;
304 FI
305 flushbuf();
306 FI
307}
308
309readregs()
310{
311 /*get REG values from pcs*/
312 REG i;
313 FOR i=24; --i>=0;
314 DO *(ADDR *)(((ADDR)&u)+reglist[i].roffs) =
ecc0ec9e 315 ptrace(PT_READ_U, pid, reglist[i].roffs, 0);
a27df539
BJ
316 OD
317 userpc= *(ADDR *)(((ADDR)&u)+PC);
318}
319
320