1 /* Standard libdwfl callbacks for debugging a live Linux process.
2 Copyright (C) 2005-2010, 2013, 2014 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
31 #include <sys/types.h>
35 #include <stdio_ext.h>
46 #define PROCMAPSFMT "/proc/%d/maps"
47 #define PROCMEMFMT "/proc/%d/mem"
48 #define PROCAUXVFMT "/proc/%d/auxv"
49 #define PROCEXEFMT "/proc/%d/exe"
52 /* Return ELFCLASS64 or ELFCLASS32 for the main ELF executable. Return
53 ELFCLASSNONE for an error. */
56 get_pid_class (pid_t pid)
59 if (asprintf (&fname, PROCEXEFMT, pid) < 0)
62 int fd = open (fname, O_RDONLY);
67 unsigned char buf[EI_CLASS + 1];
68 ssize_t nread = pread_retry (fd, &buf, sizeof buf, 0);
70 if (nread != sizeof buf || buf[EI_MAG0] != ELFMAG0
71 || buf[EI_MAG1] != ELFMAG1 || buf[EI_MAG2] != ELFMAG2
72 || buf[EI_MAG3] != ELFMAG3
73 || (buf[EI_CLASS] != ELFCLASS64 && buf[EI_CLASS] != ELFCLASS32))
79 /* Search /proc/PID/auxv for the AT_SYSINFO_EHDR tag.
81 It would be easiest to call get_pid_class and parse everything according to
82 the 32-bit or 64-bit class. But this would bring the overhead of syscalls
83 to open and read the "/proc/%d/exe" file.
85 Therefore this function tries to parse the "/proc/%d/auxv" content both
86 ways, as if it were the 32-bit format and also if it were the 64-bit format.
87 Only if it gives some valid data in both cases get_pid_class gets called.
88 In most cases only one of the format bit sizes gives valid data and the
89 get_pid_class call overhead can be saved. */
92 grovel_auxv (pid_t pid, Dwfl *dwfl, GElf_Addr *sysinfo_ehdr)
95 if (asprintf (&fname, PROCAUXVFMT, pid) < 0)
98 int fd = open (fname, O_RDONLY);
101 return errno == ENOENT ? 0 : errno;
103 GElf_Addr sysinfo_ehdr64 = 0;
104 GElf_Addr sysinfo_ehdr32 = 0;
105 GElf_Addr segment_align64 = dwfl->segment_align;
106 GElf_Addr segment_align32 = dwfl->segment_align;
111 Elf64_auxv_t a64[64];
112 Elf32_auxv_t a32[128];
116 eu_static_assert (sizeof d.a64 == sizeof d.a32);
117 nread = pread_retry (fd, d.a64, sizeof d.a64, offset);
124 for (size_t a32i = 0; a32i < nread / sizeof d.a32[0]; a32i++)
126 const Elf32_auxv_t *a32 = d.a32 + a32i;
129 case AT_SYSINFO_EHDR:
130 sysinfo_ehdr32 = a32->a_un.a_val;
133 segment_align32 = a32->a_un.a_val;
137 for (size_t a64i = 0; a64i < nread / sizeof d.a64[0]; a64i++)
139 const Elf64_auxv_t *a64 = d.a64 + a64i;
142 case AT_SYSINFO_EHDR:
143 sysinfo_ehdr64 = a64->a_un.a_val;
146 segment_align64 = a64->a_un.a_val;
152 while (nread == sizeof d.a64);
156 bool valid64 = sysinfo_ehdr64 != 0 || segment_align64 != dwfl->segment_align;
157 bool valid32 = sysinfo_ehdr32 != 0 || segment_align32 != dwfl->segment_align;
159 unsigned char pid_class = ELFCLASSNONE;
160 if (valid64 && valid32)
161 pid_class = get_pid_class (pid);
163 if (pid_class == ELFCLASS64 || (valid64 && ! valid32))
165 *sysinfo_ehdr = sysinfo_ehdr64;
166 dwfl->segment_align = segment_align64;
169 if (pid_class == ELFCLASS32 || (! valid64 && valid32))
171 *sysinfo_ehdr = sysinfo_ehdr32;
172 dwfl->segment_align = segment_align32;
179 do_report (Dwfl *dwfl, char **plast_file, Dwarf_Addr low, Dwarf_Addr high)
181 if (*plast_file != NULL)
183 Dwfl_Module *mod = INTUSE(dwfl_report_module) (dwfl, *plast_file,
187 if (unlikely (mod == NULL))
193 #define report() do_report(dwfl, &last_file, low, high)
196 proc_maps_report (Dwfl *dwfl, FILE *f, GElf_Addr sysinfo_ehdr, pid_t pid)
198 unsigned int last_dmajor = -1, last_dminor = -1;
199 uint64_t last_ino = -1;
200 char *last_file = NULL;
201 Dwarf_Addr low = 0, high = 0;
206 while ((len = getline (&line, &linesz, f)) > 0)
208 if (line[len - 1] == '\n')
209 line[len - 1] = '\0';
211 Dwarf_Addr start, end, offset;
212 unsigned int dmajor, dminor;
215 if (sscanf (line, "%" PRIx64 "-%" PRIx64 " %*s %" PRIx64
216 " %x:%x %" PRIi64 " %n",
217 &start, &end, &offset, &dmajor, &dminor, &ino, &nread) < 6
224 /* If this is the special mapping AT_SYSINFO_EHDR pointed us at,
225 report the last one and then this special one. */
226 if (start == sysinfo_ehdr && start != 0)
237 if (asprintf (&last_file, "[vdso: %d]", (int) pid) < 0
242 char *file = line + nread + strspn (line + nread, " \t");
243 if (file[0] != '/' || (ino == 0 && dmajor == 0 && dminor == 0))
244 /* This line doesn't indicate a file mapping. */
247 if (last_file != NULL
248 && ino == last_ino && dmajor == last_dmajor && dminor == last_dminor)
250 /* This is another portion of the same file's mapping. */
251 if (strcmp (last_file, file) != 0)
257 /* This is a different file mapping. Report the last one. */
262 last_file = strdup (file);
264 last_dmajor = dmajor;
265 last_dminor = dminor;
270 int result = ferror_unlocked (f) ? errno : feof_unlocked (f) ? 0 : ENOEXEC;
272 /* Report the final one. */
273 bool lose = report ();
275 return result != 0 ? result : lose ? -1 : 0;
279 dwfl_linux_proc_maps_report (Dwfl *dwfl, FILE *f)
281 return proc_maps_report (dwfl, f, 0, 0);
283 INTDEF (dwfl_linux_proc_maps_report)
286 dwfl_linux_proc_report (Dwfl *dwfl, pid_t pid)
291 /* We'll notice the AT_SYSINFO_EHDR address specially when we hit it. */
292 GElf_Addr sysinfo_ehdr = 0;
293 int result = grovel_auxv (pid, dwfl, &sysinfo_ehdr);
298 if (asprintf (&fname, PROCMAPSFMT, pid) < 0)
301 FILE *f = fopen (fname, "r");
306 (void) __fsetlocking (f, FSETLOCKING_BYCALLER);
308 result = proc_maps_report (dwfl, f, sysinfo_ehdr, pid);
314 INTDEF (dwfl_linux_proc_report)
317 read_proc_memory (void *arg, void *data, GElf_Addr address,
318 size_t minread, size_t maxread)
320 const int fd = *(const int *) arg;
322 /* This code relies on the fact the Linux kernel accepts negative
323 offsets when seeking /dev/$$/mem files, as a special case. In
324 particular pread cannot be used here, because it will always
325 return EINVAL when passed a negative offset. */
327 if (lseek (fd, (off_t) address, SEEK_SET) == -1)
330 ssize_t nread = read (fd, data, maxread);
332 if (nread > 0 && (size_t) nread < minread)
337 extern Elf *elf_from_remote_memory (GElf_Addr ehdr_vma,
339 GElf_Addr *loadbasep,
340 ssize_t (*read_memory) (void *arg,
348 /* Dwfl_Callbacks.find_elf */
351 dwfl_linux_proc_find_elf (Dwfl_Module *mod __attribute__ ((unused)),
352 void **userdata __attribute__ ((unused)),
353 const char *module_name, Dwarf_Addr base,
354 char **file_name, Elf **elfp)
357 if (module_name[0] == '/')
359 /* When this callback is used together with dwfl_linux_proc_report
360 then we might see mappings of special character devices. Make
361 sure we only open and return regular files. Special devices
362 might hang on open or read. (deleted) files are super special.
363 The image might come from memory if we are attached. */
365 if (stat (module_name, &sb) == -1 || (sb.st_mode & S_IFMT) != S_IFREG)
367 if (strcmp (strrchr (module_name, ' ') ?: "", " (deleted)") == 0)
368 pid = INTUSE(dwfl_pid) (mod->dwfl);
375 int fd = open (module_name, O_RDONLY);
378 *file_name = strdup (module_name);
379 if (*file_name == NULL)
389 if (pid != -1 || sscanf (module_name, "[vdso: %d]", &pid) == 1)
391 /* Special case for in-memory ELF image. */
394 bool tid_was_stopped = false;
395 struct __libdwfl_pid_arg *pid_arg = __libdwfl_get_pid_arg (mod->dwfl);
396 if (pid_arg != NULL && ! pid_arg->assume_ptrace_stopped)
398 /* If any thread is already attached we are fine. Read
399 through that thread. It doesn't have to be the main
401 pid_t tid = pid_arg->tid_attached;
405 detach = __libdwfl_ptrace_attach (pid, &tid_was_stopped);
409 if (asprintf (&fname, PROCMEMFMT, pid) < 0)
412 int fd = open (fname, O_RDONLY);
417 *elfp = elf_from_remote_memory (base, getpagesize (), NULL,
418 &read_proc_memory, &fd);
426 __libdwfl_ptrace_detach (pid, tid_was_stopped);
432 INTDEF (dwfl_linux_proc_find_elf)