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); }
static bool read_lib_info(struct ps_prochandle* ph) { char fname[32]; char buf[PATH_MAX]; FILE *fp = NULL; sprintf(fname, "/proc/%d/maps", ph->pid); fp = fopen(fname, "r"); if (fp == NULL) { print_debug("can't open /proc/%d/maps file\n", ph->pid); return false; } while(fgets_no_cr(buf, PATH_MAX, fp)){ char * word[7]; int nwords = split_n_str(buf, 7, word, ' ', '\0'); if (nwords < 6) { // not a shared library entry. ignore. continue; } // SA does not handle the lines with patterns: // "[stack]", "[heap]", "[vdso]", "[vsyscall]", etc. if (word[5][0] == '[') { // not a shared library entry. ignore. continue; } if (nwords > 6) { // prelink altered mapfile when the program is running. // Entries like one below have to be skipped // /lib64/libc-2.15.so (deleted) // SO name in entries like one below have to be stripped. // /lib64/libpthread-2.15.so.#prelink#.EECVts char *s = strstr(word[5],".#prelink#"); if (s == NULL) { // No prelink keyword. skip deleted library print_debug("skip shared object %s deleted by prelink\n", word[5]); continue; } // Fall through print_debug("rectifying shared object name %s changed by prelink\n", word[5]); *s = 0; } if (find_lib(ph, word[5]) == false) { intptr_t base; lib_info* lib; #ifdef _LP64 sscanf(word[0], "%lx", &base); #else sscanf(word[0], "%x", &base); #endif if ((lib = add_lib_info(ph, word[5], (uintptr_t)base)) == NULL) continue; // ignore, add_lib_info prints error // we don't need to keep the library open, symtab is already // built. Only for core dump we need to keep the fd open. close(lib->fd); lib->fd = -1; } } fclose(fp); return true; }