2 * Copyright (c) 2016 Mark Johnston <markj@FreeBSD.org>
3 * Copyright (c) 2010 The FreeBSD Foundation
4 * Copyright (c) 2008 John Birrell (jb@freebsd.org)
7 * Portions of this software were developed by Rui Paulo under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
38 #include <sys/ctf_api.h>
58 #define PATH_DEBUG_DIR "/usr/lib/debug"
61 typedef struct ctf_file ctf_file_t;
64 #ifndef NO_CXA_DEMANGLE
65 extern char *__cxa_demangle(const char *, char *, size_t *, int *);
66 #endif /* NO_CXA_DEMANGLE */
69 crc32_file(int fd, uint32_t *crc)
71 uint8_t buf[PAGE_SIZE], *p;
75 while ((n = read(fd, buf, sizeof(buf))) > 0) {
78 *crc = crc32_tab[(*crc ^ *p++) & 0xff] ^ (*crc >> 8);
85 demangle(const char *symbol, char *buf, size_t len)
87 #ifndef NO_CXA_DEMANGLE
90 if (symbol[0] == '_' && symbol[1] == 'Z' && symbol[2]) {
91 dembuf = __cxa_demangle(symbol, NULL, NULL, NULL);
94 strlcpy(buf, dembuf, len);
99 #endif /* NO_CXA_DEMANGLE */
100 strlcpy(buf, symbol, len);
104 symvalcomp(void *thunk, const void *a1, const void *a2)
106 struct symtab *symtab;
111 i1 = *(const u_int *)a1;
112 i2 = *(const u_int *)a2;
115 (void)gelf_getsym(symtab->data, i1, &sym1);
116 (void)gelf_getsym(symtab->data, i2, &sym2);
117 if (sym1.st_value < sym2.st_value)
119 else if (sym1.st_value == sym2.st_value)
127 load_symtab(Elf *e, struct symtab *symtab, u_long sh_type)
134 if (gelf_getehdr(e, &ehdr) == NULL)
138 while ((scn = elf_nextscn(e, scn)) != NULL) {
139 (void)gelf_getshdr(scn, &shdr);
140 if (shdr.sh_type == sh_type)
146 nsyms = shdr.sh_size / shdr.sh_entsize;
147 if (nsyms > (1 << 20))
150 if ((symtab->data = elf_getdata(scn, NULL)) == NULL)
153 symtab->index = calloc(nsyms, sizeof(u_int));
154 if (symtab->index == NULL)
156 for (u_int i = 0; i < nsyms; i++)
157 symtab->index[i] = i;
158 qsort_r(symtab->index, nsyms, sizeof(u_int), symtab, symvalcomp);
159 symtab->nsyms = nsyms;
160 symtab->stridx = shdr.sh_link;
165 load_symtabs(struct file_info *file)
168 file->symtab.nsyms = file->dynsymtab.nsyms = 0;
169 (void)load_symtab(file->elf, &file->symtab, SHT_SYMTAB);
170 (void)load_symtab(file->elf, &file->dynsymtab, SHT_DYNSYM);
174 open_debug_file(char *path, const char *debugfile, uint32_t crc)
181 if ((n = strlcat(path, "/", PATH_MAX)) >= PATH_MAX)
183 if (strlcat(path, debugfile, PATH_MAX) >= PATH_MAX)
185 if ((fd = open(path, O_RDONLY | O_CLOEXEC)) < 0)
187 if (crc32_file(fd, &compcrc) != 0 || crc != compcrc) {
188 DPRINTFX("ERROR: CRC32 mismatch for %s", path);
198 * Obtain an ELF descriptor for the specified mapped object. If a GNU debuglink
199 * section is present, a descriptor for the corresponding debug file is
203 open_object(struct map_info *mapping)
210 struct file_info *file;
212 const char *debugfile, *scnname;
217 if (mapping->map.pr_mapname[0] == '\0')
218 return (-1); /* anonymous object */
219 if (mapping->file->elf != NULL)
220 return (0); /* already loaded */
222 file = mapping->file;
224 if ((fd = open(map->pr_mapname, O_RDONLY | O_CLOEXEC)) < 0) {
225 DPRINTF("ERROR: open %s failed", map->pr_mapname);
228 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
229 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
232 if (gelf_getehdr(e, &file->ehdr) != &file->ehdr) {
233 DPRINTFX("ERROR: elf_getehdr() failed: %s", elf_errmsg(-1));
238 while ((scn = elf_nextscn(e, scn)) != NULL) {
239 if (gelf_getshdr(scn, &shdr) != &shdr) {
240 DPRINTFX("ERROR: gelf_getshdr failed: %s",
244 if (shdr.sh_type != SHT_PROGBITS)
246 if (elf_getshdrstrndx(e, &ndx) != 0) {
247 DPRINTFX("ERROR: elf_getshdrstrndx failed: %s",
251 if ((scnname = elf_strptr(e, ndx, shdr.sh_name)) == NULL)
254 if (strcmp(scnname, ".gnu_debuglink") == 0)
260 if ((data = elf_getdata(scn, NULL)) == NULL) {
261 DPRINTFX("ERROR: elf_getdata failed: %s", elf_errmsg(-1));
266 * The data contains a null-terminated file name followed by a 4-byte
269 if (data->d_size < sizeof(crc) + 1) {
270 DPRINTFX("ERROR: debuglink section is too small (%zd bytes)",
274 if (strnlen(data->d_buf, data->d_size) >= data->d_size - sizeof(crc)) {
275 DPRINTFX("ERROR: no null-terminator in gnu_debuglink section");
279 debugfile = data->d_buf;
280 memcpy(&crc, (char *)data->d_buf + data->d_size - sizeof(crc),
284 * Search for the debug file using the algorithm described in the gdb
286 * - look in the directory containing the object,
287 * - look in the subdirectory ".debug" of the directory containing the
289 * - look in the global debug directories (currently /usr/lib/debug).
291 (void)strlcpy(path, map->pr_mapname, sizeof(path));
294 if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
297 if (strlcat(path, "/.debug", sizeof(path)) < sizeof(path) &&
298 (fd2 = open_debug_file(path, debugfile, crc)) >= 0)
301 (void)snprintf(path, sizeof(path), PATH_DEBUG_DIR);
302 if (strlcat(path, map->pr_mapname, sizeof(path)) < sizeof(path)) {
304 if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
309 /* We didn't find a debug file, just return the object's descriptor. */
316 if ((e2 = elf_begin(fd2, ELF_C_READ, NULL)) == NULL) {
317 DPRINTFX("ERROR: elf_begin failed: %s", elf_errmsg(-1));
336 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
342 for (i = 0; i < p->nmappings; i++) {
343 map = &p->mappings[i].map;
344 if (addr >= map->pr_vaddr &&
345 addr < map->pr_vaddr + map->pr_size) {
346 strlcpy(objname, map->pr_mapname, objnamesz);
354 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
356 char last[MAXPATHLEN], path[MAXPATHLEN], *base;
361 if (p->nmappings == 0)
362 if (proc_rdagent(p) == NULL)
366 memset(last, 0, sizeof(last));
367 for (i = 0; i < p->nmappings; i++) {
368 map = &p->mappings[i].map;
369 strlcpy(path, map->pr_mapname, sizeof(path));
370 base = basename(path);
372 * We shouldn't call the callback twice with the same object.
373 * To do that we are assuming the fact that if there are
374 * repeated object names (i.e. different mappings for the
375 * same object) they occur next to each other.
377 if (strcmp(base, last) == 0)
379 if ((error = (*func)(cd, map, base)) != 0)
381 strlcpy(last, path, sizeof(last));
386 static struct map_info *
387 _proc_addr2map(struct proc_handle *p, uintptr_t addr)
389 struct map_info *mapping;
392 if (p->nmappings == 0)
393 if (proc_rdagent(p) == NULL)
395 for (i = 0; i < p->nmappings; i++) {
396 mapping = &p->mappings[i];
397 if (addr >= mapping->map.pr_vaddr &&
398 addr < mapping->map.pr_vaddr + mapping->map.pr_size)
405 proc_addr2map(struct proc_handle *p, uintptr_t addr)
408 return (&_proc_addr2map(p, addr)->map);
412 * Look up the symbol at addr using a binary search, returning a copy of the
413 * symbol and its name.
416 lookup_symbol_by_addr(Elf *elf, struct symtab *symtab, uintptr_t addr,
417 const char **namep, GElf_Sym *sym)
425 max = symtab->nsyms - 1;
428 mid = (max + min) / 2;
429 (void)gelf_getsym(data, symtab->index[mid], sym);
430 if (addr >= sym->st_value &&
431 addr < sym->st_value + sym->st_size) {
432 s = elf_strptr(elf, symtab->stridx, sym->st_name);
433 if (s != NULL && namep != NULL)
438 if (addr < sym->st_value)
447 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
448 size_t namesz, GElf_Sym *symcopy)
450 struct file_info *file;
451 struct map_info *mapping;
456 if ((mapping = _proc_addr2map(p, addr)) == NULL) {
457 DPRINTFX("ERROR: proc_addr2map failed to resolve 0x%jx", addr);
460 if (open_object(mapping) != 0) {
461 DPRINTFX("ERROR: failed to open object %s",
462 mapping->map.pr_mapname);
466 file = mapping->file;
467 off = file->ehdr.e_type == ET_DYN ? mapping->map.pr_vaddr : 0;
472 error = lookup_symbol_by_addr(file->elf, &file->dynsymtab, addr, &s,
475 error = lookup_symbol_by_addr(file->elf, &file->symtab, addr,
478 symcopy->st_value += off;
479 demangle(s, name, namesz);
484 static struct map_info *
485 _proc_name2map(struct proc_handle *p, const char *name)
487 char path[MAXPATHLEN], *base;
488 struct map_info *mapping;
491 if ((len = strlen(name)) == 0)
493 if (p->nmappings == 0)
494 if (proc_rdagent(p) == NULL)
496 for (i = 0; i < p->nmappings; i++) {
497 mapping = &p->mappings[i];
498 (void)strlcpy(path, mapping->map.pr_mapname, sizeof(path));
499 base = basename(path);
500 if (strcmp(base, name) == 0)
503 /* If we didn't find a match, try matching prefixes of the basename. */
504 for (i = 0; i < p->nmappings; i++) {
505 strlcpy(path, p->mappings[i].map.pr_mapname, sizeof(path));
506 base = basename(path);
507 if (strncmp(base, name, len) == 0)
508 return (&p->mappings[i]);
510 if (strcmp(name, "a.out") == 0)
511 return (_proc_addr2map(p, p->exec_map->pr_vaddr));
516 proc_name2map(struct proc_handle *p, const char *name)
519 return (&_proc_name2map(p, name)->map);
523 * Look up the symbol with the given name and return a copy of it.
526 lookup_symbol_by_name(Elf *elf, struct symtab *symtab, const char *symbol,
527 GElf_Sym *symcopy, prsyminfo_t *si)
534 if (symtab->nsyms == 0)
537 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
538 s = elf_strptr(elf, symtab->stridx, sym.st_name);
539 if (s != NULL && strcmp(s, symbol) == 0) {
540 memcpy(symcopy, &sym, sizeof(*symcopy));
550 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
551 GElf_Sym *symcopy, prsyminfo_t *si)
553 struct file_info *file;
554 struct map_info *mapping;
558 if ((mapping = _proc_name2map(p, object)) == NULL) {
559 DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
562 if (open_object(mapping) != 0) {
563 DPRINTFX("ERROR: failed to open object %s",
564 mapping->map.pr_mapname);
568 file = mapping->file;
569 off = file->ehdr.e_type == ET_DYN ? mapping->map.pr_vaddr : 0;
571 error = lookup_symbol_by_name(file->elf, &file->dynsymtab, symbol,
574 error = lookup_symbol_by_name(file->elf, &file->symtab, symbol,
577 symcopy->st_value += off;
582 proc_name2ctf(struct proc_handle *p, const char *name)
589 if ((map = proc_name2map(p, name)) == NULL)
592 ctf = ctf_open(map->pr_mapname, &error);
602 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
603 int mask, proc_sym_f *func, void *cd)
606 struct file_info *file;
607 struct map_info *mapping;
608 struct symtab *symtab;
612 if ((mapping = _proc_name2map(p, object)) == NULL) {
613 DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
616 if (open_object(mapping) != 0) {
617 DPRINTFX("ERROR: failed to open object %s",
618 mapping->map.pr_mapname);
622 file = mapping->file;
623 symtab = which == PR_SYMTAB ? &file->symtab : &file->dynsymtab;
624 if (symtab->nsyms == 0)
628 for (i = 0; gelf_getsym(symtab->data, i, &sym) != NULL; i++) {
629 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
630 (mask & BIND_LOCAL) == 0)
632 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
633 (mask & BIND_GLOBAL) == 0)
635 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
636 (mask & BIND_WEAK) == 0)
638 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
639 (mask & TYPE_NOTYPE) == 0)
641 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
642 (mask & TYPE_OBJECT) == 0)
644 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
645 (mask & TYPE_FUNC) == 0)
647 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
648 (mask & TYPE_SECTION) == 0)
650 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
651 (mask & TYPE_FILE) == 0)
653 s = elf_strptr(file->elf, symtab->stridx, sym.st_name);
654 if (file->ehdr.e_type == ET_DYN)
655 sym.st_value += mapping->map.pr_vaddr;
656 if ((error = (*func)(cd, &sym, s)) != 0)