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