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