2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016-2017 Mark Johnston <markj@FreeBSD.org>
5 * Copyright (c) 2010 The FreeBSD Foundation
6 * Copyright (c) 2008 John Birrell (jb@freebsd.org)
9 * Portions of this software were developed by Rui Paulo under sponsorship
10 * from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/types.h>
40 #include <sys/ctf_api.h>
60 #define PATH_DEBUG_DIR "/usr/lib/debug"
63 typedef struct ctf_file ctf_file_t;
66 #ifndef NO_CXA_DEMANGLE
67 extern char *__cxa_demangle(const char *, char *, size_t *, int *);
68 #endif /* NO_CXA_DEMANGLE */
71 crc32_file(int fd, uint32_t *crc)
73 uint8_t buf[PAGE_SIZE], *p;
77 while ((n = read(fd, buf, sizeof(buf))) > 0) {
80 *crc = crc32_tab[(*crc ^ *p++) & 0xff] ^ (*crc >> 8);
87 demangle(const char *symbol, char *buf, size_t len)
89 #ifndef NO_CXA_DEMANGLE
92 if (symbol[0] == '_' && symbol[1] == 'Z' && symbol[2]) {
93 dembuf = __cxa_demangle(symbol, NULL, NULL, NULL);
96 strlcpy(buf, dembuf, len);
101 #endif /* NO_CXA_DEMANGLE */
102 strlcpy(buf, symbol, len);
105 struct symsort_thunk {
107 struct symtab *symtab;
111 symvalcmp(void *_thunk, const void *a1, const void *a2)
114 struct symsort_thunk *thunk;
119 i1 = *(const u_int *)a1;
120 i2 = *(const u_int *)a2;
123 (void)gelf_getsym(thunk->symtab->data, i1, &sym1);
124 (void)gelf_getsym(thunk->symtab->data, i2, &sym2);
126 if (sym1.st_value != sym2.st_value)
127 return (sym1.st_value < sym2.st_value ? -1 : 1);
129 /* Prefer non-local symbols. */
130 bind1 = GELF_ST_BIND(sym1.st_info);
131 bind2 = GELF_ST_BIND(sym2.st_info);
132 if (bind1 != bind2) {
133 if (bind1 == STB_LOCAL && bind2 != STB_LOCAL)
135 if (bind1 != STB_LOCAL && bind2 == STB_LOCAL)
139 s1 = elf_strptr(thunk->e, thunk->symtab->stridx, sym1.st_name);
140 s2 = elf_strptr(thunk->e, thunk->symtab->stridx, sym2.st_name);
141 if (s1 != NULL && s2 != NULL) {
142 /* Prefer symbols without a leading '$'. */
148 /* Prefer symbols with fewer leading underscores. */
149 for (; *s1 == '_' && *s2 == '_'; s1++, s2++)
161 load_symtab(Elf *e, struct symtab *symtab, u_long sh_type)
165 struct symsort_thunk thunk;
169 if (gelf_getehdr(e, &ehdr) == NULL)
173 while ((scn = elf_nextscn(e, scn)) != NULL) {
174 (void)gelf_getshdr(scn, &shdr);
175 if (shdr.sh_type == sh_type)
181 nsyms = shdr.sh_size / shdr.sh_entsize;
182 if (nsyms > (1 << 20))
185 if ((symtab->data = elf_getdata(scn, NULL)) == NULL)
188 symtab->index = calloc(nsyms, sizeof(u_int));
189 if (symtab->index == NULL)
191 for (u_int i = 0; i < nsyms; i++)
192 symtab->index[i] = i;
193 symtab->nsyms = nsyms;
194 symtab->stridx = shdr.sh_link;
197 thunk.symtab = symtab;
198 qsort_r(symtab->index, nsyms, sizeof(u_int), &thunk, symvalcmp);
204 load_symtabs(struct file_info *file)
207 file->symtab.nsyms = file->dynsymtab.nsyms = 0;
208 (void)load_symtab(file->elf, &file->symtab, SHT_SYMTAB);
209 (void)load_symtab(file->elf, &file->dynsymtab, SHT_DYNSYM);
213 open_debug_file(char *path, const char *debugfile, uint32_t crc)
220 if ((n = strlcat(path, "/", PATH_MAX)) >= PATH_MAX)
222 if (strlcat(path, debugfile, PATH_MAX) >= PATH_MAX)
224 if ((fd = open(path, O_RDONLY | O_CLOEXEC)) < 0)
226 if (crc32_file(fd, &compcrc) != 0 || crc != compcrc) {
227 DPRINTFX("ERROR: CRC32 mismatch for %s", path);
237 * Obtain an ELF descriptor for the specified mapped object. If a GNU debuglink
238 * section is present, a descriptor for the corresponding debug file is
242 open_object(struct map_info *mapping)
249 struct file_info *file;
251 const char *debugfile, *scnname;
256 if (mapping->map.pr_mapname[0] == '\0')
257 return (-1); /* anonymous object */
258 if (mapping->file->elf != NULL)
259 return (0); /* already loaded */
261 file = mapping->file;
263 if ((fd = open(map->pr_mapname, O_RDONLY | O_CLOEXEC)) < 0) {
264 DPRINTF("ERROR: open %s failed", map->pr_mapname);
267 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
268 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
271 if (gelf_getehdr(e, &file->ehdr) != &file->ehdr) {
272 DPRINTFX("ERROR: elf_getehdr() failed: %s", elf_errmsg(-1));
277 while ((scn = elf_nextscn(e, scn)) != NULL) {
278 if (gelf_getshdr(scn, &shdr) != &shdr) {
279 DPRINTFX("ERROR: gelf_getshdr failed: %s",
283 if (shdr.sh_type != SHT_PROGBITS)
285 if (elf_getshdrstrndx(e, &ndx) != 0) {
286 DPRINTFX("ERROR: elf_getshdrstrndx failed: %s",
290 if ((scnname = elf_strptr(e, ndx, shdr.sh_name)) == NULL)
293 if (strcmp(scnname, ".gnu_debuglink") == 0)
299 if ((data = elf_getdata(scn, NULL)) == NULL) {
300 DPRINTFX("ERROR: elf_getdata failed: %s", elf_errmsg(-1));
305 * The data contains a null-terminated file name followed by a 4-byte
308 if (data->d_size < sizeof(crc) + 1) {
309 DPRINTFX("ERROR: debuglink section is too small (%zd bytes)",
313 if (strnlen(data->d_buf, data->d_size) >= data->d_size - sizeof(crc)) {
314 DPRINTFX("ERROR: no null-terminator in gnu_debuglink section");
318 debugfile = data->d_buf;
319 memcpy(&crc, (char *)data->d_buf + data->d_size - sizeof(crc),
323 * Search for the debug file using the algorithm described in the gdb
325 * - look in the directory containing the object,
326 * - look in the subdirectory ".debug" of the directory containing the
328 * - look in the global debug directories (currently /usr/lib/debug).
330 (void)strlcpy(path, map->pr_mapname, sizeof(path));
333 if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
336 if (strlcat(path, "/.debug", sizeof(path)) < sizeof(path) &&
337 (fd2 = open_debug_file(path, debugfile, crc)) >= 0)
340 (void)snprintf(path, sizeof(path), PATH_DEBUG_DIR);
341 if (strlcat(path, map->pr_mapname, sizeof(path)) < sizeof(path)) {
343 if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
348 /* We didn't find a debug file, just return the object's descriptor. */
355 if ((e2 = elf_begin(fd2, ELF_C_READ, NULL)) == NULL) {
356 DPRINTFX("ERROR: elf_begin failed: %s", elf_errmsg(-1));
375 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
381 if (p->nmappings == 0)
382 if (proc_rdagent(p) == NULL)
384 for (i = 0; i < p->nmappings; i++) {
385 map = &p->mappings[i].map;
386 if (addr >= map->pr_vaddr &&
387 addr < map->pr_vaddr + map->pr_size) {
388 strlcpy(objname, map->pr_mapname, objnamesz);
396 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
398 char last[MAXPATHLEN], path[MAXPATHLEN], *base;
403 if (p->nmappings == 0)
404 if (proc_rdagent(p) == NULL)
408 memset(last, 0, sizeof(last));
409 for (i = 0; i < p->nmappings; i++) {
410 map = &p->mappings[i].map;
411 strlcpy(path, map->pr_mapname, sizeof(path));
412 base = basename(path);
414 * We shouldn't call the callback twice with the same object.
415 * To do that we are assuming the fact that if there are
416 * repeated object names (i.e. different mappings for the
417 * same object) they occur next to each other.
419 if (strcmp(base, last) == 0)
421 if ((error = (*func)(cd, map, base)) != 0)
423 strlcpy(last, path, sizeof(last));
428 static struct map_info *
429 _proc_addr2map(struct proc_handle *p, uintptr_t addr)
431 struct map_info *mapping;
434 if (p->nmappings == 0)
435 if (proc_rdagent(p) == NULL)
437 for (i = 0; i < p->nmappings; i++) {
438 mapping = &p->mappings[i];
439 if (addr >= mapping->map.pr_vaddr &&
440 addr < mapping->map.pr_vaddr + mapping->map.pr_size)
447 proc_addr2map(struct proc_handle *p, uintptr_t addr)
450 return (&_proc_addr2map(p, addr)->map);
454 * Look up the symbol at addr using a binary search, returning a copy of the
455 * symbol and its name.
458 lookup_symbol_by_addr(Elf *e, struct symtab *symtab, uintptr_t addr,
459 const char **namep, GElf_Sym *symp)
464 u_int i, min, max, mid;
466 if (symtab->nsyms == 0)
471 max = symtab->nsyms - 1;
474 mid = (max + min) / 2;
475 (void)gelf_getsym(data, symtab->index[mid], &sym);
476 if (addr >= sym.st_value && addr < sym.st_value + sym.st_size)
479 if (addr < sym.st_value)
488 * Advance until we find the matching symbol with largest index.
490 for (i = mid; i < symtab->nsyms; i++) {
491 (void)gelf_getsym(data, symtab->index[i], &sym);
492 if (addr < sym.st_value || addr >= sym.st_value + sym.st_size)
495 (void)gelf_getsym(data, symtab->index[i - 1], symp);
496 s = elf_strptr(e, symtab->stridx, symp->st_name);
497 if (s != NULL && namep != NULL)
503 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
504 size_t namesz, GElf_Sym *symcopy)
506 struct file_info *file;
507 struct map_info *mapping;
512 if ((mapping = _proc_addr2map(p, addr)) == NULL) {
513 DPRINTFX("ERROR: proc_addr2map failed to resolve 0x%jx", addr);
516 if (open_object(mapping) != 0) {
517 DPRINTFX("ERROR: failed to open object %s",
518 mapping->map.pr_mapname);
522 file = mapping->file;
523 off = file->ehdr.e_type == ET_DYN ?
524 mapping->map.pr_vaddr - mapping->map.pr_offset : 0;
529 error = lookup_symbol_by_addr(file->elf, &file->dynsymtab, addr, &s,
532 error = lookup_symbol_by_addr(file->elf, &file->symtab, addr,
535 symcopy->st_value += off;
536 demangle(s, name, namesz);
541 static struct map_info *
542 _proc_name2map(struct proc_handle *p, const char *name)
544 char path[MAXPATHLEN], *base;
545 struct map_info *mapping;
548 if ((len = strlen(name)) == 0)
550 if (p->nmappings == 0)
551 if (proc_rdagent(p) == NULL)
553 for (i = 0; i < p->nmappings; i++) {
554 mapping = &p->mappings[i];
555 (void)strlcpy(path, mapping->map.pr_mapname, sizeof(path));
556 base = basename(path);
557 if (strcmp(base, name) == 0)
560 /* If we didn't find a match, try matching prefixes of the basename. */
561 for (i = 0; i < p->nmappings; i++) {
562 strlcpy(path, p->mappings[i].map.pr_mapname, sizeof(path));
563 base = basename(path);
564 if (strncmp(base, name, len) == 0)
565 return (&p->mappings[i]);
567 if (strcmp(name, "a.out") == 0)
568 return (_proc_addr2map(p,
569 p->mappings[p->exec_map].map.pr_vaddr));
574 proc_name2map(struct proc_handle *p, const char *name)
577 return (&_proc_name2map(p, name)->map);
581 * Look up the symbol with the given name and return a copy of it.
584 lookup_symbol_by_name(Elf *elf, struct symtab *symtab, const char *symbol,
585 GElf_Sym *symcopy, prsyminfo_t *si)
592 if (symtab->nsyms == 0)
595 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
596 s = elf_strptr(elf, symtab->stridx, sym.st_name);
597 if (s != NULL && strcmp(s, symbol) == 0) {
598 memcpy(symcopy, &sym, sizeof(*symcopy));
608 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
609 GElf_Sym *symcopy, prsyminfo_t *si)
611 struct file_info *file;
612 struct map_info *mapping;
616 if ((mapping = _proc_name2map(p, object)) == NULL) {
617 DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
620 if (open_object(mapping) != 0) {
621 DPRINTFX("ERROR: failed to open object %s",
622 mapping->map.pr_mapname);
626 file = mapping->file;
627 off = file->ehdr.e_type == ET_DYN ?
628 mapping->map.pr_vaddr - mapping->map.pr_offset : 0;
630 error = lookup_symbol_by_name(file->elf, &file->dynsymtab, symbol,
633 error = lookup_symbol_by_name(file->elf, &file->symtab, symbol,
636 symcopy->st_value += off;
641 proc_name2ctf(struct proc_handle *p, const char *name)
648 if ((map = proc_name2map(p, name)) == NULL)
651 ctf = ctf_open(map->pr_mapname, &error);
661 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
662 int mask, proc_sym_f *func, void *cd)
665 struct file_info *file;
666 struct map_info *mapping;
667 struct symtab *symtab;
671 if ((mapping = _proc_name2map(p, object)) == NULL) {
672 DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
675 if (open_object(mapping) != 0) {
676 DPRINTFX("ERROR: failed to open object %s",
677 mapping->map.pr_mapname);
681 file = mapping->file;
682 symtab = which == PR_SYMTAB ? &file->symtab : &file->dynsymtab;
683 if (symtab->nsyms == 0)
687 for (i = 0; gelf_getsym(symtab->data, i, &sym) != NULL; i++) {
688 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
689 (mask & BIND_LOCAL) == 0)
691 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
692 (mask & BIND_GLOBAL) == 0)
694 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
695 (mask & BIND_WEAK) == 0)
697 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
698 (mask & TYPE_NOTYPE) == 0)
700 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
701 (mask & TYPE_OBJECT) == 0)
703 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
704 (mask & TYPE_FUNC) == 0)
706 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
707 (mask & TYPE_SECTION) == 0)
709 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
710 (mask & TYPE_FILE) == 0)
712 s = elf_strptr(file->elf, symtab->stridx, sym.st_name);
713 if (file->ehdr.e_type == ET_DYN)
714 sym.st_value += mapping->map.pr_vaddr;
715 if ((error = (*func)(cd, &sym, s)) != 0)