1/*	$NetBSD: db_run.c,v 1.30 2007/02/22 06:41:01 thorpej Exp $	*/
2
3/*
4 * Mach Operating System
5 * Copyright (c) 1993-1990 Carnegie Mellon University
6 * All Rights Reserved.
7 *
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.
13 *
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.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21 *  School of Computer Science
22 *  Carnegie Mellon University
23 *  Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 *
28 * 	Author: David B. Golub, Carnegie Mellon University
29 *	Date:	7/90
30 */
31
32/*
33 * Commands to run process.
34 */
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: db_run.c,v 1.30 2007/02/22 06:41:01 thorpej Exp $");
38
39#include "opt_ddb.h"
40
41#include <sys/param.h>
42#include <sys/proc.h>
43
44#include <machine/db_machdep.h>
45
46#include <ddb/db_run.h>
47#include <ddb/db_access.h>
48#include <ddb/db_break.h>
49
50int	db_inst_count;
51int	db_load_count;
52int	db_store_count;
53
54#ifdef	SOFTWARE_SSTEP
55static void	db_set_temp_breakpoint(db_breakpoint_t, db_addr_t);
56static void	db_delete_temp_breakpoint(db_breakpoint_t);
57static struct	db_breakpoint	db_not_taken_bkpt;
58static struct	db_breakpoint	db_taken_bkpt;
59#endif
60
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>
67
68static 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
76
77static bool		db_sstep_print;
78static int		db_loop_count;
79static int		db_call_depth;
80
81bool
82db_stop_at_pc(db_regs_t *regs, bool *is_breakpoint)
83{
84	db_addr_t	pc;
85	db_breakpoint_t bkpt;
86
87	pc = PC_REGS(regs);
88
89#ifdef	FIXUP_PC_AFTER_BREAK
90	if (*is_breakpoint) {
91		/*
92		 * Breakpoint trap.  Regardless if we treat this as a
93		 * real breakpoint (e.g. software single-step), fix up the PC.
94		 */
95		FIXUP_PC_AFTER_BREAK(regs);
96		pc = PC_REGS(regs);
97	}
98#endif
99
100#ifdef	SOFTWARE_SSTEP
101	/*
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.
104	 */
105	if (db_taken_bkpt.address == pc || db_not_taken_bkpt.address == pc)
106		*is_breakpoint = false;
107#endif	/* SOFTWARE_SSTEP */
108
109	db_clear_single_step(regs);
110	db_clear_breakpoints();
111	db_clear_watchpoints();
112
113	/*
114	 * Now check for a breakpoint at this address.
115	 */
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 */
122		}
123	} else if (*is_breakpoint) {
124#ifdef PC_ADVANCE
125		PC_ADVANCE(regs);
126#else
127		PC_REGS(regs) += BKPT_SIZE;
128#endif
129	}
130
131	*is_breakpoint = false;
132
133	if (db_run_mode == STEP_INVISIBLE) {
134		db_run_mode = STEP_CONTINUE;
135		return (false);	/* continue */
136	}
137	if (db_run_mode == STEP_COUNT) {
138		return (false); /* continue */
139	}
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");
146			}
147			return (false);	/* continue */
148		}
149	}
150	if (db_run_mode == STEP_RETURN) {
151		db_expr_t ins = db_get_value(pc, sizeof(int), false);
152
153		/* continue until matching return */
154
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;
160
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");
167				}
168			}
169			if (inst_call(ins))
170				db_call_depth++;
171			return (false);	/* continue */
172		}
173	}
174	if (db_run_mode == STEP_CALLT) {
175		db_expr_t ins = db_get_value(pc, sizeof(int), false);
176
177		/* continue until call or return */
178
179		if (!inst_call(ins) &&
180		    !inst_return(ins) &&
181		    !inst_trap_return(ins)) {
182			return (false);	/* continue */
183		}
184	}
185	db_run_mode = STEP_NONE;
186	return (true);
187}
188
189void
190db_restart_at_pc(db_regs_t *regs, bool watchpt)
191{
192	db_addr_t pc = PC_REGS(regs);
193#ifdef SOFTWARE_SSTEP
194	db_addr_t brpc;
195#endif
196
197	if ((db_run_mode == STEP_COUNT) ||
198	    (db_run_mode == STEP_RETURN) ||
199	    (db_run_mode == STEP_CALLT)) {
200		db_expr_t		ins;
201
202		/*
203		 * We are about to execute this instruction,
204		 * so count it now.
205		 */
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);
210
211#ifdef SOFTWARE_SSTEP
212		/*
213		 * Account for instructions in delay slots.
214		 */
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);
222		}
223#endif
224	}
225
226	if (db_run_mode == STEP_CONTINUE) {
227		if (watchpt || db_find_breakpoint_here(pc)) {
228			/*
229			 * Step over breakpoint/watchpoint.
230			 */
231			db_run_mode = STEP_INVISIBLE;
232			db_set_single_step(regs);
233		} else {
234			db_set_breakpoints();
235			db_set_watchpoints();
236		}
237	} else {
238		db_set_single_step(regs);
239	}
240}
241
242void
243db_single_step(db_regs_t *regs)
244{
245
246	if (db_run_mode == STEP_CONTINUE) {
247		db_run_mode = STEP_INVISIBLE;
248		db_set_single_step(regs);
249	}
250}
251
252/* single-step */
253/*ARGSUSED*/
254void
255db_single_step_cmd(db_expr_t addr, bool have_addr,
256    db_expr_t count, const char *modif)
257{
258	bool print = false;
259
260	if (count == -1)
261		count = 1;
262
263	if (modif[0] == 'p')
264		print = true;
265
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;
272
273	db_cmd_loop_done = true;
274}
275
276/* trace and print until call/return */
277/*ARGSUSED*/
278void
279db_trace_until_call_cmd(db_expr_t addr, bool have_addr,
280    db_expr_t count, const char *modif)
281{
282	bool print = false;
283
284	if (modif[0] == 'p')
285		print = true;
286
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;
292
293	db_cmd_loop_done = true;
294}
295
296/*ARGSUSED*/
297void
298db_trace_until_matching_cmd(db_expr_t addr, bool have_addr,
299    db_expr_t count, const char *modif)
300{
301	bool print = false;
302
303	if (modif[0] == 'p')
304		print = true;
305
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;
312
313	db_cmd_loop_done = true;
314}
315
316/* continue */
317/*ARGSUSED*/
318void
319db_continue_cmd(db_expr_t addr, bool have_addr,
320    db_expr_t count, const char *modif)
321{
322
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;
330
331	db_cmd_loop_done = true;
332}
333#endif /* DDB */
334
335#ifdef SOFTWARE_SSTEP
336/*
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.
343 *
344 * bool inst_branch(int inst)
345 * bool inst_call(int inst)
346 *	returns true if the instruction might branch
347 *
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)
351 *
352 * db_addr_t branch_taken(int inst, db_addr_t pc, db_regs_t *regs)
353 *	returns the target address of the branch
354 *
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.
360 *
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.
366 */
367
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
372
373void
374db_set_single_step(db_regs_t *regs)
375{
376	db_addr_t pc = PC_REGS(regs), brpc = pc;
377	bool unconditional;
378	unsigned int inst;
379
380	/*
381	 *	User was stopped at pc, e.g. the instruction
382	 *	at pc was not executed.
383	 */
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);
392	}
393
394	/*
395	 *	Check if this control flow instruction is an
396	 *	unconditional transfer.
397	 */
398	unconditional = inst_unconditional_flow_transfer(inst);
399
400	pc = next_instr_address(pc, false);
401
402	/*
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.)
413	 *
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.
417	 */
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;
424}
425
426void
427db_clear_single_step(db_regs_t *regs)
428{
429
430	if (db_taken_bkpt.address != 0)
431		db_delete_temp_breakpoint(&db_taken_bkpt);
432
433	if (db_not_taken_bkpt.address != 0)
434		db_delete_temp_breakpoint(&db_not_taken_bkpt);
435}
436
437void
438db_set_temp_breakpoint(db_breakpoint_t bkpt, db_addr_t addr)
439{
440
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;
446
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));
450}
451
452void
453db_delete_temp_breakpoint(db_breakpoint_t bkpt)
454{
455
456	db_put_value(bkpt->address, BKPT_SIZE, bkpt->bkpt_inst);
457	bkpt->address = 0;
458}
459#endif /* SOFTWARE_SSTEP */
460