2 * SPDX-License-Identifier: BSD-2-Clause
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)
76 *crc = crc32(0L, Z_NULL, 0);
77 while ((nr = read(fd, buf, sizeof(buf))) > 0) {
78 *crc = crc32(*crc, (char *)buf, nr);
84 demangle(const char *symbol, char *buf, size_t len)
86 #ifndef NO_CXA_DEMANGLE
89 if (symbol[0] == '_' && symbol[1] == 'Z' && symbol[2]) {
90 dembuf = __cxa_demangle(symbol, NULL, NULL, NULL);
93 strlcpy(buf, dembuf, len);
98 #endif /* NO_CXA_DEMANGLE */
99 strlcpy(buf, symbol, len);
102 struct symsort_thunk {
104 struct symtab *symtab;
108 symvalcmp(const void *a1, const void *a2, void *_thunk)
111 struct symsort_thunk *thunk;
116 i1 = *(const u_int *)a1;
117 i2 = *(const u_int *)a2;
120 (void)gelf_getsym(thunk->symtab->data, i1, &sym1);
121 (void)gelf_getsym(thunk->symtab->data, i2, &sym2);
123 if (sym1.st_value != sym2.st_value)
124 return (sym1.st_value < sym2.st_value ? -1 : 1);
126 /* Prefer non-local symbols. */
127 bind1 = GELF_ST_BIND(sym1.st_info);
128 bind2 = GELF_ST_BIND(sym2.st_info);
129 if (bind1 != bind2) {
130 if (bind1 == STB_LOCAL && bind2 != STB_LOCAL)
132 if (bind1 != STB_LOCAL && bind2 == STB_LOCAL)
136 s1 = elf_strptr(thunk->e, thunk->symtab->stridx, sym1.st_name);
137 s2 = elf_strptr(thunk->e, thunk->symtab->stridx, sym2.st_name);
138 if (s1 != NULL && s2 != NULL) {
139 /* Prefer symbols without a leading '$'. */
145 /* Prefer symbols with fewer leading underscores. */
146 for (; *s1 == '_' && *s2 == '_'; s1++, s2++)
158 load_symtab(Elf *e, struct symtab *symtab, u_long sh_type)
162 struct symsort_thunk thunk;
166 if (gelf_getehdr(e, &ehdr) == NULL)
170 while ((scn = elf_nextscn(e, scn)) != NULL) {
171 (void)gelf_getshdr(scn, &shdr);
172 if (shdr.sh_type == sh_type)
178 nsyms = shdr.sh_size / shdr.sh_entsize;
179 if (nsyms > (1 << 20))
182 if ((symtab->data = elf_getdata(scn, NULL)) == NULL)
185 symtab->index = calloc(nsyms, sizeof(u_int));
186 if (symtab->index == NULL)
188 for (u_int i = 0; i < nsyms; i++)
189 symtab->index[i] = i;
190 symtab->nsyms = nsyms;
191 symtab->stridx = shdr.sh_link;
194 thunk.symtab = symtab;
195 qsort_r(symtab->index, nsyms, sizeof(u_int), symvalcmp, &thunk);
201 load_symtabs(struct file_info *file)
204 file->symtab.nsyms = file->dynsymtab.nsyms = 0;
205 (void)load_symtab(file->elf, &file->symtab, SHT_SYMTAB);
206 (void)load_symtab(file->elf, &file->dynsymtab, SHT_DYNSYM);
210 open_debug_file(char *path, const char *debugfile, uint32_t crc)
217 if ((n = strlcat(path, "/", PATH_MAX)) >= PATH_MAX)
219 if (strlcat(path, debugfile, PATH_MAX) >= PATH_MAX)
221 if ((fd = open(path, O_RDONLY | O_CLOEXEC)) < 0)
223 if (crc32_file(fd, &compcrc) != 0 || crc != compcrc) {
224 DPRINTFX("ERROR: CRC32 mismatch for %s", path);
234 * Obtain an ELF descriptor for the specified mapped object. If a GNU debuglink
235 * section is present, a descriptor for the corresponding debug file is
239 open_object(struct map_info *mapping)
246 struct file_info *file;
248 const char *debugfile, *scnname;
253 if (mapping->map.pr_mapname[0] == '\0')
254 return (-1); /* anonymous object */
255 if (mapping->file->elf != NULL)
256 return (0); /* already loaded */
258 file = mapping->file;
260 if ((fd = open(map->pr_mapname, O_RDONLY | O_CLOEXEC)) < 0) {
261 DPRINTF("ERROR: open %s failed", map->pr_mapname);
264 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
265 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
268 if (gelf_getehdr(e, &file->ehdr) != &file->ehdr) {
269 DPRINTFX("ERROR: elf_getehdr() failed: %s", elf_errmsg(-1));
274 while ((scn = elf_nextscn(e, scn)) != NULL) {
275 if (gelf_getshdr(scn, &shdr) != &shdr) {
276 DPRINTFX("ERROR: gelf_getshdr failed: %s",
280 if (shdr.sh_type != SHT_PROGBITS)
282 if (elf_getshdrstrndx(e, &ndx) != 0) {
283 DPRINTFX("ERROR: elf_getshdrstrndx failed: %s",
287 if ((scnname = elf_strptr(e, ndx, shdr.sh_name)) == NULL)
290 if (strcmp(scnname, ".gnu_debuglink") == 0)
296 if ((data = elf_getdata(scn, NULL)) == NULL) {
297 DPRINTFX("ERROR: elf_getdata failed: %s", elf_errmsg(-1));
302 * The data contains a null-terminated file name followed by a 4-byte
305 if (data->d_size < sizeof(crc) + 1) {
306 DPRINTFX("ERROR: debuglink section is too small (%zd bytes)",
307 (ssize_t)data->d_size);
310 if (strnlen(data->d_buf, data->d_size) >= data->d_size - sizeof(crc)) {
311 DPRINTFX("ERROR: no null-terminator in gnu_debuglink section");
315 debugfile = data->d_buf;
316 memcpy(&crc, (char *)data->d_buf + data->d_size - sizeof(crc),
320 * Search for the debug file using the algorithm described in the gdb
322 * - look in the directory containing the object,
323 * - look in the subdirectory ".debug" of the directory containing the
325 * - look in the global debug directories (currently /usr/lib/debug).
327 (void)strlcpy(path, map->pr_mapname, sizeof(path));
330 if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
333 if (strlcat(path, "/.debug", sizeof(path)) < sizeof(path) &&
334 (fd2 = open_debug_file(path, debugfile, crc)) >= 0)
337 (void)snprintf(path, sizeof(path), PATH_DEBUG_DIR);
338 if (strlcat(path, map->pr_mapname, sizeof(path)) < sizeof(path)) {
340 if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
345 /* We didn't find a debug file, just return the object's descriptor. */
352 if ((e2 = elf_begin(fd2, ELF_C_READ, NULL)) == NULL) {
353 DPRINTFX("ERROR: elf_begin failed: %s", elf_errmsg(-1));
372 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
378 if (p->nmappings == 0)
379 if (proc_rdagent(p) == NULL)
381 for (i = 0; i < p->nmappings; i++) {
382 map = &p->mappings[i].map;
383 if (addr >= map->pr_vaddr &&
384 addr < map->pr_vaddr + map->pr_size) {
385 strlcpy(objname, map->pr_mapname, objnamesz);
393 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
395 char last[MAXPATHLEN], path[MAXPATHLEN], *base;
400 if (p->nmappings == 0)
401 if (proc_rdagent(p) == NULL)
405 memset(last, 0, sizeof(last));
406 for (i = 0; i < p->nmappings; i++) {
407 map = &p->mappings[i].map;
408 strlcpy(path, map->pr_mapname, sizeof(path));
409 base = basename(path);
411 * We shouldn't call the callback twice with the same object.
412 * To do that we are assuming the fact that if there are
413 * repeated object names (i.e. different mappings for the
414 * same object) they occur next to each other.
416 if (strcmp(base, last) == 0)
418 if ((error = (*func)(cd, map, base)) != 0)
420 strlcpy(last, path, sizeof(last));
425 static struct map_info *
426 _proc_addr2map(struct proc_handle *p, uintptr_t addr)
428 struct map_info *mapping;
431 if (p->nmappings == 0)
432 if (proc_rdagent(p) == NULL)
434 for (i = 0; i < p->nmappings; i++) {
435 mapping = &p->mappings[i];
436 if (addr >= mapping->map.pr_vaddr &&
437 addr < mapping->map.pr_vaddr + mapping->map.pr_size)
444 proc_addr2map(struct proc_handle *p, uintptr_t addr)
447 return (&_proc_addr2map(p, addr)->map);
451 * Look up the symbol at addr using a binary search, returning a copy of the
452 * symbol and its name.
455 lookup_symbol_by_addr(Elf *e, struct symtab *symtab, uintptr_t addr,
456 const char **namep, GElf_Sym *symp)
461 u_int i, min, max, mid;
463 if (symtab->nsyms == 0)
468 max = symtab->nsyms - 1;
471 mid = (max + min) / 2;
472 (void)gelf_getsym(data, symtab->index[mid], &sym);
473 if (addr >= sym.st_value && addr < sym.st_value + sym.st_size)
476 if (addr < sym.st_value)
485 * Advance until we find the matching symbol with largest index.
487 for (i = mid; i < symtab->nsyms; i++) {
488 (void)gelf_getsym(data, symtab->index[i], &sym);
489 if (addr < sym.st_value || addr >= sym.st_value + sym.st_size)
492 (void)gelf_getsym(data, symtab->index[i - 1], symp);
493 s = elf_strptr(e, symtab->stridx, symp->st_name);
494 if (s != NULL && namep != NULL)
500 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
501 size_t namesz, GElf_Sym *symcopy)
503 struct file_info *file;
504 struct map_info *mapping;
509 if ((mapping = _proc_addr2map(p, addr)) == NULL) {
510 DPRINTFX("ERROR: proc_addr2map failed to resolve 0x%jx", (uintmax_t)addr);
513 if (open_object(mapping) != 0) {
514 DPRINTFX("ERROR: failed to open object %s",
515 mapping->map.pr_mapname);
519 file = mapping->file;
520 off = file->ehdr.e_type == ET_DYN ?
521 mapping->map.pr_vaddr - mapping->map.pr_offset : 0;
526 error = lookup_symbol_by_addr(file->elf, &file->dynsymtab, addr, &s,
529 error = lookup_symbol_by_addr(file->elf, &file->symtab, addr,
532 symcopy->st_value += off;
533 demangle(s, name, namesz);
538 static struct map_info *
539 _proc_name2map(struct proc_handle *p, const char *name)
541 char path[MAXPATHLEN], *base;
542 struct map_info *mapping;
545 if ((len = strlen(name)) == 0)
547 if (p->nmappings == 0)
548 if (proc_rdagent(p) == NULL)
550 for (i = 0; i < p->nmappings; i++) {
551 mapping = &p->mappings[i];
552 (void)strlcpy(path, mapping->map.pr_mapname, sizeof(path));
553 base = basename(path);
554 if (strcmp(base, name) == 0)
557 /* If we didn't find a match, try matching prefixes of the basename. */
558 for (i = 0; i < p->nmappings; i++) {
559 mapping = &p->mappings[i];
560 strlcpy(path, mapping->map.pr_mapname, sizeof(path));
561 base = basename(path);
562 if (strncmp(base, name, len) == 0)
565 if (strcmp(name, "a.out") == 0)
566 return (_proc_addr2map(p,
567 p->mappings[p->exec_map].map.pr_vaddr));
572 proc_name2map(struct proc_handle *p, const char *name)
575 return (&_proc_name2map(p, name)->map);
579 * Look up the symbol with the given name and return a copy of it.
582 lookup_symbol_by_name(Elf *elf, struct symtab *symtab, const char *symbol,
583 GElf_Sym *symcopy, prsyminfo_t *si)
590 if (symtab->nsyms == 0)
593 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
594 s = elf_strptr(elf, symtab->stridx, sym.st_name);
595 if (s != NULL && strcmp(s, symbol) == 0) {
596 memcpy(symcopy, &sym, sizeof(*symcopy));
606 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
607 GElf_Sym *symcopy, prsyminfo_t *si)
609 struct file_info *file;
610 struct map_info *mapping;
614 if ((mapping = _proc_name2map(p, object)) == NULL) {
615 DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
618 if (open_object(mapping) != 0) {
619 DPRINTFX("ERROR: failed to open object %s",
620 mapping->map.pr_mapname);
624 file = mapping->file;
625 off = file->ehdr.e_type == ET_DYN ?
626 mapping->map.pr_vaddr - mapping->map.pr_offset : 0;
628 error = lookup_symbol_by_name(file->elf, &file->dynsymtab, symbol,
631 error = lookup_symbol_by_name(file->elf, &file->symtab, symbol,
634 symcopy->st_value += off;
639 proc_name2ctf(struct proc_handle *p, const char *name)
646 if ((map = proc_name2map(p, name)) == NULL)
649 ctf = ctf_open(map->pr_mapname, &error);
659 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
660 int mask, proc_sym_f *func, void *cd)
663 struct file_info *file;
664 struct map_info *mapping;
665 struct symtab *symtab;
669 if ((mapping = _proc_name2map(p, object)) == NULL) {
670 DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
673 if (open_object(mapping) != 0) {
674 DPRINTFX("ERROR: failed to open object %s",
675 mapping->map.pr_mapname);
679 file = mapping->file;
680 symtab = which == PR_SYMTAB ? &file->symtab : &file->dynsymtab;
681 if (symtab->nsyms == 0)
685 for (i = 0; gelf_getsym(symtab->data, i, &sym) != NULL; i++) {
686 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
687 (mask & BIND_LOCAL) == 0)
689 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
690 (mask & BIND_GLOBAL) == 0)
692 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
693 (mask & BIND_WEAK) == 0)
695 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
696 (mask & TYPE_NOTYPE) == 0)
698 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
699 (mask & TYPE_OBJECT) == 0)
701 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
702 (mask & TYPE_FUNC) == 0)
704 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
705 (mask & TYPE_SECTION) == 0)
707 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
708 (mask & TYPE_FILE) == 0)
710 s = elf_strptr(file->elf, symtab->stridx, sym.st_name);
711 if (file->ehdr.e_type == ET_DYN)
712 sym.st_value += mapping->map.pr_vaddr;
713 if ((error = (*func)(cd, &sym, s)) != 0)