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.
26 * $Id: db_sym.c,v 1.2 2003/01/16 01:06:09 mikesw Exp $
30 * Author: David B. Golub, Carnegie Mellon University
34 //#include <sys/param.h>
35 //#include <sys/systm.h>
38 #include <linux/kernel.h>
39 #include <linux/string.h>
40 #include <linux/kallsyms.h>
49 * Multiple symbol tables
52 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
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 *,
68 * Add symbol table, with given name, to list of symbol tables.
71 db_add_symbol_table(start
, end
, name
, 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
;
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.
96 db_qualify(sym
, symtabname
)
98 register char *symtabname
;
101 static char tmp
[256];
103 db_symbol_values(sym
, &symname
, 0);
104 strcpy(tmp
,symtabname
);
112 db_eqname(src
, dst
, c
)
117 if (!strcmp(src
, dst
))
120 return (!strcmp(src
+1,dst
));
125 db_value_of_name(name
, valuep
)
131 sym
= db_lookup(name
);
132 if (sym
== DB_SYM_NULL
)
134 db_symbol_values(sym
, &name
, valuep
);
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.
151 int symtab_start
= 0;
152 int symtab_end
= db_nsymtab
;
156 * Look for, remove, and remember any symbol table specifier.
158 for (cp
= symstr
; *cp
; cp
++) {
161 for (i
= 0; i
< db_nsymtab
; i
++) {
162 if (! strcmp(symstr
, db_symtabs
[i
].name
)) {
169 if (i
== db_nsymtab
) {
170 db_error("invalid symbol table name");
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
);
183 db_last_symtab
= &db_symtabs
[i
];
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
;
197 db_symbol_is_ambiguous(sym
)
203 boolean_t found_once
= FALSE
;
205 if (!db_qualify_ambiguous_names
)
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
)) {
220 * Find the closest symbol to val, and return its name
221 * and the difference between val and the symbol found.
224 db_search_symbol( val
, strategy
, offp
)
225 register db_addr_t val
;
226 db_strategy_t strategy
;
231 unsigned int newdiff
;
233 db_sym_t ret
= DB_SYM_NULL
, sym
;
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
];
250 * Return name and value of a symbol
253 db_symbol_values(sym
, namep
, valuep
)
260 if (sym
== DB_SYM_NULL
) {
265 X_db_symbol_values(sym
, namep
, &value
);
266 if (db_symbol_is_ambiguous(sym
))
267 *namep
= db_qualify(sym
, db_last_symtab
->name
);
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).
291 db_printsym(off
, strategy
)
293 db_strategy_t strategy
;
302 cursym
= db_search_symbol(off
, strategy
, &d
);
303 db_symbol_values(cursym
, &name
, &value
);
306 if (value
>= DB_SMALL_VALUE_MIN
&& value
<= DB_SMALL_VALUE_MAX
) {
310 if (name
== 0 || d
>= db_maxoff
) {
317 if (strategy
== DB_STGY_PROC
) {
318 // if (db_line_at_pc(cursym, &filename, &linenum, off))
319 // printk(" [%s:%d]", filename, linenum);
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
337 find_faulty_instr(db_expr_t off
, int type
, int *instr_len
)
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
;
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) {
362 value
= (db_expr_t
) sym_start
;
364 name
= (char *) sym_name
;
370 if (value
>= DB_SMALL_VALUE_MIN
&& value
<= DB_SMALL_VALUE_MAX
) {
375 if (name
== 0 || d
>= db_maxoff
) {
379 /* 2) backup to start of function (SOF)
380 * 3) delineate instruction boundaries, find instruction length too.
384 printk("function %s", sym_name
);
387 /* 4) skip instructions until we get to our faulty address */
389 while(cur_value
< sec_end
) {
392 // db_printsym(cur_value, DB_STGY_PROC);
396 prev_value
=cur_value
;
400 //cur_value=db_disasm(prev_value, FALSE);
403 cur_value
=my_disasm(prev_value
, FALSE
);
406 /* 4a) bail out if instruction is leave (0xc9) */
407 if(cur_value
-prev_value
== 1) {
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");
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
) {
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 */
437 found
=1; /* negative displacement */
443 } else if(type
==NOP_FAULT
) {
444 /* 5b) nop*: replace instruction with nop */
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) {
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.
461 c
=(unsigned char *) prev_value
;
463 /* look for repX prefix */
465 if(text_read_ub(c
)==0xf3 || text_read_ub(c
)==0xf2) {
467 printk("found repX prefix\n");
468 /* take out repX prefix only */
470 cur_value
=prev_value
+1;
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) */
477 printk("found jXX rel8, loop or jcx\n");
479 } else if(text_read_ub(c
)==0x66 ||
480 text_read_ub(c
)==0x67) { /* override prefix */
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");
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
) {
495 c
=(unsigned char *) modAddr
;
496 if( text_read_ub(c
)>0x3f && text_read_ub(c
)<0xc0 &&
497 (text_read_ub(c
)&7)!=5 ) {
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
508 c
=(unsigned char *) prev_value
;
509 if( text_read_ub(c
)==0x8a || text_read_ub(c
)==0x8b) {
511 if( ((text_read_ub(c
++))&0xc7)==0x45 && (text_read_ub(c
)&0x80)==0 ) {
512 /* 75% chance that we'll choose the next arg */
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
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) {
534 // Look for push x(ebp)
536 if ((text_read_ub(c
) & 0x78) != 0x70) {
545 if (text_read_ub(c
) == 0x9d) {
547 // Increment cur_value to include the
558 /* if we're doing nop fault, then we're done.
561 *instr_len
=cur_value
-prev_value
;
565 if (d
) printk("+0x%x", d
);
566 printk(" @ %x, ", value
);
567 printk("instr @ %x, len=%d, ", off
, *instr_len
);
569 // db_disasm(prev_value, FALSE);
574 if(verbose
) printk("cannot locate instruction in function\n");
582 db_line_at_pc( sym
, filename
, linenum
, pc
)
588 return X_db_line_at_pc( db_last_symtab
, sym
, filename
, linenum
, pc
);
592 db_sym_numargs(sym
, nargp
, argnames
)
597 return X_db_sym_numargs(db_last_symtab
, sym
, nargp
, argnames
);