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/cdefs.h>
34__FBSDID("$FreeBSD: stable/11/sys/arm/arm/db_trace.c 314506 2017-03-01 19:55:04Z ian $");
35#include <sys/param.h>
36#include <sys/systm.h>
37
38
39#include <sys/proc.h>
40#include <sys/kdb.h>
41#include <sys/stack.h>
42
43#include <machine/armreg.h>
44#include <machine/asm.h>
45#include <machine/cpufunc.h>
46#include <machine/db_machdep.h>
47#include <machine/debug_monitor.h>
48#include <machine/pcb.h>
49#include <machine/stack.h>
50#include <machine/vmparam.h>
51
52#include <ddb/ddb.h>
53#include <ddb/db_access.h>
54#include <ddb/db_sym.h>
55#include <ddb/db_output.h>
56
57static void
58db_stack_trace_cmd(struct unwind_state *state)
59{
60	const char *name;
61	db_expr_t value;
62	db_expr_t offset;
63	c_db_sym_t sym;
64	u_int reg, i;
65	char *sep;
66	uint16_t upd_mask;
67	bool finished;
68
69	finished = false;
70	while (!finished) {
71		finished = unwind_stack_one(state, 1);
72
73		/* Print the frame details */
74		sym = db_search_symbol(state->start_pc, DB_STGY_ANY, &offset);
75		if (sym == C_DB_SYM_NULL) {
76			value = 0;
77			name = "(null)";
78		} else
79			db_symbol_values(sym, &name, &value);
80		db_printf("%s() at ", name);
81		db_printsym(state->start_pc, DB_STGY_PROC);
82		db_printf("\n");
83		db_printf("\t pc = 0x%08x  lr = 0x%08x (", state->start_pc,
84		    state->registers[LR]);
85		db_printsym(state->registers[LR], DB_STGY_PROC);
86		db_printf(")\n");
87		db_printf("\t sp = 0x%08x  fp = 0x%08x",
88		    state->registers[SP], state->registers[FP]);
89
90		/* Don't print the registers we have already printed */
91		upd_mask = state->update_mask &
92		    ~((1 << SP) | (1 << FP) | (1 << LR) | (1 << PC));
93		sep = "\n\t";
94		for (i = 0, reg = 0; upd_mask != 0; upd_mask >>= 1, reg++) {
95			if ((upd_mask & 1) != 0) {
96				db_printf("%s%sr%d = 0x%08x", sep,
97				    (reg < 10) ? " " : "", reg,
98				    state->registers[reg]);
99				i++;
100				if (i == 2) {
101					sep = "\n\t";
102					i = 0;
103				} else
104					sep = " ";
105
106			}
107		}
108		db_printf("\n");
109
110		if (finished)
111			break;
112
113		/*
114		 * Stop if directed to do so, or if we've unwound back to the
115		 * kernel entry point, or if the unwind function didn't change
116		 * anything (to avoid getting stuck in this loop forever).
117		 * If the latter happens, it's an indication that the unwind
118		 * information is incorrect somehow for the function named in
119		 * the last frame printed before you see the unwind failure
120		 * message (maybe it needs a STOP_UNWINDING).
121		 */
122		if (state->registers[PC] < VM_MIN_KERNEL_ADDRESS) {
123			db_printf("Unable to unwind into user mode\n");
124			finished = true;
125		} else if (state->update_mask == 0) {
126			db_printf("Unwind failure (no registers changed)\n");
127			finished = true;
128		}
129	}
130}
131
132void
133db_md_list_watchpoints(void)
134{
135
136	dbg_show_watchpoint();
137}
138
139int
140db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
141{
142
143	return (dbg_remove_watchpoint(addr, size));
144}
145
146int
147db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
148{
149
150	return (dbg_setup_watchpoint(addr, size, HW_WATCHPOINT_RW));
151}
152
153int
154db_trace_thread(struct thread *thr, int count)
155{
156	struct unwind_state state;
157	struct pcb *ctx;
158
159	if (thr != curthread) {
160		ctx = kdb_thr_ctx(thr);
161
162		state.registers[FP] = ctx->pcb_regs.sf_r11;
163		state.registers[SP] = ctx->pcb_regs.sf_sp;
164		state.registers[LR] = ctx->pcb_regs.sf_lr;
165		state.registers[PC] = ctx->pcb_regs.sf_pc;
166
167		db_stack_trace_cmd(&state);
168	} else
169		db_trace_self();
170	return (0);
171}
172
173void
174db_trace_self(void)
175{
176	struct unwind_state state;
177	uint32_t sp;
178
179	/* Read the stack pointer */
180	__asm __volatile("mov %0, sp" : "=&r" (sp));
181
182	state.registers[FP] = (uint32_t)__builtin_frame_address(0);
183	state.registers[SP] = sp;
184	state.registers[LR] = (uint32_t)__builtin_return_address(0);
185	state.registers[PC] = (uint32_t)db_trace_self;
186
187	db_stack_trace_cmd(&state);
188}
189