svn commit: r211184 - head/lib/libproc

Rui Paulo rpaulo at FreeBSD.org
Wed Aug 11 17:33:27 UTC 2010


Author: rpaulo
Date: Wed Aug 11 17:33:26 2010
New Revision: 211184
URL: http://svn.freebsd.org/changeset/base/211184

Log:
  Several fixes for libproc:
  o return the correct status in proc_wstatus()
  o proc_read takes a void *
  o correctly allocate the objs structure array
  
  Sponsored by:	The FreeBSD Foundation

Modified:
  head/lib/libproc/libproc.h
  head/lib/libproc/proc_bkpt.c
  head/lib/libproc/proc_rtld.c
  head/lib/libproc/proc_sym.c
  head/lib/libproc/proc_util.c

Modified: head/lib/libproc/libproc.h
==============================================================================
--- head/lib/libproc/libproc.h	Wed Aug 11 17:25:14 2010	(r211183)
+++ head/lib/libproc/libproc.h	Wed Aug 11 17:33:26 2010	(r211184)
@@ -131,7 +131,7 @@ pid_t	proc_getpid(struct proc_handle *);
 int	proc_wstatus(struct proc_handle *);
 int	proc_getwstat(struct proc_handle *);
 char *	proc_signame(int, char *, size_t);
-int	proc_read(struct proc_handle *, char *, size_t, size_t);
+int	proc_read(struct proc_handle *, void *, size_t, size_t);
 const lwpstatus_t *
 	proc_getlwpstatus(struct proc_handle *);
 void	proc_free(struct proc_handle *);

Modified: head/lib/libproc/proc_bkpt.c
==============================================================================
--- head/lib/libproc/proc_bkpt.c	Wed Aug 11 17:25:14 2010	(r211183)
+++ head/lib/libproc/proc_bkpt.c	Wed Aug 11 17:33:26 2010	(r211184)
@@ -164,7 +164,8 @@ proc_bkptexec(struct proc_handle *phdl, 
 		warn("ERROR: ptrace step failed");
 		return (-1);
 	}
-	status = proc_wstatus(phdl);
+	proc_wstatus(phdl);
+	status = proc_getwstat(phdl);
 	if (!WIFSTOPPED(status)) {
 		warn("ERROR: don't know why process stopped");
 		return (-1);

Modified: head/lib/libproc/proc_rtld.c
==============================================================================
--- head/lib/libproc/proc_rtld.c	Wed Aug 11 17:25:14 2010	(r211183)
+++ head/lib/libproc/proc_rtld.c	Wed Aug 11 17:33:26 2010	(r211184)
@@ -42,14 +42,13 @@ map_iter(const rd_loadobj_t *lop, void *
 {
 	struct proc_handle *phdl = arg;
 
-	phdl->nobjs++;
 	if (phdl->nobjs >= phdl->rdobjsz) {
 		phdl->rdobjsz *= 2;
 		phdl->rdobjs = realloc(phdl->rdobjs, phdl->rdobjsz);
 		if (phdl->rdobjs == NULL)
 			return (-1);
 	}
-	memcpy(&phdl->rdobjs[phdl->nobjs++], lop, sizeof(*phdl->rdobjs));
+	memcpy(&phdl->rdobjs[phdl->nobjs++], lop, sizeof(*lop));
 
 	return (0);
 }
@@ -61,6 +60,7 @@ proc_rdagent(struct proc_handle *phdl)
 	    phdl->status != PS_IDLE) {
 		if ((phdl->rdap = rd_new(phdl)) != NULL) {
 			phdl->rdobjs = malloc(sizeof(*phdl->rdobjs) * 64);
+			phdl->rdobjsz = 64;
 			if (phdl->rdobjs == NULL)
 				return (phdl->rdap);
 			rd_loadobj_iter(phdl->rdap, map_iter, phdl);
@@ -73,7 +73,8 @@ proc_rdagent(struct proc_handle *phdl)
 void
 proc_updatesyms(struct proc_handle *phdl)
 {
-	memset(&phdl->rdobjs, 0, sizeof(*phdl->rdobjs) * phdl->rdobjsz);
+
+	memset(phdl->rdobjs, 0, sizeof(*phdl->rdobjs) * phdl->rdobjsz);
 	phdl->nobjs = 0;
 	rd_loadobj_iter(phdl->rdap, map_iter, phdl);
 }

Modified: head/lib/libproc/proc_sym.c
==============================================================================
--- head/lib/libproc/proc_sym.c	Wed Aug 11 17:25:14 2010	(r211183)
+++ head/lib/libproc/proc_sym.c	Wed Aug 11 17:33:26 2010	(r211184)
@@ -110,14 +110,25 @@ proc_iter_objs(struct proc_handle *p, pr
 	rd_loadobj_t *rdl;
 	prmap_t map;
 	char path[MAXPATHLEN];
+	char last[MAXPATHLEN];
 
 	if (p->nobjs == 0)
 		return (-1);
+	memset(last, 0, sizeof(last));
 	for (i = 0; i < p->nobjs; i++) {
 		rdl = &p->rdobjs[i];
 		proc_rdl2prmap(rdl, &map);
 		basename_r(rdl->rdl_path, path);
+		/*
+		 * We shouldn't call the callback twice with the same object.
+		 * To do that we are assuming the fact that if there are
+		 * repeated object names (i.e. different mappings for the
+		 * same object) they occur next to each other.
+		 */
+		if (strcmp(path, last) == 0)
+			continue;
 		(*func)(cd, &map, path);
+		strlcpy(last, path, sizeof(last));
 	}
 
 	return (0);

Modified: head/lib/libproc/proc_util.c
==============================================================================
--- head/lib/libproc/proc_util.c	Wed Aug 11 17:25:14 2010	(r211183)
+++ head/lib/libproc/proc_util.c	Wed Aug 11 17:33:26 2010	(r211184)
@@ -144,15 +144,17 @@ proc_wstatus(struct proc_handle *phdl)
 
 	if (phdl == NULL)
 		return (-1);
-	if (waitpid(phdl->pid, &status, WUNTRACED) < 0)
+	if (waitpid(phdl->pid, &status, WUNTRACED) < 0) {
+		warn("waitpid");
 		return (-1);
+	}
 	if (WIFSTOPPED(status))
 		phdl->status = PS_STOP;
 	if (WIFEXITED(status) || WIFSIGNALED(status))
 		phdl->status = PS_UNDEAD;
 	phdl->wstat = status;
 
-	return (status);
+	return (phdl->status);
 }
 
 int
@@ -175,7 +177,7 @@ proc_signame(int sig, char *name, size_t
 }
 
 int
-proc_read(struct proc_handle *phdl, char *buf, size_t size, size_t addr)
+proc_read(struct proc_handle *phdl, void *buf, size_t size, size_t addr)
 {
 	struct ptrace_io_desc piod;
 
@@ -200,7 +202,8 @@ proc_getlwpstatus(struct proc_handle *ph
 
 	if (phdl == NULL)
 		return (NULL);
-	if (ptrace(PT_LWPINFO, phdl->pid, (caddr_t)&lwpinfo,sizeof(lwpinfo)) < 0)
+	if (ptrace(PT_LWPINFO, phdl->pid, (caddr_t)&lwpinfo,
+	    sizeof(lwpinfo)) < 0)
 		return (NULL);
 	siginfo = &lwpinfo.pl_siginfo;
 	if (lwpinfo.pl_event == PL_EVENT_SIGNAL &&


More information about the svn-src-head mailing list