1/*	$NetBSD: db_trace.c,v 1.8 2003/01/17 22:28:48 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 2000, 2001 Ben Harris
5 * Copyright (c) 1996 Scott K. Stevens
6 *
7 * Mach Operating System
8 * Copyright (c) 1991,1990 Carnegie Mellon University
9 * All Rights Reserved.
10 *
11 * Permission to use, copy, modify and distribute this software and its
12 * documentation is hereby granted, provided that both the copyright
13 * notice and this permission notice appear in all copies of the
14 * software, derivative works or modified versions, and any portions
15 * thereof, and that both notices appear in supporting documentation.
16 *
17 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
18 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
19 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
20 *
21 * Carnegie Mellon requests users of this software to return to
22 *
23 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
24 *  School of Computer Science
25 *  Carnegie Mellon University
26 *  Pittsburgh PA 15213-3890
27 *
28 * any improvements or extensions that they make and grant Carnegie the
29 * rights to redistribute these changes.
30 */
31#include "opt_ddb.h"
32
33#include <sys/param.h>
34#include <sys/systm.h>
35
36#include <sys/proc.h>
37#include <sys/kdb.h>
38#include <sys/stack.h>
39
40#include <machine/armreg.h>
41#include <machine/asm.h>
42#include <machine/cpufunc.h>
43#include <machine/db_machdep.h>
44#include <machine/debug_monitor.h>
45#include <machine/pcb.h>
46#include <machine/stack.h>
47#include <machine/vmparam.h>
48
49#include <ddb/ddb.h>
50#include <ddb/db_access.h>
51#include <ddb/db_sym.h>
52#include <ddb/db_output.h>
53
54static void
55db_stack_trace_cmd(struct unwind_state *state)
56{
57	const char *name;
58	db_expr_t value;
59	db_expr_t offset;
60	c_db_sym_t sym;
61	u_int reg, i;
62	char *sep;
63	uint16_t upd_mask;
64	bool finished;
65
66	finished = false;
67	while (!finished) {
68		finished = unwind_stack_one(state, 1);
69
70		/* Print the frame details */
71		sym = db_search_symbol(state->start_pc, DB_STGY_ANY, &offset);
72		if (sym == C_DB_SYM_NULL) {
73			value = 0;
74			name = "(null)";
75		} else
76			db_symbol_values(sym, &name, &value);
77		db_printf("%s() at ", name);
78		db_printsym(state->start_pc, DB_STGY_PROC);
79		db_printf("\n");
80		db_printf("\t pc = 0x%08x  lr = 0x%08x (", state->start_pc,
81		    state->registers[LR]);
82		db_printsym(state->registers[LR], DB_STGY_PROC);
83		db_printf(")\n");
84		db_printf("\t sp = 0x%08x  fp = 0x%08x",
85		    state->registers[SP], state->registers[FP]);
86
87		/* Don't print the registers we have already printed */
88		upd_mask = state->update_mask &
89		    ~((1 << SP) | (1 << FP) | (1 << LR) | (1 << PC));
90		sep = "\n\t";
91		for (i = 0, reg = 0; upd_mask != 0; upd_mask >>= 1, reg++) {
92			if ((upd_mask & 1) != 0) {
93				db_printf("%s%sr%d = 0x%08x", sep,
94				    (reg < 10) ? " " : "", reg,
95				    state->registers[reg]);
96				i++;
97				if (i == 2) {
98					sep = "\n\t";
99					i = 0;
100				} else
101					sep = " ";
102			}
103		}
104		db_printf("\n");
105
106		if (finished)
107			break;
108
109		/*
110		 * Stop if directed to do so, or if we've unwound back to the
111		 * kernel entry point, or if the unwind function didn't change
112		 * anything (to avoid getting stuck in this loop forever).
113		 * If the latter happens, it's an indication that the unwind
114		 * information is incorrect somehow for the function named in
115		 * the last frame printed before you see the unwind failure
116		 * message (maybe it needs a STOP_UNWINDING).
117		 */
118		if (state->registers[PC] < VM_MIN_KERNEL_ADDRESS) {
119			db_printf("Unable to unwind into user mode\n");
120			finished = true;
121		} else if (state->update_mask == 0) {
122			db_printf("Unwind failure (no registers changed)\n");
123			finished = true;
124		}
125	}
126}
127
128void
129db_md_list_watchpoints(void)
130{
131
132	dbg_show_watchpoint();
133}
134
135int
136db_trace_thread(struct thread *thr, int count)
137{
138	struct unwind_state state;
139	struct pcb *ctx;
140
141	if (thr != curthread) {
142		ctx = kdb_thr_ctx(thr);
143
144		state.registers[FP] = ctx->pcb_regs.sf_r11;
145		state.registers[SP] = ctx->pcb_regs.sf_sp;
146		state.registers[LR] = ctx->pcb_regs.sf_lr;
147		state.registers[PC] = ctx->pcb_regs.sf_pc;
148
149		db_stack_trace_cmd(&state);
150	} else
151		db_trace_self();
152	return (0);
153}
154
155void
156db_trace_self(void)
157{
158	struct unwind_state state;
159	uint32_t sp;
160
161	/* Read the stack pointer */
162	__asm __volatile("mov %0, sp" : "=&r" (sp));
163
164	state.registers[FP] = (uint32_t)__builtin_frame_address(0);
165	state.registers[SP] = sp;
166	state.registers[LR] = (uint32_t)__builtin_return_address(0);
167	state.registers[PC] = (uint32_t)db_trace_self;
168
169	db_stack_trace_cmd(&state);
170}
171