From cc181af35a9880eadf5c9f3c9d4a1facd8ce7be0 Mon Sep 17 00:00:00 2001 From: markj Date: Thu, 17 Oct 2013 03:39:21 +0000 Subject: Fix the libproc build when DEBUG is defined. --- lib/libproc/_libproc.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/libproc') diff --git a/lib/libproc/_libproc.h b/lib/libproc/_libproc.h index aee1ac1..aab0850 100644 --- a/lib/libproc/_libproc.h +++ b/lib/libproc/_libproc.h @@ -49,7 +49,7 @@ struct proc_handle { }; #ifdef DEBUG -#define DPRINTF(fmt, ...) warn(fmt, __VA_ARGS__) +#define DPRINTF(...) warn(__VA_ARGS__) #else -#define DPRINTF(fmt, ...) +#define DPRINTF(...) #endif -- cgit v1.1 From 7799e7909514ade5a6bed2c5c96437f480fb66ff Mon Sep 17 00:00:00 2001 From: markj Date: Sun, 27 Oct 2013 20:39:10 +0000 Subject: Clean up the debug printing in libproc a bit. In particular: * Don't print any error messages to stderr unless DEBUG is defined. * Add a DPRINTFX macro for use when errno isn't set. * Print the error string from libelf when appropriate. --- lib/libproc/_libproc.h | 6 ++++-- lib/libproc/proc_bkpt.c | 24 ++++++++++++------------ lib/libproc/proc_create.c | 6 +++--- lib/libproc/proc_regs.c | 4 ++-- lib/libproc/proc_sym.c | 26 +++++++++++++------------- lib/libproc/proc_util.c | 2 +- 6 files changed, 35 insertions(+), 33 deletions(-) (limited to 'lib/libproc') diff --git a/lib/libproc/_libproc.h b/lib/libproc/_libproc.h index aab0850..a650f45 100644 --- a/lib/libproc/_libproc.h +++ b/lib/libproc/_libproc.h @@ -49,7 +49,9 @@ struct proc_handle { }; #ifdef DEBUG -#define DPRINTF(...) warn(__VA_ARGS__) +#define DPRINTF(...) warn(__VA_ARGS__) +#define DPRINTFX(...) warnx(__VA_ARGS__) #else -#define DPRINTF(...) +#define DPRINTF(...) +#define DPRINTFX(...) #endif diff --git a/lib/libproc/proc_bkpt.c b/lib/libproc/proc_bkpt.c index c15e53c..1878c7f 100644 --- a/lib/libproc/proc_bkpt.c +++ b/lib/libproc/proc_bkpt.c @@ -78,8 +78,8 @@ proc_bkptset(struct proc_handle *phdl, uintptr_t address, piod.piod_addr = &paddr; piod.piod_len = BREAKPOINT_INSTR_SZ; if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { - DPRINTF("ERROR: couldn't read instruction at address 0x%" PRIuPTR, - address); + DPRINTF("ERROR: couldn't read instruction at address 0x%" + PRIuPTR, address); return (-1); } *saved = paddr; @@ -93,8 +93,8 @@ proc_bkptset(struct proc_handle *phdl, uintptr_t address, piod.piod_addr = &paddr; piod.piod_len = BREAKPOINT_INSTR_SZ; if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { - warn("ERROR: couldn't write instruction at address 0x%" PRIuPTR, - address); + DPRINTF("ERROR: couldn't write instruction at address 0x%" + PRIuPTR, address); return (-1); } @@ -113,7 +113,7 @@ proc_bkptdel(struct proc_handle *phdl, uintptr_t address, errno = ENOENT; return (-1); } - DPRINTF("removing breakpoint at 0x%lx\n", address); + DPRINTFX("removing breakpoint at 0x%lx\n", address); /* * Overwrite the breakpoint instruction that we setup previously. */ @@ -124,8 +124,8 @@ proc_bkptdel(struct proc_handle *phdl, uintptr_t address, piod.piod_addr = &paddr; piod.piod_len = BREAKPOINT_INSTR_SZ; if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { - DPRINTF("ERROR: couldn't write instruction at address 0x%" PRIuPTR, - address); + DPRINTF("ERROR: couldn't write instruction at address 0x%" + PRIuPTR, address); return (-1); } @@ -153,12 +153,12 @@ proc_bkptexec(struct proc_handle *phdl, unsigned long saved) int status; if (proc_regget(phdl, REG_PC, &pc) < 0) { - warn("ERROR: couldn't get PC register"); + DPRINTFX("ERROR: couldn't get PC register"); return (-1); } proc_bkptregadj(&pc); if (proc_bkptdel(phdl, pc, saved) < 0) { - warn("ERROR: couldn't delete breakpoint"); + DPRINTFX("ERROR: couldn't delete breakpoint"); return (-1); } /* @@ -167,13 +167,13 @@ proc_bkptexec(struct proc_handle *phdl, unsigned long saved) */ proc_regset(phdl, REG_PC, pc); if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) { - warn("ERROR: ptrace step failed"); + DPRINTFX("ERROR: ptrace step failed"); return (-1); } proc_wstatus(phdl); status = proc_getwstat(phdl); if (!WIFSTOPPED(status)) { - warn("ERROR: don't know why process stopped"); + DPRINTFX("ERROR: don't know why process stopped"); return (-1); } /* @@ -181,7 +181,7 @@ proc_bkptexec(struct proc_handle *phdl, unsigned long saved) * the same as the one that we were passed in. */ if (proc_bkptset(phdl, pc, &samesaved) < 0) { - warn("ERROR: couldn't restore breakpoint"); + DPRINTFX("ERROR: couldn't restore breakpoint"); return (-1); } assert(samesaved == saved); diff --git a/lib/libproc/proc_create.c b/lib/libproc/proc_create.c index 9bd24a2..d02eccf 100644 --- a/lib/libproc/proc_create.c +++ b/lib/libproc/proc_create.c @@ -75,7 +75,7 @@ proc_attach(pid_t pid, int flags, struct proc_handle **pphdl) /* Check for an unexpected status. */ if (WIFSTOPPED(status) == 0) - DPRINTF("ERROR: child process %d status 0x%x", pid, status); + DPRINTFX("ERROR: child process %d status 0x%x", pid, status); else phdl->status = PS_STOP; @@ -130,14 +130,14 @@ proc_create(const char *file, char * const *argv, proc_child_func *pcf, /* Wait for the child process to stop. */ if (waitpid(pid, &status, WUNTRACED) == -1) { error = errno; - DPRINTF("ERROR: child process %d didn't stop as expected", pid); + DPRINTF("ERROR: child process %d didn't stop as expected", pid); goto bad; } /* Check for an unexpected status. */ if (WIFSTOPPED(status) == 0) { error = errno; - DPRINTF("ERROR: child process %d status 0x%x", pid, status); + DPRINTFX("ERROR: child process %d status 0x%x", pid, status); goto bad; } else phdl->status = PS_STOP; diff --git a/lib/libproc/proc_regs.c b/lib/libproc/proc_regs.c index aac0125..145c8fe 100644 --- a/lib/libproc/proc_regs.c +++ b/lib/libproc/proc_regs.c @@ -76,7 +76,7 @@ proc_regget(struct proc_handle *phdl, proc_reg_t reg, unsigned long *regvalue) #endif break; default: - warn("ERROR: no support for reg number %d", reg); + DPRINTFX("ERROR: no support for reg number %d", reg); return (-1); } @@ -119,7 +119,7 @@ proc_regset(struct proc_handle *phdl, proc_reg_t reg, unsigned long regvalue) #endif break; default: - warn("ERROR: no support for reg number %d", reg); + DPRINTFX("ERROR: no support for reg number %d", reg); return (-1); } if (ptrace(PT_SETREGS, proc_getpid(phdl), (caddr_t)®s, 0) < 0) diff --git a/lib/libproc/proc_sym.c b/lib/libproc/proc_sym.c index 2dd21fe..e9f79ea 100644 --- a/lib/libproc/proc_sym.c +++ b/lib/libproc/proc_sym.c @@ -238,16 +238,16 @@ proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name, if ((map = proc_addr2map(p, addr)) == NULL) return (-1); - if (!map->pr_mapname || (fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) { - warn("ERROR: open %s failed", map->pr_mapname); + if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) { + DPRINTF("ERROR: open %s failed", map->pr_mapname); goto err0; } if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { - warn("ERROR: elf_begin() failed"); + DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1)); goto err1; } if (gelf_getehdr(e, &ehdr) == NULL) { - warn("ERROR: gelf_getehdr() failed"); + DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1)); goto err2; } /* @@ -275,7 +275,7 @@ proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name, * Then look up the string name in STRTAB (.dynstr) */ if ((data = elf_getdata(dynsymscn, NULL)) == NULL) { - DPRINTF("ERROR: elf_getdata() failed"); + DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1)); goto symtab; } i = 0; @@ -312,7 +312,7 @@ symtab: if (symtabscn == NULL) goto err2; if ((data = elf_getdata(symtabscn, NULL)) == NULL) { - DPRINTF("ERROR: elf_getdata() failed"); + DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1)); goto err2; } i = 0; @@ -420,7 +420,7 @@ proc_name2sym(struct proc_handle *p, const char *object, const char *symbol, unsigned long symtabstridx = 0, dynsymstridx = 0; if ((map = proc_name2map(p, object)) == NULL) { - DPRINTF("ERROR: couldn't find object %s", object); + DPRINTFX("ERROR: couldn't find object %s", object); goto err0; } if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) { @@ -428,11 +428,11 @@ proc_name2sym(struct proc_handle *p, const char *object, const char *symbol, goto err0; } if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { - warn("ERROR: elf_begin() failed"); + DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1)); goto err1; } if (gelf_getehdr(e, &ehdr) == NULL) { - warn("ERROR: gelf_getehdr() failed"); + DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1)); goto err2; } /* @@ -460,7 +460,7 @@ proc_name2sym(struct proc_handle *p, const char *object, const char *symbol, * Then look up the string name in STRTAB (.dynstr) */ if ((data = elf_getdata(dynsymscn, NULL))) { - DPRINTF("ERROR: elf_getdata() failed"); + DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1)); i = 0; while (gelf_getsym(data, i++, &sym) != NULL) { s = elf_strptr(e, dynsymstridx, sym.st_name); @@ -519,11 +519,11 @@ proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which, if ((map = proc_name2map(p, object)) == NULL) return (-1); if ((fd = open(map->pr_mapname, O_RDONLY)) < 0) { - warn("ERROR: open %s failed", map->pr_mapname); + DPRINTF("ERROR: open %s failed", map->pr_mapname); goto err0; } if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { - warn("ERROR: elf_begin() failed"); + DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1)); goto err1; } /* @@ -546,7 +546,7 @@ proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which, return (-1); stridx = shdr.sh_link; if ((data = elf_getdata(foundscn, NULL)) == NULL) { - DPRINTF("ERROR: elf_getdata() failed"); + DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1)); goto err2; } i = 0; diff --git a/lib/libproc/proc_util.c b/lib/libproc/proc_util.c index 089095e..1c3d522 100644 --- a/lib/libproc/proc_util.c +++ b/lib/libproc/proc_util.c @@ -146,7 +146,7 @@ proc_wstatus(struct proc_handle *phdl) return (-1); if (waitpid(phdl->pid, &status, WUNTRACED) < 0) { if (errno != EINTR) - warn("waitpid"); + DPRINTF("waitpid"); return (-1); } if (WIFSTOPPED(status)) -- cgit v1.1 From 64b3e71588690565509558d3e46f67ffbb8c9764 Mon Sep 17 00:00:00 2001 From: markj Date: Mon, 28 Oct 2013 01:41:59 +0000 Subject: Remove an incorrect debug printf. --- lib/libproc/proc_sym.c | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/libproc') diff --git a/lib/libproc/proc_sym.c b/lib/libproc/proc_sym.c index e9f79ea..4f3d83b 100644 --- a/lib/libproc/proc_sym.c +++ b/lib/libproc/proc_sym.c @@ -460,7 +460,6 @@ proc_name2sym(struct proc_handle *p, const char *object, const char *symbol, * Then look up the string name in STRTAB (.dynstr) */ if ((data = elf_getdata(dynsymscn, NULL))) { - DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1)); i = 0; while (gelf_getsym(data, i++, &sym) != NULL) { s = elf_strptr(e, dynsymstridx, sym.st_name); -- cgit v1.1 From 1da863f2b411f1138a0405549ac8bdcf550014ce Mon Sep 17 00:00:00 2001 From: markj Date: Mon, 28 Oct 2013 12:42:27 +0000 Subject: Fix the build with gcc. --- lib/libproc/proc_util.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/libproc') diff --git a/lib/libproc/proc_util.c b/lib/libproc/proc_util.c index 1c3d522..646ee97 100644 --- a/lib/libproc/proc_util.c +++ b/lib/libproc/proc_util.c @@ -145,8 +145,9 @@ proc_wstatus(struct proc_handle *phdl) if (phdl == NULL) return (-1); if (waitpid(phdl->pid, &status, WUNTRACED) < 0) { - if (errno != EINTR) + if (errno != EINTR) { DPRINTF("waitpid"); + } return (-1); } if (WIFSTOPPED(status)) -- cgit v1.1 From bb1072d7ef9101f89db4769045b34123c3e344ec Mon Sep 17 00:00:00 2001 From: markj Date: Tue, 29 Oct 2013 03:12:31 +0000 Subject: Revert r257248 and fix the problem in a way that doesn't violate style(9). Suggested by: jmg --- lib/libproc/_libproc.h | 4 ++-- lib/libproc/proc_util.c | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'lib/libproc') diff --git a/lib/libproc/_libproc.h b/lib/libproc/_libproc.h index a650f45..8099ba1 100644 --- a/lib/libproc/_libproc.h +++ b/lib/libproc/_libproc.h @@ -52,6 +52,6 @@ struct proc_handle { #define DPRINTF(...) warn(__VA_ARGS__) #define DPRINTFX(...) warnx(__VA_ARGS__) #else -#define DPRINTF(...) -#define DPRINTFX(...) +#define DPRINTF(...) do { } while (0) +#define DPRINTFX(...) do { } while (0) #endif diff --git a/lib/libproc/proc_util.c b/lib/libproc/proc_util.c index 646ee97..1c3d522 100644 --- a/lib/libproc/proc_util.c +++ b/lib/libproc/proc_util.c @@ -145,9 +145,8 @@ proc_wstatus(struct proc_handle *phdl) if (phdl == NULL) return (-1); if (waitpid(phdl->pid, &status, WUNTRACED) < 0) { - if (errno != EINTR) { + if (errno != EINTR) DPRINTF("waitpid"); - } return (-1); } if (WIFSTOPPED(status)) -- cgit v1.1 From 756d4686b39ff2a93f1aab3ac223d5257ce6d7cc Mon Sep 17 00:00:00 2001 From: markj Date: Tue, 29 Oct 2013 03:52:05 +0000 Subject: Fix an off-by-one error when checking whether a given address is within the extent of a symbol. Submitted by: Prashanth Kumar Reviewed by: rpaulo MFC after: 1 week --- lib/libproc/proc_sym.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/libproc') diff --git a/lib/libproc/proc_sym.c b/lib/libproc/proc_sym.c index 4f3d83b..87ac471 100644 --- a/lib/libproc/proc_sym.c +++ b/lib/libproc/proc_sym.c @@ -285,7 +285,7 @@ proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name, * by rtld. */ rsym = map->pr_vaddr + sym.st_value; - if (addr >= rsym && addr <= (rsym + sym.st_size)) { + if (addr >= rsym && addr < rsym + sym.st_size) { s = elf_strptr(e, dynsymstridx, sym.st_name); if (s) { if (s[0] == '_' && s[1] == 'Z' && s[2]) @@ -325,7 +325,7 @@ symtab: rsym = map->pr_vaddr + sym.st_value; else rsym = sym.st_value; - if (addr >= rsym && addr <= (rsym + sym.st_size)) { + if (addr >= rsym && addr < rsym + sym.st_size) { s = elf_strptr(e, symtabstridx, sym.st_name); if (s) { if (s[0] == '_' && s[1] == 'Z' && s[2]) -- cgit v1.1 From 3250f228876dac940ab67d0ed1b0f3d5878935ce Mon Sep 17 00:00:00 2001 From: markj Date: Tue, 5 Nov 2013 03:23:54 +0000 Subject: Modify the libproc breakpoint add/remove functions to stop the target process if it has not already been stopped, since this is required for ptrace(2) to work. libdtrace does not seem to stop target processes before trying to remove their breakpoints, so we were previously failing to remove the breakpoint on r_debug_state() in rtld. This was causing processes to die with SIGTRAP if they called dlopen(3) after dtrace(1) had detached. Reported by: symbolics@gmx.com Reviewed by: rpaulo MFC after: 1 month --- lib/libproc/proc_bkpt.c | 66 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 7 deletions(-) (limited to 'lib/libproc') diff --git a/lib/libproc/proc_bkpt.c b/lib/libproc/proc_bkpt.c index 1878c7f..2c2761a 100644 --- a/lib/libproc/proc_bkpt.c +++ b/lib/libproc/proc_bkpt.c @@ -37,8 +37,9 @@ __FBSDID("$FreeBSD$"); #include #include -#include #include +#include +#include #include "_libproc.h" #if defined(__i386__) || defined(__amd64__) @@ -54,12 +55,39 @@ __FBSDID("$FreeBSD$"); #error "Add support for your architecture" #endif +static void +proc_cont(struct proc_handle *phdl) +{ + + ptrace(PT_CONTINUE, proc_getpid(phdl), (caddr_t)1, 0); +} + +static int +proc_stop(struct proc_handle *phdl) +{ + int status; + + if (kill(proc_getpid(phdl), SIGSTOP) == -1) { + DPRINTF("kill %d", proc_getpid(phdl)); + return (-1); + } else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) { + DPRINTF("waitpid %d", proc_getpid(phdl)); + return (-1); + } else if (!WIFSTOPPED(status)) { + DPRINTFX("waitpid: unexpected status 0x%x", status); + return (-1); + } + + return (0); +} + int proc_bkptset(struct proc_handle *phdl, uintptr_t address, unsigned long *saved) { struct ptrace_io_desc piod; unsigned long paddr, caddr; + int ret = 0; *saved = 0; if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD || @@ -68,6 +96,12 @@ proc_bkptset(struct proc_handle *phdl, uintptr_t address, return (-1); } + DPRINTFX("adding breakpoint at 0x%lx", address); + + if (phdl->status != PS_STOP) + if (proc_stop(phdl) != 0) + return (-1); + /* * Read the original instruction. */ @@ -80,7 +114,8 @@ proc_bkptset(struct proc_handle *phdl, uintptr_t address, if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { DPRINTF("ERROR: couldn't read instruction at address 0x%" PRIuPTR, address); - return (-1); + ret = -1; + goto done; } *saved = paddr; /* @@ -95,10 +130,16 @@ proc_bkptset(struct proc_handle *phdl, uintptr_t address, if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { DPRINTF("ERROR: couldn't write instruction at address 0x%" PRIuPTR, address); - return (-1); + ret = -1; + goto done; } - return (0); +done: + if (phdl->status != PS_STOP) + /* Restart the process if we had to stop it. */ + proc_cont(phdl); + + return (ret); } int @@ -107,13 +148,20 @@ proc_bkptdel(struct proc_handle *phdl, uintptr_t address, { struct ptrace_io_desc piod; unsigned long paddr, caddr; + int ret = 0; if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD || phdl->status == PS_IDLE) { errno = ENOENT; return (-1); } - DPRINTFX("removing breakpoint at 0x%lx\n", address); + + DPRINTFX("removing breakpoint at 0x%lx", address); + + if (phdl->status != PS_STOP) + if (proc_stop(phdl) != 0) + return (-1); + /* * Overwrite the breakpoint instruction that we setup previously. */ @@ -126,10 +174,14 @@ proc_bkptdel(struct proc_handle *phdl, uintptr_t address, if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { DPRINTF("ERROR: couldn't write instruction at address 0x%" PRIuPTR, address); - return (-1); + ret = -1; } + + if (phdl->status != PS_STOP) + /* Restart the process if we had to stop it. */ + proc_cont(phdl); - return (0); + return (ret); } /* -- cgit v1.1 From 9c14c6242ebc899d8ce4f34b83cb688739eb1f18 Mon Sep 17 00:00:00 2001 From: markj Date: Mon, 11 Nov 2013 16:57:57 +0000 Subject: Consistently add the relocation offset only when the ELF type is not ET_EXEC. This fixes several problems with the DTrace pid provider not being able to match probes. Reviewed by: rpaulo MFC after: 2 weeks --- lib/libproc/proc_sym.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'lib/libproc') diff --git a/lib/libproc/proc_sym.c b/lib/libproc/proc_sym.c index 87ac471..f848040 100644 --- a/lib/libproc/proc_sym.c +++ b/lib/libproc/proc_sym.c @@ -284,7 +284,10 @@ proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name, * Calculate the address mapped to the virtual memory * by rtld. */ - rsym = map->pr_vaddr + sym.st_value; + if (ehdr.e_type != ET_EXEC) + rsym = map->pr_vaddr + sym.st_value; + else + rsym = sym.st_value; if (addr >= rsym && addr < rsym + sym.st_size) { s = elf_strptr(e, dynsymstridx, sym.st_name); if (s) { @@ -309,8 +312,6 @@ symtab: * Iterate over the Symbols Table to find the symbol. * Then look up the string name in STRTAB (.dynstr) */ - if (symtabscn == NULL) - goto err2; if ((data = elf_getdata(symtabscn, NULL)) == NULL) { DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1)); goto err2; @@ -465,7 +466,8 @@ proc_name2sym(struct proc_handle *p, const char *object, const char *symbol, s = elf_strptr(e, dynsymstridx, sym.st_name); if (s && strcmp(s, symbol) == 0) { memcpy(symcopy, &sym, sizeof(sym)); - symcopy->st_value = map->pr_vaddr + sym.st_value; + if (ehdr.e_type != ET_EXEC) + symcopy->st_value += map->pr_vaddr; error = 0; goto out; } @@ -475,20 +477,21 @@ proc_name2sym(struct proc_handle *p, const char *object, const char *symbol, * Iterate over the Symbols Table to find the symbol. * Then look up the string name in STRTAB (.dynstr) */ - if (symtabscn == NULL) - goto err2; if ((data = elf_getdata(symtabscn, NULL))) { i = 0; while (gelf_getsym(data, i++, &sym) != NULL) { s = elf_strptr(e, symtabstridx, sym.st_name); if (s && strcmp(s, symbol) == 0) { memcpy(symcopy, &sym, sizeof(sym)); + if (ehdr.e_type != ET_EXEC) + symcopy->st_value += map->pr_vaddr; error = 0; goto out; } } } out: + DPRINTFX("found addr 0x%lx for %s", symcopy->st_value, symbol); err2: elf_end(e); err1: @@ -509,6 +512,7 @@ proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which, prmap_t *map; Elf_Scn *scn, *foundscn = NULL; Elf_Data *data; + GElf_Ehdr ehdr; GElf_Shdr shdr; GElf_Sym sym; unsigned long stridx = -1; @@ -525,6 +529,10 @@ proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which, DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1)); goto err1; } + if (gelf_getehdr(e, &ehdr) == NULL) { + DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1)); + goto err2; + } /* * Find the section we are looking for. */ @@ -575,7 +583,8 @@ proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which, (mask & TYPE_FILE) == 0) continue; s = elf_strptr(e, stridx, sym.st_name); - sym.st_value += map->pr_vaddr; + if (ehdr.e_type != ET_EXEC) + sym.st_value += map->pr_vaddr; (*func)(cd, &sym, s); } error = 0; -- cgit v1.1 From 5db7302acc2a0f5121bdf5f1e3362f6ca16b277e Mon Sep 17 00:00:00 2001 From: imp Date: Sat, 5 Apr 2014 17:54:36 +0000 Subject: The proper way to request no man pages currently is NO_MAN=xxx. Use it in preference to the user WITHOUT_MAN knob, which should never be set in normal src Makefiles. --- lib/libproc/Makefile | 2 +- lib/libproc/test/t1-bkpt/Makefile | 2 +- lib/libproc/test/t2-name2map/Makefile | 2 +- lib/libproc/test/t3-name2sym/Makefile | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/libproc') diff --git a/lib/libproc/Makefile b/lib/libproc/Makefile index 4449c06..03992c0 100644 --- a/lib/libproc/Makefile +++ b/lib/libproc/Makefile @@ -25,6 +25,6 @@ DPADD+= ${LIBSTDCPLUSPLUS} SHLIB_MAJOR= 2 -WITHOUT_MAN= +NO_MAN= .include diff --git a/lib/libproc/test/t1-bkpt/Makefile b/lib/libproc/test/t1-bkpt/Makefile index fd93fdd..a1c05e1 100644 --- a/lib/libproc/test/t1-bkpt/Makefile +++ b/lib/libproc/test/t1-bkpt/Makefile @@ -7,6 +7,6 @@ SRCS= t1-bkpt.c LDADD= -lproc -lelf -lrtld_db -lutil DPADD= ${LIBPROC} ${LIBELF} -WITHOUT_MAN= +NO_MAN= .include diff --git a/lib/libproc/test/t2-name2map/Makefile b/lib/libproc/test/t2-name2map/Makefile index 3dca51c..087df81 100644 --- a/lib/libproc/test/t2-name2map/Makefile +++ b/lib/libproc/test/t2-name2map/Makefile @@ -7,6 +7,6 @@ SRCS= t2-name2map.c LDADD= -lproc -lelf -lrtld_db -lutil DPADD= ${LIBPROC} ${LIBELF} -WITHOUT_MAN= +NO_MAN= .include diff --git a/lib/libproc/test/t3-name2sym/Makefile b/lib/libproc/test/t3-name2sym/Makefile index 187f9c1..55468c0 100644 --- a/lib/libproc/test/t3-name2sym/Makefile +++ b/lib/libproc/test/t3-name2sym/Makefile @@ -7,6 +7,6 @@ SRCS= t3-name2sym.c LDADD= -lproc -lelf -lrtld_db -lutil DPADD= ${LIBPROC} ${LIBELF} -WITHOUT_MAN= +NO_MAN= .include -- cgit v1.1 From c39e6fc2c924f2c36a49949d87e26da056aaa71b Mon Sep 17 00:00:00 2001 From: imp Date: Sun, 13 Apr 2014 05:21:56 +0000 Subject: NO_MAN= has been deprecated in favor of MAN= for some time, go ahead and finish the job. ncurses is now the only Makefile in the tree that uses it since it wasn't a simple mechanical change, and will be addressed in a future commit. --- lib/libproc/Makefile | 2 +- lib/libproc/test/t1-bkpt/Makefile | 2 +- lib/libproc/test/t2-name2map/Makefile | 2 +- lib/libproc/test/t3-name2sym/Makefile | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/libproc') diff --git a/lib/libproc/Makefile b/lib/libproc/Makefile index 03992c0..fea6569 100644 --- a/lib/libproc/Makefile +++ b/lib/libproc/Makefile @@ -25,6 +25,6 @@ DPADD+= ${LIBSTDCPLUSPLUS} SHLIB_MAJOR= 2 -NO_MAN= +MAN= .include diff --git a/lib/libproc/test/t1-bkpt/Makefile b/lib/libproc/test/t1-bkpt/Makefile index a1c05e1..eb5b37f 100644 --- a/lib/libproc/test/t1-bkpt/Makefile +++ b/lib/libproc/test/t1-bkpt/Makefile @@ -7,6 +7,6 @@ SRCS= t1-bkpt.c LDADD= -lproc -lelf -lrtld_db -lutil DPADD= ${LIBPROC} ${LIBELF} -NO_MAN= +MAN= .include diff --git a/lib/libproc/test/t2-name2map/Makefile b/lib/libproc/test/t2-name2map/Makefile index 087df81..9002acc 100644 --- a/lib/libproc/test/t2-name2map/Makefile +++ b/lib/libproc/test/t2-name2map/Makefile @@ -7,6 +7,6 @@ SRCS= t2-name2map.c LDADD= -lproc -lelf -lrtld_db -lutil DPADD= ${LIBPROC} ${LIBELF} -NO_MAN= +MAN= .include diff --git a/lib/libproc/test/t3-name2sym/Makefile b/lib/libproc/test/t3-name2sym/Makefile index 55468c0..68e23c6 100644 --- a/lib/libproc/test/t3-name2sym/Makefile +++ b/lib/libproc/test/t3-name2sym/Makefile @@ -7,6 +7,6 @@ SRCS= t3-name2sym.c LDADD= -lproc -lelf -lrtld_db -lutil DPADD= ${LIBPROC} ${LIBELF} -NO_MAN= +MAN= .include -- cgit v1.1 From efaf328cf593e0da0109f9a7487e285651f6365f Mon Sep 17 00:00:00 2001 From: markj Date: Mon, 14 Apr 2014 00:24:04 +0000 Subject: Fix some off-by-one errors. The kve_end and rdl_eaddr fields contain the first address after the end of the map entry and should therefore be excluded. MFC after: 2 weeks --- lib/libproc/proc_sym.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/libproc') diff --git a/lib/libproc/proc_sym.c b/lib/libproc/proc_sym.c index f848040..2338895 100644 --- a/lib/libproc/proc_sym.c +++ b/lib/libproc/proc_sym.c @@ -96,7 +96,7 @@ proc_objname(struct proc_handle *p, uintptr_t addr, char *objname, for (i = 0; i < p->nobjs; i++) { rdl = &p->rdobjs[i]; - if (addr >= rdl->rdl_saddr && addr <= rdl->rdl_eaddr) { + if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) { strlcpy(objname, rdl->rdl_path, objnamesz); return (objname); } @@ -176,7 +176,7 @@ proc_addr2map(struct proc_handle *p, uintptr_t addr) kve = kves + i; if (kve->kve_type == KVME_TYPE_VNODE) lastvn = i; - if (addr >= kve->kve_start && addr <= kve->kve_end) { + if (addr >= kve->kve_start && addr < kve->kve_end) { if ((map = malloc(sizeof(*map))) == NULL) { free(kves); return (NULL); @@ -209,7 +209,7 @@ proc_addr2map(struct proc_handle *p, uintptr_t addr) for (i = 0; i < p->nobjs; i++) { rdl = &p->rdobjs[i]; - if (addr >= rdl->rdl_saddr && addr <= rdl->rdl_eaddr) { + if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) { if ((map = malloc(sizeof(*map))) == NULL) return (NULL); proc_rdl2prmap(rdl, map); -- cgit v1.1