2 * Copyright (c) 2010 The FreeBSD Foundation
3 * Copyright (c) 2008 John Birrell (jb@freebsd.org)
6 * Portions of this software were developed by Rui Paulo under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
37 #include <sys/ctf_api.h>
57 typedef struct ctf_file ctf_file_t;
60 #ifndef NO_CXA_DEMANGLE
61 extern char *__cxa_demangle(const char *, char *, size_t *, int *);
62 #endif /* NO_CXA_DEMANGLE */
64 static void proc_rdl2prmap(rd_loadobj_t *, prmap_t *);
67 demangle(const char *symbol, char *buf, size_t len)
69 #ifndef NO_CXA_DEMANGLE
72 if (symbol[0] == '_' && symbol[1] == 'Z' && symbol[2]) {
73 dembuf = __cxa_demangle(symbol, NULL, NULL, NULL);
76 strlcpy(buf, dembuf, len);
81 #endif /* NO_CXA_DEMANGLE */
82 strlcpy(buf, symbol, len);
86 find_dbg_obj(const char *path)
89 char dbg_path[PATH_MAX];
91 snprintf(dbg_path, sizeof(dbg_path),
92 "/usr/lib/debug/%s.debug", path);
93 fd = open(dbg_path, O_RDONLY);
97 return (open(path, O_RDONLY));
101 proc_rdl2prmap(rd_loadobj_t *rdl, prmap_t *map)
103 map->pr_vaddr = rdl->rdl_saddr;
104 map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr;
105 map->pr_offset = rdl->rdl_offset;
107 if (rdl->rdl_prot & RD_RDL_R)
108 map->pr_mflags |= MA_READ;
109 if (rdl->rdl_prot & RD_RDL_W)
110 map->pr_mflags |= MA_WRITE;
111 if (rdl->rdl_prot & RD_RDL_X)
112 map->pr_mflags |= MA_EXEC;
113 strlcpy(map->pr_mapname, rdl->rdl_path,
114 sizeof(map->pr_mapname));
118 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
124 for (i = 0; i < p->nobjs; i++) {
126 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
127 strlcpy(objname, rdl->rdl_path, objnamesz);
135 proc_obj2map(struct proc_handle *p, const char *objname)
140 char path[MAXPATHLEN];
143 for (i = 0; i < p->nobjs; i++) {
144 basename_r(p->rdobjs[i].rdl_path, path);
145 if (strcmp(path, objname) == 0) {
151 if (strcmp(objname, "a.out") == 0 && p->rdexec != NULL)
157 if ((map = malloc(sizeof(*map))) == NULL)
159 proc_rdl2prmap(rdl, map);
164 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
169 char path[MAXPATHLEN];
170 char last[MAXPATHLEN];
177 memset(last, 0, sizeof(last));
178 for (i = 0; i < p->nobjs; i++) {
180 proc_rdl2prmap(rdl, &map);
181 basename_r(rdl->rdl_path, path);
183 * We shouldn't call the callback twice with the same object.
184 * To do that we are assuming the fact that if there are
185 * repeated object names (i.e. different mappings for the
186 * same object) they occur next to each other.
188 if (strcmp(path, last) == 0)
190 if ((error = (*func)(cd, &map, path)) != 0)
192 strlcpy(last, path, sizeof(last));
198 proc_addr2map(struct proc_handle *p, uintptr_t addr)
204 struct kinfo_vmentry *kves, *kve;
207 * If we don't have a cache of listed objects, we need to query
211 if ((kves = kinfo_getvmmap(p->pid, &cnt)) == NULL)
213 for (i = 0; i < (size_t)cnt; i++) {
215 if (kve->kve_type == KVME_TYPE_VNODE)
217 if (addr >= kve->kve_start && addr < kve->kve_end) {
218 if ((map = malloc(sizeof(*map))) == NULL) {
222 map->pr_vaddr = kve->kve_start;
223 map->pr_size = kve->kve_end - kve->kve_start;
224 map->pr_offset = kve->kve_offset;
226 if (kve->kve_protection & KVME_PROT_READ)
227 map->pr_mflags |= MA_READ;
228 if (kve->kve_protection & KVME_PROT_WRITE)
229 map->pr_mflags |= MA_WRITE;
230 if (kve->kve_protection & KVME_PROT_EXEC)
231 map->pr_mflags |= MA_EXEC;
232 if (kve->kve_flags & KVME_FLAG_COW)
233 map->pr_mflags |= MA_COW;
234 if (kve->kve_flags & KVME_FLAG_NEEDS_COPY)
235 map->pr_mflags |= MA_NEEDS_COPY;
236 if (kve->kve_flags & KVME_FLAG_NOCOREDUMP)
237 map->pr_mflags |= MA_NOCOREDUMP;
238 strlcpy(map->pr_mapname, kves[lastvn].kve_path,
239 sizeof(map->pr_mapname));
248 for (i = 0; i < p->nobjs; i++) {
250 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
251 if ((map = malloc(sizeof(*map))) == NULL)
253 proc_rdl2prmap(rdl, map);
261 * Look up the symbol at addr, returning a copy of the symbol and its name.
264 lookup_addr(Elf *e, Elf_Scn *scn, u_long stridx, uintptr_t off, uintptr_t addr,
265 const char **name, GElf_Sym *symcopy)
273 if ((data = elf_getdata(scn, NULL)) == NULL) {
274 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
277 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
278 rsym = off + sym.st_value;
279 if (addr >= rsym && addr < rsym + sym.st_size) {
280 s = elf_strptr(e, stridx, sym.st_name);
283 memcpy(symcopy, &sym, sizeof(*symcopy));
285 * DTrace expects the st_value to contain
286 * only the address relative to the start of
289 symcopy->st_value = rsym;
298 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
299 size_t namesz, GElf_Sym *symcopy)
304 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
308 u_long symtabstridx = 0, dynsymstridx = 0;
311 if ((map = proc_addr2map(p, addr)) == NULL)
313 if ((fd = find_dbg_obj(map->pr_mapname)) < 0) {
314 DPRINTF("ERROR: open %s failed", map->pr_mapname);
317 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
318 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
321 if (gelf_getehdr(e, &ehdr) == NULL) {
322 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
327 * Find the index of the STRTAB and SYMTAB sections to locate
331 while ((scn = elf_nextscn(e, scn)) != NULL) {
332 gelf_getshdr(scn, &shdr);
333 switch (shdr.sh_type) {
336 symtabstridx = shdr.sh_link;
340 dynsymstridx = shdr.sh_link;
345 off = ehdr.e_type == ET_EXEC ? 0 : map->pr_vaddr;
348 * First look up the symbol in the dynsymtab, and fall back to the
349 * symtab if the lookup fails.
351 error = lookup_addr(e, dynsymscn, dynsymstridx, off, addr, &s, symcopy);
355 error = lookup_addr(e, symtabscn, symtabstridx, off, addr, &s, symcopy);
360 demangle(s, name, namesz);
371 proc_name2map(struct proc_handle *p, const char *name)
376 char tmppath[MAXPATHLEN];
377 struct kinfo_vmentry *kves, *kve;
381 * If we haven't iterated over the list of loaded objects,
382 * librtld_db isn't yet initialized and it's very likely
383 * that librtld_db called us. We need to do the heavy
384 * lifting here to find the symbol librtld_db is looking for.
387 if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
389 for (i = 0; i < (size_t)cnt; i++) {
391 basename_r(kve->kve_path, tmppath);
392 if (strcmp(tmppath, name) == 0) {
393 map = proc_addr2map(p, kve->kve_start);
399 for (i = 0; i < p->nobjs; i++) {
401 basename_r(rdl->rdl_path, tmppath);
402 if (strcmp(tmppath, name) == 0) {
403 if ((map = malloc(sizeof(*map))) == NULL)
405 proc_rdl2prmap(rdl, map);
410 if (map == NULL && strcmp(name, "a.out") == 0 && p->rdexec != NULL)
411 map = proc_addr2map(p, p->rdexec->rdl_saddr);
417 * Look up the symbol with the given name and return a copy of it.
420 lookup_name(Elf *e, Elf_Scn *scn, u_long stridx, const char *symbol,
421 GElf_Sym *symcopy, prsyminfo_t *si)
428 if ((data = elf_getdata(scn, NULL)) == NULL) {
429 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
432 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
433 s = elf_strptr(e, stridx, sym.st_name);
434 if (s != NULL && strcmp(s, symbol) == 0) {
435 memcpy(symcopy, &sym, sizeof(*symcopy));
445 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
446 GElf_Sym *symcopy, prsyminfo_t *si)
449 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
454 u_long symtabstridx = 0, dynsymstridx = 0;
457 if ((map = proc_name2map(p, object)) == NULL) {
458 DPRINTFX("ERROR: couldn't find object %s", object);
461 if ((fd = find_dbg_obj(map->pr_mapname)) < 0) {
462 DPRINTF("ERROR: open %s failed", map->pr_mapname);
465 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
466 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
469 if (gelf_getehdr(e, &ehdr) == NULL) {
470 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
474 * Find the index of the STRTAB and SYMTAB sections to locate
478 while ((scn = elf_nextscn(e, scn)) != NULL) {
479 gelf_getshdr(scn, &shdr);
480 switch (shdr.sh_type) {
483 symtabstridx = shdr.sh_link;
487 dynsymstridx = shdr.sh_link;
493 * First look up the symbol in the dynsymtab, and fall back to the
494 * symtab if the lookup fails.
496 error = lookup_name(e, dynsymscn, dynsymstridx, symbol, symcopy, si);
500 error = lookup_name(e, symtabscn, symtabstridx, symbol, symcopy, si);
505 off = ehdr.e_type == ET_EXEC ? 0 : map->pr_vaddr;
506 symcopy->st_value += off;
519 proc_name2ctf(struct proc_handle *p, const char *name)
526 if ((map = proc_name2map(p, name)) == NULL)
529 ctf = ctf_open(map->pr_mapname, &error);
540 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
541 int mask, proc_sym_f *func, void *cd)
546 Elf_Scn *scn, *foundscn = NULL;
551 unsigned long stridx = -1;
555 if ((map = proc_name2map(p, object)) == NULL)
557 if ((fd = find_dbg_obj(map->pr_mapname)) < 0) {
558 DPRINTF("ERROR: open %s failed", map->pr_mapname);
561 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
562 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
565 if (gelf_getehdr(e, &ehdr) == NULL) {
566 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
570 * Find the section we are looking for.
573 while ((scn = elf_nextscn(e, scn)) != NULL) {
574 gelf_getshdr(scn, &shdr);
575 if (which == PR_SYMTAB &&
576 shdr.sh_type == SHT_SYMTAB) {
579 } else if (which == PR_DYNSYM &&
580 shdr.sh_type == SHT_DYNSYM) {
587 stridx = shdr.sh_link;
588 if ((data = elf_getdata(foundscn, NULL)) == NULL) {
589 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
592 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
593 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
594 (mask & BIND_LOCAL) == 0)
596 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
597 (mask & BIND_GLOBAL) == 0)
599 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
600 (mask & BIND_WEAK) == 0)
602 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
603 (mask & TYPE_NOTYPE) == 0)
605 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
606 (mask & TYPE_OBJECT) == 0)
608 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
609 (mask & TYPE_FUNC) == 0)
611 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
612 (mask & TYPE_SECTION) == 0)
614 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
615 (mask & TYPE_FILE) == 0)
617 s = elf_strptr(e, stridx, sym.st_name);
618 if (ehdr.e_type != ET_EXEC)
619 sym.st_value += map->pr_vaddr;
620 if ((error = (*func)(cd, &sym, s)) != 0)