2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
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.
28 * Author: David B. Golub, Carnegie Mellon University
32 //#include <sys/param.h>
33 //#include <sys/systm.h>
36 #include <linux/kernel.h>
37 #include <linux/string.h>
38 #include <linux/kallsyms.h>
47 * Multiple symbol tables
50 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
54 static db_symtab_t db_symtabs
[MAXNOSYMTABS
] = {{0,},};
55 static int db_nsymtab
= 0;
57 static db_symtab_t
*db_last_symtab
;
59 static db_sym_t db_lookup
__P(( char *symstr
));
60 static char *db_qualify
__P((db_sym_t sym
, char *symtabname
));
61 static boolean_t db_symbol_is_ambiguous
__P((db_sym_t sym
));
62 static boolean_t db_line_at_pc
__P((db_sym_t
, char **, int *,
66 * Add symbol table, with given name, to list of symbol tables.
69 db_add_symbol_table(start
, end
, name
, ref
)
75 if (db_nsymtab
>= MAXNOSYMTABS
) {
76 printk ("No slots left for %s symbol table", name
);
77 panic ("db_sym.c: db_add_symbol_table");
80 db_symtabs
[db_nsymtab
].start
= start
;
81 db_symtabs
[db_nsymtab
].end
= end
;
82 db_symtabs
[db_nsymtab
].name
= name
;
83 db_symtabs
[db_nsymtab
].private = ref
;
88 * db_qualify("vm_map", "ux") returns "unix:vm_map".
90 * Note: return value points to static data whose content is
91 * overwritten by each call... but in practice this seems okay.
94 db_qualify(sym
, symtabname
)
96 register char *symtabname
;
101 db_symbol_values(sym
, &symname
, 0);
102 strcpy(tmp
,symtabname
);
110 db_eqname(src
, dst
, c
)
115 if (!strcmp(src
, dst
))
118 return (!strcmp(src
+1,dst
));
123 db_value_of_name(name
, valuep
)
129 sym
= db_lookup(name
);
130 if (sym
== DB_SYM_NULL
)
132 db_symbol_values(sym
, &name
, valuep
);
139 * If the symbol has a qualifier (e.g., ux:vm_map),
140 * then only the specified symbol table will be searched;
141 * otherwise, all symbol tables will be searched.
149 int symtab_start
= 0;
150 int symtab_end
= db_nsymtab
;
154 * Look for, remove, and remember any symbol table specifier.
156 for (cp
= symstr
; *cp
; cp
++) {
159 for (i
= 0; i
< db_nsymtab
; i
++) {
160 if (! strcmp(symstr
, db_symtabs
[i
].name
)) {
167 if (i
== db_nsymtab
) {
168 db_error("invalid symbol table name");
175 * Look in the specified set of symbol tables.
176 * Return on first match.
178 for (i
= symtab_start
; i
< symtab_end
; i
++) {
179 sp
= X_db_lookup(&db_symtabs
[i
], symstr
);
181 db_last_symtab
= &db_symtabs
[i
];
189 * Does this symbol name appear in more than one symbol table?
190 * Used by db_symbol_values to decide whether to qualify a symbol.
192 static boolean_t db_qualify_ambiguous_names
= FALSE
;
195 db_symbol_is_ambiguous(sym
)
201 boolean_t found_once
= FALSE
;
203 if (!db_qualify_ambiguous_names
)
206 db_symbol_values(sym
, &sym_name
, 0);
207 for (i
= 0; i
< db_nsymtab
; i
++) {
208 if (X_db_lookup(&db_symtabs
[i
], sym_name
)) {
218 * Find the closest symbol to val, and return its name
219 * and the difference between val and the symbol found.
222 db_search_symbol( val
, strategy
, offp
)
223 register db_addr_t val
;
224 db_strategy_t strategy
;
229 unsigned int newdiff
;
231 db_sym_t ret
= DB_SYM_NULL
, sym
;
235 for (i
= 0; i
< db_nsymtab
; i
++) {
236 sym
= X_db_search_symbol(&db_symtabs
[i
], val
, strategy
, &newdiff
);
237 if (newdiff
< diff
) {
238 db_last_symtab
= &db_symtabs
[i
];
248 * Return name and value of a symbol
251 db_symbol_values(sym
, namep
, valuep
)
258 if (sym
== DB_SYM_NULL
) {
263 X_db_symbol_values(sym
, namep
, &value
);
264 if (db_symbol_is_ambiguous(sym
))
265 *namep
= db_qualify(sym
, db_last_symtab
->name
);
272 * Print a the closest symbol to value
274 * After matching the symbol according to the given strategy
275 * we print it in the name+offset format, provided the symbol's
276 * value is close enough (eg smaller than db_maxoff).
277 * We also attempt to print [filename:linenum] when applicable
278 * (eg for procedure names).
280 * If we could not find a reasonable name+offset representation,
281 * then we just print the value in hex. Small values might get
282 * bogus symbol associations, e.g. 3 might get some absolute
283 * value like _INCLUDE_VERSION or something, therefore we do
284 * not accept symbols whose value is "small" (and use plain hex).
289 db_printsym(off
, strategy
)
291 db_strategy_t strategy
;
300 cursym
= db_search_symbol(off
, strategy
, &d
);
301 db_symbol_values(cursym
, &name
, &value
);
304 if (value
>= DB_SMALL_VALUE_MIN
&& value
<= DB_SMALL_VALUE_MAX
) {
308 if (name
== 0 || d
>= db_maxoff
) {
315 if (strategy
== DB_STGY_PROC
) {
316 // if (db_line_at_pc(cursym, &filename, &linenum, off))
317 // printk(" [%s:%d]", filename, linenum);
323 unsigned int db_maxoff
= 0x10000;
324 unsigned long modAddr
= 0;
326 /* NWT: fault injection routine only.
327 * figure out start of function address given an address (off) in kernel text.
328 * name = function name
329 * value = function address
330 * d = difference between off and function address
331 * input is the desired address off and fault type
332 * returns closest instruction address (if found), NULL otherwise
335 find_faulty_instr(db_expr_t off
, int type
, int *instr_len
)
339 db_expr_t value
, cur_value
, prev_value
= 0;
340 int verbose
=0, found
=0;
341 const char * mod_name
= NULL
;
342 unsigned long mod_start
;
343 unsigned long mod_end
;
344 const char * sec_name
= NULL
;
345 unsigned long sec_start
;
346 unsigned long sec_end
;
347 const char * sym_name
= NULL
;
348 unsigned long sym_start
;
349 unsigned long sym_end
;
353 if (kallsyms_address_to_symbol(off
,
354 &mod_name
, &mod_start
, &mod_end
,
355 &sec_name
, &sec_start
, &sec_end
,
356 &sym_name
, &sym_start
, &sym_end
) == 0) {
360 value
= (db_expr_t
) sym_start
;
362 name
= (char *) sym_name
;
368 if (value
>= DB_SMALL_VALUE_MIN
&& value
<= DB_SMALL_VALUE_MAX
) {
373 if (name
== 0 || d
>= db_maxoff
) {
377 /* 2) backup to start of function (SOF)
378 * 3) delineate instruction boundaries, find instruction length too.
382 printk("function %s", sym_name
);
385 /* 4) skip instructions until we get to our faulty address */
387 while(cur_value
< sec_end
) {
390 // db_printsym(cur_value, DB_STGY_PROC);
394 prev_value
=cur_value
;
398 //cur_value=db_disasm(prev_value, FALSE);
401 cur_value
=my_disasm(prev_value
, FALSE
);
404 /* 4a) bail out if instruction is leave (0xc9) */
405 if(cur_value
-prev_value
== 1) {
407 c
=(unsigned char *) prev_value
;
408 if(text_read_ub(c
)==0xc9) {
409 if(verbose
) printk("bailing out as we hit a leave\n");
414 /* 5a) init fault: from SOF, look for movl $X, -Y(%ebp),
415 * (C645Fxxx or C745Fxxx) and replace with nop.
417 if(type
==INIT_FAULT
) {
419 c
=(unsigned char *) prev_value
;
421 if(*c
==0x66 || *c
==0x67)
422 c
++; /* override prefix */
424 if(*c
==0xC6 || *c
==0xC7)
425 c
++; /* movb or movl imm */
435 found
=1; /* negative displacement */
441 } else if(type
==NOP_FAULT
) {
442 /* 5b) nop*: replace instruction with nop */
447 } else if(type
==DST_FAULT
|| type
==SRC_FAULT
) {
448 /* 5c) dst/src: flip bits in mod/rm, sib, disp or imm fields */
449 if(cur_value
>off
&& (cur_value
-prev_value
) > 1) {
453 } else if(type
==BRANCH_FAULT
|| type
==LOOP_FAULT
) {
454 /* 5e) brc*: search forward utnil we hit a Jxx or rep (F3 or F2).
455 * replace instr with nop.
459 c
=(unsigned char *) prev_value
;
461 /* look for repX prefix */
463 if(text_read_ub(c
)==0xf3 || text_read_ub(c
)==0xf2) {
465 printk("found repX prefix\n");
466 /* take out repX prefix only */
468 cur_value
=prev_value
+1;
470 } else if( (text_read_ub(c
)&0xf0)==0x70 ||
471 (text_read_ub(c
)>=0xe0 && text_read_ub(c
)<=0xe2) ) {
472 /* look for jXX 8 (7X), loop,jcx (e0-3), jXX 16/32 (0f 8X) */
475 printk("found jXX rel8, loop or jcx\n");
477 } else if(text_read_ub(c
)==0x66 ||
478 text_read_ub(c
)==0x67) { /* override prefix */
480 } else if(text_read_ub(c
++)==0xf && (text_read_ub(c
)&0xf0)==0x80 ) {
481 found
=1; /* 0x0f 0x8X */
482 if(verbose
) printk("found branch!\n");
485 } else if(type
==PTR_FAULT
) {
486 /* 5f) ptr: if instruction has regmodrm byte (i_has_modrm),
487 * and mod field has address ([eyy]dispxx), eyy!=ebp
488 * flip 1 bit in lower byte (0x0f) or any bit in following
489 * bytes (sib, imm or disp).
491 if(cur_value
>off
&& modAddr
) {
493 c
=(unsigned char *) modAddr
;
494 if( text_read_ub(c
)>0x3f && text_read_ub(c
)<0xc0 &&
495 (text_read_ub(c
)&7)!=5 ) {
500 } else if(type
==INTERFACE_FAULT
) {
501 /* 5f) i/f: look for movl XX(ebp), reg or movb XX(ebp), reg,
502 * where XX is positive. replace instr with nop.
503 * movl=0x8a, movb=0x8b, mod=01XXX101 (disp8[ebp]), disp>0
506 c
=(unsigned char *) prev_value
;
507 if( text_read_ub(c
)==0x8a || text_read_ub(c
)==0x8b) {
509 if( ((text_read_ub(c
++))&0xc7)==0x45 && (text_read_ub(c
)&0x80)==0 ) {
510 /* 75% chance that we'll choose the next arg */
515 if(verbose
) printk("skipped...\n");
519 }else if(type
==IRQ_FAULT
) {
520 /* 5g) i/f: look for push reg or offset(reg) / popf,
521 * where XX is positive. replace instr with nop.
522 * movl=0x8a, movb=0x8b, mod=01XXX101 (disp8[ebp]), disp>0
525 c
=(unsigned char *) prev_value
;
526 if (((text_read_ub(c
) & 0xf8) == 0x50) ||
527 (text_read_ub(c
) == 0xff)) {
528 if (text_read_ub(c
) == 0xff) {
532 // Look for push x(ebp)
534 if ((text_read_ub(c
) & 0x78) != 0x70) {
543 if (text_read_ub(c
) == 0x9d) {
545 // Increment cur_value to include the
556 /* if we're doing nop fault, then we're done.
559 *instr_len
=cur_value
-prev_value
;
563 if (d
) printk("+0x%x", d
);
564 printk(" @ %x, ", value
);
565 printk("instr @ %x, len=%d, ", off
, *instr_len
);
567 // db_disasm(prev_value, FALSE);
572 if(verbose
) printk("cannot locate instruction in function\n");
580 db_line_at_pc( sym
, filename
, linenum
, pc
)
586 return X_db_line_at_pc( db_last_symtab
, sym
, filename
, linenum
, pc
);
590 db_sym_numargs(sym
, nargp
, argnames
)
595 return X_db_sym_numargs(db_last_symtab
, sym
, nargp
, argnames
);