xref: /freebsd/sys/ddb/db_main.c (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
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 
40 #include <machine/kdb.h>
41 #include <machine/pcb.h>
42 #include <machine/setjmp.h>
43 
44 #include <ddb/ddb.h>
45 #include <ddb/db_command.h>
46 #include <ddb/db_sym.h>
47 
48 static dbbe_init_f db_init;
49 static dbbe_trap_f db_trap;
50 static dbbe_trace_f db_trace_self_wrapper;
51 
52 KDB_BACKEND(ddb, db_init, db_trace_self_wrapper, db_trap);
53 
54 vm_offset_t ksym_start, ksym_end;
55 
56 boolean_t
57 X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line,
58     db_expr_t off)
59 {
60 	return (FALSE);
61 }
62 
63 c_db_sym_t
64 X_db_lookup(db_symtab_t *symtab, const char *symbol)
65 {
66 	c_linker_sym_t lsym;
67 	Elf_Sym *sym;
68 
69 	if (symtab->private == NULL) {
70 		return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym))
71 			? lsym : NULL));
72 	} else {
73 		sym = (Elf_Sym *)symtab->start;
74 		while ((char *)sym < symtab->end) {
75 			if (sym->st_name != 0 &&
76 			    !strcmp(symtab->private + sym->st_name, symbol))
77 				return ((c_db_sym_t)sym);
78 			sym++;
79 		}
80 	}
81 	return (NULL);
82 }
83 
84 c_db_sym_t
85 X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat,
86     db_expr_t *diffp)
87 {
88 	c_linker_sym_t lsym;
89 	Elf_Sym *sym, *match;
90 	unsigned long diff;
91 
92 	if (symtab->private == NULL) {
93 		if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) {
94 			*diffp = (db_expr_t)diff;
95 			return ((c_db_sym_t)lsym);
96 		}
97 		return (NULL);
98 	}
99 
100 	diff = ~0UL;
101 	match = NULL;
102 	for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) {
103 		if (sym->st_name == 0)
104 			continue;
105 		if (off < sym->st_value)
106 			continue;
107 		if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
108 		    ELF_ST_TYPE(sym->st_info) != STT_FUNC &&
109 		    ELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
110 			continue;
111 		if ((off - sym->st_value) > diff)
112 			continue;
113 		if ((off - sym->st_value) < diff) {
114 			diff = off - sym->st_value;
115 			match = sym;
116 		} else {
117 			if (match == NULL)
118 				match = sym;
119 			else if (ELF_ST_BIND(match->st_info) == STB_LOCAL &&
120 			    ELF_ST_BIND(sym->st_info) != STB_LOCAL)
121 				match = sym;
122 		}
123 		if (diff == 0) {
124 			if (strat == DB_STGY_PROC &&
125 			    ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
126 			    ELF_ST_BIND(sym->st_info) != STB_LOCAL)
127 				break;
128 			if (strat == DB_STGY_ANY &&
129 			    ELF_ST_BIND(sym->st_info) != STB_LOCAL)
130 				break;
131 		}
132 	}
133 
134 	*diffp = (match == NULL) ? off : diff;
135 	return ((c_db_sym_t)match);
136 }
137 
138 boolean_t
139 X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp,
140     char **argp)
141 {
142 	return (FALSE);
143 }
144 
145 void
146 X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep,
147     db_expr_t *valp)
148 {
149 	linker_symval_t lval;
150 
151 	if (symtab->private == NULL) {
152 		linker_ddb_symbol_values((c_linker_sym_t)sym, &lval);
153 		if (namep != NULL)
154 			*namep = (const char*)lval.name;
155 		if (valp != NULL)
156 			*valp = (db_expr_t)lval.value;
157 	} else {
158 		if (namep != NULL)
159 			*namep = (const char *)symtab->private +
160 			    ((const Elf_Sym *)sym)->st_name;
161 		if (valp != NULL)
162 			*valp = (db_expr_t)((const Elf_Sym *)sym)->st_value;
163 	}
164 }
165 
166 static int
167 db_init(void)
168 {
169 	uintptr_t symtab, strtab;
170 	Elf_Size tabsz, strsz;
171 
172 	if (ksym_end > ksym_start && ksym_start != 0) {
173 		symtab = ksym_start;
174 		tabsz = *((Elf_Size*)symtab);
175 		symtab += sizeof(Elf_Size);
176 		strtab = symtab + tabsz;
177 		strsz = *((Elf_Size*)strtab);
178 		strtab += sizeof(Elf_Size);
179 		if (strtab + strsz <= ksym_end) {
180 			db_add_symbol_table((char *)symtab,
181 			    (char *)(symtab + tabsz), "elf", (char *)strtab);
182 		}
183 	}
184 	db_add_symbol_table(NULL, NULL, "kld", NULL);
185 	return (1);	/* We're the default debugger. */
186 }
187 
188 static int
189 db_trap(int type, int code)
190 {
191 	jmp_buf jb;
192 	void *prev_jb;
193 	boolean_t bkpt, watchpt;
194 
195 	/*
196 	 * Don't handle the trap if the console is unavailable (i.e. it
197 	 * is in graphics mode).
198 	 */
199 	if (cnunavailable())
200 		return (0);
201 
202 	bkpt = IS_BREAKPOINT_TRAP(type, code);
203 	watchpt = IS_WATCHPOINT_TRAP(type, code);
204 
205 	if (db_stop_at_pc(&bkpt)) {
206 		if (db_inst_count) {
207 			db_printf("After %d instructions (%d loads, %d stores),\n",
208 			    db_inst_count, db_load_count, db_store_count);
209 		}
210 		prev_jb = kdb_jmpbuf(jb);
211 		if (setjmp(jb) == 0) {
212 			db_dot = PC_REGS();
213 			db_print_thread();
214 			if (bkpt)
215 				db_printf("Breakpoint at\t");
216 			else if (watchpt)
217 				db_printf("Watchpoint at\t");
218 			else
219 				db_printf("Stopped at\t");
220 			db_print_loc_and_inst(db_dot);
221 		}
222 		db_command_loop();
223 		(void)kdb_jmpbuf(prev_jb);
224 	}
225 
226 	db_restart_at_pc(watchpt);
227 
228 	return (1);
229 }
230 
231 static void
232 db_trace_self_wrapper(void)
233 {
234 	jmp_buf jb;
235 	void *prev_jb;
236 
237 	prev_jb = kdb_jmpbuf(jb);
238 	if (setjmp(jb) == 0)
239 		db_trace_self();
240 	(void)kdb_jmpbuf(prev_jb);
241 }
242