No empty .Rs/.Re
[netbsd-mini2440.git] / sys / ddb / db_aout.c
blob70f424f166101fbb778102eaad82fcbe86bc71da
1 /* $NetBSD: db_aout.c,v 1.40 2007/02/21 22:59:56 thorpej Exp $ */
3 /*
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * All Rights Reserved.
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: db_aout.c,v 1.40 2007/02/21 22:59:56 thorpej Exp $");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/proc.h>
36 #include <machine/db_machdep.h>
38 #include <ddb/db_sym.h>
39 #include <ddb/db_output.h>
40 #include <ddb/db_extern.h>
42 #ifdef DB_AOUT_SYMBOLS
44 #include <ddb/db_aout.h>
46 static bool db_aout_sym_init(int, void *, void *, const char *);
47 static db_sym_t db_aout_lookup(db_symtab_t *, const char *);
48 static db_sym_t db_aout_search_symbol(db_symtab_t *, db_addr_t, db_strategy_t,
49 db_expr_t *);
50 static void db_aout_symbol_values(db_symtab_t *, db_sym_t, const char **,
51 db_expr_t *);
52 static bool db_aout_line_at_pc(db_symtab_t *, db_sym_t, char **, int *,
53 db_expr_t);
54 static bool db_aout_sym_numargs(db_symtab_t *, db_sym_t, int *, char **);
55 static void db_aout_forall(db_symtab_t *, db_forall_func_t db_forall_func,
56 void *);
57 static int db_add_symbol_table(char *, char *, const char *, char *);
59 /* Only one symbol table, please */
60 static db_symtab_t db_symtabs;
63 const db_symformat_t db_symformat_aout = {
64 "a.out",
65 db_aout_sym_init,
66 db_aout_lookup,
67 db_aout_search_symbol,
68 db_aout_symbol_values,
69 db_aout_line_at_pc,
70 db_aout_sym_numargs,
71 db_aout_forall
75 * An a.out symbol table as loaded into the kernel debugger:
77 * symtab -> size of symbol entries, in bytes
78 * sp -> first symbol entry
79 * ...
80 * ep -> last symbol entry + 1
81 * strtab == start of string table
82 * size of string table in bytes,
83 * including this word
84 * -> strings
88 * Find the symbol table and strings; tell ddb about them.
90 static bool
91 db_aout_sym_init(
92 int symsize, /* size of symbol table */
93 void *vsymtab, /* pointer to start of symbol table */
94 void *vesymtab, /* pointer to end of string table,
95 for checking - rounded up to integer
96 boundary */
97 const char *name
100 struct nlist *sym_start, *sym_end;
101 struct nlist *sp;
102 char *strtab;
103 int slen, bad = 0;
104 char *estrtab;
106 if (ALIGNED_POINTER(vsymtab, long) == 0) {
107 printf("[ %s symbol table has bad start address %p ]\n",
108 name, vsymtab);
109 return (false);
113 * Find pointers to the start and end of the symbol entries,
114 * given a pointer to the start of the symbol table.
116 sym_start = (struct nlist *)vsymtab;
117 sym_end = (struct nlist *)((char *)sym_start + symsize);
119 strtab = (char *)sym_end;
120 if (ALIGNED_POINTER(strtab, int) == 0) {
121 printf("[ %s symbol table has bad string table address %p ]\n",
122 name, strtab);
123 return (false);
125 slen = *(int *)strtab;
127 estrtab = strtab + slen;
129 #define round_to_size(x) \
130 (((vaddr_t)(x) + sizeof(vsize_t) - 1) & ~(sizeof(vsize_t) - 1))
132 if (round_to_size(estrtab) != round_to_size(vesymtab)) {
133 printf("[ %s a.out symbol table not valid ]\n", name);
134 return (false);
136 #undef round_to_size
138 for (sp = sym_start; sp < sym_end; sp++) {
139 int strx;
140 strx = sp->n_un.n_strx;
141 if (strx != 0) {
142 if (strx > slen) {
143 printf("[ %s has bad a.out string table "
144 "index (0x%x) ]\n", name, strx);
145 sp->n_un.n_name = 0;
146 bad = 1;
147 continue;
149 sp->n_un.n_name = strtab + strx;
153 if (bad)
154 return (false);
156 if (db_add_symbol_table((char *)sym_start, (char *)sym_end, name,
157 NULL) != -1) {
158 printf("[ using %ld bytes of %s a.out symbol table ]\n",
159 (long)vesymtab - (long)vsymtab, name);
160 return (true);
163 return (false);
166 static db_sym_t
167 db_aout_lookup(db_symtab_t *stab, const char *symstr)
169 struct nlist *sp, *ep;
171 stab = &db_symtabs;
173 sp = (struct nlist *)stab->start;
174 ep = (struct nlist *)stab->end;
176 for (; sp < ep; sp++) {
177 if (sp->n_un.n_name == 0)
178 continue;
179 if ((sp->n_type & N_STAB) == 0 &&
180 sp->n_un.n_name != 0 &&
181 db_eqname(sp->n_un.n_name, symstr, '_'))
182 return ((db_sym_t)sp);
184 return ((db_sym_t)0);
187 static db_sym_t
188 db_aout_search_symbol(db_symtab_t *symtab, db_addr_t off,
189 db_strategy_t strategy, db_expr_t *diffp)
191 unsigned int diff = *diffp;
192 struct nlist *symp = 0;
193 struct nlist *sp, *ep;
195 symtab = &db_symtabs;
197 sp = (struct nlist *)symtab->start;
198 ep = (struct nlist *)symtab->end;
200 for (; sp < ep; sp++) {
201 if (sp->n_un.n_name == 0)
202 continue;
203 if ((sp->n_type & N_STAB) != 0 || (sp->n_type & N_TYPE) == N_FN)
204 continue;
205 if (off >= sp->n_value) {
206 if (off - sp->n_value < diff) {
207 diff = off - sp->n_value;
208 symp = sp;
209 if (diff == 0 &&
210 ((strategy == DB_STGY_PROC &&
211 sp->n_type == (N_TEXT|N_EXT)) ||
212 (strategy == DB_STGY_ANY &&
213 (sp->n_type & N_EXT))))
214 break;
216 else if (off - sp->n_value == diff) {
217 if (symp == 0)
218 symp = sp;
219 else if ((symp->n_type & N_EXT) == 0 &&
220 (sp->n_type & N_EXT) != 0)
221 /* pick the external symbol */
222 symp = sp;
226 if (symp == 0)
227 *diffp = off;
228 else
229 *diffp = diff;
230 return ((db_sym_t)symp);
234 * Return the name and value for a symbol.
236 static void
237 db_aout_symbol_values(db_symtab_t *symtab, db_sym_t sym,
238 const char **namep, db_expr_t *valuep)
240 struct nlist *sp;
242 sp = (struct nlist *)sym;
243 if (namep)
244 *namep = sp->n_un.n_name;
245 if (valuep)
246 *valuep = sp->n_value;
250 static bool
251 db_aout_line_at_pc(db_symtab_t *symtab, db_sym_t cursym,
252 char **filename, int *linenum, db_expr_t off)
254 struct nlist *sp, *ep;
255 unsigned long sodiff = -1UL, lndiff = -1UL, ln = 0;
256 char *fname = NULL;
258 symtab = &db_symtabs;
260 sp = (struct nlist *)symtab->start;
261 ep = (struct nlist *)symtab->end;
263 /* XXX - gcc specific */
264 #define NEWSRC(str) ((str) != NULL && \
265 (str)[0] == 'g' && strcmp((str), "gcc_compiled.") == 0)
267 for (; sp < ep; sp++) {
270 * Prevent bogus linenumbers in case module not compiled
271 * with debugging options
273 #if 0
274 if (sp->n_value <= off && (off - sp->n_value) <= sodiff &&
275 NEWSRC(sp->n_un.n_name)) {
276 #endif
277 if ((sp->n_type & N_TYPE) == N_FN || NEWSRC(sp->n_un.n_name)) {
278 sodiff = lndiff = -1UL;
279 ln = 0;
280 fname = NULL;
283 if (sp->n_type == N_SO) {
284 if (sp->n_value <= off && (off - sp->n_value) < sodiff) {
285 sodiff = off - sp->n_value;
286 fname = sp->n_un.n_name;
288 continue;
291 if (sp->n_type != N_SLINE)
292 continue;
294 if (sp->n_value > off)
295 break;
297 if (off - sp->n_value < lndiff) {
298 lndiff = off - sp->n_value;
299 ln = sp->n_desc;
303 if (fname != NULL && ln != 0) {
304 *filename = fname;
305 *linenum = ln;
306 return true;
309 return (false);
312 static bool
313 db_aout_sym_numargs(db_symtab_t *symtab, db_sym_t cursym, int *nargp,
314 char **argnamep)
316 struct nlist *sp, *ep;
317 u_long addr;
318 int maxnarg = *nargp, nargs = 0;
319 static char question[] = "???";
321 if ((struct nlist *)cursym == NULL)
322 return false;
324 symtab = &db_symtabs;
326 addr = ((struct nlist *)cursym)->n_value;
327 sp = (struct nlist *)symtab->start;
328 ep = (struct nlist *)symtab->end;
330 for (; sp < ep; sp++) {
331 if (sp->n_type == N_FUN && sp->n_value == addr) {
332 while (++sp < ep && sp->n_type == N_PSYM) {
333 if (nargs >= maxnarg)
334 break;
335 nargs++;
336 *argnamep++ = sp->n_un.n_name ?
337 sp->n_un.n_name : question;
339 /* XXX - remove trailers */
340 char *cp = *(argnamep-1);
341 while (*cp != '\0' && *cp != ':') cp++;
342 if (*cp == ':') *cp = '\0';
345 *nargp = nargs;
346 return true;
349 return false;
352 static void
353 db_aout_forall(db_symtab_t *stab, db_forall_func_t db_forall_func, void *arg)
355 static char suffix[2];
356 struct nlist *sp, *ep;
358 stab = &db_symtabs;
360 sp = (struct nlist *)stab->start;
361 ep = (struct nlist *)stab->end;
363 for (; sp < ep; sp++) {
364 if (sp->n_un.n_name == 0)
365 continue;
366 if ((sp->n_type & N_STAB) == 0 && sp->n_un.n_name != 0) {
367 suffix[1] = '\0';
368 switch(sp->n_type & N_TYPE) {
369 case N_ABS:
370 suffix[0] = '@';
371 break;
372 case N_TEXT:
373 suffix[0] = '*';
374 break;
375 case N_DATA:
376 suffix[0] = '+';
377 break;
378 case N_BSS:
379 suffix[0] = '-';
380 break;
381 case N_FN:
382 suffix[0] = '/';
383 break;
384 default:
385 suffix[0] = '\0';
387 (*db_forall_func)(stab, (db_sym_t)sp, sp->n_un.n_name,
388 suffix, '_', arg);
391 return;
395 * Add symbol table, with given name, to symbol tables.
398 db_add_symbol_table(char *start, char *end, const char *name, char *ref)
401 db_symtabs.start = start;
402 db_symtabs.end = end;
403 db_symtabs.name = name;
404 db_symtabs.private = ref;
406 return(0);
409 #endif /* DB_AOUT_SYMBOLS */