db_run.c revision 1.16 1 1.16 mycroft /* $NetBSD: db_run.c,v 1.16 1998/11/25 06:38:03 mycroft 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.16 mycroft if ((brpc != pc) &&
214 1.16 mycroft (inst_branch(ins) || inst_call(ins) || inst_return(ins))) {
215 1.11 thorpej ins = db_get_value(brpc, sizeof(int), FALSE);
216 1.11 thorpej db_inst_count++;
217 1.11 thorpej db_load_count += inst_load(ins);
218 1.11 thorpej db_store_count += inst_store(ins);
219 1.11 thorpej }
220 1.1 cgd }
221 1.9 cgd #endif
222 1.1 cgd }
223 1.1 cgd
224 1.1 cgd if (db_run_mode == STEP_CONTINUE) {
225 1.1 cgd if (watchpt || db_find_breakpoint_here(pc)) {
226 1.1 cgd /*
227 1.1 cgd * Step over breakpoint/watchpoint.
228 1.1 cgd */
229 1.1 cgd db_run_mode = STEP_INVISIBLE;
230 1.6 mycroft db_set_single_step(regs);
231 1.1 cgd } else {
232 1.1 cgd db_set_breakpoints();
233 1.1 cgd db_set_watchpoints();
234 1.1 cgd }
235 1.1 cgd } else {
236 1.6 mycroft db_set_single_step(regs);
237 1.1 cgd }
238 1.1 cgd }
239 1.1 cgd
240 1.1 cgd void
241 1.1 cgd db_single_step(regs)
242 1.1 cgd db_regs_t *regs;
243 1.1 cgd {
244 1.1 cgd if (db_run_mode == STEP_CONTINUE) {
245 1.1 cgd db_run_mode = STEP_INVISIBLE;
246 1.1 cgd db_set_single_step(regs);
247 1.1 cgd }
248 1.1 cgd }
249 1.1 cgd
250 1.14 pk
251 1.14 pk extern int db_cmd_loop_done;
252 1.14 pk
253 1.14 pk /* single-step */
254 1.14 pk /*ARGSUSED*/
255 1.14 pk void
256 1.14 pk db_single_step_cmd(addr, have_addr, count, modif)
257 1.14 pk db_expr_t addr;
258 1.14 pk int have_addr;
259 1.14 pk db_expr_t count;
260 1.14 pk char * modif;
261 1.14 pk {
262 1.14 pk boolean_t print = FALSE;
263 1.14 pk
264 1.14 pk if (count == -1)
265 1.14 pk count = 1;
266 1.14 pk
267 1.14 pk if (modif[0] == 'p')
268 1.14 pk print = TRUE;
269 1.14 pk
270 1.14 pk db_run_mode = STEP_ONCE;
271 1.14 pk db_loop_count = count;
272 1.14 pk db_sstep_print = print;
273 1.14 pk db_inst_count = 0;
274 1.14 pk db_load_count = 0;
275 1.14 pk db_store_count = 0;
276 1.14 pk
277 1.14 pk db_cmd_loop_done = 1;
278 1.14 pk }
279 1.14 pk
280 1.14 pk /* trace and print until call/return */
281 1.14 pk /*ARGSUSED*/
282 1.14 pk void
283 1.14 pk db_trace_until_call_cmd(addr, have_addr, count, modif)
284 1.14 pk db_expr_t addr;
285 1.14 pk int have_addr;
286 1.14 pk db_expr_t count;
287 1.14 pk char * modif;
288 1.14 pk {
289 1.14 pk boolean_t print = FALSE;
290 1.14 pk
291 1.14 pk if (modif[0] == 'p')
292 1.14 pk print = TRUE;
293 1.14 pk
294 1.14 pk db_run_mode = STEP_CALLT;
295 1.14 pk db_sstep_print = print;
296 1.14 pk db_inst_count = 0;
297 1.14 pk db_load_count = 0;
298 1.14 pk db_store_count = 0;
299 1.14 pk
300 1.14 pk db_cmd_loop_done = 1;
301 1.14 pk }
302 1.14 pk
303 1.14 pk /*ARGSUSED*/
304 1.14 pk void
305 1.14 pk db_trace_until_matching_cmd(addr, have_addr, count, modif)
306 1.14 pk db_expr_t addr;
307 1.14 pk int have_addr;
308 1.14 pk db_expr_t count;
309 1.14 pk char * modif;
310 1.14 pk {
311 1.14 pk boolean_t print = FALSE;
312 1.14 pk
313 1.14 pk if (modif[0] == 'p')
314 1.14 pk print = TRUE;
315 1.14 pk
316 1.14 pk db_run_mode = STEP_RETURN;
317 1.14 pk db_call_depth = 1;
318 1.14 pk db_sstep_print = print;
319 1.14 pk db_inst_count = 0;
320 1.14 pk db_load_count = 0;
321 1.14 pk db_store_count = 0;
322 1.14 pk
323 1.14 pk db_cmd_loop_done = 1;
324 1.14 pk }
325 1.14 pk
326 1.14 pk /* continue */
327 1.14 pk /*ARGSUSED*/
328 1.14 pk void
329 1.14 pk db_continue_cmd(addr, have_addr, count, modif)
330 1.14 pk db_expr_t addr;
331 1.14 pk int have_addr;
332 1.14 pk db_expr_t count;
333 1.14 pk char * modif;
334 1.14 pk {
335 1.14 pk if (modif[0] == 'c')
336 1.14 pk db_run_mode = STEP_COUNT;
337 1.14 pk else
338 1.14 pk db_run_mode = STEP_CONTINUE;
339 1.14 pk db_inst_count = 0;
340 1.14 pk db_load_count = 0;
341 1.14 pk db_store_count = 0;
342 1.14 pk
343 1.14 pk db_cmd_loop_done = 1;
344 1.14 pk }
345 1.14 pk #endif /* DDB */
346 1.14 pk
347 1.11 thorpej #ifdef SOFTWARE_SSTEP
348 1.1 cgd /*
349 1.1 cgd * Software implementation of single-stepping.
350 1.1 cgd * If your machine does not have a trace mode
351 1.1 cgd * similar to the vax or sun ones you can use
352 1.1 cgd * this implementation, done for the mips.
353 1.1 cgd * Just define the above conditional and provide
354 1.1 cgd * the functions/macros defined below.
355 1.1 cgd *
356 1.11 thorpej * boolean_t inst_branch(int inst)
357 1.11 thorpej * boolean_t inst_call(int inst)
358 1.11 thorpej * returns TRUE if the instruction might branch
359 1.11 thorpej *
360 1.11 thorpej * boolean_t inst_unconditional_flow_transfer(int inst)
361 1.11 thorpej * returns TRUE if the instruction is an unconditional
362 1.11 thorpej * transter of flow (i.e. unconditional branch)
363 1.11 thorpej *
364 1.11 thorpej * db_addr_t branch_taken(int inst, db_addr_t pc, db_regs_t *regs)
365 1.11 thorpej * returns the target address of the branch
366 1.11 thorpej *
367 1.11 thorpej * db_addr_t next_instr_address(db_addr_t pc, boolean_t bd)
368 1.11 thorpej * returns the address of the first instruction following the
369 1.11 thorpej * one at "pc", which is either in the taken path of the branch
370 1.11 thorpej * (bd == TRUE) or not. This is for machines (e.g. mips) with
371 1.11 thorpej * branch delays.
372 1.1 cgd *
373 1.1 cgd * A single-step may involve at most 2 breakpoints -
374 1.1 cgd * one for branch-not-taken and one for branch taken.
375 1.1 cgd * If one of these addresses does not already have a breakpoint,
376 1.1 cgd * we allocate a breakpoint and save it here.
377 1.1 cgd * These breakpoints are deleted on return.
378 1.1 cgd */
379 1.1 cgd
380 1.14 pk #if !defined(DDB)
381 1.14 pk /* XXX - don't check for existing breakpoints in KGDB-only case */
382 1.14 pk #define db_find_breakpoint_here(pc) (0)
383 1.14 pk #endif
384 1.14 pk
385 1.1 cgd void
386 1.1 cgd db_set_single_step(regs)
387 1.1 cgd register db_regs_t *regs;
388 1.1 cgd {
389 1.13 pk db_addr_t pc = PC_REGS(regs), brpc = pc;
390 1.11 thorpej boolean_t unconditional;
391 1.11 thorpej unsigned int inst;
392 1.1 cgd
393 1.1 cgd /*
394 1.1 cgd * User was stopped at pc, e.g. the instruction
395 1.1 cgd * at pc was not executed.
396 1.1 cgd */
397 1.1 cgd inst = db_get_value(pc, sizeof(int), FALSE);
398 1.16 mycroft if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) {
399 1.11 thorpej brpc = branch_taken(inst, pc, regs);
400 1.11 thorpej if (brpc != pc) { /* self-branches are hopeless */
401 1.14 pk db_set_temp_breakpoint(&db_taken_bkpt, brpc);
402 1.11 thorpej } else
403 1.14 pk db_taken_bkpt.address = 0;
404 1.11 thorpej pc = next_instr_address(pc, TRUE);
405 1.11 thorpej }
406 1.11 thorpej
407 1.11 thorpej /*
408 1.11 thorpej * Check if this control flow instruction is an
409 1.11 thorpej * unconditional transfer.
410 1.11 thorpej */
411 1.11 thorpej unconditional = inst_unconditional_flow_transfer(inst);
412 1.11 thorpej
413 1.11 thorpej pc = next_instr_address(pc, FALSE);
414 1.1 cgd
415 1.11 thorpej /*
416 1.11 thorpej * We only set the sequential breakpoint if previous
417 1.11 thorpej * instruction was not an unconditional change of flow
418 1.11 thorpej * control. If the previous instruction is an
419 1.11 thorpej * unconditional change of flow control, setting a
420 1.11 thorpej * breakpoint in the next sequential location may set
421 1.11 thorpej * a breakpoint in data or in another routine, which
422 1.11 thorpej * could screw up in either the program or the debugger.
423 1.11 thorpej * (Consider, for instance, that the next sequential
424 1.11 thorpej * instruction is the start of a routine needed by the
425 1.11 thorpej * debugger.)
426 1.13 pk *
427 1.13 pk * Also, don't set both the taken and not-taken breakpoints
428 1.13 pk * in the same place even if the MD code would otherwise
429 1.13 pk * have us do so.
430 1.11 thorpej */
431 1.13 pk if (unconditional == FALSE &&
432 1.13 pk db_find_breakpoint_here(pc) == 0 &&
433 1.13 pk pc != brpc)
434 1.14 pk db_set_temp_breakpoint(&db_not_taken_bkpt, pc);
435 1.11 thorpej else
436 1.14 pk db_not_taken_bkpt.address = 0;
437 1.1 cgd }
438 1.1 cgd
439 1.1 cgd void
440 1.1 cgd db_clear_single_step(regs)
441 1.1 cgd db_regs_t *regs;
442 1.1 cgd {
443 1.1 cgd
444 1.14 pk if (db_taken_bkpt.address != 0)
445 1.14 pk db_delete_temp_breakpoint(&db_taken_bkpt);
446 1.1 cgd
447 1.14 pk if (db_not_taken_bkpt.address != 0)
448 1.14 pk db_delete_temp_breakpoint(&db_not_taken_bkpt);
449 1.1 cgd }
450 1.1 cgd
451 1.1 cgd void
452 1.14 pk db_set_temp_breakpoint(bkpt, addr)
453 1.14 pk db_breakpoint_t bkpt;
454 1.14 pk db_addr_t addr;
455 1.1 cgd {
456 1.1 cgd
457 1.14 pk bkpt->map = NULL;
458 1.14 pk bkpt->address = addr;
459 1.14 pk /* bkpt->flags = BKPT_TEMP; - this is not used */
460 1.14 pk bkpt->init_count = 1;
461 1.14 pk bkpt->count = 1;
462 1.1 cgd
463 1.14 pk bkpt->bkpt_inst = db_get_value(bkpt->address, BKPT_SIZE, FALSE);
464 1.14 pk db_put_value(bkpt->address, BKPT_SIZE, BKPT_SET(bkpt->bkpt_inst));
465 1.1 cgd }
466 1.1 cgd
467 1.1 cgd void
468 1.14 pk db_delete_temp_breakpoint(bkpt)
469 1.14 pk db_breakpoint_t bkpt;
470 1.1 cgd {
471 1.14 pk db_put_value(bkpt->address, BKPT_SIZE, bkpt->bkpt_inst);
472 1.14 pk bkpt->address = 0;
473 1.1 cgd }
474 1.1 cgd
475 1.14 pk #endif /* SOFTWARE_SSTEP */
476