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