[PATCH 2/6] linux: Add __libc_procmaps_iterate
Adhemerval Zanella
adhemerval.zanella@linaro.org
Tue Sep 15 18:34:46 GMT 2026
It abstracts reading and parsing /proc/self/maps on top of
__libc_procutils_read_file.
Checked on x86_64-linux-gnu.
---
sysdeps/unix/sysv/linux/Makefile | 1 +
sysdeps/unix/sysv/linux/procmaps.c | 70 ++++++++++++++++++++++++++++++
sysdeps/unix/sysv/linux/procmaps.h | 37 ++++++++++++++++
3 files changed, 108 insertions(+)
create mode 100644 sysdeps/unix/sysv/linux/procmaps.c
create mode 100644 sysdeps/unix/sysv/linux/procmaps.h
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 31f3a7534ae..d9c2b981f3d 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -554,6 +554,7 @@ sysdep_routines += \
pidfd_getpid \
pidfd_spawn \
pidfd_spawnp \
+ procmaps \
procutils \
sched_getcpu \
spawnattr_getcgroup_np \
diff --git a/sysdeps/unix/sysv/linux/procmaps.c b/sysdeps/unix/sysv/linux/procmaps.c
new file mode 100644
index 00000000000..ddb3aedb4cf
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/procmaps.c
@@ -0,0 +1,70 @@
+/* Utility function to parse the Linux /proc/self/maps.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <procmaps.h>
+#include <stdlib.h>
+
+struct parse_line_args
+{
+ procmaps_closure_t closure;
+ void *arg;
+};
+
+/* Parse the leading "<start>-<end> <perms>" fields of a /proc/self/maps line
+ and pass them to the user provided closure. */
+static bool
+parse_line (const char *line, void *arg)
+{
+ struct parse_line_args *args = arg;
+
+ char *p;
+ uintptr_t start = strtoul (line, &p, 16);
+ if (p == line || *p++ != '-')
+ return false;
+
+ char *q;
+ uintptr_t end = strtoul (p, &q, 16);
+ if (q == p || *q++ != ' ')
+ return false;
+
+ return args->closure (start, end, q, args->arg);
+}
+
+enum
+{
+ /* Each /proc/self/maps line starts with "<start>-<end> <perms> ", where
+ START and END are hexadecimal numbers and PERMS is a 4 character string.
+ The parse_line only parses these leading fields and the reader passes
+ longer lines truncated, discarding the rest of the line (such as the
+ pathname). */
+ minimum_proc_maps_size = 2 * (2 * sizeof (uintptr_t)) + sizeof ("- rwxp"),
+};
+
+enum procutils_read_result_t
+__libc_procmaps_iterate (procmaps_closure_t closure, void *arg)
+{
+ struct parse_line_args args = { .closure = closure, .arg = arg };
+
+ /* A buffer larger than the minimum required size reduces the number
+ of read syscalls. */
+ char buffer[2048];
+ _Static_assert (sizeof buffer >= minimum_proc_maps_size,
+ "sizeof buffer < minimum_proc_maps_size");
+ return __libc_procutils_read_file ("/proc/self/maps", buffer,
+ sizeof buffer, parse_line, &args);
+}
diff --git a/sysdeps/unix/sysv/linux/procmaps.h b/sysdeps/unix/sysv/linux/procmaps.h
new file mode 100644
index 00000000000..8aab97d6bf3
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/procmaps.h
@@ -0,0 +1,37 @@
+/* Utility function to parse the Linux /proc/self/maps.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#ifndef _PROCMAPS_H
+#define _PROCMAPS_H
+
+#include <procutils.h>
+#include <stdint.h>
+
+/* Called for each /proc/self/maps entry, with the mapping address range
+ [START, END) and PERM pointing to its 4 character permission field. It
+ should return true to stop the iteration. */
+typedef bool (*procmaps_closure_t) (uintptr_t start, uintptr_t end,
+ const char *perm, void *arg);
+
+/* Open and read /proc/self/maps, and call CLOSURE with argument ARG on
+ each parsed entry. Entries that fail to parse are skipped. */
+enum procutils_read_result_t
+__libc_procmaps_iterate (procmaps_closure_t closure, void *arg)
+ attribute_hidden;
+
+#endif
--
2.53.0
More information about the Libc-alpha
mailing list