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 proc_rdl2prmap(rd_loadobj_t *rdl, prmap_t *map)
88 map->pr_vaddr = rdl->rdl_saddr;
89 map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr;
90 map->pr_offset = rdl->rdl_offset;
92 if (rdl->rdl_prot & RD_RDL_R)
93 map->pr_mflags |= MA_READ;
94 if (rdl->rdl_prot & RD_RDL_W)
95 map->pr_mflags |= MA_WRITE;
96 if (rdl->rdl_prot & RD_RDL_X)
97 map->pr_mflags |= MA_EXEC;
98 strlcpy(map->pr_mapname, rdl->rdl_path,
99 sizeof(map->pr_mapname));
103 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
109 for (i = 0; i < p->nobjs; i++) {
111 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
112 strlcpy(objname, rdl->rdl_path, objnamesz);
120 proc_obj2map(struct proc_handle *p, const char *objname)
125 char path[MAXPATHLEN];
128 for (i = 0; i < p->nobjs; i++) {
129 basename_r(p->rdobjs[i].rdl_path, path);
130 if (strcmp(path, objname) == 0) {
136 if (strcmp(objname, "a.out") == 0 && p->rdexec != NULL)
142 if ((map = malloc(sizeof(*map))) == NULL)
144 proc_rdl2prmap(rdl, map);
149 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
154 char path[MAXPATHLEN];
155 char last[MAXPATHLEN];
162 memset(last, 0, sizeof(last));
163 for (i = 0; i < p->nobjs; i++) {
165 proc_rdl2prmap(rdl, &map);
166 basename_r(rdl->rdl_path, path);
168 * We shouldn't call the callback twice with the same object.
169 * To do that we are assuming the fact that if there are
170 * repeated object names (i.e. different mappings for the
171 * same object) they occur next to each other.
173 if (strcmp(path, last) == 0)
175 if ((error = (*func)(cd, &map, path)) != 0)
177 strlcpy(last, path, sizeof(last));
183 proc_addr2map(struct proc_handle *p, uintptr_t addr)
189 struct kinfo_vmentry *kves, *kve;
192 * If we don't have a cache of listed objects, we need to query
196 if ((kves = kinfo_getvmmap(p->pid, &cnt)) == NULL)
198 for (i = 0; i < (size_t)cnt; i++) {
200 if (kve->kve_type == KVME_TYPE_VNODE)
202 if (addr >= kve->kve_start && addr < kve->kve_end) {
203 if ((map = malloc(sizeof(*map))) == NULL) {
207 map->pr_vaddr = kve->kve_start;
208 map->pr_size = kve->kve_end - kve->kve_start;
209 map->pr_offset = kve->kve_offset;
211 if (kve->kve_protection & KVME_PROT_READ)
212 map->pr_mflags |= MA_READ;
213 if (kve->kve_protection & KVME_PROT_WRITE)
214 map->pr_mflags |= MA_WRITE;
215 if (kve->kve_protection & KVME_PROT_EXEC)
216 map->pr_mflags |= MA_EXEC;
217 if (kve->kve_flags & KVME_FLAG_COW)
218 map->pr_mflags |= MA_COW;
219 if (kve->kve_flags & KVME_FLAG_NEEDS_COPY)
220 map->pr_mflags |= MA_NEEDS_COPY;
221 if (kve->kve_flags & KVME_FLAG_NOCOREDUMP)
222 map->pr_mflags |= MA_NOCOREDUMP;
223 strlcpy(map->pr_mapname, kves[lastvn].kve_path,
224 sizeof(map->pr_mapname));
233 for (i = 0; i < p->nobjs; i++) {
235 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
236 if ((map = malloc(sizeof(*map))) == NULL)
238 proc_rdl2prmap(rdl, map);
246 * Look up the symbol at addr, returning a copy of the symbol and its name.
249 lookup_addr(Elf *e, Elf_Scn *scn, u_long stridx, uintptr_t off, uintptr_t addr,
250 const char **name, GElf_Sym *symcopy)
258 if ((data = elf_getdata(scn, NULL)) == NULL) {
259 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
262 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
263 rsym = off + sym.st_value;
264 if (addr >= rsym && addr < rsym + sym.st_size) {
265 s = elf_strptr(e, stridx, sym.st_name);
268 memcpy(symcopy, &sym, sizeof(*symcopy));
270 * DTrace expects the st_value to contain
271 * only the address relative to the start of
274 symcopy->st_value = rsym;
283 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
284 size_t namesz, GElf_Sym *symcopy)
289 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
293 u_long symtabstridx = 0, dynsymstridx = 0;
296 if ((map = proc_addr2map(p, addr)) == NULL)
298 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
299 DPRINTF("ERROR: open %s failed", map->pr_mapname);
302 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
303 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
306 if (gelf_getehdr(e, &ehdr) == NULL) {
307 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
312 * Find the index of the STRTAB and SYMTAB sections to locate
316 while ((scn = elf_nextscn(e, scn)) != NULL) {
317 gelf_getshdr(scn, &shdr);
318 switch (shdr.sh_type) {
321 symtabstridx = shdr.sh_link;
325 dynsymstridx = shdr.sh_link;
330 off = ehdr.e_type == ET_EXEC ? 0 : map->pr_vaddr;
333 * First look up the symbol in the dynsymtab, and fall back to the
334 * symtab if the lookup fails.
336 error = lookup_addr(e, dynsymscn, dynsymstridx, off, addr, &s, symcopy);
340 error = lookup_addr(e, symtabscn, symtabstridx, off, addr, &s, symcopy);
345 demangle(s, name, namesz);
356 proc_name2map(struct proc_handle *p, const char *name)
361 char tmppath[MAXPATHLEN];
362 struct kinfo_vmentry *kves, *kve;
366 * If we haven't iterated over the list of loaded objects,
367 * librtld_db isn't yet initialized and it's very likely
368 * that librtld_db called us. We need to do the heavy
369 * lifting here to find the symbol librtld_db is looking for.
372 if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
374 for (i = 0; i < (size_t)cnt; i++) {
376 basename_r(kve->kve_path, tmppath);
377 if (strcmp(tmppath, name) == 0) {
378 map = proc_addr2map(p, kve->kve_start);
384 for (i = 0; i < p->nobjs; i++) {
386 basename_r(rdl->rdl_path, tmppath);
387 if (strcmp(tmppath, name) == 0) {
388 if ((map = malloc(sizeof(*map))) == NULL)
390 proc_rdl2prmap(rdl, map);
395 if (map == NULL && strcmp(name, "a.out") == 0 && p->rdexec != NULL)
396 map = proc_addr2map(p, p->rdexec->rdl_saddr);
402 * Look up the symbol with the given name and return a copy of it.
405 lookup_name(Elf *e, Elf_Scn *scn, u_long stridx, const char *symbol,
406 GElf_Sym *symcopy, prsyminfo_t *si)
413 if ((data = elf_getdata(scn, NULL)) == NULL) {
414 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
417 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
418 s = elf_strptr(e, stridx, sym.st_name);
419 if (s != NULL && strcmp(s, symbol) == 0) {
420 memcpy(symcopy, &sym, sizeof(*symcopy));
430 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
431 GElf_Sym *symcopy, prsyminfo_t *si)
434 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
439 u_long symtabstridx = 0, dynsymstridx = 0;
442 if ((map = proc_name2map(p, object)) == NULL) {
443 DPRINTFX("ERROR: couldn't find object %s", object);
446 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
447 DPRINTF("ERROR: open %s failed", map->pr_mapname);
450 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
451 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
454 if (gelf_getehdr(e, &ehdr) == NULL) {
455 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
459 * Find the index of the STRTAB and SYMTAB sections to locate
463 while ((scn = elf_nextscn(e, scn)) != NULL) {
464 gelf_getshdr(scn, &shdr);
465 switch (shdr.sh_type) {
468 symtabstridx = shdr.sh_link;
472 dynsymstridx = shdr.sh_link;
478 * First look up the symbol in the dynsymtab, and fall back to the
479 * symtab if the lookup fails.
481 error = lookup_name(e, dynsymscn, dynsymstridx, symbol, symcopy, si);
485 error = lookup_name(e, symtabscn, symtabstridx, symbol, symcopy, si);
490 off = ehdr.e_type == ET_EXEC ? 0 : map->pr_vaddr;
491 symcopy->st_value += off;
504 proc_name2ctf(struct proc_handle *p, const char *name)
511 if ((map = proc_name2map(p, name)) == NULL)
514 ctf = ctf_open(map->pr_mapname, &error);
525 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
526 int mask, proc_sym_f *func, void *cd)
531 Elf_Scn *scn, *foundscn = NULL;
536 unsigned long stridx = -1;
540 if ((map = proc_name2map(p, object)) == NULL)
542 if ((fd = open(map->pr_mapname, O_RDONLY)) < 0) {
543 DPRINTF("ERROR: open %s failed", map->pr_mapname);
546 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
547 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
550 if (gelf_getehdr(e, &ehdr) == NULL) {
551 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
555 * Find the section we are looking for.
558 while ((scn = elf_nextscn(e, scn)) != NULL) {
559 gelf_getshdr(scn, &shdr);
560 if (which == PR_SYMTAB &&
561 shdr.sh_type == SHT_SYMTAB) {
564 } else if (which == PR_DYNSYM &&
565 shdr.sh_type == SHT_DYNSYM) {
572 stridx = shdr.sh_link;
573 if ((data = elf_getdata(foundscn, NULL)) == NULL) {
574 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
577 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
578 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
579 (mask & BIND_LOCAL) == 0)
581 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
582 (mask & BIND_GLOBAL) == 0)
584 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
585 (mask & BIND_WEAK) == 0)
587 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
588 (mask & TYPE_NOTYPE) == 0)
590 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
591 (mask & TYPE_OBJECT) == 0)
593 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
594 (mask & TYPE_FUNC) == 0)
596 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
597 (mask & TYPE_SECTION) == 0)
599 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
600 (mask & TYPE_FILE) == 0)
602 s = elf_strptr(e, stridx, sym.st_name);
603 if (ehdr.e_type != ET_EXEC)
604 sym.st_value += map->pr_vaddr;
605 if ((error = (*func)(cd, &sym, s)) != 0)