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.
This commit is contained in:
Mark Johnston 2013-10-27 20:39:10 +00:00
parent 46038d7fa1
commit 30e81f7e3b
6 changed files with 35 additions and 33 deletions

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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)&regs, 0) < 0)

View File

@ -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 @@ proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
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;

View File

@ -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))