1 /* Standard libdwfl callbacks for debugging a live Linux process.
2 Copyright (C) 2005-2010 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
5 Red Hat elfutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by the
7 Free Software Foundation; version 2 of the License.
9 Red Hat elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with Red Hat elfutils; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18 In addition, as a special exception, Red Hat, Inc. gives You the
19 additional right to link the code of Red Hat elfutils with code licensed
20 under any Open Source Initiative certified open source license
21 (http://www.opensource.org/licenses/index.php) which requires the
22 distribution of source code with any binary distribution and to
23 distribute linked combinations of the two. Non-GPL Code permitted under
24 this exception must only link to the code of Red Hat elfutils through
25 those well defined interfaces identified in the file named EXCEPTION
26 found in the source code files (the "Approved Interfaces"). The files
27 of Non-GPL Code may instantiate templates or use macros or inline
28 functions from the Approved Interfaces without causing the resulting
29 work to be covered by the GNU General Public License. Only Red Hat,
30 Inc. may make changes or additions to the list of Approved Interfaces.
31 Red Hat's grant of this exception is conditioned upon your not adding
32 any new exceptions. If you wish to add a new Approved Interface or
33 exception, please contact Red Hat. You must obey the GNU General Public
34 License in all respects for all of the Red Hat elfutils code and other
35 code used in conjunction with Red Hat elfutils except the Non-GPL Code
36 covered by this exception. If you modify this file, you may extend this
37 exception to your version of the file, but you are not obligated to do
38 so. If you do not wish to provide this exception without modification,
39 you must delete this exception statement from your version and license
40 this file solely under the GPL without exception.
42 Red Hat elfutils is an included package of the Open Invention Network.
43 An included package of the Open Invention Network is a package for which
44 Open Invention Network licensees cross-license their patents. No patent
45 license is granted, either expressly or impliedly, by designation as an
46 included package. Should you wish to participate in the Open Invention
47 Network licensing program, please visit www.openinventionnetwork.com
48 <http://www.openinventionnetwork.com>. */
52 #include <sys/types.h>
55 #include <stdio_ext.h>
65 #define PROCMAPSFMT "/proc/%d/maps"
66 #define PROCMEMFMT "/proc/%d/mem"
67 #define PROCAUXVFMT "/proc/%d/auxv"
70 /* Search /proc/PID/auxv for the AT_SYSINFO_EHDR tag. */
73 grovel_auxv (pid_t pid, Dwfl *dwfl, GElf_Addr *sysinfo_ehdr)
76 if (asprintf (&fname, PROCAUXVFMT, pid) < 0)
79 int fd = open64 (fname, O_RDONLY);
82 return errno == ENOENT ? 0 : errno;
89 char buffer[sizeof (long int) * 2 * 64];
90 Elf64_auxv_t a64[sizeof (long int) * 2 * 64 / sizeof (Elf64_auxv_t)];
91 Elf32_auxv_t a32[sizeof (long int) * 2 * 32 / sizeof (Elf32_auxv_t)];
93 nread = read (fd, &d, sizeof d);
96 switch (sizeof (long int))
99 for (size_t i = 0; (char *) &d.a32[i] < &d.buffer[nread]; ++i)
100 if (d.a32[i].a_type == AT_SYSINFO_EHDR)
102 *sysinfo_ehdr = d.a32[i].a_un.a_val;
103 if (dwfl->segment_align > 1)
109 else if (d.a32[i].a_type == AT_PAGESZ
110 && dwfl->segment_align <= 1)
111 dwfl->segment_align = d.a32[i].a_un.a_val;
114 for (size_t i = 0; (char *) &d.a64[i] < &d.buffer[nread]; ++i)
115 if (d.a64[i].a_type == AT_SYSINFO_EHDR)
117 *sysinfo_ehdr = d.a64[i].a_un.a_val;
118 if (dwfl->segment_align > 1)
124 else if (d.a64[i].a_type == AT_PAGESZ
125 && dwfl->segment_align <= 1)
126 dwfl->segment_align = d.a64[i].a_un.a_val;
138 return nread < 0 ? errno : 0;
142 proc_maps_report (Dwfl *dwfl, FILE *f, GElf_Addr sysinfo_ehdr, pid_t pid)
144 unsigned int last_dmajor = -1, last_dminor = -1;
145 uint64_t last_ino = -1;
146 char *last_file = NULL;
147 Dwarf_Addr low = 0, high = 0;
149 inline bool report (void)
151 if (last_file != NULL)
153 Dwfl_Module *mod = INTUSE(dwfl_report_module) (dwfl, last_file,
157 if (unlikely (mod == NULL))
166 while ((len = getline (&line, &linesz, f)) > 0)
168 if (line[len - 1] == '\n')
169 line[len - 1] = '\0';
171 Dwarf_Addr start, end, offset;
172 unsigned int dmajor, dminor;
175 if (sscanf (line, "%" PRIx64 "-%" PRIx64 " %*s %" PRIx64
176 " %x:%x %" PRIi64 " %n",
177 &start, &end, &offset, &dmajor, &dminor, &ino, &nread) < 6
184 /* If this is the special mapping AT_SYSINFO_EHDR pointed us at,
185 report the last one and then this special one. */
186 if (start == sysinfo_ehdr && start != 0)
198 if (asprintf (&last_file, "[vdso: %d]", (int) pid) < 0
203 char *file = line + nread + strspn (line + nread, " \t");
204 if (file[0] == '\0' || (ino == 0 && dmajor == 0 && dminor == 0))
205 /* This line doesn't indicate a file mapping. */
208 if (last_file != NULL
209 && ino == last_ino && dmajor == last_dmajor && dminor == last_dminor)
211 /* This is another portion of the same file's mapping. */
212 assert (!strcmp (last_file, file));
217 /* This is a different file mapping. Report the last one. */
222 last_file = strdup (file);
224 last_dmajor = dmajor;
225 last_dminor = dminor;
230 int result = ferror_unlocked (f) ? errno : feof_unlocked (f) ? 0 : ENOEXEC;
232 /* Report the final one. */
233 bool lose = report ();
235 return result != 0 ? result : lose ? -1 : 0;
239 dwfl_linux_proc_maps_report (Dwfl *dwfl, FILE *f)
241 return proc_maps_report (dwfl, f, 0, 0);
243 INTDEF (dwfl_linux_proc_maps_report)
246 dwfl_linux_proc_report (Dwfl *dwfl, pid_t pid)
251 /* We'll notice the AT_SYSINFO_EHDR address specially when we hit it. */
252 GElf_Addr sysinfo_ehdr = 0;
253 int result = grovel_auxv (pid, dwfl, &sysinfo_ehdr);
258 if (asprintf (&fname, PROCMAPSFMT, pid) < 0)
261 FILE *f = fopen (fname, "r");
266 (void) __fsetlocking (f, FSETLOCKING_BYCALLER);
268 result = proc_maps_report (dwfl, f, sysinfo_ehdr, pid);
274 INTDEF (dwfl_linux_proc_report)
277 read_proc_memory (void *arg, void *data, GElf_Addr address,
278 size_t minread, size_t maxread)
280 const int fd = *(const int *) arg;
281 ssize_t nread = pread64 (fd, data, maxread, (off64_t) address);
282 /* Some kernels don't actually let us do this read, ignore those errors. */
283 if (nread < 0 && (errno == EINVAL || errno == EPERM))
285 if (nread > 0 && (size_t) nread < minread)
290 extern Elf *elf_from_remote_memory (GElf_Addr ehdr_vma,
291 GElf_Addr *loadbasep,
292 ssize_t (*read_memory) (void *arg,
300 /* Dwfl_Callbacks.find_elf */
303 dwfl_linux_proc_find_elf (Dwfl_Module *mod __attribute__ ((unused)),
304 void **userdata __attribute__ ((unused)),
305 const char *module_name, Dwarf_Addr base,
306 char **file_name, Elf **elfp)
308 if (module_name[0] == '/')
310 int fd = open64 (module_name, O_RDONLY);
313 *file_name = strdup (module_name);
314 if (*file_name == NULL)
324 if (sscanf (module_name, "[vdso: %d]", &pid) == 1)
326 /* Special case for in-memory ELF image. */
329 if (asprintf (&fname, PROCMEMFMT, pid) < 0)
332 int fd = open64 (fname, O_RDONLY);
337 *elfp = elf_from_remote_memory (base, NULL, &read_proc_memory, &fd);
348 INTDEF (dwfl_linux_proc_find_elf)