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
62 if (symbol[0] == '_' && symbol[1] == 'Z' && symbol[2]) {
67 dembuf = __cxa_demangle(symbol, dembuf, &demlen, NULL);
70 strlcpy(buf, dembuf, len);
76 #endif /* NO_CXA_DEMANGLE */
77 strlcpy(buf, symbol, len);
81 proc_rdl2prmap(rd_loadobj_t *rdl, prmap_t *map)
83 map->pr_vaddr = rdl->rdl_saddr;
84 map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr;
85 map->pr_offset = rdl->rdl_offset;
87 if (rdl->rdl_prot & RD_RDL_R)
88 map->pr_mflags |= MA_READ;
89 if (rdl->rdl_prot & RD_RDL_W)
90 map->pr_mflags |= MA_WRITE;
91 if (rdl->rdl_prot & RD_RDL_X)
92 map->pr_mflags |= MA_EXEC;
93 strlcpy(map->pr_mapname, rdl->rdl_path,
94 sizeof(map->pr_mapname));
98 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
104 for (i = 0; i < p->nobjs; i++) {
106 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
107 strlcpy(objname, rdl->rdl_path, objnamesz);
115 proc_obj2map(struct proc_handle *p, const char *objname)
120 char path[MAXPATHLEN];
123 for (i = 0; i < p->nobjs; i++) {
124 basename_r(p->rdobjs[i].rdl_path, path);
125 if (strcmp(path, objname) == 0) {
130 if (rdl == NULL && strcmp(objname, "a.out") == 0 && p->rdexec != NULL)
135 if ((map = malloc(sizeof(*map))) == NULL)
137 proc_rdl2prmap(rdl, map);
142 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
147 char path[MAXPATHLEN];
148 char last[MAXPATHLEN];
152 memset(last, 0, sizeof(last));
153 for (i = 0; i < p->nobjs; i++) {
155 proc_rdl2prmap(rdl, &map);
156 basename_r(rdl->rdl_path, path);
158 * We shouldn't call the callback twice with the same object.
159 * To do that we are assuming the fact that if there are
160 * repeated object names (i.e. different mappings for the
161 * same object) they occur next to each other.
163 if (strcmp(path, last) == 0)
165 (*func)(cd, &map, path);
166 strlcpy(last, path, sizeof(last));
173 proc_addr2map(struct proc_handle *p, uintptr_t addr)
179 struct kinfo_vmentry *kves, *kve;
182 * If we don't have a cache of listed objects, we need to query
186 if ((kves = kinfo_getvmmap(p->pid, &cnt)) == NULL)
188 for (i = 0; i < (size_t)cnt; i++) {
190 if (kve->kve_type == KVME_TYPE_VNODE)
192 if (addr >= kve->kve_start && addr < kve->kve_end) {
193 if ((map = malloc(sizeof(*map))) == NULL) {
197 map->pr_vaddr = kve->kve_start;
198 map->pr_size = kve->kve_end - kve->kve_start;
199 map->pr_offset = kve->kve_offset;
201 if (kve->kve_protection & KVME_PROT_READ)
202 map->pr_mflags |= MA_READ;
203 if (kve->kve_protection & KVME_PROT_WRITE)
204 map->pr_mflags |= MA_WRITE;
205 if (kve->kve_protection & KVME_PROT_EXEC)
206 map->pr_mflags |= MA_EXEC;
207 if (kve->kve_flags & KVME_FLAG_COW)
208 map->pr_mflags |= MA_COW;
209 if (kve->kve_flags & KVME_FLAG_NEEDS_COPY)
210 map->pr_mflags |= MA_NEEDS_COPY;
211 if (kve->kve_flags & KVME_FLAG_NOCOREDUMP)
212 map->pr_mflags |= MA_NOCOREDUMP;
213 strlcpy(map->pr_mapname, kves[lastvn].kve_path,
214 sizeof(map->pr_mapname));
223 for (i = 0; i < p->nobjs; i++) {
225 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
226 if ((map = malloc(sizeof(*map))) == NULL)
228 proc_rdl2prmap(rdl, map);
236 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
237 size_t namesz, GElf_Sym *symcopy)
240 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
250 unsigned long symtabstridx = 0, dynsymstridx = 0;
252 if ((map = proc_addr2map(p, addr)) == NULL)
254 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
255 DPRINTF("ERROR: open %s failed", map->pr_mapname);
258 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
259 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
262 if (gelf_getehdr(e, &ehdr) == NULL) {
263 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
267 * Find the index of the STRTAB and SYMTAB sections to locate
271 while ((scn = elf_nextscn(e, scn)) != NULL) {
272 gelf_getshdr(scn, &shdr);
273 switch (shdr.sh_type) {
276 symtabstridx = shdr.sh_link;
280 dynsymstridx = shdr.sh_link;
287 * Iterate over the Dynamic Symbols table to find the symbol.
288 * Then look up the string name in STRTAB (.dynstr)
290 if ((data = elf_getdata(dynsymscn, NULL)) == NULL) {
291 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
295 while (gelf_getsym(data, i++, &sym) != NULL) {
297 * Calculate the address mapped to the virtual memory
300 if (ehdr.e_type != ET_EXEC)
301 rsym = map->pr_vaddr + sym.st_value;
304 if (addr >= rsym && addr < rsym + sym.st_size) {
305 s = elf_strptr(e, dynsymstridx, sym.st_name);
307 demangle(s, name, namesz);
308 memcpy(symcopy, &sym, sizeof(sym));
310 * DTrace expects the st_value to contain
311 * only the address relative to the start of
314 symcopy->st_value = rsym;
322 * Iterate over the Symbols Table to find the symbol.
323 * Then look up the string name in STRTAB (.dynstr)
325 if ((data = elf_getdata(symtabscn, NULL)) == NULL) {
326 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
330 while (gelf_getsym(data, i++, &sym) != NULL) {
332 * Calculate the address mapped to the virtual memory
335 if (ehdr.e_type != ET_EXEC)
336 rsym = map->pr_vaddr + sym.st_value;
339 if (addr >= rsym && addr < rsym + sym.st_size) {
340 s = elf_strptr(e, symtabstridx, sym.st_name);
342 demangle(s, name, namesz);
343 memcpy(symcopy, &sym, sizeof(sym));
345 * DTrace expects the st_value to contain
346 * only the address relative to the start of
349 symcopy->st_value = rsym;
366 proc_name2map(struct proc_handle *p, const char *name)
371 char tmppath[MAXPATHLEN];
372 struct kinfo_vmentry *kves, *kve;
376 * If we haven't iterated over the list of loaded objects,
377 * librtld_db isn't yet initialized and it's very likely
378 * that librtld_db called us. We need to do the heavy
379 * lifting here to find the symbol librtld_db is looking for.
382 if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
384 for (i = 0; i < (size_t)cnt; i++) {
386 basename_r(kve->kve_path, tmppath);
387 if (strcmp(tmppath, name) == 0) {
388 map = proc_addr2map(p, kve->kve_start);
396 if ((name == NULL || strcmp(name, "a.out") == 0) &&
398 map = proc_addr2map(p, p->rdexec->rdl_saddr);
401 for (i = 0; i < p->nobjs; i++) {
403 basename_r(rdl->rdl_path, tmppath);
404 if (strcmp(tmppath, name) == 0) {
405 if ((map = malloc(sizeof(*map))) == NULL)
407 proc_rdl2prmap(rdl, map);
416 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
420 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
429 unsigned long symtabstridx = 0, dynsymstridx = 0;
431 if ((map = proc_name2map(p, object)) == NULL) {
432 DPRINTFX("ERROR: couldn't find object %s", object);
435 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
436 DPRINTF("ERROR: open %s failed", map->pr_mapname);
439 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
440 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
443 if (gelf_getehdr(e, &ehdr) == NULL) {
444 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
448 * Find the index of the STRTAB and SYMTAB sections to locate
452 while ((scn = elf_nextscn(e, scn)) != NULL) {
453 gelf_getshdr(scn, &shdr);
454 switch (shdr.sh_type) {
457 symtabstridx = shdr.sh_link;
461 dynsymstridx = shdr.sh_link;
468 * Iterate over the Dynamic Symbols table to find the symbol.
469 * Then look up the string name in STRTAB (.dynstr)
471 if ((data = elf_getdata(dynsymscn, NULL))) {
473 while (gelf_getsym(data, i++, &sym) != NULL) {
474 s = elf_strptr(e, dynsymstridx, sym.st_name);
475 if (s && strcmp(s, symbol) == 0) {
476 memcpy(symcopy, &sym, sizeof(sym));
477 if (ehdr.e_type != ET_EXEC)
478 symcopy->st_value += map->pr_vaddr;
485 * Iterate over the Symbols Table to find the symbol.
486 * Then look up the string name in STRTAB (.dynstr)
488 if ((data = elf_getdata(symtabscn, NULL))) {
490 while (gelf_getsym(data, i++, &sym) != NULL) {
491 s = elf_strptr(e, symtabstridx, sym.st_name);
492 if (s && strcmp(s, symbol) == 0) {
493 memcpy(symcopy, &sym, sizeof(sym));
494 if (ehdr.e_type != ET_EXEC)
495 symcopy->st_value += map->pr_vaddr;
502 DPRINTFX("found addr 0x%lx for %s", symcopy->st_value, symbol);
515 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
516 int mask, proc_sym_f *func, void *cd)
521 Elf_Scn *scn, *foundscn = NULL;
526 unsigned long stridx = -1;
530 if ((map = proc_name2map(p, object)) == NULL)
532 if ((fd = open(map->pr_mapname, O_RDONLY)) < 0) {
533 DPRINTF("ERROR: open %s failed", map->pr_mapname);
536 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
537 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
540 if (gelf_getehdr(e, &ehdr) == NULL) {
541 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
545 * Find the section we are looking for.
548 while ((scn = elf_nextscn(e, scn)) != NULL) {
549 gelf_getshdr(scn, &shdr);
550 if (which == PR_SYMTAB &&
551 shdr.sh_type == SHT_SYMTAB) {
554 } else if (which == PR_DYNSYM &&
555 shdr.sh_type == SHT_DYNSYM) {
562 stridx = shdr.sh_link;
563 if ((data = elf_getdata(foundscn, NULL)) == NULL) {
564 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
568 while (gelf_getsym(data, i++, &sym) != NULL) {
569 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
570 (mask & BIND_LOCAL) == 0)
572 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
573 (mask & BIND_GLOBAL) == 0)
575 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
576 (mask & BIND_WEAK) == 0)
578 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
579 (mask & TYPE_NOTYPE) == 0)
581 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
582 (mask & TYPE_OBJECT) == 0)
584 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
585 (mask & TYPE_FUNC) == 0)
587 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
588 (mask & TYPE_SECTION) == 0)
590 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
591 (mask & TYPE_FILE) == 0)
593 s = elf_strptr(e, stridx, sym.st_name);
594 if (ehdr.e_type != ET_EXEC)
595 sym.st_value += map->pr_vaddr;
596 (*func)(cd, &sym, s);