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];
159 memset(last, 0, sizeof(last));
160 for (i = 0; i < p->nobjs; i++) {
162 proc_rdl2prmap(rdl, &map);
163 basename_r(rdl->rdl_path, path);
165 * We shouldn't call the callback twice with the same object.
166 * To do that we are assuming the fact that if there are
167 * repeated object names (i.e. different mappings for the
168 * same object) they occur next to each other.
170 if (strcmp(path, last) == 0)
172 (*func)(cd, &map, path);
173 strlcpy(last, path, sizeof(last));
180 proc_addr2map(struct proc_handle *p, uintptr_t addr)
186 struct kinfo_vmentry *kves, *kve;
189 * If we don't have a cache of listed objects, we need to query
193 if ((kves = kinfo_getvmmap(p->pid, &cnt)) == NULL)
195 for (i = 0; i < (size_t)cnt; i++) {
197 if (kve->kve_type == KVME_TYPE_VNODE)
199 if (addr >= kve->kve_start && addr < kve->kve_end) {
200 if ((map = malloc(sizeof(*map))) == NULL) {
204 map->pr_vaddr = kve->kve_start;
205 map->pr_size = kve->kve_end - kve->kve_start;
206 map->pr_offset = kve->kve_offset;
208 if (kve->kve_protection & KVME_PROT_READ)
209 map->pr_mflags |= MA_READ;
210 if (kve->kve_protection & KVME_PROT_WRITE)
211 map->pr_mflags |= MA_WRITE;
212 if (kve->kve_protection & KVME_PROT_EXEC)
213 map->pr_mflags |= MA_EXEC;
214 if (kve->kve_flags & KVME_FLAG_COW)
215 map->pr_mflags |= MA_COW;
216 if (kve->kve_flags & KVME_FLAG_NEEDS_COPY)
217 map->pr_mflags |= MA_NEEDS_COPY;
218 if (kve->kve_flags & KVME_FLAG_NOCOREDUMP)
219 map->pr_mflags |= MA_NOCOREDUMP;
220 strlcpy(map->pr_mapname, kves[lastvn].kve_path,
221 sizeof(map->pr_mapname));
230 for (i = 0; i < p->nobjs; i++) {
232 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
233 if ((map = malloc(sizeof(*map))) == NULL)
235 proc_rdl2prmap(rdl, map);
243 * Look up the symbol at addr, returning a copy of the symbol and its name.
246 lookup_addr(Elf *e, Elf_Scn *scn, u_long stridx, uintptr_t off, uintptr_t addr,
247 const char **name, GElf_Sym *symcopy)
255 if ((data = elf_getdata(scn, NULL)) == NULL) {
256 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
259 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
260 rsym = off + sym.st_value;
261 if (addr >= rsym && addr < rsym + sym.st_size) {
262 s = elf_strptr(e, stridx, sym.st_name);
265 memcpy(symcopy, &sym, sizeof(*symcopy));
267 * DTrace expects the st_value to contain
268 * only the address relative to the start of
271 symcopy->st_value = rsym;
280 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
281 size_t namesz, GElf_Sym *symcopy)
286 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
290 u_long symtabstridx = 0, dynsymstridx = 0;
293 if ((map = proc_addr2map(p, addr)) == NULL)
295 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
296 DPRINTF("ERROR: open %s failed", map->pr_mapname);
299 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
300 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
303 if (gelf_getehdr(e, &ehdr) == NULL) {
304 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
309 * Find the index of the STRTAB and SYMTAB sections to locate
313 while ((scn = elf_nextscn(e, scn)) != NULL) {
314 gelf_getshdr(scn, &shdr);
315 switch (shdr.sh_type) {
318 symtabstridx = shdr.sh_link;
322 dynsymstridx = shdr.sh_link;
327 off = ehdr.e_type == ET_EXEC ? 0 : map->pr_vaddr;
330 * First look up the symbol in the dynsymtab, and fall back to the
331 * symtab if the lookup fails.
333 error = lookup_addr(e, dynsymscn, dynsymstridx, off, addr, &s, symcopy);
337 error = lookup_addr(e, symtabscn, symtabstridx, off, addr, &s, symcopy);
342 demangle(s, name, namesz);
353 proc_name2map(struct proc_handle *p, const char *name)
358 char tmppath[MAXPATHLEN];
359 struct kinfo_vmentry *kves, *kve;
363 * If we haven't iterated over the list of loaded objects,
364 * librtld_db isn't yet initialized and it's very likely
365 * that librtld_db called us. We need to do the heavy
366 * lifting here to find the symbol librtld_db is looking for.
369 if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
371 for (i = 0; i < (size_t)cnt; i++) {
373 basename_r(kve->kve_path, tmppath);
374 if (strcmp(tmppath, name) == 0) {
375 map = proc_addr2map(p, kve->kve_start);
381 for (i = 0; i < p->nobjs; i++) {
383 basename_r(rdl->rdl_path, tmppath);
384 if (strcmp(tmppath, name) == 0) {
385 if ((map = malloc(sizeof(*map))) == NULL)
387 proc_rdl2prmap(rdl, map);
392 if (map == NULL && strcmp(name, "a.out") == 0 && p->rdexec != NULL)
393 map = proc_addr2map(p, p->rdexec->rdl_saddr);
399 * Look up the symbol with the given name and return a copy of it.
402 lookup_name(Elf *e, Elf_Scn *scn, u_long stridx, const char *symbol,
403 GElf_Sym *symcopy, prsyminfo_t *si)
410 if ((data = elf_getdata(scn, NULL)) == NULL) {
411 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
414 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
415 s = elf_strptr(e, stridx, sym.st_name);
416 if (s != NULL && strcmp(s, symbol) == 0) {
417 memcpy(symcopy, &sym, sizeof(*symcopy));
427 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
428 GElf_Sym *symcopy, prsyminfo_t *si)
431 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
436 u_long symtabstridx = 0, dynsymstridx = 0;
439 if ((map = proc_name2map(p, object)) == NULL) {
440 DPRINTFX("ERROR: couldn't find object %s", object);
443 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
444 DPRINTF("ERROR: open %s failed", map->pr_mapname);
447 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
448 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
451 if (gelf_getehdr(e, &ehdr) == NULL) {
452 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
456 * Find the index of the STRTAB and SYMTAB sections to locate
460 while ((scn = elf_nextscn(e, scn)) != NULL) {
461 gelf_getshdr(scn, &shdr);
462 switch (shdr.sh_type) {
465 symtabstridx = shdr.sh_link;
469 dynsymstridx = shdr.sh_link;
475 * First look up the symbol in the dynsymtab, and fall back to the
476 * symtab if the lookup fails.
478 error = lookup_name(e, dynsymscn, dynsymstridx, symbol, symcopy, si);
482 error = lookup_name(e, symtabscn, symtabstridx, symbol, symcopy, si);
487 off = ehdr.e_type == ET_EXEC ? 0 : map->pr_vaddr;
488 symcopy->st_value += off;
501 proc_name2ctf(struct proc_handle *p, const char *name)
507 if ((map = proc_name2map(p, name)) == NULL)
510 return (ctf_open(map->pr_mapname, &error));
519 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
520 int mask, proc_sym_f *func, void *cd)
525 Elf_Scn *scn, *foundscn = NULL;
530 unsigned long stridx = -1;
534 if ((map = proc_name2map(p, object)) == NULL)
536 if ((fd = open(map->pr_mapname, O_RDONLY)) < 0) {
537 DPRINTF("ERROR: open %s failed", map->pr_mapname);
540 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
541 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
544 if (gelf_getehdr(e, &ehdr) == NULL) {
545 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
549 * Find the section we are looking for.
552 while ((scn = elf_nextscn(e, scn)) != NULL) {
553 gelf_getshdr(scn, &shdr);
554 if (which == PR_SYMTAB &&
555 shdr.sh_type == SHT_SYMTAB) {
558 } else if (which == PR_DYNSYM &&
559 shdr.sh_type == SHT_DYNSYM) {
566 stridx = shdr.sh_link;
567 if ((data = elf_getdata(foundscn, NULL)) == NULL) {
568 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
571 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
572 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
573 (mask & BIND_LOCAL) == 0)
575 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
576 (mask & BIND_GLOBAL) == 0)
578 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
579 (mask & BIND_WEAK) == 0)
581 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
582 (mask & TYPE_NOTYPE) == 0)
584 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
585 (mask & TYPE_OBJECT) == 0)
587 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
588 (mask & TYPE_FUNC) == 0)
590 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
591 (mask & TYPE_SECTION) == 0)
593 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
594 (mask & TYPE_FILE) == 0)
596 s = elf_strptr(e, stridx, sym.st_name);
597 if (ehdr.e_type != ET_EXEC)
598 sym.st_value += map->pr_vaddr;
599 (*func)(cd, &sym, s);