db_run.c revision 1.15 1 1.15 jonathan /* $NetBSD: db_run.c,v 1.15 1998/07/04 22:18:48 jonathan Exp $ */
2 1.5 cgd
3 1.1 cgd /*
4 1.1 cgd * Mach Operating System
5 1.11 thorpej * Copyright (c) 1993-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.15 jonathan
36 1.15 jonathan #include "opt_ddb.h"
37 1.15 jonathan
38 1.7 mycroft #include <sys/param.h>
39 1.7 mycroft #include <sys/proc.h>
40 1.7 mycroft
41 1.7 mycroft #include <machine/db_machdep.h>
42 1.7 mycroft
43 1.6 mycroft #include <ddb/db_run.h>
44 1.14 pk #include <ddb/db_access.h>
45 1.14 pk #include <ddb/db_break.h>
46 1.14 pk
47 1.14 pk #ifdef SOFTWARE_SSTEP
48 1.14 pk static void db_set_temp_breakpoint __P((db_breakpoint_t, db_addr_t));
49 1.14 pk static void db_delete_temp_breakpoint __P((db_breakpoint_t));
50 1.14 pk static struct db_breakpoint db_not_taken_bkpt;
51 1.14 pk static struct db_breakpoint db_taken_bkpt;
52 1.14 pk #endif
53 1.14 pk
54 1.14 pk #if defined(DDB)
55 1.1 cgd #include <ddb/db_lex.h>
56 1.8 christos #include <ddb/db_watch.h>
57 1.8 christos #include <ddb/db_output.h>
58 1.8 christos #include <ddb/db_sym.h>
59 1.8 christos #include <ddb/db_extern.h>
60 1.1 cgd
61 1.1 cgd int db_run_mode;
62 1.1 cgd #define STEP_NONE 0
63 1.1 cgd #define STEP_ONCE 1
64 1.1 cgd #define STEP_RETURN 2
65 1.1 cgd #define STEP_CALLT 3
66 1.1 cgd #define STEP_CONTINUE 4
67 1.1 cgd #define STEP_INVISIBLE 5
68 1.1 cgd #define STEP_COUNT 6
69 1.1 cgd
70 1.1 cgd boolean_t db_sstep_print;
71 1.1 cgd int db_loop_count;
72 1.1 cgd int db_call_depth;
73 1.1 cgd
74 1.1 cgd boolean_t
75 1.6 mycroft db_stop_at_pc(regs, is_breakpoint)
76 1.6 mycroft db_regs_t *regs;
77 1.1 cgd boolean_t *is_breakpoint;
78 1.1 cgd {
79 1.1 cgd register db_addr_t pc;
80 1.1 cgd register db_breakpoint_t bkpt;
81 1.1 cgd
82 1.13 pk pc = PC_REGS(regs);
83 1.13 pk
84 1.13 pk #ifdef SOFTWARE_SSTEP
85 1.13 pk /*
86 1.13 pk * If we stopped at one of the single-step breakpoints,
87 1.13 pk * say it's not really a breakpoint so that
88 1.13 pk * we don't skip over the real instruction.
89 1.13 pk */
90 1.14 pk if (db_taken_bkpt.address == pc || db_not_taken_bkpt.address == pc)
91 1.13 pk *is_breakpoint = FALSE;
92 1.13 pk #endif
93 1.13 pk
94 1.6 mycroft db_clear_single_step(regs);
95 1.1 cgd db_clear_breakpoints();
96 1.1 cgd db_clear_watchpoints();
97 1.1 cgd
98 1.1 cgd #ifdef FIXUP_PC_AFTER_BREAK
99 1.1 cgd if (*is_breakpoint) {
100 1.1 cgd /*
101 1.1 cgd * Breakpoint trap. Fix up the PC if the
102 1.1 cgd * machine requires it.
103 1.1 cgd */
104 1.10 gwr FIXUP_PC_AFTER_BREAK(regs);
105 1.6 mycroft pc = PC_REGS(regs);
106 1.1 cgd }
107 1.1 cgd #endif
108 1.1 cgd
109 1.1 cgd /*
110 1.1 cgd * Now check for a breakpoint at this address.
111 1.1 cgd */
112 1.1 cgd bkpt = db_find_breakpoint_here(pc);
113 1.1 cgd if (bkpt) {
114 1.1 cgd if (--bkpt->count == 0) {
115 1.1 cgd bkpt->count = bkpt->init_count;
116 1.1 cgd *is_breakpoint = TRUE;
117 1.1 cgd return (TRUE); /* stop here */
118 1.1 cgd }
119 1.1 cgd } else if (*is_breakpoint) {
120 1.12 pk #ifdef PC_ADVANCE
121 1.12 pk PC_ADVANCE(regs);
122 1.12 pk #else
123 1.6 mycroft PC_REGS(regs) += BKPT_SIZE;
124 1.12 pk #endif
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd *is_breakpoint = FALSE;
128 1.1 cgd
129 1.1 cgd if (db_run_mode == STEP_INVISIBLE) {
130 1.1 cgd db_run_mode = STEP_CONTINUE;
131 1.1 cgd return (FALSE); /* continue */
132 1.1 cgd }
133 1.1 cgd if (db_run_mode == STEP_COUNT) {
134 1.1 cgd return (FALSE); /* continue */
135 1.1 cgd }
136 1.1 cgd if (db_run_mode == STEP_ONCE) {
137 1.1 cgd if (--db_loop_count > 0) {
138 1.1 cgd if (db_sstep_print) {
139 1.1 cgd db_printf("\t\t");
140 1.1 cgd db_print_loc_and_inst(pc);
141 1.1 cgd db_printf("\n");
142 1.1 cgd }
143 1.1 cgd return (FALSE); /* continue */
144 1.1 cgd }
145 1.1 cgd }
146 1.1 cgd if (db_run_mode == STEP_RETURN) {
147 1.1 cgd db_expr_t ins = db_get_value(pc, sizeof(int), FALSE);
148 1.1 cgd
149 1.1 cgd /* continue until matching return */
150 1.1 cgd
151 1.1 cgd if (!inst_trap_return(ins) &&
152 1.1 cgd (!inst_return(ins) || --db_call_depth != 0)) {
153 1.1 cgd if (db_sstep_print) {
154 1.1 cgd if (inst_call(ins) || inst_return(ins)) {
155 1.1 cgd register int i;
156 1.1 cgd
157 1.1 cgd db_printf("[after %6d] ", db_inst_count);
158 1.1 cgd for (i = db_call_depth; --i > 0; )
159 1.1 cgd db_printf(" ");
160 1.1 cgd db_print_loc_and_inst(pc);
161 1.1 cgd db_printf("\n");
162 1.1 cgd }
163 1.1 cgd }
164 1.1 cgd if (inst_call(ins))
165 1.1 cgd db_call_depth++;
166 1.1 cgd return (FALSE); /* continue */
167 1.1 cgd }
168 1.1 cgd }
169 1.1 cgd if (db_run_mode == STEP_CALLT) {
170 1.1 cgd db_expr_t ins = db_get_value(pc, sizeof(int), FALSE);
171 1.1 cgd
172 1.1 cgd /* continue until call or return */
173 1.1 cgd
174 1.1 cgd if (!inst_call(ins) &&
175 1.1 cgd !inst_return(ins) &&
176 1.1 cgd !inst_trap_return(ins)) {
177 1.1 cgd return (FALSE); /* continue */
178 1.1 cgd }
179 1.1 cgd }
180 1.1 cgd db_run_mode = STEP_NONE;
181 1.1 cgd return (TRUE);
182 1.1 cgd }
183 1.1 cgd
184 1.1 cgd void
185 1.6 mycroft db_restart_at_pc(regs, watchpt)
186 1.6 mycroft db_regs_t *regs;
187 1.1 cgd boolean_t watchpt;
188 1.1 cgd {
189 1.6 mycroft register db_addr_t pc = PC_REGS(regs);
190 1.1 cgd
191 1.1 cgd if ((db_run_mode == STEP_COUNT) ||
192 1.1 cgd (db_run_mode == STEP_RETURN) ||
193 1.1 cgd (db_run_mode == STEP_CALLT)) {
194 1.1 cgd db_expr_t ins;
195 1.1 cgd
196 1.1 cgd /*
197 1.1 cgd * We are about to execute this instruction,
198 1.1 cgd * so count it now.
199 1.1 cgd */
200 1.1 cgd ins = db_get_value(pc, sizeof(int), FALSE);
201 1.1 cgd db_inst_count++;
202 1.1 cgd db_load_count += inst_load(ins);
203 1.1 cgd db_store_count += inst_store(ins);
204 1.11 thorpej
205 1.11 thorpej #ifdef SOFTWARE_SSTEP
206 1.11 thorpej /*
207 1.11 thorpej * Account for instructions in delay slots.
208 1.11 thorpej */
209 1.11 thorpej {
210 1.11 thorpej db_addr_t brpc;
211 1.11 thorpej
212 1.11 thorpej brpc = next_instr_address(pc, TRUE);
213 1.11 thorpej if ((brpc != pc) && (inst_branch(ins) || inst_call(ins))) {
214 1.11 thorpej ins = db_get_value(brpc, sizeof(int), FALSE);
215 1.11 thorpej db_inst_count++;
216 1.11 thorpej db_load_count += inst_load(ins);
217 1.11 thorpej db_store_count += inst_store(ins);
218 1.11 thorpej }
219 1.1 cgd }
220 1.9 cgd #endif
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd if (db_run_mode == STEP_CONTINUE) {
224 1.1 cgd if (watchpt || db_find_breakpoint_here(pc)) {
225 1.1 cgd /*
226 1.1 cgd * Step over breakpoint/watchpoint.
227 1.1 cgd */
228 1.1 cgd db_run_mode = STEP_INVISIBLE;
229 1.6 mycroft db_set_single_step(regs);
230 1.1 cgd } else {
231 1.1 cgd db_set_breakpoints();
232 1.1 cgd db_set_watchpoints();
233 1.1 cgd }
234 1.1 cgd } else {
235 1.6 mycroft db_set_single_step(regs);
236 1.1 cgd }
237 1.1 cgd }
238 1.1 cgd
239 1.1 cgd void
240 1.1 cgd db_single_step(regs)
241 1.1 cgd db_regs_t *regs;
242 1.1 cgd {
243 1.1 cgd if (db_run_mode == STEP_CONTINUE) {
244 1.1 cgd db_run_mode = STEP_INVISIBLE;
245 1.1 cgd db_set_single_step(regs);
246 1.1 cgd }
247 1.1 cgd }
248 1.1 cgd
249 1.14 pk
250 1.14 pk extern int db_cmd_loop_done;
251 1.14 pk
252 1.14 pk /* single-step */
253 1.14 pk /*ARGSUSED*/
254 1.14 pk void
255 1.14 pk db_single_step_cmd(addr, have_addr, count, modif)
256 1.14 pk db_expr_t addr;
257 1.14 pk int have_addr;
258 1.14 pk db_expr_t count;
259 1.14 pk char * modif;
260 1.14 pk {
261 1.14 pk boolean_t print = FALSE;
262 1.14 pk
263 1.14 pk if (count == -1)
264 1.14 pk count = 1;
265 1.14 pk
266 1.14 pk if (modif[0] == 'p')
267 1.14 pk print = TRUE;
268 1.14 pk
269 1.14 pk db_run_mode = STEP_ONCE;
270 1.14 pk db_loop_count = count;
271 1.14 pk db_sstep_print = print;
272 1.14 pk db_inst_count = 0;
273 1.14 pk db_load_count = 0;
274 1.14 pk db_store_count = 0;
275 1.14 pk
276 1.14 pk db_cmd_loop_done = 1;
277 1.14 pk }
278 1.14 pk
279 1.14 pk /* trace and print until call/return */
280 1.14 pk /*ARGSUSED*/
281 1.14 pk void
282 1.14 pk db_trace_until_call_cmd(addr, have_addr, count, modif)
283 1.14 pk db_expr_t addr;
284 1.14 pk int have_addr;
285 1.14 pk db_expr_t count;
286 1.14 pk char * modif;
287 1.14 pk {
288 1.14 pk boolean_t print = FALSE;
289 1.14 pk
290 1.14 pk if (modif[0] == 'p')
291 1.14 pk print = TRUE;
292 1.14 pk
293 1.14 pk db_run_mode = STEP_CALLT;
294 1.14 pk db_sstep_print = print;
295 1.14 pk db_inst_count = 0;
296 1.14 pk db_load_count = 0;
297 1.14 pk db_store_count = 0;
298 1.14 pk
299 1.14 pk db_cmd_loop_done = 1;
300 1.14 pk }
301 1.14 pk
302 1.14 pk /*ARGSUSED*/
303 1.14 pk void
304 1.14 pk db_trace_until_matching_cmd(addr, have_addr, count, modif)
305 1.14 pk db_expr_t addr;
306 1.14 pk int have_addr;
307 1.14 pk db_expr_t count;
308 1.14 pk char * modif;
309 1.14 pk {
310 1.14 pk boolean_t print = FALSE;
311 1.14 pk
312 1.14 pk if (modif[0] == 'p')
313 1.14 pk print = TRUE;
314 1.14 pk
315 1.14 pk db_run_mode = STEP_RETURN;
316 1.14 pk db_call_depth = 1;
317 1.14 pk db_sstep_print = print;
318 1.14 pk db_inst_count = 0;
319 1.14 pk db_load_count = 0;
320 1.14 pk db_store_count = 0;
321 1.14 pk
322 1.14 pk db_cmd_loop_done = 1;
323 1.14 pk }
324 1.14 pk
325 1.14 pk /* continue */
326 1.14 pk /*ARGSUSED*/
327 1.14 pk void
328 1.14 pk db_continue_cmd(addr, have_addr, count, modif)
329 1.14 pk db_expr_t addr;
330 1.14 pk int have_addr;
331 1.14 pk db_expr_t count;
332 1.14 pk char * modif;
333 1.14 pk {
334 1.14 pk if (modif[0] == 'c')
335 1.14 pk db_run_mode = STEP_COUNT;
336 1.14 pk else
337 1.14 pk db_run_mode = STEP_CONTINUE;
338 1.14 pk db_inst_count = 0;
339 1.14 pk db_load_count = 0;
340 1.14 pk db_store_count = 0;
341 1.14 pk
342 1.14 pk db_cmd_loop_done = 1;
343 1.14 pk }
344 1.14 pk #endif /* DDB */
345 1.14 pk
346 1.11 thorpej #ifdef SOFTWARE_SSTEP
347 1.1 cgd /*
348 1.1 cgd * Software implementation of single-stepping.
349 1.1 cgd * If your machine does not have a trace mode
350 1.1 cgd * similar to the vax or sun ones you can use
351 1.1 cgd * this implementation, done for the mips.
352 1.1 cgd * Just define the above conditional and provide
353 1.1 cgd * the functions/macros defined below.
354 1.1 cgd *
355 1.11 thorpej * boolean_t inst_branch(int inst)
356 1.11 thorpej * boolean_t inst_call(int inst)
357 1.11 thorpej * returns TRUE if the instruction might branch
358 1.11 thorpej *
359 1.11 thorpej * boolean_t inst_unconditional_flow_transfer(int inst)
360 1.11 thorpej * returns TRUE if the instruction is an unconditional
361 1.11 thorpej * transter of flow (i.e. unconditional branch)
362 1.11 thorpej *
363 1.11 thorpej * db_addr_t branch_taken(int inst, db_addr_t pc, db_regs_t *regs)
364 1.11 thorpej * returns the target address of the branch
365 1.11 thorpej *
366 1.11 thorpej * db_addr_t next_instr_address(db_addr_t pc, boolean_t bd)
367 1.11 thorpej * returns the address of the first instruction following the
368 1.11 thorpej * one at "pc", which is either in the taken path of the branch
369 1.11 thorpej * (bd == TRUE) or not. This is for machines (e.g. mips) with
370 1.11 thorpej * branch delays.
371 1.1 cgd *
372 1.1 cgd * A single-step may involve at most 2 breakpoints -
373 1.1 cgd * one for branch-not-taken and one for branch taken.
374 1.1 cgd * If one of these addresses does not already have a breakpoint,
375 1.1 cgd * we allocate a breakpoint and save it here.
376 1.1 cgd * These breakpoints are deleted on return.
377 1.1 cgd */
378 1.1 cgd
379 1.14 pk #if !defined(DDB)
380 1.14 pk /* XXX - don't check for existing breakpoints in KGDB-only case */
381 1.14 pk #define db_find_breakpoint_here(pc) (0)
382 1.14 pk #endif
383 1.14 pk
384 1.1 cgd void
385 1.1 cgd db_set_single_step(regs)
386 1.1 cgd register db_regs_t *regs;
387 1.1 cgd {
388 1.13 pk db_addr_t pc = PC_REGS(regs), brpc = pc;
389 1.11 thorpej boolean_t unconditional;
390 1.11 thorpej unsigned int inst;
391 1.1 cgd
392 1.1 cgd /*
393 1.1 cgd * User was stopped at pc, e.g. the instruction
394 1.1 cgd * at pc was not executed.
395 1.1 cgd */
396 1.1 cgd inst = db_get_value(pc, sizeof(int), FALSE);
397 1.1 cgd if (inst_branch(inst) || inst_call(inst)) {
398 1.11 thorpej brpc = branch_taken(inst, pc, regs);
399 1.11 thorpej if (brpc != pc) { /* self-branches are hopeless */
400 1.14 pk db_set_temp_breakpoint(&db_taken_bkpt, brpc);
401 1.11 thorpej } else
402 1.14 pk db_taken_bkpt.address = 0;
403 1.11 thorpej pc = next_instr_address(pc, TRUE);
404 1.11 thorpej }
405 1.11 thorpej
406 1.11 thorpej /*
407 1.11 thorpej * Check if this control flow instruction is an
408 1.11 thorpej * unconditional transfer.
409 1.11 thorpej */
410 1.11 thorpej unconditional = inst_unconditional_flow_transfer(inst);
411 1.11 thorpej
412 1.11 thorpej pc = next_instr_address(pc, FALSE);
413 1.1 cgd
414 1.11 thorpej /*
415 1.11 thorpej * We only set the sequential breakpoint if previous
416 1.11 thorpej * instruction was not an unconditional change of flow
417 1.11 thorpej * control. If the previous instruction is an
418 1.11 thorpej * unconditional change of flow control, setting a
419 1.11 thorpej * breakpoint in the next sequential location may set
420 1.11 thorpej * a breakpoint in data or in another routine, which
421 1.11 thorpej * could screw up in either the program or the debugger.
422 1.11 thorpej * (Consider, for instance, that the next sequential
423 1.11 thorpej * instruction is the start of a routine needed by the
424 1.11 thorpej * debugger.)
425 1.13 pk *
426 1.13 pk * Also, don't set both the taken and not-taken breakpoints
427 1.13 pk * in the same place even if the MD code would otherwise
428 1.13 pk * have us do so.
429 1.11 thorpej */
430 1.13 pk if (unconditional == FALSE &&
431 1.13 pk db_find_breakpoint_here(pc) == 0 &&
432 1.13 pk pc != brpc)
433 1.14 pk db_set_temp_breakpoint(&db_not_taken_bkpt, pc);
434 1.11 thorpej else
435 1.14 pk db_not_taken_bkpt.address = 0;
436 1.1 cgd }
437 1.1 cgd
438 1.1 cgd void
439 1.1 cgd db_clear_single_step(regs)
440 1.1 cgd db_regs_t *regs;
441 1.1 cgd {
442 1.1 cgd
443 1.14 pk if (db_taken_bkpt.address != 0)
444 1.14 pk db_delete_temp_breakpoint(&db_taken_bkpt);
445 1.1 cgd
446 1.14 pk if (db_not_taken_bkpt.address != 0)
447 1.14 pk db_delete_temp_breakpoint(&db_not_taken_bkpt);
448 1.1 cgd }
449 1.1 cgd
450 1.1 cgd void
451 1.14 pk db_set_temp_breakpoint(bkpt, addr)
452 1.14 pk db_breakpoint_t bkpt;
453 1.14 pk db_addr_t addr;
454 1.1 cgd {
455 1.1 cgd
456 1.14 pk bkpt->map = NULL;
457 1.14 pk bkpt->address = addr;
458 1.14 pk /* bkpt->flags = BKPT_TEMP; - this is not used */
459 1.14 pk bkpt->init_count = 1;
460 1.14 pk bkpt->count = 1;
461 1.1 cgd
462 1.14 pk bkpt->bkpt_inst = db_get_value(bkpt->address, BKPT_SIZE, FALSE);
463 1.14 pk db_put_value(bkpt->address, BKPT_SIZE, BKPT_SET(bkpt->bkpt_inst));
464 1.1 cgd }
465 1.1 cgd
466 1.1 cgd void
467 1.14 pk db_delete_temp_breakpoint(bkpt)
468 1.14 pk db_breakpoint_t bkpt;
469 1.1 cgd {
470 1.14 pk db_put_value(bkpt->address, BKPT_SIZE, bkpt->bkpt_inst);
471 1.14 pk bkpt->address = 0;
472 1.1 cgd }
473 1.1 cgd
474 1.14 pk #endif /* SOFTWARE_SSTEP */
475