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) {
125 if (strcmp(objname, "a.out") == 0 && p->rdexec != NULL)
131 if ((map = malloc(sizeof(*map))) == NULL)
133 proc_rdl2prmap(rdl, map);
138 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
143 char path[MAXPATHLEN];
144 char last[MAXPATHLEN];
148 memset(last, 0, sizeof(last));
149 for (i = 0; i < p->nobjs; i++) {
151 proc_rdl2prmap(rdl, &map);
152 basename_r(rdl->rdl_path, path);
154 * We shouldn't call the callback twice with the same object.
155 * To do that we are assuming the fact that if there are
156 * repeated object names (i.e. different mappings for the
157 * same object) they occur next to each other.
159 if (strcmp(path, last) == 0)
161 (*func)(cd, &map, path);
162 strlcpy(last, path, sizeof(last));
169 proc_addr2map(struct proc_handle *p, uintptr_t addr)
175 struct kinfo_vmentry *kves, *kve;
178 * If we don't have a cache of listed objects, we need to query
182 if ((kves = kinfo_getvmmap(p->pid, &cnt)) == NULL)
184 for (i = 0; i < (size_t)cnt; i++) {
186 if (kve->kve_type == KVME_TYPE_VNODE)
188 if (addr >= kve->kve_start && addr < kve->kve_end) {
189 if ((map = malloc(sizeof(*map))) == NULL) {
193 map->pr_vaddr = kve->kve_start;
194 map->pr_size = kve->kve_end - kve->kve_start;
195 map->pr_offset = kve->kve_offset;
197 if (kve->kve_protection & KVME_PROT_READ)
198 map->pr_mflags |= MA_READ;
199 if (kve->kve_protection & KVME_PROT_WRITE)
200 map->pr_mflags |= MA_WRITE;
201 if (kve->kve_protection & KVME_PROT_EXEC)
202 map->pr_mflags |= MA_EXEC;
203 if (kve->kve_flags & KVME_FLAG_COW)
204 map->pr_mflags |= MA_COW;
205 if (kve->kve_flags & KVME_FLAG_NEEDS_COPY)
206 map->pr_mflags |= MA_NEEDS_COPY;
207 if (kve->kve_flags & KVME_FLAG_NOCOREDUMP)
208 map->pr_mflags |= MA_NOCOREDUMP;
209 strlcpy(map->pr_mapname, kves[lastvn].kve_path,
210 sizeof(map->pr_mapname));
219 for (i = 0; i < p->nobjs; i++) {
221 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
222 if ((map = malloc(sizeof(*map))) == NULL)
224 proc_rdl2prmap(rdl, map);
232 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
233 size_t namesz, GElf_Sym *symcopy)
236 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
246 unsigned long symtabstridx = 0, dynsymstridx = 0;
248 if ((map = proc_addr2map(p, addr)) == NULL)
250 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
251 DPRINTF("ERROR: open %s failed", map->pr_mapname);
254 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
255 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
258 if (gelf_getehdr(e, &ehdr) == NULL) {
259 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
263 * Find the index of the STRTAB and SYMTAB sections to locate
267 while ((scn = elf_nextscn(e, scn)) != NULL) {
268 gelf_getshdr(scn, &shdr);
269 switch (shdr.sh_type) {
272 symtabstridx = shdr.sh_link;
276 dynsymstridx = shdr.sh_link;
283 * Iterate over the Dynamic Symbols table to find the symbol.
284 * Then look up the string name in STRTAB (.dynstr)
286 if ((data = elf_getdata(dynsymscn, NULL)) == NULL) {
287 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
291 while (gelf_getsym(data, i++, &sym) != NULL) {
293 * Calculate the address mapped to the virtual memory
296 if (ehdr.e_type != ET_EXEC)
297 rsym = map->pr_vaddr + sym.st_value;
300 if (addr >= rsym && addr < rsym + sym.st_size) {
301 s = elf_strptr(e, dynsymstridx, sym.st_name);
303 demangle(s, name, namesz);
304 memcpy(symcopy, &sym, sizeof(sym));
306 * DTrace expects the st_value to contain
307 * only the address relative to the start of
310 symcopy->st_value = rsym;
318 * Iterate over the Symbols Table to find the symbol.
319 * Then look up the string name in STRTAB (.dynstr)
321 if ((data = elf_getdata(symtabscn, NULL)) == NULL) {
322 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
326 while (gelf_getsym(data, i++, &sym) != NULL) {
328 * Calculate the address mapped to the virtual memory
331 if (ehdr.e_type != ET_EXEC)
332 rsym = map->pr_vaddr + sym.st_value;
335 if (addr >= rsym && addr < rsym + sym.st_size) {
336 s = elf_strptr(e, symtabstridx, sym.st_name);
338 demangle(s, name, namesz);
339 memcpy(symcopy, &sym, sizeof(sym));
341 * DTrace expects the st_value to contain
342 * only the address relative to the start of
345 symcopy->st_value = rsym;
362 proc_name2map(struct proc_handle *p, const char *name)
367 char tmppath[MAXPATHLEN];
368 struct kinfo_vmentry *kves, *kve;
372 * If we haven't iterated over the list of loaded objects,
373 * librtld_db isn't yet initialized and it's very likely
374 * that librtld_db called us. We need to do the heavy
375 * lifting here to find the symbol librtld_db is looking for.
378 if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
380 for (i = 0; i < (size_t)cnt; i++) {
382 basename_r(kve->kve_path, tmppath);
383 if (strcmp(tmppath, name) == 0) {
384 map = proc_addr2map(p, kve->kve_start);
392 if ((name == NULL || strcmp(name, "a.out") == 0) &&
394 map = proc_addr2map(p, p->rdexec->rdl_saddr);
397 for (i = 0; i < p->nobjs; i++) {
399 basename_r(rdl->rdl_path, tmppath);
400 if (strcmp(tmppath, name) == 0) {
401 if ((map = malloc(sizeof(*map))) == NULL)
403 proc_rdl2prmap(rdl, map);
412 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
416 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
425 unsigned long symtabstridx = 0, dynsymstridx = 0;
427 if ((map = proc_name2map(p, object)) == NULL) {
428 DPRINTFX("ERROR: couldn't find object %s", object);
431 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
432 DPRINTF("ERROR: open %s failed", map->pr_mapname);
435 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
436 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
439 if (gelf_getehdr(e, &ehdr) == NULL) {
440 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
444 * Find the index of the STRTAB and SYMTAB sections to locate
448 while ((scn = elf_nextscn(e, scn)) != NULL) {
449 gelf_getshdr(scn, &shdr);
450 switch (shdr.sh_type) {
453 symtabstridx = shdr.sh_link;
457 dynsymstridx = shdr.sh_link;
464 * Iterate over the Dynamic Symbols table to find the symbol.
465 * Then look up the string name in STRTAB (.dynstr)
467 if ((data = elf_getdata(dynsymscn, NULL))) {
469 while (gelf_getsym(data, i++, &sym) != NULL) {
470 s = elf_strptr(e, dynsymstridx, sym.st_name);
471 if (s && strcmp(s, symbol) == 0) {
472 memcpy(symcopy, &sym, sizeof(sym));
473 if (ehdr.e_type != ET_EXEC)
474 symcopy->st_value += map->pr_vaddr;
481 * Iterate over the Symbols Table to find the symbol.
482 * Then look up the string name in STRTAB (.dynstr)
484 if ((data = elf_getdata(symtabscn, NULL))) {
486 while (gelf_getsym(data, i++, &sym) != NULL) {
487 s = elf_strptr(e, symtabstridx, sym.st_name);
488 if (s && strcmp(s, symbol) == 0) {
489 memcpy(symcopy, &sym, sizeof(sym));
490 if (ehdr.e_type != ET_EXEC)
491 symcopy->st_value += map->pr_vaddr;
498 DPRINTFX("found addr 0x%lx for %s", symcopy->st_value, symbol);
511 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
512 int mask, proc_sym_f *func, void *cd)
517 Elf_Scn *scn, *foundscn = NULL;
522 unsigned long stridx = -1;
526 if ((map = proc_name2map(p, object)) == NULL)
528 if ((fd = open(map->pr_mapname, O_RDONLY)) < 0) {
529 DPRINTF("ERROR: open %s failed", map->pr_mapname);
532 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
533 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
536 if (gelf_getehdr(e, &ehdr) == NULL) {
537 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
541 * Find the section we are looking for.
544 while ((scn = elf_nextscn(e, scn)) != NULL) {
545 gelf_getshdr(scn, &shdr);
546 if (which == PR_SYMTAB &&
547 shdr.sh_type == SHT_SYMTAB) {
550 } else if (which == PR_DYNSYM &&
551 shdr.sh_type == SHT_DYNSYM) {
558 stridx = shdr.sh_link;
559 if ((data = elf_getdata(foundscn, NULL)) == NULL) {
560 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
564 while (gelf_getsym(data, i++, &sym) != NULL) {
565 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
566 (mask & BIND_LOCAL) == 0)
568 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
569 (mask & BIND_GLOBAL) == 0)
571 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
572 (mask & BIND_WEAK) == 0)
574 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
575 (mask & TYPE_NOTYPE) == 0)
577 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
578 (mask & TYPE_OBJECT) == 0)
580 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
581 (mask & TYPE_FUNC) == 0)
583 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
584 (mask & TYPE_SECTION) == 0)
586 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
587 (mask & TYPE_FILE) == 0)
589 s = elf_strptr(e, stridx, sym.st_name);
590 if (ehdr.e_type != ET_EXEC)
591 sym.st_value += map->pr_vaddr;
592 (*func)(cd, &sym, s);