]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/db_trace.c
Stack unwinding robustness fixes for RISC-V.
[FreeBSD/FreeBSD.git] / sys / riscv / riscv / db_trace.c
1 /*-
2  * Copyright (c) 2015 The FreeBSD Foundation
3  * Copyright (c) 2016 Ruslan Bukin <br@bsdpad.com>
4  * All rights reserved.
5  * Copyright (c) 2020 John Baldwin <jhb@FreeBSD.org>
6  *
7  * Portions of this software were developed by Semihalf under
8  * the sponsorship of the FreeBSD Foundation.
9  *
10  * Portions of this software were developed by SRI International and the
11  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
12  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
13  *
14  * Portions of this software were developed by the University of Cambridge
15  * Computer Laboratory as part of the CTSRD Project, with support from the
16  * UK Higher Education Innovation Fund (HEIF).
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <sys/param.h>
44 #include <sys/kdb.h>
45 #include <sys/proc.h>
46
47 #include <ddb/ddb.h>
48 #include <ddb/db_sym.h>
49
50 #include <machine/pcb.h>
51 #include <machine/riscvreg.h>
52 #include <machine/stack.h>
53 #include <machine/vmparam.h>
54
55 void
56 db_md_list_watchpoints()
57 {
58
59 }
60
61 int
62 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
63 {
64
65         return (0);
66 }
67
68 int
69 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
70 {
71
72         return (0);
73 }
74
75 static void
76 db_stack_trace_cmd(struct thread *td, struct unwind_state *frame)
77 {
78         const char *name;
79         db_expr_t offset;
80         db_expr_t value;
81         c_db_sym_t sym;
82         uint64_t pc;
83
84         while (1) {
85                 pc = frame->pc;
86
87                 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
88                 if (sym == C_DB_SYM_NULL) {
89                         value = 0;
90                         name = "(null)";
91                 } else
92                         db_symbol_values(sym, &name, &value);
93
94                 db_printf("%s() at ", name);
95                 db_printsym(frame->pc, DB_STGY_PROC);
96                 db_printf("\n");
97
98                 if (strcmp(name, "cpu_exception_handler_supervisor") == 0 ||
99                     strcmp(name, "cpu_exception_handler_user") == 0) {
100                         struct trapframe *tf;
101
102                         tf = (struct trapframe *)(uintptr_t)frame->sp;
103                         if (!kstack_contains(td, (vm_offset_t)tf,
104                             sizeof(*tf))) {
105                                 db_printf("--- invalid trapframe %p\n", tf);
106                                 break;
107                         }
108
109                         if ((tf->tf_scause & SCAUSE_INTR) != 0)
110                                 db_printf("--- interrupt %ld\n",
111                                     tf->tf_scause & SCAUSE_CODE);
112                         else
113                                 db_printf("--- exception %ld, tval = %#lx\n",
114                                     tf->tf_scause & SCAUSE_CODE,
115                                     tf->tf_stval);
116                         frame->sp = tf->tf_sp;
117                         frame->fp = tf->tf_s[0];
118                         frame->pc = tf->tf_sepc;
119                         if (!INKERNEL(frame->fp))
120                                 break;
121                         continue;
122                 }
123
124                 if (strcmp(name, "fork_trampoline") == 0)
125                         break;
126
127                 if (!unwind_frame(td, frame))
128                         break;
129         }
130 }
131
132 int
133 db_trace_thread(struct thread *thr, int count)
134 {
135         struct unwind_state frame;
136         struct pcb *ctx;
137
138         ctx = kdb_thr_ctx(thr);
139
140         frame.sp = ctx->pcb_sp;
141         frame.fp = ctx->pcb_s[0];
142         frame.pc = ctx->pcb_ra;
143         db_stack_trace_cmd(thr, &frame);
144         return (0);
145 }
146
147 void
148 db_trace_self(void)
149 {
150         struct unwind_state frame;
151         uintptr_t sp;
152
153         __asm __volatile("mv %0, sp" : "=&r" (sp));
154
155         frame.sp = sp;
156         frame.fp = (uintptr_t)__builtin_frame_address(0);
157         frame.pc = (uintptr_t)db_trace_self;
158         db_stack_trace_cmd(curthread, &frame);
159 }