16fc729afSOlivier Houchard /* $NetBSD: db_trace.c,v 1.8 2003/01/17 22:28:48 thorpej Exp $ */
26fc729afSOlivier Houchard
3d8315c79SWarner Losh /*-
46fc729afSOlivier Houchard * Copyright (c) 2000, 2001 Ben Harris
56fc729afSOlivier Houchard * Copyright (c) 1996 Scott K. Stevens
66fc729afSOlivier Houchard *
76fc729afSOlivier Houchard * Mach Operating System
86fc729afSOlivier Houchard * Copyright (c) 1991,1990 Carnegie Mellon University
96fc729afSOlivier Houchard * All Rights Reserved.
106fc729afSOlivier Houchard *
116fc729afSOlivier Houchard * Permission to use, copy, modify and distribute this software and its
126fc729afSOlivier Houchard * documentation is hereby granted, provided that both the copyright
136fc729afSOlivier Houchard * notice and this permission notice appear in all copies of the
146fc729afSOlivier Houchard * software, derivative works or modified versions, and any portions
156fc729afSOlivier Houchard * thereof, and that both notices appear in supporting documentation.
166fc729afSOlivier Houchard *
176fc729afSOlivier Houchard * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
186fc729afSOlivier Houchard * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
196fc729afSOlivier Houchard * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
206fc729afSOlivier Houchard *
216fc729afSOlivier Houchard * Carnegie Mellon requests users of this software to return to
226fc729afSOlivier Houchard *
236fc729afSOlivier Houchard * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
246fc729afSOlivier Houchard * School of Computer Science
256fc729afSOlivier Houchard * Carnegie Mellon University
266fc729afSOlivier Houchard * Pittsburgh PA 15213-3890
276fc729afSOlivier Houchard *
286fc729afSOlivier Houchard * any improvements or extensions that they make and grant Carnegie the
296fc729afSOlivier Houchard * rights to redistribute these changes.
306fc729afSOlivier Houchard */
31595f8a59SZbigniew Bodek #include "opt_ddb.h"
326fc729afSOlivier Houchard
336fc729afSOlivier Houchard #include <sys/param.h>
349cdb2bfcSOlivier Houchard #include <sys/systm.h>
356fc729afSOlivier Houchard
366fc729afSOlivier Houchard #include <sys/proc.h>
372f6d0d8fSOlivier Houchard #include <sys/kdb.h>
388d511e2aSJeff Roberson #include <sys/stack.h>
396da235a3SAndrew Turner
406fc729afSOlivier Houchard #include <machine/armreg.h>
416fc729afSOlivier Houchard #include <machine/asm.h>
426fc729afSOlivier Houchard #include <machine/cpufunc.h>
436fc729afSOlivier Houchard #include <machine/db_machdep.h>
44595f8a59SZbigniew Bodek #include <machine/debug_monitor.h>
456004362eSDavid Schultz #include <machine/pcb.h>
463c90d1eaSRobert Watson #include <machine/stack.h>
476fc729afSOlivier Houchard #include <machine/vmparam.h>
486da235a3SAndrew Turner
496fc729afSOlivier Houchard #include <ddb/ddb.h>
506fc729afSOlivier Houchard #include <ddb/db_access.h>
516fc729afSOlivier Houchard #include <ddb/db_sym.h>
526fc729afSOlivier Houchard #include <ddb/db_output.h>
536fc729afSOlivier Houchard
5429ce0a2aSAndrew Turner static void
db_stack_trace_cmd(struct unwind_state * state)5529ce0a2aSAndrew Turner db_stack_trace_cmd(struct unwind_state *state)
5629ce0a2aSAndrew Turner {
5729ce0a2aSAndrew Turner const char *name;
5829ce0a2aSAndrew Turner db_expr_t value;
5929ce0a2aSAndrew Turner db_expr_t offset;
6029ce0a2aSAndrew Turner c_db_sym_t sym;
6129ce0a2aSAndrew Turner u_int reg, i;
6229ce0a2aSAndrew Turner char *sep;
63abf29ad1SIan Lepore uint16_t upd_mask;
64abf29ad1SIan Lepore bool finished;
6529ce0a2aSAndrew Turner
66abf29ad1SIan Lepore finished = false;
67abf29ad1SIan Lepore while (!finished) {
684a8169d9SAndrew Turner finished = unwind_stack_one(state, 1);
6929ce0a2aSAndrew Turner
7029ce0a2aSAndrew Turner /* Print the frame details */
7129ce0a2aSAndrew Turner sym = db_search_symbol(state->start_pc, DB_STGY_ANY, &offset);
7229ce0a2aSAndrew Turner if (sym == C_DB_SYM_NULL) {
7329ce0a2aSAndrew Turner value = 0;
7429ce0a2aSAndrew Turner name = "(null)";
7529ce0a2aSAndrew Turner } else
7629ce0a2aSAndrew Turner db_symbol_values(sym, &name, &value);
7729ce0a2aSAndrew Turner db_printf("%s() at ", name);
7829ce0a2aSAndrew Turner db_printsym(state->start_pc, DB_STGY_PROC);
7929ce0a2aSAndrew Turner db_printf("\n");
8029ce0a2aSAndrew Turner db_printf("\t pc = 0x%08x lr = 0x%08x (", state->start_pc,
8129ce0a2aSAndrew Turner state->registers[LR]);
8229ce0a2aSAndrew Turner db_printsym(state->registers[LR], DB_STGY_PROC);
8329ce0a2aSAndrew Turner db_printf(")\n");
8429ce0a2aSAndrew Turner db_printf("\t sp = 0x%08x fp = 0x%08x",
8529ce0a2aSAndrew Turner state->registers[SP], state->registers[FP]);
8629ce0a2aSAndrew Turner
8729ce0a2aSAndrew Turner /* Don't print the registers we have already printed */
88abf29ad1SIan Lepore upd_mask = state->update_mask &
89abf29ad1SIan Lepore ~((1 << SP) | (1 << FP) | (1 << LR) | (1 << PC));
9029ce0a2aSAndrew Turner sep = "\n\t";
91abf29ad1SIan Lepore for (i = 0, reg = 0; upd_mask != 0; upd_mask >>= 1, reg++) {
92abf29ad1SIan Lepore if ((upd_mask & 1) != 0) {
9329ce0a2aSAndrew Turner db_printf("%s%sr%d = 0x%08x", sep,
9429ce0a2aSAndrew Turner (reg < 10) ? " " : "", reg,
9529ce0a2aSAndrew Turner state->registers[reg]);
9629ce0a2aSAndrew Turner i++;
9729ce0a2aSAndrew Turner if (i == 2) {
9829ce0a2aSAndrew Turner sep = "\n\t";
9929ce0a2aSAndrew Turner i = 0;
10029ce0a2aSAndrew Turner } else
10129ce0a2aSAndrew Turner sep = " ";
10229ce0a2aSAndrew Turner }
10329ce0a2aSAndrew Turner }
10429ce0a2aSAndrew Turner db_printf("\n");
105abf29ad1SIan Lepore
1066da235a3SAndrew Turner if (finished)
1076da235a3SAndrew Turner break;
1086da235a3SAndrew Turner
109d0e8071aSIan Lepore /*
110d0e8071aSIan Lepore * Stop if directed to do so, or if we've unwound back to the
111abf29ad1SIan Lepore * kernel entry point, or if the unwind function didn't change
112abf29ad1SIan Lepore * anything (to avoid getting stuck in this loop forever).
113abf29ad1SIan Lepore * If the latter happens, it's an indication that the unwind
114abf29ad1SIan Lepore * information is incorrect somehow for the function named in
115abf29ad1SIan Lepore * the last frame printed before you see the unwind failure
116abf29ad1SIan Lepore * message (maybe it needs a STOP_UNWINDING).
117abf29ad1SIan Lepore */
1186da235a3SAndrew Turner if (state->registers[PC] < VM_MIN_KERNEL_ADDRESS) {
119abf29ad1SIan Lepore db_printf("Unable to unwind into user mode\n");
120abf29ad1SIan Lepore finished = true;
121abf29ad1SIan Lepore } else if (state->update_mask == 0) {
122abf29ad1SIan Lepore db_printf("Unwind failure (no registers changed)\n");
123abf29ad1SIan Lepore finished = true;
124abf29ad1SIan Lepore }
12529ce0a2aSAndrew Turner }
12629ce0a2aSAndrew Turner }
1276fc729afSOlivier Houchard
1286fc729afSOlivier Houchard void
db_md_list_watchpoints(void)129*59249a51SAndrew Turner db_md_list_watchpoints(void)
1306fc729afSOlivier Houchard {
131595f8a59SZbigniew Bodek
132595f8a59SZbigniew Bodek dbg_show_watchpoint();
1336fc729afSOlivier Houchard }
1346fc729afSOlivier Houchard
1356fc729afSOlivier Houchard int
db_trace_thread(struct thread * thr,int count)1362f6d0d8fSOlivier Houchard db_trace_thread(struct thread *thr, int count)
1376fc729afSOlivier Houchard {
13829ce0a2aSAndrew Turner struct unwind_state state;
1392ffa4420SMarcel Moolenaar struct pcb *ctx;
1406fc729afSOlivier Houchard
1410cdf4611SGrzegorz Bernacki if (thr != curthread) {
1422ffa4420SMarcel Moolenaar ctx = kdb_thr_ctx(thr);
14329ce0a2aSAndrew Turner
144c4c27bc9SIan Lepore state.registers[FP] = ctx->pcb_regs.sf_r11;
145c4c27bc9SIan Lepore state.registers[SP] = ctx->pcb_regs.sf_sp;
146c4c27bc9SIan Lepore state.registers[LR] = ctx->pcb_regs.sf_lr;
147c4c27bc9SIan Lepore state.registers[PC] = ctx->pcb_regs.sf_pc;
14829ce0a2aSAndrew Turner
14929ce0a2aSAndrew Turner db_stack_trace_cmd(&state);
1500cdf4611SGrzegorz Bernacki } else
1510cdf4611SGrzegorz Bernacki db_trace_self();
1522f6d0d8fSOlivier Houchard return (0);
1532f6d0d8fSOlivier Houchard }
1542f6d0d8fSOlivier Houchard
1552f6d0d8fSOlivier Houchard void
db_trace_self(void)1562f6d0d8fSOlivier Houchard db_trace_self(void)
1572f6d0d8fSOlivier Houchard {
15829ce0a2aSAndrew Turner struct unwind_state state;
159d80f56e1SAndrew Turner uint32_t sp;
160d80f56e1SAndrew Turner
161d80f56e1SAndrew Turner /* Read the stack pointer */
162d80f56e1SAndrew Turner __asm __volatile("mov %0, sp" : "=&r" (sp));
16329ce0a2aSAndrew Turner
16429ce0a2aSAndrew Turner state.registers[FP] = (uint32_t)__builtin_frame_address(0);
165d80f56e1SAndrew Turner state.registers[SP] = sp;
16629ce0a2aSAndrew Turner state.registers[LR] = (uint32_t)__builtin_return_address(0);
16729ce0a2aSAndrew Turner state.registers[PC] = (uint32_t)db_trace_self;
16829ce0a2aSAndrew Turner
16929ce0a2aSAndrew Turner db_stack_trace_cmd(&state);
1706fc729afSOlivier Houchard }
171