From owner-svn-src-all@FreeBSD.ORG Wed Aug 27 19:51:09 2014 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 2D691AC9; Wed, 27 Aug 2014 19:51:09 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 0D4CF31D1; Wed, 27 Aug 2014 19:51:09 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id s7RJp88i050445; Wed, 27 Aug 2014 19:51:08 GMT (envelope-from markj@FreeBSD.org) Received: (from markj@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id s7RJp8YU050440; Wed, 27 Aug 2014 19:51:08 GMT (envelope-from markj@FreeBSD.org) Message-Id: <201408271951.s7RJp8YU050440@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: markj set sender to markj@FreeBSD.org using -f From: Mark Johnston Date: Wed, 27 Aug 2014 19:51:08 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-9@freebsd.org Subject: svn commit: r270730 - stable/9/lib/libproc X-SVN-Group: stable-9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 Aug 2014 19:51:09 -0000 Author: markj Date: Wed Aug 27 19:51:07 2014 New Revision: 270730 URL: http://svnweb.freebsd.org/changeset/base/270730 Log: MFC r265255, r270506: Allow "a.out" as an alias for the executable if no other matching entries are found. Modified: stable/9/lib/libproc/_libproc.h stable/9/lib/libproc/proc_create.c stable/9/lib/libproc/proc_rtld.c stable/9/lib/libproc/proc_sym.c Directory Properties: stable/9/lib/libproc/ (props changed) Modified: stable/9/lib/libproc/_libproc.h ============================================================================== --- stable/9/lib/libproc/_libproc.h Wed Aug 27 19:34:49 2014 (r270729) +++ stable/9/lib/libproc/_libproc.h Wed Aug 27 19:51:07 2014 (r270730) @@ -46,6 +46,8 @@ struct proc_handle { size_t rdobjsz; size_t nobjs; struct lwpstatus lwps; + rd_loadobj_t *rdexec; /* rdobj index of program executable. */ + char execname[MAXPATHLEN]; /* Path to program executable. */ }; #ifdef DEBUG Modified: stable/9/lib/libproc/proc_create.c ============================================================================== --- stable/9/lib/libproc/proc_create.c Wed Aug 27 19:34:49 2014 (r270729) +++ stable/9/lib/libproc/proc_create.c Wed Aug 27 19:51:07 2014 (r270730) @@ -26,8 +26,10 @@ * $FreeBSD$ */ -#include "_libproc.h" -#include +#include +#include +#include + #include #include #include @@ -35,7 +37,37 @@ #include #include #include -#include + +#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 proc_attach(pid_t pid, int flags, struct proc_handle **pphdl) @@ -54,12 +86,12 @@ proc_attach(pid_t pid, int flags, struct if ((phdl = malloc(sizeof(struct proc_handle))) == NULL) return (ENOMEM); - memset(phdl, 0, sizeof(struct proc_handle)); - phdl->pid = pid; - phdl->flags = flags; - phdl->status = PS_RUN; 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) { error = errno; DPRINTF("ERROR: cannot ptrace child process %d", pid); @@ -123,9 +155,9 @@ proc_create(const char *file, char * con _exit(2); } else { /* The parent owns the process handle. */ - memset(phdl, 0, sizeof(struct proc_handle)); - phdl->pid = pid; - phdl->status = PS_IDLE; + error = proc_init(pid, 0, PS_IDLE, phdl); + if (error != 0) + goto bad; /* Wait for the child process to stop. */ if (waitpid(pid, &status, WUNTRACED) == -1) { Modified: stable/9/lib/libproc/proc_rtld.c ============================================================================== --- stable/9/lib/libproc/proc_rtld.c Wed Aug 27 19:34:49 2014 (r270729) +++ stable/9/lib/libproc/proc_rtld.c Wed Aug 27 19:51:07 2014 (r270730) @@ -49,6 +49,9 @@ map_iter(const rd_loadobj_t *lop, void * if (phdl->rdobjs == NULL) 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)); return (0); Modified: stable/9/lib/libproc/proc_sym.c ============================================================================== --- stable/9/lib/libproc/proc_sym.c Wed Aug 27 19:34:49 2014 (r270729) +++ stable/9/lib/libproc/proc_sym.c Wed Aug 27 19:51:07 2014 (r270730) @@ -90,17 +90,25 @@ proc_obj2map(struct proc_handle *p, cons rd_loadobj_t *rdl; char path[MAXPATHLEN]; + rdl = NULL; for (i = 0; i < p->nobjs; i++) { - rdl = &p->rdobjs[i]; - basename_r(rdl->rdl_path, path); + basename_r(p->rdobjs[i].rdl_path, path); if (strcmp(path, objname) == 0) { - if ((map = malloc(sizeof(*map))) == NULL) - return (NULL); - proc_rdl2prmap(rdl, map); - return (map); + rdl = &p->rdobjs[i]; + break; } } - 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 @@ -358,8 +366,9 @@ proc_name2map(struct proc_handle *p, con free(kves); return (NULL); } - if (name == NULL || strcmp(name, "a.out") == 0) { - map = proc_addr2map(p, p->rdobjs[0].rdl_saddr); + if ((name == NULL || strcmp(name, "a.out") == 0) && + p->rdexec != NULL) { + map = proc_addr2map(p, p->rdexec->rdl_saddr); return (map); } for (i = 0; i < p->nobjs; i++) {