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 extern char *__cxa_demangle(const char *, char *, size_t *, int *);
51 static void proc_rdl2prmap(rd_loadobj_t *, prmap_t *);
54 demangle(const char *symbol, char *buf, size_t len)
62 dembuf = __cxa_demangle(symbol, dembuf, &demlen, NULL);
65 strlcpy(buf, dembuf, len);
70 strlcpy(buf, symbol, len);
74 proc_rdl2prmap(rd_loadobj_t *rdl, prmap_t *map)
76 map->pr_vaddr = rdl->rdl_saddr;
77 map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr;
78 map->pr_offset = rdl->rdl_offset;
80 if (rdl->rdl_prot & RD_RDL_R)
81 map->pr_mflags |= MA_READ;
82 if (rdl->rdl_prot & RD_RDL_W)
83 map->pr_mflags |= MA_WRITE;
84 if (rdl->rdl_prot & RD_RDL_X)
85 map->pr_mflags |= MA_EXEC;
86 strlcpy(map->pr_mapname, rdl->rdl_path,
87 sizeof(map->pr_mapname));
91 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
97 for (i = 0; i < p->nobjs; i++) {
99 if (addr >= rdl->rdl_saddr && addr <= rdl->rdl_eaddr) {
100 strlcpy(objname, rdl->rdl_path, objnamesz);
108 proc_obj2map(struct proc_handle *p, const char *objname)
113 char path[MAXPATHLEN];
115 for (i = 0; i < p->nobjs; i++) {
117 basename_r(rdl->rdl_path, path);
118 if (strcmp(path, objname) == 0) {
119 if ((map = malloc(sizeof(*map))) == NULL)
121 proc_rdl2prmap(rdl, map);
129 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
134 char path[MAXPATHLEN];
135 char last[MAXPATHLEN];
139 memset(last, 0, sizeof(last));
140 for (i = 0; i < p->nobjs; i++) {
142 proc_rdl2prmap(rdl, &map);
143 basename_r(rdl->rdl_path, path);
145 * We shouldn't call the callback twice with the same object.
146 * To do that we are assuming the fact that if there are
147 * repeated object names (i.e. different mappings for the
148 * same object) they occur next to each other.
150 if (strcmp(path, last) == 0)
152 (*func)(cd, &map, path);
153 strlcpy(last, path, sizeof(last));
160 proc_addr2map(struct proc_handle *p, uintptr_t addr)
166 struct kinfo_vmentry *kves, *kve;
169 * If we don't have a cache of listed objects, we need to query
173 if ((kves = kinfo_getvmmap(p->pid, &cnt)) == NULL)
175 for (i = 0; i < (size_t)cnt; i++) {
177 if (kve->kve_type == KVME_TYPE_VNODE)
179 if (addr >= kve->kve_start && addr <= kve->kve_end) {
180 if ((map = malloc(sizeof(*map))) == NULL) {
184 map->pr_vaddr = kve->kve_start;
185 map->pr_size = kve->kve_end - kve->kve_start;
186 map->pr_offset = kve->kve_offset;
188 if (kve->kve_protection & KVME_PROT_READ)
189 map->pr_mflags |= MA_READ;
190 if (kve->kve_protection & KVME_PROT_WRITE)
191 map->pr_mflags |= MA_WRITE;
192 if (kve->kve_protection & KVME_PROT_EXEC)
193 map->pr_mflags |= MA_EXEC;
194 if (kve->kve_flags & KVME_FLAG_COW)
195 map->pr_mflags |= MA_COW;
196 if (kve->kve_flags & KVME_FLAG_NEEDS_COPY)
197 map->pr_mflags |= MA_NEEDS_COPY;
198 if (kve->kve_flags & KVME_FLAG_NOCOREDUMP)
199 map->pr_mflags |= MA_NOCOREDUMP;
200 strlcpy(map->pr_mapname, kves[lastvn].kve_path,
201 sizeof(map->pr_mapname));
210 for (i = 0; i < p->nobjs; i++) {
212 if (addr >= rdl->rdl_saddr && addr <= rdl->rdl_eaddr) {
213 if ((map = malloc(sizeof(*map))) == NULL)
215 proc_rdl2prmap(rdl, map);
223 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
224 size_t namesz, GElf_Sym *symcopy)
227 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
237 unsigned long symtabstridx = 0, dynsymstridx = 0;
239 if ((map = proc_addr2map(p, addr)) == NULL)
241 if (!map->pr_mapname || (fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
242 warn("ERROR: open %s failed", map->pr_mapname);
245 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
246 warn("ERROR: elf_begin() failed");
249 if (gelf_getehdr(e, &ehdr) == NULL) {
250 warn("ERROR: gelf_getehdr() failed");
254 * Find the index of the STRTAB and SYMTAB sections to locate
258 while ((scn = elf_nextscn(e, scn)) != NULL) {
259 gelf_getshdr(scn, &shdr);
260 switch (shdr.sh_type) {
263 symtabstridx = shdr.sh_link;
267 dynsymstridx = shdr.sh_link;
274 * Iterate over the Dynamic Symbols table to find the symbol.
275 * Then look up the string name in STRTAB (.dynstr)
277 if ((data = elf_getdata(dynsymscn, NULL)) == NULL) {
278 DPRINTF("ERROR: elf_getdata() failed");
282 while (gelf_getsym(data, i++, &sym) != NULL) {
284 * Calculate the address mapped to the virtual memory
287 rsym = map->pr_vaddr + sym.st_value;
288 if (addr >= rsym && addr <= (rsym + sym.st_size)) {
289 s = elf_strptr(e, dynsymstridx, sym.st_name);
291 if (s[0] == '_' && s[1] == 'Z' && s[2])
292 demangle(s, name, namesz);
294 strlcpy(name, s, namesz);
295 memcpy(symcopy, &sym, sizeof(sym));
297 * DTrace expects the st_value to contain
298 * only the address relative to the start of
301 symcopy->st_value = rsym;
309 * Iterate over the Symbols Table to find the symbol.
310 * Then look up the string name in STRTAB (.dynstr)
312 if (symtabscn == NULL)
314 if ((data = elf_getdata(symtabscn, NULL)) == NULL) {
315 DPRINTF("ERROR: elf_getdata() failed");
319 while (gelf_getsym(data, i++, &sym) != NULL) {
321 * Calculate the address mapped to the virtual memory
324 if (ehdr.e_type != ET_EXEC)
325 rsym = map->pr_vaddr + sym.st_value;
328 if (addr >= rsym && addr <= (rsym + sym.st_size)) {
329 s = elf_strptr(e, symtabstridx, sym.st_name);
331 if (s[0] == '_' && s[1] == 'Z' && s[2])
332 demangle(s, name, namesz);
334 strlcpy(name, s, namesz);
335 memcpy(symcopy, &sym, sizeof(sym));
337 * DTrace expects the st_value to contain
338 * only the address relative to the start of
341 symcopy->st_value = rsym;
358 proc_name2map(struct proc_handle *p, const char *name)
363 char tmppath[MAXPATHLEN];
364 struct kinfo_vmentry *kves, *kve;
368 * If we haven't iterated over the list of loaded objects,
369 * librtld_db isn't yet initialized and it's very likely
370 * that librtld_db called us. We need to do the heavy
371 * lifting here to find the symbol librtld_db is looking for.
374 if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
376 for (i = 0; i < (size_t)cnt; i++) {
378 basename_r(kve->kve_path, tmppath);
379 if (strcmp(tmppath, name) == 0) {
380 map = proc_addr2map(p, kve->kve_start);
388 if (name == NULL || strcmp(name, "a.out") == 0) {
389 map = proc_addr2map(p, p->rdobjs[0].rdl_saddr);
392 for (i = 0; i < p->nobjs; i++) {
394 basename_r(rdl->rdl_path, tmppath);
395 if (strcmp(tmppath, name) == 0) {
396 if ((map = malloc(sizeof(*map))) == NULL)
398 proc_rdl2prmap(rdl, map);
407 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
411 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
420 unsigned long symtabstridx = 0, dynsymstridx = 0;
422 if ((map = proc_name2map(p, object)) == NULL) {
423 DPRINTF("ERROR: couldn't find object %s", object);
426 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
427 DPRINTF("ERROR: open %s failed", map->pr_mapname);
430 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
431 warn("ERROR: elf_begin() failed");
434 if (gelf_getehdr(e, &ehdr) == NULL) {
435 warn("ERROR: gelf_getehdr() failed");
439 * Find the index of the STRTAB and SYMTAB sections to locate
443 while ((scn = elf_nextscn(e, scn)) != NULL) {
444 gelf_getshdr(scn, &shdr);
445 switch (shdr.sh_type) {
448 symtabstridx = shdr.sh_link;
452 dynsymstridx = shdr.sh_link;
459 * Iterate over the Dynamic Symbols table to find the symbol.
460 * Then look up the string name in STRTAB (.dynstr)
462 if ((data = elf_getdata(dynsymscn, NULL))) {
463 DPRINTF("ERROR: elf_getdata() failed");
465 while (gelf_getsym(data, i++, &sym) != NULL) {
466 s = elf_strptr(e, dynsymstridx, sym.st_name);
467 if (s && strcmp(s, symbol) == 0) {
468 memcpy(symcopy, &sym, sizeof(sym));
469 symcopy->st_value = map->pr_vaddr + sym.st_value;
476 * Iterate over the Symbols Table to find the symbol.
477 * Then look up the string name in STRTAB (.dynstr)
479 if (symtabscn == NULL)
481 if ((data = elf_getdata(symtabscn, NULL))) {
483 while (gelf_getsym(data, i++, &sym) != NULL) {
484 s = elf_strptr(e, symtabstridx, sym.st_name);
485 if (s && strcmp(s, symbol) == 0) {
486 memcpy(symcopy, &sym, sizeof(sym));
505 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
506 int mask, proc_sym_f *func, void *cd)
511 Elf_Scn *scn, *foundscn = NULL;
515 unsigned long stridx = -1;
519 if ((map = proc_name2map(p, object)) == NULL)
521 if ((fd = open(map->pr_mapname, O_RDONLY)) < 0) {
522 warn("ERROR: open %s failed", map->pr_mapname);
525 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
526 warn("ERROR: elf_begin() failed");
530 * Find the section we are looking for.
533 while ((scn = elf_nextscn(e, scn)) != NULL) {
534 gelf_getshdr(scn, &shdr);
535 if (which == PR_SYMTAB &&
536 shdr.sh_type == SHT_SYMTAB) {
539 } else if (which == PR_DYNSYM &&
540 shdr.sh_type == SHT_DYNSYM) {
547 stridx = shdr.sh_link;
548 if ((data = elf_getdata(foundscn, NULL)) == NULL) {
549 DPRINTF("ERROR: elf_getdata() failed");
553 while (gelf_getsym(data, i++, &sym) != NULL) {
554 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
555 (mask & BIND_LOCAL) == 0)
557 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
558 (mask & BIND_GLOBAL) == 0)
560 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
561 (mask & BIND_WEAK) == 0)
563 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
564 (mask & TYPE_NOTYPE) == 0)
566 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
567 (mask & TYPE_OBJECT) == 0)
569 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
570 (mask & TYPE_FUNC) == 0)
572 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
573 (mask & TYPE_SECTION) == 0)
575 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
576 (mask & TYPE_FILE) == 0)
578 s = elf_strptr(e, stridx, sym.st_name);
579 sym.st_value += map->pr_vaddr;
580 (*func)(cd, &sym, s);