MFC r265255, r270506:

Allow "a.out" as an alias for the executable if no other matching entries
are found.
This commit is contained in:
markj 2014-08-27 19:51:42 +00:00
parent 3d2bab33aa
commit 385503e19f
4 changed files with 65 additions and 19 deletions

View File

@ -46,6 +46,8 @@ struct proc_handle {
size_t rdobjsz; size_t rdobjsz;
size_t nobjs; size_t nobjs;
struct lwpstatus lwps; struct lwpstatus lwps;
rd_loadobj_t *rdexec; /* rdobj index of program executable. */
char execname[MAXPATHLEN]; /* Path to program executable. */
}; };
#ifdef DEBUG #ifdef DEBUG

View File

@ -26,8 +26,10 @@
* $FreeBSD$ * $FreeBSD$
*/ */
#include "_libproc.h" #include <sys/types.h>
#include <stdio.h> #include <sys/sysctl.h>
#include <sys/wait.h>
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
@ -35,7 +37,37 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h>
#include "_libproc.h"
static int proc_init(pid_t, int, int, struct proc_handle *);
static int
proc_init(pid_t pid, int flags, int status, struct proc_handle *phdl)
{
int mib[4], error;
size_t len;
memset(phdl, 0, sizeof(*phdl));
phdl->pid = pid;
phdl->flags = flags;
phdl->status = status;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = pid;
len = sizeof(phdl->execname);
if (sysctl(mib, 4, phdl->execname, &len, NULL, 0) != 0) {
error = errno;
DPRINTF("ERROR: cannot get pathname for child process %d", pid);
return (error);
}
if (len == 0)
phdl->execname[0] = '\0';
return (0);
}
int int
proc_attach(pid_t pid, int flags, struct proc_handle **pphdl) proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
@ -54,12 +86,12 @@ proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
if ((phdl = malloc(sizeof(struct proc_handle))) == NULL) if ((phdl = malloc(sizeof(struct proc_handle))) == NULL)
return (ENOMEM); return (ENOMEM);
memset(phdl, 0, sizeof(struct proc_handle));
phdl->pid = pid;
phdl->flags = flags;
phdl->status = PS_RUN;
elf_version(EV_CURRENT); elf_version(EV_CURRENT);
error = proc_init(pid, flags, PS_RUN, phdl);
if (error != 0)
goto out;
if (ptrace(PT_ATTACH, phdl->pid, 0, 0) != 0) { if (ptrace(PT_ATTACH, phdl->pid, 0, 0) != 0) {
error = errno; error = errno;
DPRINTF("ERROR: cannot ptrace child process %d", pid); DPRINTF("ERROR: cannot ptrace child process %d", pid);
@ -123,9 +155,9 @@ proc_create(const char *file, char * const *argv, proc_child_func *pcf,
_exit(2); _exit(2);
} else { } else {
/* The parent owns the process handle. */ /* The parent owns the process handle. */
memset(phdl, 0, sizeof(struct proc_handle)); error = proc_init(pid, 0, PS_IDLE, phdl);
phdl->pid = pid; if (error != 0)
phdl->status = PS_IDLE; goto bad;
/* Wait for the child process to stop. */ /* Wait for the child process to stop. */
if (waitpid(pid, &status, WUNTRACED) == -1) { if (waitpid(pid, &status, WUNTRACED) == -1) {

View File

@ -49,6 +49,9 @@ map_iter(const rd_loadobj_t *lop, void *arg)
if (phdl->rdobjs == NULL) if (phdl->rdobjs == NULL)
return (-1); return (-1);
} }
if (strcmp(lop->rdl_path, phdl->execname) == 0 &&
(lop->rdl_prot & RD_RDL_X) != 0)
phdl->rdexec = &phdl->rdobjs[phdl->nobjs];
memcpy(&phdl->rdobjs[phdl->nobjs++], lop, sizeof(*lop)); memcpy(&phdl->rdobjs[phdl->nobjs++], lop, sizeof(*lop));
return (0); return (0);

View File

@ -113,17 +113,25 @@ proc_obj2map(struct proc_handle *p, const char *objname)
rd_loadobj_t *rdl; rd_loadobj_t *rdl;
char path[MAXPATHLEN]; char path[MAXPATHLEN];
rdl = NULL;
for (i = 0; i < p->nobjs; i++) { for (i = 0; i < p->nobjs; i++) {
rdl = &p->rdobjs[i]; basename_r(p->rdobjs[i].rdl_path, path);
basename_r(rdl->rdl_path, path);
if (strcmp(path, objname) == 0) { if (strcmp(path, objname) == 0) {
if ((map = malloc(sizeof(*map))) == NULL) rdl = &p->rdobjs[i];
return (NULL); break;
proc_rdl2prmap(rdl, map);
return (map);
} }
} }
return (NULL); if (rdl == NULL) {
if (strcmp(objname, "a.out") == 0 && p->rdexec != NULL)
rdl = p->rdexec;
else
return (NULL);
}
if ((map = malloc(sizeof(*map))) == NULL)
return (NULL);
proc_rdl2prmap(rdl, map);
return (map);
} }
int int
@ -381,8 +389,9 @@ proc_name2map(struct proc_handle *p, const char *name)
free(kves); free(kves);
return (NULL); return (NULL);
} }
if (name == NULL || strcmp(name, "a.out") == 0) { if ((name == NULL || strcmp(name, "a.out") == 0) &&
map = proc_addr2map(p, p->rdobjs[0].rdl_saddr); p->rdexec != NULL) {
map = proc_addr2map(p, p->rdexec->rdl_saddr);
return (map); return (map);
} }
for (i = 0; i < p->nobjs; i++) { for (i = 0; i < p->nobjs; i++) {