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