db_run.c revision 1.5 1 1.5 cgd /* $NetBSD: db_run.c,v 1.5 1994/06/29 06:31:16 cgd Exp $ */
2 1.5 cgd
3 1.1 cgd /*
4 1.1 cgd * Mach Operating System
5 1.1 cgd * Copyright (c) 1991,1990 Carnegie Mellon University
6 1.1 cgd * All Rights Reserved.
7 1.1 cgd *
8 1.1 cgd * Permission to use, copy, modify and distribute this software and its
9 1.1 cgd * documentation is hereby granted, provided that both the copyright
10 1.1 cgd * notice and this permission notice appear in all copies of the
11 1.1 cgd * software, derivative works or modified versions, and any portions
12 1.1 cgd * thereof, and that both notices appear in supporting documentation.
13 1.1 cgd *
14 1.1 cgd * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15 1.1 cgd * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 1.1 cgd * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 1.1 cgd *
18 1.1 cgd * Carnegie Mellon requests users of this software to return to
19 1.1 cgd *
20 1.1 cgd * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 1.1 cgd * School of Computer Science
22 1.1 cgd * Carnegie Mellon University
23 1.1 cgd * Pittsburgh PA 15213-3890
24 1.1 cgd *
25 1.1 cgd * any improvements or extensions that they make and grant Carnegie the
26 1.1 cgd * rights to redistribute these changes.
27 1.2 cgd *
28 1.1 cgd * Author: David B. Golub, Carnegie Mellon University
29 1.1 cgd * Date: 7/90
30 1.1 cgd */
31 1.1 cgd
32 1.1 cgd /*
33 1.1 cgd * Commands to run process.
34 1.1 cgd */
35 1.4 mycroft #include <sys/param.h>
36 1.4 mycroft #include <sys/proc.h>
37 1.4 mycroft
38 1.1 cgd #include <machine/db_machdep.h>
39 1.1 cgd
40 1.1 cgd #include <ddb/db_lex.h>
41 1.1 cgd #include <ddb/db_break.h>
42 1.1 cgd #include <ddb/db_access.h>
43 1.1 cgd
44 1.1 cgd int db_run_mode;
45 1.1 cgd #define STEP_NONE 0
46 1.1 cgd #define STEP_ONCE 1
47 1.1 cgd #define STEP_RETURN 2
48 1.1 cgd #define STEP_CALLT 3
49 1.1 cgd #define STEP_CONTINUE 4
50 1.1 cgd #define STEP_INVISIBLE 5
51 1.1 cgd #define STEP_COUNT 6
52 1.1 cgd
53 1.1 cgd boolean_t db_sstep_print;
54 1.1 cgd int db_loop_count;
55 1.1 cgd int db_call_depth;
56 1.1 cgd
57 1.1 cgd int db_inst_count;
58 1.1 cgd int db_load_count;
59 1.1 cgd int db_store_count;
60 1.1 cgd
61 1.1 cgd #ifndef db_set_single_step
62 1.1 cgd void db_set_single_step(/* db_regs_t *regs */); /* forward */
63 1.1 cgd #endif
64 1.1 cgd #ifndef db_clear_single_step
65 1.1 cgd void db_clear_single_step(/* db_regs_t *regs */);
66 1.1 cgd #endif
67 1.1 cgd
68 1.1 cgd boolean_t
69 1.1 cgd db_stop_at_pc(is_breakpoint)
70 1.1 cgd boolean_t *is_breakpoint;
71 1.1 cgd {
72 1.1 cgd register db_addr_t pc;
73 1.1 cgd register db_breakpoint_t bkpt;
74 1.1 cgd
75 1.1 cgd db_clear_single_step(DDB_REGS);
76 1.1 cgd db_clear_breakpoints();
77 1.1 cgd db_clear_watchpoints();
78 1.1 cgd pc = PC_REGS(DDB_REGS);
79 1.1 cgd
80 1.1 cgd #ifdef FIXUP_PC_AFTER_BREAK
81 1.1 cgd if (*is_breakpoint) {
82 1.1 cgd /*
83 1.1 cgd * Breakpoint trap. Fix up the PC if the
84 1.1 cgd * machine requires it.
85 1.1 cgd */
86 1.1 cgd FIXUP_PC_AFTER_BREAK
87 1.1 cgd pc = PC_REGS(DDB_REGS);
88 1.1 cgd }
89 1.1 cgd #endif
90 1.1 cgd
91 1.1 cgd /*
92 1.1 cgd * Now check for a breakpoint at this address.
93 1.1 cgd */
94 1.1 cgd bkpt = db_find_breakpoint_here(pc);
95 1.1 cgd if (bkpt) {
96 1.1 cgd if (--bkpt->count == 0) {
97 1.1 cgd bkpt->count = bkpt->init_count;
98 1.1 cgd *is_breakpoint = TRUE;
99 1.1 cgd return (TRUE); /* stop here */
100 1.1 cgd }
101 1.1 cgd } else if (*is_breakpoint) {
102 1.3 briggs PC_REGS(&ddb_regs) += BKPT_SIZE;
103 1.1 cgd }
104 1.1 cgd
105 1.1 cgd *is_breakpoint = FALSE;
106 1.1 cgd
107 1.1 cgd if (db_run_mode == STEP_INVISIBLE) {
108 1.1 cgd db_run_mode = STEP_CONTINUE;
109 1.1 cgd return (FALSE); /* continue */
110 1.1 cgd }
111 1.1 cgd if (db_run_mode == STEP_COUNT) {
112 1.1 cgd return (FALSE); /* continue */
113 1.1 cgd }
114 1.1 cgd if (db_run_mode == STEP_ONCE) {
115 1.1 cgd if (--db_loop_count > 0) {
116 1.1 cgd if (db_sstep_print) {
117 1.1 cgd db_printf("\t\t");
118 1.1 cgd db_print_loc_and_inst(pc);
119 1.1 cgd db_printf("\n");
120 1.1 cgd }
121 1.1 cgd return (FALSE); /* continue */
122 1.1 cgd }
123 1.1 cgd }
124 1.1 cgd if (db_run_mode == STEP_RETURN) {
125 1.1 cgd db_expr_t ins = db_get_value(pc, sizeof(int), FALSE);
126 1.1 cgd
127 1.1 cgd /* continue until matching return */
128 1.1 cgd
129 1.1 cgd if (!inst_trap_return(ins) &&
130 1.1 cgd (!inst_return(ins) || --db_call_depth != 0)) {
131 1.1 cgd if (db_sstep_print) {
132 1.1 cgd if (inst_call(ins) || inst_return(ins)) {
133 1.1 cgd register int i;
134 1.1 cgd
135 1.1 cgd db_printf("[after %6d] ", db_inst_count);
136 1.1 cgd for (i = db_call_depth; --i > 0; )
137 1.1 cgd db_printf(" ");
138 1.1 cgd db_print_loc_and_inst(pc);
139 1.1 cgd db_printf("\n");
140 1.1 cgd }
141 1.1 cgd }
142 1.1 cgd if (inst_call(ins))
143 1.1 cgd db_call_depth++;
144 1.1 cgd return (FALSE); /* continue */
145 1.1 cgd }
146 1.1 cgd }
147 1.1 cgd if (db_run_mode == STEP_CALLT) {
148 1.1 cgd db_expr_t ins = db_get_value(pc, sizeof(int), FALSE);
149 1.1 cgd
150 1.1 cgd /* continue until call or return */
151 1.1 cgd
152 1.1 cgd if (!inst_call(ins) &&
153 1.1 cgd !inst_return(ins) &&
154 1.1 cgd !inst_trap_return(ins)) {
155 1.1 cgd return (FALSE); /* continue */
156 1.1 cgd }
157 1.1 cgd }
158 1.1 cgd db_run_mode = STEP_NONE;
159 1.1 cgd return (TRUE);
160 1.1 cgd }
161 1.1 cgd
162 1.1 cgd void
163 1.1 cgd db_restart_at_pc(watchpt)
164 1.1 cgd boolean_t watchpt;
165 1.1 cgd {
166 1.1 cgd register db_addr_t pc = PC_REGS(DDB_REGS);
167 1.1 cgd
168 1.1 cgd if ((db_run_mode == STEP_COUNT) ||
169 1.1 cgd (db_run_mode == STEP_RETURN) ||
170 1.1 cgd (db_run_mode == STEP_CALLT)) {
171 1.1 cgd db_expr_t ins;
172 1.1 cgd
173 1.1 cgd /*
174 1.1 cgd * We are about to execute this instruction,
175 1.1 cgd * so count it now.
176 1.1 cgd */
177 1.1 cgd
178 1.1 cgd ins = db_get_value(pc, sizeof(int), FALSE);
179 1.1 cgd db_inst_count++;
180 1.1 cgd db_load_count += inst_load(ins);
181 1.1 cgd db_store_count += inst_store(ins);
182 1.1 cgd #ifdef SOFTWARE_SSTEP
183 1.1 cgd /* XXX works on mips, but... */
184 1.1 cgd if (inst_branch(ins) || inst_call(ins)) {
185 1.1 cgd ins = db_get_value(next_instr_address(pc,1),
186 1.1 cgd sizeof(int), FALSE);
187 1.1 cgd db_inst_count++;
188 1.1 cgd db_load_count += inst_load(ins);
189 1.1 cgd db_store_count += inst_store(ins);
190 1.1 cgd }
191 1.1 cgd #endif SOFTWARE_SSTEP
192 1.1 cgd }
193 1.1 cgd
194 1.1 cgd if (db_run_mode == STEP_CONTINUE) {
195 1.1 cgd if (watchpt || db_find_breakpoint_here(pc)) {
196 1.1 cgd /*
197 1.1 cgd * Step over breakpoint/watchpoint.
198 1.1 cgd */
199 1.1 cgd db_run_mode = STEP_INVISIBLE;
200 1.1 cgd db_set_single_step(DDB_REGS);
201 1.1 cgd } else {
202 1.1 cgd db_set_breakpoints();
203 1.1 cgd db_set_watchpoints();
204 1.1 cgd }
205 1.1 cgd } else {
206 1.1 cgd db_set_single_step(DDB_REGS);
207 1.1 cgd }
208 1.1 cgd }
209 1.1 cgd
210 1.1 cgd void
211 1.1 cgd db_single_step(regs)
212 1.1 cgd db_regs_t *regs;
213 1.1 cgd {
214 1.1 cgd if (db_run_mode == STEP_CONTINUE) {
215 1.1 cgd db_run_mode = STEP_INVISIBLE;
216 1.1 cgd db_set_single_step(regs);
217 1.1 cgd }
218 1.1 cgd }
219 1.1 cgd
220 1.1 cgd #ifdef SOFTWARE_SSTEP
221 1.1 cgd /*
222 1.1 cgd * Software implementation of single-stepping.
223 1.1 cgd * If your machine does not have a trace mode
224 1.1 cgd * similar to the vax or sun ones you can use
225 1.1 cgd * this implementation, done for the mips.
226 1.1 cgd * Just define the above conditional and provide
227 1.1 cgd * the functions/macros defined below.
228 1.1 cgd *
229 1.1 cgd * extern boolean_t
230 1.1 cgd * inst_branch(), returns true if the instruction might branch
231 1.1 cgd * extern unsigned
232 1.1 cgd * branch_taken(), return the address the instruction might
233 1.1 cgd * branch to
234 1.1 cgd * db_getreg_val(); return the value of a user register,
235 1.1 cgd * as indicated in the hardware instruction
236 1.1 cgd * encoding, e.g. 8 for r8
237 1.1 cgd *
238 1.1 cgd * next_instr_address(pc,bd) returns the address of the first
239 1.1 cgd * instruction following the one at "pc",
240 1.1 cgd * which is either in the taken path of
241 1.1 cgd * the branch (bd==1) or not. This is
242 1.1 cgd * for machines (mips) with branch delays.
243 1.1 cgd *
244 1.1 cgd * A single-step may involve at most 2 breakpoints -
245 1.1 cgd * one for branch-not-taken and one for branch taken.
246 1.1 cgd * If one of these addresses does not already have a breakpoint,
247 1.1 cgd * we allocate a breakpoint and save it here.
248 1.1 cgd * These breakpoints are deleted on return.
249 1.1 cgd */
250 1.1 cgd db_breakpoint_t db_not_taken_bkpt = 0;
251 1.1 cgd db_breakpoint_t db_taken_bkpt = 0;
252 1.1 cgd
253 1.1 cgd void
254 1.1 cgd db_set_single_step(regs)
255 1.1 cgd register db_regs_t *regs;
256 1.1 cgd {
257 1.1 cgd db_addr_t pc = PC_REGS(regs);
258 1.1 cgd register unsigned inst, brpc;
259 1.1 cgd
260 1.1 cgd /*
261 1.1 cgd * User was stopped at pc, e.g. the instruction
262 1.1 cgd * at pc was not executed.
263 1.1 cgd */
264 1.1 cgd inst = db_get_value(pc, sizeof(int), FALSE);
265 1.1 cgd if (inst_branch(inst) || inst_call(inst)) {
266 1.1 cgd extern unsigned getreg_val();
267 1.1 cgd
268 1.1 cgd brpc = branch_taken(inst, pc, getreg_val, regs);
269 1.1 cgd if (brpc != pc) { /* self-branches are hopeless */
270 1.1 cgd db_taken_bkpt = db_set_temp_breakpoint(brpc);
271 1.1 cgd }
272 1.1 cgd pc = next_instr_address(pc,1);
273 1.1 cgd }
274 1.1 cgd pc = next_instr_address(pc,0);
275 1.1 cgd db_not_taken_bkpt = db_set_temp_breakpoint(pc);
276 1.1 cgd }
277 1.1 cgd
278 1.1 cgd void
279 1.1 cgd db_clear_single_step(regs)
280 1.1 cgd db_regs_t *regs;
281 1.1 cgd {
282 1.1 cgd register db_breakpoint_t bkpt;
283 1.1 cgd
284 1.1 cgd if (db_taken_bkpt != 0) {
285 1.1 cgd db_delete_temp_breakpoint(db_taken_bkpt);
286 1.1 cgd db_taken_bkpt = 0;
287 1.1 cgd }
288 1.1 cgd if (db_not_taken_bkpt != 0) {
289 1.1 cgd db_delete_temp_breakpoint(db_not_taken_bkpt);
290 1.1 cgd db_not_taken_bkpt = 0;
291 1.1 cgd }
292 1.1 cgd }
293 1.1 cgd
294 1.1 cgd #endif SOFTWARE_SSTEP
295 1.1 cgd
296 1.1 cgd extern int db_cmd_loop_done;
297 1.1 cgd
298 1.1 cgd /* single-step */
299 1.1 cgd /*ARGSUSED*/
300 1.1 cgd void
301 1.1 cgd db_single_step_cmd(addr, have_addr, count, modif)
302 1.1 cgd db_expr_t addr;
303 1.1 cgd int have_addr;
304 1.1 cgd db_expr_t count;
305 1.1 cgd char * modif;
306 1.1 cgd {
307 1.1 cgd boolean_t print = FALSE;
308 1.1 cgd
309 1.1 cgd if (count == -1)
310 1.1 cgd count = 1;
311 1.1 cgd
312 1.1 cgd if (modif[0] == 'p')
313 1.1 cgd print = TRUE;
314 1.1 cgd
315 1.1 cgd db_run_mode = STEP_ONCE;
316 1.1 cgd db_loop_count = count;
317 1.1 cgd db_sstep_print = print;
318 1.1 cgd db_inst_count = 0;
319 1.1 cgd db_load_count = 0;
320 1.1 cgd db_store_count = 0;
321 1.1 cgd
322 1.1 cgd db_cmd_loop_done = 1;
323 1.1 cgd }
324 1.1 cgd
325 1.1 cgd /* trace and print until call/return */
326 1.1 cgd /*ARGSUSED*/
327 1.1 cgd void
328 1.1 cgd db_trace_until_call_cmd(addr, have_addr, count, modif)
329 1.1 cgd db_expr_t addr;
330 1.1 cgd int have_addr;
331 1.1 cgd db_expr_t count;
332 1.1 cgd char * modif;
333 1.1 cgd {
334 1.1 cgd boolean_t print = FALSE;
335 1.1 cgd
336 1.1 cgd if (modif[0] == 'p')
337 1.1 cgd print = TRUE;
338 1.1 cgd
339 1.1 cgd db_run_mode = STEP_CALLT;
340 1.1 cgd db_sstep_print = print;
341 1.1 cgd db_inst_count = 0;
342 1.1 cgd db_load_count = 0;
343 1.1 cgd db_store_count = 0;
344 1.1 cgd
345 1.1 cgd db_cmd_loop_done = 1;
346 1.1 cgd }
347 1.1 cgd
348 1.1 cgd /*ARGSUSED*/
349 1.1 cgd void
350 1.1 cgd db_trace_until_matching_cmd(addr, have_addr, count, modif)
351 1.1 cgd db_expr_t addr;
352 1.1 cgd int have_addr;
353 1.1 cgd db_expr_t count;
354 1.1 cgd char * modif;
355 1.1 cgd {
356 1.1 cgd boolean_t print = FALSE;
357 1.1 cgd
358 1.1 cgd if (modif[0] == 'p')
359 1.1 cgd print = TRUE;
360 1.1 cgd
361 1.1 cgd db_run_mode = STEP_RETURN;
362 1.1 cgd db_call_depth = 1;
363 1.1 cgd db_sstep_print = print;
364 1.1 cgd db_inst_count = 0;
365 1.1 cgd db_load_count = 0;
366 1.1 cgd db_store_count = 0;
367 1.1 cgd
368 1.1 cgd db_cmd_loop_done = 1;
369 1.1 cgd }
370 1.1 cgd
371 1.1 cgd /* continue */
372 1.1 cgd /*ARGSUSED*/
373 1.1 cgd void
374 1.1 cgd db_continue_cmd(addr, have_addr, count, modif)
375 1.1 cgd db_expr_t addr;
376 1.1 cgd int have_addr;
377 1.1 cgd db_expr_t count;
378 1.1 cgd char * modif;
379 1.1 cgd {
380 1.1 cgd if (modif[0] == 'c')
381 1.1 cgd db_run_mode = STEP_COUNT;
382 1.1 cgd else
383 1.1 cgd db_run_mode = STEP_CONTINUE;
384 1.1 cgd db_inst_count = 0;
385 1.1 cgd db_load_count = 0;
386 1.1 cgd db_store_count = 0;
387 1.1 cgd
388 1.1 cgd db_cmd_loop_done = 1;
389 1.1 cgd }
390