1 /*	kdb_runpcs.c	7.2	86/11/23	*/
2 
3 #include "../kdb/defs.h"
4 
5 char	*lp;
6 
7 /* breakpoints */
8 BKPTR	bkpthead;
9 
10 char	lastc;
11 
12 long	dot;
13 int	adrflg;
14 long	loopcnt;
15 ADDR	userpc = 1;
16 
17 runpcs(runmode, execsig)
18 {
19 	register BKPTR bkpt;
20 
21 	if (adrflg)
22 		userpc = dot;
23 	if (execsig == 0)
24 		printf("kdb: running\n");
25 	if (runmode==SINGLE) {
26 		/*
27 		 * To single step, delete the
28 		 * breakpoints and invoke the
29 		 * hardware single step in the
30 		 * main loop.
31 		 */
32 		delbp();
33 		reset(SINGLE);
34 	}
35 	/*
36 	 * If we're currently at a breakpoint,
37 	 * restore the instruction and single
38 	 * step before continuing.  Otherwise,
39 	 * we can just set our breakpoints and
40 	 * continue.
41 	 */
42 	if (bkpt = scanbkpt(userpc)) {
43 		execbkpt(bkpt, execsig);
44 		/*NOTREACHED*/
45 	}
46 	setbp();
47 	reset(CONTIN);
48 }
49 
50 static	int execbkptf;
51 
52 /*
53  * Continue execution after a trap.
54  *
55  * If tracetrap is nonzero, we've entered here because of a
56  * trace trap.  If we're skipping a breakpoint (execbkptf),
57  * or this is the next iteration of a breakpoint, continue.
58  * If this is the next iteration of a single step, do the
59  * next step.  Otherwise return 1 if we stopped because
60  * of a breakpoint,
61  */
62 nextpcs(tracetrap, execsig)
63 	int tracetrap, execsig;
64 {
65 	register BKPTR bkpt;
66 	short rc;
67 
68 	clrsstep();			/* clear hardware single step */
69 	delbp();
70 	if (execbkptf) {
71 		execbkptf = 0;
72 		runpcs(CONTIN, 1);
73 		/*NOTREACHED*/
74 	}
75 	if (!tracetrap && (bkpt = scanbkpt(userpc))) {
76 		/*
77 		 * Stopped at a breakpoint,
78 		 * execute any command.
79 		 */
80 		dot = bkpt->loc;
81 		if (bkpt->flag == BKPTEXEC ||
82 		    ((bkpt->flag = BKPTEXEC) && bkpt->comm[0] != EOR &&
83 		    command(bkpt->comm, ':') && --bkpt->count)) {
84 			loopcnt++;
85 			execbkpt(bkpt, execsig);
86 			execsig = 0;
87 		} else {
88 			bkpt->count = bkpt->initcnt;
89 			rc = 1;
90 		}
91 	} else {
92 		execsig = 0;
93 		rc = 0;
94 	}
95 	if (--loopcnt > 0)
96 		runpcs(rc ? CONTIN : SINGLE, 1);
97 	return (rc);
98 }
99 
100 #define BPOUT 0
101 #define BPIN 1
102 static	int bpstate = BPOUT;
103 
104 execbkpt(bkptr,execsig)
105 	BKPTR	bkptr;
106 {
107 
108 	delbp();
109 	bkptr->flag = BKPTSET;
110 	execbkptf++;
111 	reset(SINGLE);
112 }
113 
114 BKPTR
115 scanbkpt(addr)
116 	ADDR addr;
117 {
118 	register BKPTR	bkptr;
119 
120 	for (bkptr = bkpthead; bkptr; bkptr = bkptr->nxtbkpt)
121 		if (bkptr->flag && bkptr->loc == addr)
122 			break;
123 	return (bkptr);
124 }
125 
126 delbp()
127 {
128 	register ADDR a;
129 	register BKPTR bkptr;
130 
131 	if (bpstate == BPOUT)
132 		return;
133 	for (bkptr = bkpthead; bkptr; bkptr = bkptr->nxtbkpt)
134 		if (bkptr->flag) {
135 			a = bkptr->loc;
136 			put(a, ISP, bkptr->ins);
137 		}
138 	bpstate = BPOUT;
139 }
140 
141 setbp()
142 {
143 	register ADDR a;
144 	register BKPTR bkptr;
145 
146 	if (bpstate == BPIN)
147 		return;
148 	for (bkptr = bkpthead; bkptr; bkptr = bkptr->nxtbkpt)
149 		if (bkptr->flag) {
150 			a = bkptr->loc;
151 			bkptr->ins = get(a, ISP);
152 			put(a, ISP, SETBP(bkptr->ins));
153 		}
154 	bpstate = BPIN;
155 }
156