[DTrace-devel] [PATCH 2/5] libproc: fix mapping names low in the address space

Nick Alcock nick.alcock at oracle.com
Mon Apr 24 19:08:13 UTC 2023


For reasons which escape my understanding, address ranges in
/proc/$pid/maps are zero-padded up to eight digits, but filenames in
/proc/$pid/map_files are not zero-padded at all.  Strip the zeroes off
when deriving the latter from the former.

(Only observable with non-PIE executables, since only they are ever that
low in the address space.)

Signed-off-by: Nick Alcock <nick.alcock at oracle.com>
---
 libproc/Psymtab.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/libproc/Psymtab.c b/libproc/Psymtab.c
index cd55b8cf1678a..a95161884c27a 100644
--- a/libproc/Psymtab.c
+++ b/libproc/Psymtab.c
@@ -53,6 +53,19 @@ static int Pxlookup_by_name_internal(struct ps_prochandle *P, Lmid_t lmid,
 	(1 << STT_COMMON) | (1 << STT_TLS))
 #define	IS_DATA_TYPE(tp)	(((1 << (tp)) & DATA_TYPES) != 0)
 
+static char *
+dezerostr(char *str)
+{
+	size_t i;
+
+	if (str[0] != '0')
+		return str;
+
+	i = strspn(str, "0");
+	memmove(str, str + i, strlen(str) - i + 1);
+
+	return str;
+}
 
 static ulong_t
 string_hash(const char *key)
@@ -527,6 +540,7 @@ Pupdate_maps(struct ps_prochandle *P)
 		unsigned int minor;
 		char	perms[5];
 		const char *first_space;
+		char *dash;
 		map_info_t *mptr;
 		prmap_file_t *prf;
 		prmap_t *pmptr;
@@ -545,10 +559,19 @@ Pupdate_maps(struct ps_prochandle *P)
 
 		/*
 		 * Skip anonymous mappings, and special mappings like the stack,
-		 * heap, and vdso; also skip on OOM.
+		 * heap, and vdso; also skip on OOM.  Drop leading zeroes from
+		 * the addresses as well (map_files entries lack them).
 		 */
 		first_space = strchr(line, ' ');
 		mapaddrname = strndup(line, first_space - line);
+		dezerostr(mapaddrname);
+
+		dash = strchr(mapaddrname, '-');
+		if (dash) {
+			dash++;
+			dezerostr(dash);
+		}
+
 		if ((fn == NULL) || (mapaddrname == NULL) || (fn[0] == '[')) {
 			free(fn);
 			free(mapaddrname);
-- 
2.39.1.268.g9de2f9a303




More information about the DTrace-devel mailing list