No empty .Rs/.Re
[netbsd-mini2440.git] / sys / ddb / db_run.c
blob1c5a26392f353d02f8f4896dc0e2ee17bf9b973a
1 /* $NetBSD: db_run.c,v 1.30 2007/02/22 06:41:01 thorpej Exp $ */
3 /*
4 * Mach Operating System
5 * Copyright (c) 1993-1990 Carnegie Mellon University
6 * All Rights Reserved.
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
28 * Author: David B. Golub, Carnegie Mellon University
29 * Date: 7/90
33 * Commands to run process.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: db_run.c,v 1.30 2007/02/22 06:41:01 thorpej Exp $");
39 #include "opt_ddb.h"
41 #include <sys/param.h>
42 #include <sys/proc.h>
44 #include <machine/db_machdep.h>
46 #include <ddb/db_run.h>
47 #include <ddb/db_access.h>
48 #include <ddb/db_break.h>
50 int db_inst_count;
51 int db_load_count;
52 int db_store_count;
54 #ifdef SOFTWARE_SSTEP
55 static void db_set_temp_breakpoint(db_breakpoint_t, db_addr_t);
56 static void db_delete_temp_breakpoint(db_breakpoint_t);
57 static struct db_breakpoint db_not_taken_bkpt;
58 static struct db_breakpoint db_taken_bkpt;
59 #endif
61 #if defined(DDB)
62 #include <ddb/db_lex.h>
63 #include <ddb/db_watch.h>
64 #include <ddb/db_output.h>
65 #include <ddb/db_sym.h>
66 #include <ddb/db_extern.h>
68 static int db_run_mode;
69 #define STEP_NONE 0
70 #define STEP_ONCE 1
71 #define STEP_RETURN 2
72 #define STEP_CALLT 3
73 #define STEP_CONTINUE 4
74 #define STEP_INVISIBLE 5
75 #define STEP_COUNT 6
77 static bool db_sstep_print;
78 static int db_loop_count;
79 static int db_call_depth;
81 bool
82 db_stop_at_pc(db_regs_t *regs, bool *is_breakpoint)
84 db_addr_t pc;
85 db_breakpoint_t bkpt;
87 pc = PC_REGS(regs);
89 #ifdef FIXUP_PC_AFTER_BREAK
90 if (*is_breakpoint) {
92 * Breakpoint trap. Regardless if we treat this as a
93 * real breakpoint (e.g. software single-step), fix up the PC.
95 FIXUP_PC_AFTER_BREAK(regs);
96 pc = PC_REGS(regs);
98 #endif
100 #ifdef SOFTWARE_SSTEP
102 * If we stopped at one of the single-step breakpoints, say it's not
103 * really a breakpoint so that we don't skip over the real instruction.
105 if (db_taken_bkpt.address == pc || db_not_taken_bkpt.address == pc)
106 *is_breakpoint = false;
107 #endif /* SOFTWARE_SSTEP */
109 db_clear_single_step(regs);
110 db_clear_breakpoints();
111 db_clear_watchpoints();
114 * Now check for a breakpoint at this address.
116 bkpt = db_find_breakpoint_here(pc);
117 if (bkpt) {
118 if (--bkpt->count == 0) {
119 bkpt->count = bkpt->init_count;
120 *is_breakpoint = true;
121 return (true); /* stop here */
123 } else if (*is_breakpoint) {
124 #ifdef PC_ADVANCE
125 PC_ADVANCE(regs);
126 #else
127 PC_REGS(regs) += BKPT_SIZE;
128 #endif
131 *is_breakpoint = false;
133 if (db_run_mode == STEP_INVISIBLE) {
134 db_run_mode = STEP_CONTINUE;
135 return (false); /* continue */
137 if (db_run_mode == STEP_COUNT) {
138 return (false); /* continue */
140 if (db_run_mode == STEP_ONCE) {
141 if (--db_loop_count > 0) {
142 if (db_sstep_print) {
143 db_printf("\t\t");
144 db_print_loc_and_inst(pc);
145 db_printf("\n");
147 return (false); /* continue */
150 if (db_run_mode == STEP_RETURN) {
151 db_expr_t ins = db_get_value(pc, sizeof(int), false);
153 /* continue until matching return */
155 if (!inst_trap_return(ins) &&
156 (!inst_return(ins) || --db_call_depth != 0)) {
157 if (db_sstep_print) {
158 if (inst_call(ins) || inst_return(ins)) {
159 int i;
161 db_printf("[after %6d] ",
162 db_inst_count);
163 for (i = db_call_depth; --i > 0; )
164 db_printf(" ");
165 db_print_loc_and_inst(pc);
166 db_printf("\n");
169 if (inst_call(ins))
170 db_call_depth++;
171 return (false); /* continue */
174 if (db_run_mode == STEP_CALLT) {
175 db_expr_t ins = db_get_value(pc, sizeof(int), false);
177 /* continue until call or return */
179 if (!inst_call(ins) &&
180 !inst_return(ins) &&
181 !inst_trap_return(ins)) {
182 return (false); /* continue */
185 db_run_mode = STEP_NONE;
186 return (true);
189 void
190 db_restart_at_pc(db_regs_t *regs, bool watchpt)
192 db_addr_t pc = PC_REGS(regs);
193 #ifdef SOFTWARE_SSTEP
194 db_addr_t brpc;
195 #endif
197 if ((db_run_mode == STEP_COUNT) ||
198 (db_run_mode == STEP_RETURN) ||
199 (db_run_mode == STEP_CALLT)) {
200 db_expr_t ins;
203 * We are about to execute this instruction,
204 * so count it now.
206 ins = db_get_value(pc, sizeof(int), false);
207 db_inst_count++;
208 db_load_count += inst_load(ins);
209 db_store_count += inst_store(ins);
211 #ifdef SOFTWARE_SSTEP
213 * Account for instructions in delay slots.
215 brpc = next_instr_address(pc, true);
216 if ((brpc != pc) &&
217 (inst_branch(ins) || inst_call(ins) || inst_return(ins))) {
218 ins = db_get_value(brpc, sizeof(int), false);
219 db_inst_count++;
220 db_load_count += inst_load(ins);
221 db_store_count += inst_store(ins);
223 #endif
226 if (db_run_mode == STEP_CONTINUE) {
227 if (watchpt || db_find_breakpoint_here(pc)) {
229 * Step over breakpoint/watchpoint.
231 db_run_mode = STEP_INVISIBLE;
232 db_set_single_step(regs);
233 } else {
234 db_set_breakpoints();
235 db_set_watchpoints();
237 } else {
238 db_set_single_step(regs);
242 void
243 db_single_step(db_regs_t *regs)
246 if (db_run_mode == STEP_CONTINUE) {
247 db_run_mode = STEP_INVISIBLE;
248 db_set_single_step(regs);
252 /* single-step */
253 /*ARGSUSED*/
254 void
255 db_single_step_cmd(db_expr_t addr, bool have_addr,
256 db_expr_t count, const char *modif)
258 bool print = false;
260 if (count == -1)
261 count = 1;
263 if (modif[0] == 'p')
264 print = true;
266 db_run_mode = STEP_ONCE;
267 db_loop_count = count;
268 db_sstep_print = print;
269 db_inst_count = 0;
270 db_load_count = 0;
271 db_store_count = 0;
273 db_cmd_loop_done = true;
276 /* trace and print until call/return */
277 /*ARGSUSED*/
278 void
279 db_trace_until_call_cmd(db_expr_t addr, bool have_addr,
280 db_expr_t count, const char *modif)
282 bool print = false;
284 if (modif[0] == 'p')
285 print = true;
287 db_run_mode = STEP_CALLT;
288 db_sstep_print = print;
289 db_inst_count = 0;
290 db_load_count = 0;
291 db_store_count = 0;
293 db_cmd_loop_done = true;
296 /*ARGSUSED*/
297 void
298 db_trace_until_matching_cmd(db_expr_t addr, bool have_addr,
299 db_expr_t count, const char *modif)
301 bool print = false;
303 if (modif[0] == 'p')
304 print = true;
306 db_run_mode = STEP_RETURN;
307 db_call_depth = 1;
308 db_sstep_print = print;
309 db_inst_count = 0;
310 db_load_count = 0;
311 db_store_count = 0;
313 db_cmd_loop_done = true;
316 /* continue */
317 /*ARGSUSED*/
318 void
319 db_continue_cmd(db_expr_t addr, bool have_addr,
320 db_expr_t count, const char *modif)
323 if (modif[0] == 'c')
324 db_run_mode = STEP_COUNT;
325 else
326 db_run_mode = STEP_CONTINUE;
327 db_inst_count = 0;
328 db_load_count = 0;
329 db_store_count = 0;
331 db_cmd_loop_done = true;
333 #endif /* DDB */
335 #ifdef SOFTWARE_SSTEP
337 * Software implementation of single-stepping.
338 * If your machine does not have a trace mode
339 * similar to the vax or sun ones you can use
340 * this implementation, done for the mips.
341 * Just define the above conditional and provide
342 * the functions/macros defined below.
344 * bool inst_branch(int inst)
345 * bool inst_call(int inst)
346 * returns true if the instruction might branch
348 * bool inst_unconditional_flow_transfer(int inst)
349 * returns true if the instruction is an unconditional
350 * transter of flow (i.e. unconditional branch)
352 * db_addr_t branch_taken(int inst, db_addr_t pc, db_regs_t *regs)
353 * returns the target address of the branch
355 * db_addr_t next_instr_address(db_addr_t pc, bool bd)
356 * returns the address of the first instruction following the
357 * one at "pc", which is either in the taken path of the branch
358 * (bd == true) or not. This is for machines (e.g. mips) with
359 * branch delays.
361 * A single-step may involve at most 2 breakpoints -
362 * one for branch-not-taken and one for branch taken.
363 * If one of these addresses does not already have a breakpoint,
364 * we allocate a breakpoint and save it here.
365 * These breakpoints are deleted on return.
368 #if !defined(DDB)
369 /* XXX - don't check for existing breakpoints in KGDB-only case */
370 #define db_find_breakpoint_here(pc) (0)
371 #endif
373 void
374 db_set_single_step(db_regs_t *regs)
376 db_addr_t pc = PC_REGS(regs), brpc = pc;
377 bool unconditional;
378 unsigned int inst;
381 * User was stopped at pc, e.g. the instruction
382 * at pc was not executed.
384 inst = db_get_value(pc, sizeof(int), false);
385 if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) {
386 brpc = branch_taken(inst, pc, regs);
387 if (brpc != pc) { /* self-branches are hopeless */
388 db_set_temp_breakpoint(&db_taken_bkpt, brpc);
389 } else
390 db_taken_bkpt.address = 0;
391 pc = next_instr_address(pc, true);
395 * Check if this control flow instruction is an
396 * unconditional transfer.
398 unconditional = inst_unconditional_flow_transfer(inst);
400 pc = next_instr_address(pc, false);
403 * We only set the sequential breakpoint if previous
404 * instruction was not an unconditional change of flow
405 * control. If the previous instruction is an
406 * unconditional change of flow control, setting a
407 * breakpoint in the next sequential location may set
408 * a breakpoint in data or in another routine, which
409 * could screw up in either the program or the debugger.
410 * (Consider, for instance, that the next sequential
411 * instruction is the start of a routine needed by the
412 * debugger.)
414 * Also, don't set both the taken and not-taken breakpoints
415 * in the same place even if the MD code would otherwise
416 * have us do so.
418 if (unconditional == false &&
419 db_find_breakpoint_here(pc) == 0 &&
420 pc != brpc)
421 db_set_temp_breakpoint(&db_not_taken_bkpt, pc);
422 else
423 db_not_taken_bkpt.address = 0;
426 void
427 db_clear_single_step(db_regs_t *regs)
430 if (db_taken_bkpt.address != 0)
431 db_delete_temp_breakpoint(&db_taken_bkpt);
433 if (db_not_taken_bkpt.address != 0)
434 db_delete_temp_breakpoint(&db_not_taken_bkpt);
437 void
438 db_set_temp_breakpoint(db_breakpoint_t bkpt, db_addr_t addr)
441 bkpt->map = NULL;
442 bkpt->address = addr;
443 /* bkpt->flags = BKPT_TEMP; - this is not used */
444 bkpt->init_count = 1;
445 bkpt->count = 1;
447 bkpt->bkpt_inst = db_get_value(bkpt->address, BKPT_SIZE, false);
448 db_put_value(bkpt->address, BKPT_SIZE,
449 BKPT_SET(bkpt->bkpt_inst, bkpt->address));
452 void
453 db_delete_temp_breakpoint(db_breakpoint_t bkpt)
456 db_put_value(bkpt->address, BKPT_SIZE, bkpt->bkpt_inst);
457 bkpt->address = 0;
459 #endif /* SOFTWARE_SSTEP */