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
33 #include <sys/types.h>
49 #ifndef NO_CXA_DEMANGLE
50 extern char *__cxa_demangle(const char *, char *, size_t *, int *);
51 #endif /* NO_CXA_DEMANGLE */
53 static void proc_rdl2prmap(rd_loadobj_t *, prmap_t *);
56 demangle(const char *symbol, char *buf, size_t len)
58 #ifndef NO_CXA_DEMANGLE
61 if (symbol[0] == '_' && symbol[1] == 'Z' && symbol[2]) {
62 dembuf = __cxa_demangle(symbol, NULL, NULL, NULL);
65 strlcpy(buf, dembuf, len);
70 #endif /* NO_CXA_DEMANGLE */
71 strlcpy(buf, symbol, len);
75 proc_rdl2prmap(rd_loadobj_t *rdl, prmap_t *map)
77 map->pr_vaddr = rdl->rdl_saddr;
78 map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr;
79 map->pr_offset = rdl->rdl_offset;
81 if (rdl->rdl_prot & RD_RDL_R)
82 map->pr_mflags |= MA_READ;
83 if (rdl->rdl_prot & RD_RDL_W)
84 map->pr_mflags |= MA_WRITE;
85 if (rdl->rdl_prot & RD_RDL_X)
86 map->pr_mflags |= MA_EXEC;
87 strlcpy(map->pr_mapname, rdl->rdl_path,
88 sizeof(map->pr_mapname));
92 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
98 for (i = 0; i < p->nobjs; i++) {
100 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
101 strlcpy(objname, rdl->rdl_path, objnamesz);
109 proc_obj2map(struct proc_handle *p, const char *objname)
114 char path[MAXPATHLEN];
117 for (i = 0; i < p->nobjs; i++) {
118 basename_r(p->rdobjs[i].rdl_path, path);
119 if (strcmp(path, objname) == 0) {
124 if (rdl == NULL && strcmp(objname, "a.out") == 0 && p->rdexec != NULL)
129 if ((map = malloc(sizeof(*map))) == NULL)
131 proc_rdl2prmap(rdl, map);
136 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
141 char path[MAXPATHLEN];
142 char last[MAXPATHLEN];
146 memset(last, 0, sizeof(last));
147 for (i = 0; i < p->nobjs; i++) {
149 proc_rdl2prmap(rdl, &map);
150 basename_r(rdl->rdl_path, path);
152 * We shouldn't call the callback twice with the same object.
153 * To do that we are assuming the fact that if there are
154 * repeated object names (i.e. different mappings for the
155 * same object) they occur next to each other.
157 if (strcmp(path, last) == 0)
159 (*func)(cd, &map, path);
160 strlcpy(last, path, sizeof(last));
167 proc_addr2map(struct proc_handle *p, uintptr_t addr)
173 struct kinfo_vmentry *kves, *kve;
176 * If we don't have a cache of listed objects, we need to query
180 if ((kves = kinfo_getvmmap(p->pid, &cnt)) == NULL)
182 for (i = 0; i < (size_t)cnt; i++) {
184 if (kve->kve_type == KVME_TYPE_VNODE)
186 if (addr >= kve->kve_start && addr < kve->kve_end) {
187 if ((map = malloc(sizeof(*map))) == NULL) {
191 map->pr_vaddr = kve->kve_start;
192 map->pr_size = kve->kve_end - kve->kve_start;
193 map->pr_offset = kve->kve_offset;
195 if (kve->kve_protection & KVME_PROT_READ)
196 map->pr_mflags |= MA_READ;
197 if (kve->kve_protection & KVME_PROT_WRITE)
198 map->pr_mflags |= MA_WRITE;
199 if (kve->kve_protection & KVME_PROT_EXEC)
200 map->pr_mflags |= MA_EXEC;
201 if (kve->kve_flags & KVME_FLAG_COW)
202 map->pr_mflags |= MA_COW;
203 if (kve->kve_flags & KVME_FLAG_NEEDS_COPY)
204 map->pr_mflags |= MA_NEEDS_COPY;
205 if (kve->kve_flags & KVME_FLAG_NOCOREDUMP)
206 map->pr_mflags |= MA_NOCOREDUMP;
207 strlcpy(map->pr_mapname, kves[lastvn].kve_path,
208 sizeof(map->pr_mapname));
217 for (i = 0; i < p->nobjs; i++) {
219 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
220 if ((map = malloc(sizeof(*map))) == NULL)
222 proc_rdl2prmap(rdl, map);
230 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
231 size_t namesz, GElf_Sym *symcopy)
234 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
244 unsigned long symtabstridx = 0, dynsymstridx = 0;
246 if ((map = proc_addr2map(p, addr)) == NULL)
248 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
249 DPRINTF("ERROR: open %s failed", map->pr_mapname);
252 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
253 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
256 if (gelf_getehdr(e, &ehdr) == NULL) {
257 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
261 * Find the index of the STRTAB and SYMTAB sections to locate
265 while ((scn = elf_nextscn(e, scn)) != NULL) {
266 gelf_getshdr(scn, &shdr);
267 switch (shdr.sh_type) {
270 symtabstridx = shdr.sh_link;
274 dynsymstridx = shdr.sh_link;
281 * Iterate over the Dynamic Symbols table to find the symbol.
282 * Then look up the string name in STRTAB (.dynstr)
284 if ((data = elf_getdata(dynsymscn, NULL)) == NULL) {
285 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
289 while (gelf_getsym(data, i++, &sym) != NULL) {
291 * Calculate the address mapped to the virtual memory
294 if (ehdr.e_type != ET_EXEC)
295 rsym = map->pr_vaddr + sym.st_value;
298 if (addr >= rsym && addr < rsym + sym.st_size) {
299 s = elf_strptr(e, dynsymstridx, sym.st_name);
301 demangle(s, name, namesz);
302 memcpy(symcopy, &sym, sizeof(sym));
304 * DTrace expects the st_value to contain
305 * only the address relative to the start of
308 symcopy->st_value = rsym;
316 * Iterate over the Symbols Table to find the symbol.
317 * Then look up the string name in STRTAB (.dynstr)
319 if ((data = elf_getdata(symtabscn, NULL)) == NULL) {
320 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
324 while (gelf_getsym(data, i++, &sym) != NULL) {
326 * Calculate the address mapped to the virtual memory
329 if (ehdr.e_type != ET_EXEC)
330 rsym = map->pr_vaddr + sym.st_value;
333 if (addr >= rsym && addr < rsym + sym.st_size) {
334 s = elf_strptr(e, symtabstridx, sym.st_name);
336 demangle(s, name, namesz);
337 memcpy(symcopy, &sym, sizeof(sym));
339 * DTrace expects the st_value to contain
340 * only the address relative to the start of
343 symcopy->st_value = rsym;
360 proc_name2map(struct proc_handle *p, const char *name)
365 char tmppath[MAXPATHLEN];
366 struct kinfo_vmentry *kves, *kve;
370 * If we haven't iterated over the list of loaded objects,
371 * librtld_db isn't yet initialized and it's very likely
372 * that librtld_db called us. We need to do the heavy
373 * lifting here to find the symbol librtld_db is looking for.
376 if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
378 for (i = 0; i < (size_t)cnt; i++) {
380 basename_r(kve->kve_path, tmppath);
381 if (strcmp(tmppath, name) == 0) {
382 map = proc_addr2map(p, kve->kve_start);
390 if ((name == NULL || strcmp(name, "a.out") == 0) &&
392 map = proc_addr2map(p, p->rdexec->rdl_saddr);
395 for (i = 0; i < p->nobjs; i++) {
397 basename_r(rdl->rdl_path, tmppath);
398 if (strcmp(tmppath, name) == 0) {
399 if ((map = malloc(sizeof(*map))) == NULL)
401 proc_rdl2prmap(rdl, map);
410 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
414 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
423 unsigned long symtabstridx = 0, dynsymstridx = 0;
425 if ((map = proc_name2map(p, object)) == NULL) {
426 DPRINTFX("ERROR: couldn't find object %s", object);
429 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
430 DPRINTF("ERROR: open %s failed", map->pr_mapname);
433 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
434 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
437 if (gelf_getehdr(e, &ehdr) == NULL) {
438 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
442 * Find the index of the STRTAB and SYMTAB sections to locate
446 while ((scn = elf_nextscn(e, scn)) != NULL) {
447 gelf_getshdr(scn, &shdr);
448 switch (shdr.sh_type) {
451 symtabstridx = shdr.sh_link;
455 dynsymstridx = shdr.sh_link;
462 * Iterate over the Dynamic Symbols table to find the symbol.
463 * Then look up the string name in STRTAB (.dynstr)
465 if ((data = elf_getdata(dynsymscn, NULL))) {
467 while (gelf_getsym(data, i++, &sym) != NULL) {
468 s = elf_strptr(e, dynsymstridx, sym.st_name);
469 if (s && strcmp(s, symbol) == 0) {
470 memcpy(symcopy, &sym, sizeof(sym));
471 if (ehdr.e_type != ET_EXEC)
472 symcopy->st_value += map->pr_vaddr;
479 * Iterate over the Symbols Table to find the symbol.
480 * Then look up the string name in STRTAB (.dynstr)
482 if ((data = elf_getdata(symtabscn, NULL))) {
484 while (gelf_getsym(data, i++, &sym) != NULL) {
485 s = elf_strptr(e, symtabstridx, sym.st_name);
486 if (s && strcmp(s, symbol) == 0) {
487 memcpy(symcopy, &sym, sizeof(sym));
488 if (ehdr.e_type != ET_EXEC)
489 symcopy->st_value += map->pr_vaddr;
496 DPRINTFX("found addr 0x%lx for %s", symcopy->st_value, symbol);
509 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
510 int mask, proc_sym_f *func, void *cd)
515 Elf_Scn *scn, *foundscn = NULL;
520 unsigned long stridx = -1;
524 if ((map = proc_name2map(p, object)) == NULL)
526 if ((fd = open(map->pr_mapname, O_RDONLY)) < 0) {
527 DPRINTF("ERROR: open %s failed", map->pr_mapname);
530 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
531 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
534 if (gelf_getehdr(e, &ehdr) == NULL) {
535 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
539 * Find the section we are looking for.
542 while ((scn = elf_nextscn(e, scn)) != NULL) {
543 gelf_getshdr(scn, &shdr);
544 if (which == PR_SYMTAB &&
545 shdr.sh_type == SHT_SYMTAB) {
548 } else if (which == PR_DYNSYM &&
549 shdr.sh_type == SHT_DYNSYM) {
556 stridx = shdr.sh_link;
557 if ((data = elf_getdata(foundscn, NULL)) == NULL) {
558 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
562 while (gelf_getsym(data, i++, &sym) != NULL) {
563 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
564 (mask & BIND_LOCAL) == 0)
566 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
567 (mask & BIND_GLOBAL) == 0)
569 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
570 (mask & BIND_WEAK) == 0)
572 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
573 (mask & TYPE_NOTYPE) == 0)
575 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
576 (mask & TYPE_OBJECT) == 0)
578 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
579 (mask & TYPE_FUNC) == 0)
581 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
582 (mask & TYPE_SECTION) == 0)
584 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
585 (mask & TYPE_FILE) == 0)
587 s = elf_strptr(e, stridx, sym.st_name);
588 if (ehdr.e_type != ET_EXEC)
589 sym.st_value += map->pr_vaddr;
590 (*func)(cd, &sym, s);