pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / swifi / db_sym.c
blob8d2e2af444bdf3451808260abb99662a06ab6afb
1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
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.
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.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
26 * $Id: db_sym.c,v 1.2 2003/01/16 01:06:09 mikesw Exp $
30 * Author: David B. Golub, Carnegie Mellon University
31 * Date: 7/90
33 #if 0
34 //#include <sys/param.h>
35 //#include <sys/systm.h>
36 #endif
37 #if 0
38 #include <linux/kernel.h>
39 #include <linux/string.h>
40 #include <linux/kallsyms.h>
41 #endif
42 #include "ddb.h"
43 #include "db_sym.h"
44 #include "swifi.h"
46 #include "extra.h"
49 * Multiple symbol tables
51 #ifndef MAXNOSYMTABS
52 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
53 #endif
54 #if 0
56 static db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},};
57 static int db_nsymtab = 0;
59 static db_symtab_t *db_last_symtab;
61 static db_sym_t db_lookup __P(( char *symstr));
62 static char *db_qualify __P((db_sym_t sym, char *symtabname));
63 static boolean_t db_symbol_is_ambiguous __P((db_sym_t sym));
64 static boolean_t db_line_at_pc __P((db_sym_t, char **, int *,
65 db_expr_t));
68 * Add symbol table, with given name, to list of symbol tables.
70 void
71 db_add_symbol_table(start, end, name, ref)
72 char *start;
73 char *end;
74 char *name;
75 char *ref;
77 if (db_nsymtab >= MAXNOSYMTABS) {
78 printk ("No slots left for %s symbol table", name);
79 panic ("db_sym.c: db_add_symbol_table");
82 db_symtabs[db_nsymtab].start = start;
83 db_symtabs[db_nsymtab].end = end;
84 db_symtabs[db_nsymtab].name = name;
85 db_symtabs[db_nsymtab].private = ref;
86 db_nsymtab++;
90 * db_qualify("vm_map", "ux") returns "unix:vm_map".
92 * Note: return value points to static data whose content is
93 * overwritten by each call... but in practice this seems okay.
95 static char *
96 db_qualify(sym, symtabname)
97 db_sym_t sym;
98 register char *symtabname;
100 char *symname;
101 static char tmp[256];
103 db_symbol_values(sym, &symname, 0);
104 strcpy(tmp,symtabname);
105 strcat(tmp,":");
106 strcat(tmp,symname);
107 return tmp;
111 boolean_t
112 db_eqname(src, dst, c)
113 char *src;
114 char *dst;
115 char c;
117 if (!strcmp(src, dst))
118 return (TRUE);
119 if (src[0] == c)
120 return (!strcmp(src+1,dst));
121 return (FALSE);
124 boolean_t
125 db_value_of_name(name, valuep)
126 char *name;
127 db_expr_t *valuep;
129 db_sym_t sym;
131 sym = db_lookup(name);
132 if (sym == DB_SYM_NULL)
133 return (FALSE);
134 db_symbol_values(sym, &name, valuep);
135 return (TRUE);
140 * Lookup a symbol.
141 * If the symbol has a qualifier (e.g., ux:vm_map),
142 * then only the specified symbol table will be searched;
143 * otherwise, all symbol tables will be searched.
145 static db_sym_t
146 db_lookup(symstr)
147 char *symstr;
149 db_sym_t sp;
150 register int i;
151 int symtab_start = 0;
152 int symtab_end = db_nsymtab;
153 register char *cp;
156 * Look for, remove, and remember any symbol table specifier.
158 for (cp = symstr; *cp; cp++) {
159 if (*cp == ':') {
160 *cp = '\0';
161 for (i = 0; i < db_nsymtab; i++) {
162 if (! strcmp(symstr, db_symtabs[i].name)) {
163 symtab_start = i;
164 symtab_end = i + 1;
165 break;
168 *cp = ':';
169 if (i == db_nsymtab) {
170 db_error("invalid symbol table name");
172 symstr = cp+1;
177 * Look in the specified set of symbol tables.
178 * Return on first match.
180 for (i = symtab_start; i < symtab_end; i++) {
181 sp = X_db_lookup(&db_symtabs[i], symstr);
182 if (sp) {
183 db_last_symtab = &db_symtabs[i];
184 return sp;
187 return 0;
191 * Does this symbol name appear in more than one symbol table?
192 * Used by db_symbol_values to decide whether to qualify a symbol.
194 static boolean_t db_qualify_ambiguous_names = FALSE;
196 static boolean_t
197 db_symbol_is_ambiguous(sym)
198 db_sym_t sym;
200 char *sym_name;
201 register int i;
202 register
203 boolean_t found_once = FALSE;
205 if (!db_qualify_ambiguous_names)
206 return FALSE;
208 db_symbol_values(sym, &sym_name, 0);
209 for (i = 0; i < db_nsymtab; i++) {
210 if (X_db_lookup(&db_symtabs[i], sym_name)) {
211 if (found_once)
212 return TRUE;
213 found_once = TRUE;
216 return FALSE;
220 * Find the closest symbol to val, and return its name
221 * and the difference between val and the symbol found.
223 db_sym_t
224 db_search_symbol( val, strategy, offp)
225 register db_addr_t val;
226 db_strategy_t strategy;
227 db_expr_t *offp;
229 register
230 unsigned int diff;
231 unsigned int newdiff;
232 register int i;
233 db_sym_t ret = DB_SYM_NULL, sym;
235 newdiff = diff = ~0;
236 db_last_symtab = 0;
237 for (i = 0; i < db_nsymtab; i++) {
238 sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
239 if (newdiff < diff) {
240 db_last_symtab = &db_symtabs[i];
241 diff = newdiff;
242 ret = sym;
245 *offp = diff;
246 return ret;
250 * Return name and value of a symbol
252 void
253 db_symbol_values(sym, namep, valuep)
254 db_sym_t sym;
255 char **namep;
256 db_expr_t *valuep;
258 db_expr_t value;
260 if (sym == DB_SYM_NULL) {
261 *namep = 0;
262 return;
265 X_db_symbol_values(sym, namep, &value);
266 if (db_symbol_is_ambiguous(sym))
267 *namep = db_qualify(sym, db_last_symtab->name);
268 if (valuep)
269 *valuep = value;
274 * Print a the closest symbol to value
276 * After matching the symbol according to the given strategy
277 * we print it in the name+offset format, provided the symbol's
278 * value is close enough (eg smaller than db_maxoff).
279 * We also attempt to print [filename:linenum] when applicable
280 * (eg for procedure names).
282 * If we could not find a reasonable name+offset representation,
283 * then we just print the value in hex. Small values might get
284 * bogus symbol associations, e.g. 3 might get some absolute
285 * value like _INCLUDE_VERSION or something, therefore we do
286 * not accept symbols whose value is "small" (and use plain hex).
290 void
291 db_printsym(off, strategy)
292 db_expr_t off;
293 db_strategy_t strategy;
295 db_expr_t d;
296 char *filename;
297 char *name;
298 db_expr_t value;
299 int linenum;
300 db_sym_t cursym;
302 cursym = db_search_symbol(off, strategy, &d);
303 db_symbol_values(cursym, &name, &value);
304 if (name == 0)
305 value = off;
306 if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
307 printk("0x%x", off);
308 return;
310 if (name == 0 || d >= db_maxoff) {
311 printk("0x%x", off);
312 return;
314 printk("%s", name);
315 if (d)
316 printk("+0x%x", d);
317 if (strategy == DB_STGY_PROC) {
318 // if (db_line_at_pc(cursym, &filename, &linenum, off))
319 // printk(" [%s:%d]", filename, linenum);
323 #endif
325 unsigned int db_maxoff = 0x10000;
326 unsigned long modAddr = 0;
328 /* NWT: fault injection routine only.
329 * figure out start of function address given an address (off) in kernel text.
330 * name = function name
331 * value = function address
332 * d = difference between off and function address
333 * input is the desired address off and fault type
334 * returns closest instruction address (if found), NULL otherwise
336 unsigned long
337 find_faulty_instr(db_expr_t off, int type, int *instr_len)
339 db_expr_t d;
340 char *name;
341 db_expr_t value, cur_value, prev_value = 0;
342 int verbose=0, found=0;
343 const char * mod_name = NULL;
344 unsigned long mod_start;
345 unsigned long mod_end;
346 const char * sec_name = NULL;
347 unsigned long sec_start;
348 unsigned long sec_end;
349 const char * sym_name = NULL;
350 unsigned long sym_start;
351 unsigned long sym_end;
354 *instr_len = 0;
355 if (kallsyms_address_to_symbol(off,
356 &mod_name, &mod_start, &mod_end,
357 &sec_name, &sec_start, &sec_end,
358 &sym_name, &sym_start, &sym_end) == 0) {
359 return(0);
362 value = (db_expr_t) sym_start;
363 d = off - sym_start;
364 name = (char *) sym_name;
366 if (name == 0) {
367 value = off;
370 if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
371 printk("0x%x", off);
372 return 0;
375 if (name == 0 || d >= db_maxoff) {
376 printk("0x%x", off);
377 return 0 ;
379 /* 2) backup to start of function (SOF)
380 * 3) delineate instruction boundaries, find instruction length too.
383 if(verbose) {
384 printk("function %s", sym_name);
387 /* 4) skip instructions until we get to our faulty address */
388 cur_value = value;
389 while(cur_value < sec_end) {
390 if(verbose) {
391 #if 0
392 // db_printsym(cur_value, DB_STGY_PROC);
393 // printk(":\t");
394 #endif
396 prev_value=cur_value;
397 modAddr=0;
398 if(verbose) {
399 #if 0
400 //cur_value=db_disasm(prev_value, FALSE);
401 #endif
402 } else {
403 cur_value=my_disasm(prev_value, FALSE);
406 /* 4a) bail out if instruction is leave (0xc9) */
407 if(cur_value-prev_value == 1) {
408 unsigned char *c;
409 c=(unsigned char *) prev_value;
410 if(text_read_ub(c)==0xc9) {
411 if(verbose) printk("bailing out as we hit a leave\n");
412 found=0;
413 break;
416 /* 5a) init fault: from SOF, look for movl $X, -Y(%ebp),
417 * (C645Fxxx or C745Fxxx) and replace with nop.
419 if(type==INIT_FAULT) {
420 unsigned char *c;
421 c=(unsigned char *) prev_value;
423 if(*c==0x66 || *c==0x67)
424 c++; /* override prefix */
426 if(*c==0xC6 || *c==0xC7)
427 c++; /* movb or movl imm */
428 else
429 continue;
431 if(*c==0x45)
432 c++; /* [ebp] */
433 else
434 continue;
436 if(*c & 0x80)
437 found=1; /* negative displacement */
438 else
439 continue;
441 found=1;
442 break;
443 } else if(type==NOP_FAULT) {
444 /* 5b) nop*: replace instruction with nop */
445 if(cur_value> off) {
446 found=1;
447 break;
449 } else if(type==DST_FAULT || type==SRC_FAULT) {
450 /* 5c) dst/src: flip bits in mod/rm, sib, disp or imm fields */
451 if(cur_value>off && (cur_value-prev_value) > 1) {
452 found=1;
453 break;
455 } else if(type==BRANCH_FAULT || type==LOOP_FAULT) {
456 /* 5e) brc*: search forward utnil we hit a Jxx or rep (F3 or F2).
457 * replace instr with nop.
459 unsigned char *c;
461 c=(unsigned char *) prev_value;
463 /* look for repX prefix */
465 if(text_read_ub(c)==0xf3 || text_read_ub(c)==0xf2) {
466 if(verbose)
467 printk("found repX prefix\n");
468 /* take out repX prefix only */
469 found=1;
470 cur_value=prev_value+1;
471 break;
472 } else if( (text_read_ub(c)&0xf0)==0x70 ||
473 (text_read_ub(c)>=0xe0 && text_read_ub(c)<=0xe2) ) {
474 /* look for jXX 8 (7X), loop,jcx (e0-3), jXX 16/32 (0f 8X) */
475 found=1;
476 if(verbose)
477 printk("found jXX rel8, loop or jcx\n");
478 break;
479 } else if(text_read_ub(c)==0x66 ||
480 text_read_ub(c)==0x67) { /* override prefix */
481 c++;
482 } else if(text_read_ub(c++)==0xf && (text_read_ub(c)&0xf0)==0x80 ) {
483 found=1; /* 0x0f 0x8X */
484 if(verbose) printk("found branch!\n");
485 break;
487 } else if(type==PTR_FAULT) {
488 /* 5f) ptr: if instruction has regmodrm byte (i_has_modrm),
489 * and mod field has address ([eyy]dispxx), eyy!=ebp
490 * flip 1 bit in lower byte (0x0f) or any bit in following
491 * bytes (sib, imm or disp).
493 if(cur_value>off && modAddr) {
494 unsigned char *c;
495 c=(unsigned char *) modAddr;
496 if( text_read_ub(c)>0x3f && text_read_ub(c)<0xc0 &&
497 (text_read_ub(c)&7)!=5 ) {
498 found=1;
499 break;
502 } else if(type==INTERFACE_FAULT) {
503 /* 5f) i/f: look for movl XX(ebp), reg or movb XX(ebp), reg,
504 * where XX is positive. replace instr with nop.
505 * movl=0x8a, movb=0x8b, mod=01XXX101 (disp8[ebp]), disp>0
507 unsigned char *c;
508 c=(unsigned char *) prev_value;
509 if( text_read_ub(c)==0x8a || text_read_ub(c)==0x8b) {
510 c++;
511 if( ((text_read_ub(c++))&0xc7)==0x45 && (text_read_ub(c)&0x80)==0 ) {
512 /* 75% chance that we'll choose the next arg */
513 if(random()&0x3) {
514 found=1;
515 break;
516 } else {
517 if(verbose) printk("skipped...\n");
521 }else if(type==IRQ_FAULT) {
522 /* 5g) i/f: look for push reg or offset(reg) / popf,
523 * where XX is positive. replace instr with nop.
524 * movl=0x8a, movb=0x8b, mod=01XXX101 (disp8[ebp]), disp>0
526 unsigned char *c;
527 c=(unsigned char *) prev_value;
528 if (((text_read_ub(c) & 0xf8) == 0x50) ||
529 (text_read_ub(c) == 0xff)) {
530 if (text_read_ub(c) == 0xff) {
531 c++;
532 #if 0
534 // Look for push x(ebp)
535 #endif
536 if ((text_read_ub(c) & 0x78) != 0x70) {
537 continue;
540 // Skip the offset
542 c++;
544 c++;
545 if (text_read_ub(c) == 0x9d) {
547 // Increment cur_value to include the
548 // popf instruction
550 cur_value++;
551 found = 1;
552 break;
558 /* if we're doing nop fault, then we're done.
560 if(found) {
561 *instr_len=cur_value-prev_value;
562 off=prev_value;
563 if(verbose) {
564 printk("%s", name);
565 if (d) printk("+0x%x", d);
566 printk(" @ %x, ", value);
567 printk("instr @ %x, len=%d, ", off, *instr_len);
568 #if 0
569 // db_disasm(prev_value, FALSE);
570 #endif
572 return off;
573 } else {
574 if(verbose) printk("cannot locate instruction in function\n");
575 *instr_len=0;
576 return 0;
580 #if 0
581 static boolean_t
582 db_line_at_pc( sym, filename, linenum, pc)
583 db_sym_t sym;
584 char **filename;
585 int *linenum;
586 db_expr_t pc;
588 return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
592 db_sym_numargs(sym, nargp, argnames)
593 db_sym_t sym;
594 int *nargp;
595 char **argnames;
597 return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
600 #endif