xref: /freebsd/sys/ddb/db_main.c (revision d2ce15bd43b3a1dcce08eecbff8d5d359946d972)
1 /*-
2  * Mach Operating System
3  * Copyright (c) 1991,1990 Carnegie Mellon University
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  *
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie the
24  * rights to redistribute these changes.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/cons.h>
33 #include <sys/linker.h>
34 #include <sys/kdb.h>
35 #include <sys/kernel.h>
36 #include <sys/pcpu.h>
37 #include <sys/proc.h>
38 #include <sys/reboot.h>
39 #include <sys/sysctl.h>
40 
41 #include <machine/kdb.h>
42 #include <machine/pcb.h>
43 #include <machine/setjmp.h>
44 
45 #include <ddb/ddb.h>
46 #include <ddb/db_command.h>
47 #include <ddb/db_sym.h>
48 
49 SYSCTL_NODE(_debug, OID_AUTO, ddb, CTLFLAG_RW, 0, "DDB settings");
50 
51 static dbbe_init_f db_init;
52 static dbbe_trap_f db_trap;
53 static dbbe_trace_f db_trace_self_wrapper;
54 static dbbe_trace_thread_f db_trace_thread_wrapper;
55 
56 KDB_BACKEND(ddb, db_init, db_trace_self_wrapper, db_trace_thread_wrapper,
57     db_trap);
58 
59 vm_offset_t ksym_start, ksym_end;
60 
61 boolean_t
62 X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line,
63     db_expr_t off)
64 {
65 	return (FALSE);
66 }
67 
68 c_db_sym_t
69 X_db_lookup(db_symtab_t *symtab, const char *symbol)
70 {
71 	c_linker_sym_t lsym;
72 	Elf_Sym *sym;
73 
74 	if (symtab->private == NULL) {
75 		return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym))
76 			? lsym : NULL));
77 	} else {
78 		sym = (Elf_Sym *)symtab->start;
79 		while ((char *)sym < symtab->end) {
80 			if (sym->st_name != 0 &&
81 			    !strcmp(symtab->private + sym->st_name, symbol))
82 				return ((c_db_sym_t)sym);
83 			sym++;
84 		}
85 	}
86 	return (NULL);
87 }
88 
89 c_db_sym_t
90 X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat,
91     db_expr_t *diffp)
92 {
93 	c_linker_sym_t lsym;
94 	Elf_Sym *sym, *match;
95 	unsigned long diff;
96 
97 	if (symtab->private == NULL) {
98 		if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) {
99 			*diffp = (db_expr_t)diff;
100 			return ((c_db_sym_t)lsym);
101 		}
102 		return (NULL);
103 	}
104 
105 	diff = ~0UL;
106 	match = NULL;
107 	for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) {
108 		if (sym->st_name == 0)
109 			continue;
110 		if (off < sym->st_value)
111 			continue;
112 		if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
113 		    ELF_ST_TYPE(sym->st_info) != STT_FUNC &&
114 		    ELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
115 			continue;
116 		if ((off - sym->st_value) > diff)
117 			continue;
118 		if ((off - sym->st_value) < diff) {
119 			diff = off - sym->st_value;
120 			match = sym;
121 		} else {
122 			if (match == NULL)
123 				match = sym;
124 			else if (ELF_ST_BIND(match->st_info) == STB_LOCAL &&
125 			    ELF_ST_BIND(sym->st_info) != STB_LOCAL)
126 				match = sym;
127 		}
128 		if (diff == 0) {
129 			if (strat == DB_STGY_PROC &&
130 			    ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
131 			    ELF_ST_BIND(sym->st_info) != STB_LOCAL)
132 				break;
133 			if (strat == DB_STGY_ANY &&
134 			    ELF_ST_BIND(sym->st_info) != STB_LOCAL)
135 				break;
136 		}
137 	}
138 
139 	*diffp = (match == NULL) ? off : diff;
140 	return ((c_db_sym_t)match);
141 }
142 
143 boolean_t
144 X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp,
145     char **argp)
146 {
147 	return (FALSE);
148 }
149 
150 void
151 X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep,
152     db_expr_t *valp)
153 {
154 	linker_symval_t lval;
155 
156 	if (symtab->private == NULL) {
157 		linker_ddb_symbol_values((c_linker_sym_t)sym, &lval);
158 		if (namep != NULL)
159 			*namep = (const char*)lval.name;
160 		if (valp != NULL)
161 			*valp = (db_expr_t)lval.value;
162 	} else {
163 		if (namep != NULL)
164 			*namep = (const char *)symtab->private +
165 			    ((const Elf_Sym *)sym)->st_name;
166 		if (valp != NULL)
167 			*valp = (db_expr_t)((const Elf_Sym *)sym)->st_value;
168 	}
169 }
170 
171 static int
172 db_init(void)
173 {
174 	uintptr_t symtab, strtab;
175 	Elf_Size tabsz, strsz;
176 
177 	db_command_init();
178 	if (ksym_end > ksym_start && ksym_start != 0) {
179 		symtab = ksym_start;
180 		tabsz = *((Elf_Size*)symtab);
181 		symtab += sizeof(Elf_Size);
182 		strtab = symtab + tabsz;
183 		strsz = *((Elf_Size*)strtab);
184 		strtab += sizeof(Elf_Size);
185 		if (strtab + strsz <= ksym_end) {
186 			db_add_symbol_table((char *)symtab,
187 			    (char *)(symtab + tabsz), "elf", (char *)strtab);
188 		}
189 	}
190 	db_add_symbol_table(NULL, NULL, "kld", NULL);
191 	return (1);	/* We're the default debugger. */
192 }
193 
194 static int
195 db_trap(int type, int code)
196 {
197 	jmp_buf jb;
198 	void *prev_jb;
199 	boolean_t bkpt, watchpt;
200 	const char *why;
201 
202 	/*
203 	 * Don't handle the trap if the console is unavailable (i.e. it
204 	 * is in graphics mode).
205 	 */
206 	if (cnunavailable())
207 		return (0);
208 
209 	bkpt = IS_BREAKPOINT_TRAP(type, code);
210 	watchpt = IS_WATCHPOINT_TRAP(type, code);
211 
212 	if (db_stop_at_pc(&bkpt)) {
213 		if (db_inst_count) {
214 			db_printf("After %d instructions (%d loads, %d stores),\n",
215 			    db_inst_count, db_load_count, db_store_count);
216 		}
217 		prev_jb = kdb_jmpbuf(jb);
218 		if (setjmp(jb) == 0) {
219 			db_dot = PC_REGS();
220 			db_print_thread();
221 			if (bkpt)
222 				db_printf("Breakpoint at\t");
223 			else if (watchpt)
224 				db_printf("Watchpoint at\t");
225 			else
226 				db_printf("Stopped at\t");
227 			db_print_loc_and_inst(db_dot);
228 		}
229 		why = kdb_why;
230 		db_script_kdbenter(why != KDB_WHY_UNSET ? why : "unknown");
231 		db_command_loop();
232 		(void)kdb_jmpbuf(prev_jb);
233 	}
234 
235 	db_restart_at_pc(watchpt);
236 
237 	return (1);
238 }
239 
240 static void
241 db_trace_self_wrapper(void)
242 {
243 	jmp_buf jb;
244 	void *prev_jb;
245 
246 	prev_jb = kdb_jmpbuf(jb);
247 	if (setjmp(jb) == 0)
248 		db_trace_self();
249 	(void)kdb_jmpbuf(prev_jb);
250 }
251 
252 static void
253 db_trace_thread_wrapper(struct thread *td)
254 {
255 	jmp_buf jb;
256 	void *prev_jb;
257 
258 	prev_jb = kdb_jmpbuf(jb);
259 	if (setjmp(jb) == 0)
260 		db_trace_thread(td, -1);
261 	(void)kdb_jmpbuf(prev_jb);
262 }
263