lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) { lib_info* newlib; print_debug("add_lib_info_fd %s\n", libname); if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) { print_debug("can't allocate memory for lib_info\n"); return NULL; } strncpy(newlib->name, libname, sizeof(newlib->name)); newlib->base = base; if (fd == -1) { if ( (newlib->fd = pathmap_open(newlib->name)) < 0) { print_debug("can't open shared object %s\n", newlib->name); free(newlib); return NULL; } } else { newlib->fd = fd; } #ifdef __APPLE__ // check whether we have got an Macho file. if (is_macho_file(newlib->fd) == false) { close(newlib->fd); free(newlib); print_debug("not a mach-o file\n"); return NULL; } #else // check whether we have got an ELF file. /proc/<pid>/map // gives out all file mappings and not just shared objects if (is_elf_file(newlib->fd) == false) { close(newlib->fd); free(newlib); return NULL; } #endif // __APPLE__ newlib->symtab = build_symtab(newlib->fd); if (newlib->symtab == NULL) { print_debug("symbol table build failed for %s\n", newlib->name); } else { print_debug("built symbol table for %s\n", newlib->name); } // even if symbol table building fails, we add the lib_info. // This is because we may need to read from the ELF file or MachO file for core file // address read functionality. lookup_symbol checks for NULL symtab. if (ph->libs) { ph->lib_tail->next = newlib; ph->lib_tail = newlib; } else { ph->libs = ph->lib_tail = newlib; } ph->num_libs++; return newlib; }
bool read_lib_info(pid_t pid) { char fname[32]; char buf[256]; FILE *fp = NULL; char *execfile = ""; guarantee(debuggee.pid = pid, "wrong pid value"); sprintf(fname, "/proc/%d/maps", pid); fp = fopen(fname, "r"); if (fp == NULL) { fatal("can't open maps file"); } while(fgets_no_cr(buf, 256, fp)){ char * word[6]; int nwords = split_n_str(buf, 6, word, ' ', '\0'); # define EXE_LOC "08048000-" if (strncmp(buf, EXE_LOC, strlen(EXE_LOC)) == 0){ execfile = strdup(word[5]); } else if (nwords > 5 && strcmp(word[5], execfile)!=0 && find_lib(word[5]) == NULL) { lib_info *lib = malloc(sizeof(lib_info)); lib->symtab = build_symtab(word[5]); if (lib->symtab) { sscanf(word[0], "%x", &lib->base); strcpy(lib->name, word[5]); lib->next = debuggee.libs; debuggee.libs = lib; } else { // not an elf file // FIXME: we could also reach here if library does not have // symbol table. free(lib); } } } fclose(fp); }
int main(int argc, char** argv) { int print_t = 0; int print_v = 0; int print_f = 0; int i; int rc; g_type = (char*) malloc(10); symboltable tracking_table = create_symboltable(); flat_symtab var_table = create_flat_symtab(); func_table function_table = create_functable(); if (argc < 2){ printf("usage: scan filename\n"); return -1; } FILE* in = fopen(argv[1], "r"); if (in == NULL){ printf("file not found\n"); return -1; } for(i = 2; i < argc; i++){ if(strcmp(argv[i], "-t") == 0){ print_t = 1; }else if (strcmp(argv[i], "-v") == 0){ print_v = 1; }else if (strcmp(argv[i], "-f") == 0){ print_f = 1; }else { printf("acceptable options are -t, -v and -f\n"); } } // set input source yyset_in(in); // parse input rc = yyparse(); if (rc == 0){ printf("succeeded parsing input.\n"); } else{ printf("--- error --- failed parsing input: error at line %d, abort.\n", lineNumber); return -1; } // build symbol table build_symtab(tracking_table, var_table, function_table, root); if (sym_error != 0){ printf("--- error --- errors found building symboltable, abort. \n"); return -1; } // type check rc = type_check(var_table, function_table, root); if (rc != 0){ printf("--- error --- errors found in type checking, abort.\n"); // return -1; } // print data structures if(print_t == 1) print_ast(root, 0); if(print_v == 1) print_flat_table(var_table); if(print_f == 1) print_func_table(function_table); return 0; }