[Fault-injection-developer] Updated: fi_dbp's user mode utility (fi_attach)
Status: Alpha
Brought to you by:
rustyl
From: Wang, S. <sta...@in...> - 2003-01-07 09:00:49
|
According to the recent changes in kernel module format, I rewrited the fi_attach. (We can merge it into "ficl") Usage: 1. insmod fi_dbp and the tested module 2. fi_attach tested_module_name tested_module_file 3. insert fault ...... Compile fi_attach: gcc -lbfd -ldisasm -o fi_attach fi_attach.c Here is the source code: #include <stdio.h> #include <bfd.h> #include <libdis.h> #define CTL "/sys/fault_injection/interceptors/dbp_interceptor/ctl" int main(int argc, char **argv) { bfd *f; asection *sec; unsigned char *s; struct instr ins; char **matching; FILE * fp; int i; int counter=0; unsigned long addr[10000]; char line[4096]; unsigned long baseaddr = 0; bfd_byte *data = 0; unsigned int pos = 0; unsigned int size = 0; if (argc != 3) { fprintf(stderr, "Usage: fi_attach modname modfile\n"); exit(-1); } bfd_init(); bfd_set_default_target("elf32-i386"); f = bfd_openr(argv[2], "elf32-i386"); if (f == NULL) { fprintf(stderr, "bfd_openr ERROR!\n"); return 0; } if (bfd_check_format_matches (f, bfd_object, &matching)){ sec = bfd_get_section_by_name(f, ".text"); if (sec == NULL) { fprintf(stderr, "bfd_get_section_by_name ERROR!\n"); goto err; } } else { fprintf(stderr, "Isn't bdf_object file!\n"); goto err; } fp = fopen("/proc/modules", "r"); if (!fp) { fprintf(stderr, "Can't open /proc/modules\n"); goto err; } while (fgets(line, sizeof(line), fp)) { if (!strncmp(line, argv[1], strlen(argv[1]))) { s = line; while (*s!='\0') { s++; } while ( *s != ' ') { s--; } s++; baseaddr=strtoul(s, NULL, 16); } } fclose(fp); if(baseaddr == 0) { fprintf(stderr, "Please insmod first.\n"); goto err; } data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (f, sec)); bfd_get_section_contents (f, sec, (PTR) data, 0, bfd_section_size (f, sec)); s = data; disassemble_init(0,INTEL_SYNTAX); while(pos < sec->_raw_size) { size = disassemble_address(s+pos, &ins); if ( size > 0 ) { if (!strcmp(ins.mnemonic, "in") || !strcmp(ins.mnemonic, "out")) { addr[counter++] = pos + baseaddr; } pos += size; } else { fprintf(stderr, "ERROR!\n"); return -1; } } disassemble_cleanup(); for (i=0;i<counter;i++) { fp = fopen (CTL, "w+"); fprintf(fp, "add %lu\n", addr[i]); fclose(fp); } err: bfd_close(f); return 0; } Your Sincerely, Stanley Wang SW Engineer, Intel Corporation. Intel China Software Lab. Tel: 021-52574545 ext. 1171 iNet: 8-752-1171 Opinions expressed are those of the author and do not represent Intel Corporation |