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