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