xref: /freebsd/sys/arm64/arm64/db_trace.c (revision 788ca347b816afd83b2885e0c79aeeb88649b2ab)
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 <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 #include <sys/kdb.h>
35 #include <machine/pcb.h>
36 #include <ddb/ddb.h>
37 #include <ddb/db_sym.h>
38 
39 #include <machine/armreg.h>
40 #include <machine/debug_monitor.h>
41 
42 struct unwind_state {
43 	uint64_t fp;
44 	uint64_t sp;
45 	uint64_t pc;
46 };
47 
48 void
49 db_md_list_watchpoints()
50 {
51 
52 	dbg_show_watchpoint();
53 }
54 
55 int
56 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
57 {
58 
59 	return (dbg_remove_watchpoint(addr, size, DBG_FROM_EL1));
60 }
61 
62 int
63 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
64 {
65 
66 	return (dbg_setup_watchpoint(addr, size, DBG_FROM_EL1,
67 	    HW_BREAKPOINT_RW));
68 }
69 
70 static int
71 db_unwind_frame(struct unwind_state *frame)
72 {
73 	uint64_t fp = frame->fp;
74 
75 	if (fp == 0)
76 		return -1;
77 
78 	frame->sp = fp + 0x10;
79 	/* FP to previous frame (X29) */
80 	frame->fp = *(uint64_t *)(fp);
81 	/* LR (X30) */
82 	frame->pc = *(uint64_t *)(fp + 8) - 4;
83 	return (0);
84 }
85 
86 static void
87 db_stack_trace_cmd(struct unwind_state *frame)
88 {
89 	c_db_sym_t sym;
90 	const char *name;
91 	db_expr_t value;
92 	db_expr_t offset;
93 
94 	while (1) {
95 		uint64_t pc = frame->pc;
96 		int ret;
97 
98 		ret = db_unwind_frame(frame);
99 		if (ret < 0)
100 			break;
101 
102 		sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
103 		if (sym == C_DB_SYM_NULL) {
104 			value = 0;
105 			name = "(null)";
106 		} else
107 			db_symbol_values(sym, &name, &value);
108 
109 		db_printf("%s() at ", name);
110 		db_printsym(frame->pc, DB_STGY_PROC);
111 		db_printf("\n");
112 
113 		db_printf("\t pc = 0x%016lx  lr = 0x%016lx\n", pc,
114 		    frame->pc);
115 		db_printf("\t sp = 0x%016lx  fp = 0x%016lx\n", frame->sp,
116 		    frame->fp);
117 		/* TODO: Show some more registers */
118 		db_printf("\n");
119 	}
120 }
121 
122 int
123 db_trace_thread(struct thread *thr, int count)
124 {
125 	struct unwind_state frame;
126 	struct pcb *ctx;
127 
128 	if (thr != curthread) {
129 		ctx = kdb_thr_ctx(thr);
130 
131 		frame.sp = (uint64_t)ctx->pcb_sp;
132 		frame.fp = (uint64_t)ctx->pcb_x[29];
133 		frame.pc = (uint64_t)ctx->pcb_x[30];
134 		db_stack_trace_cmd(&frame);
135 	} else
136 		db_trace_self();
137 	return (0);
138 }
139 
140 void
141 db_trace_self(void)
142 {
143 	struct unwind_state frame;
144 	uint64_t sp;
145 
146 	__asm __volatile("mov %0, sp" : "=&r" (sp));
147 
148 	frame.sp = sp;
149 	frame.fp = (uint64_t)__builtin_frame_address(0);
150 	frame.pc = (uint64_t)db_trace_self;
151 	db_stack_trace_cmd(&frame);
152 }
153