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