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