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