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 static void proc_rdl2prmap(rd_loadobj_t *, prmap_t *);
52 proc_rdl2prmap(rd_loadobj_t *rdl, prmap_t *map)
54 map->pr_vaddr = rdl->rdl_saddr;
55 map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr;
56 map->pr_offset = rdl->rdl_offset;
58 if (rdl->rdl_prot & RD_RDL_R)
59 map->pr_mflags |= MA_READ;
60 if (rdl->rdl_prot & RD_RDL_W)
61 map->pr_mflags |= MA_WRITE;
62 if (rdl->rdl_prot & RD_RDL_X)
63 map->pr_mflags |= MA_EXEC;
64 strlcpy(map->pr_mapname, rdl->rdl_path,
65 sizeof(map->pr_mapname));
69 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
75 for (i = 0; i < p->nobjs; i++) {
77 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
78 strlcpy(objname, rdl->rdl_path, objnamesz);
86 proc_obj2map(struct proc_handle *p, const char *objname)
91 char path[MAXPATHLEN];
93 for (i = 0; i < p->nobjs; i++) {
95 basename_r(rdl->rdl_path, path);
96 if (strcmp(path, objname) == 0) {
97 if ((map = malloc(sizeof(*map))) == NULL)
99 proc_rdl2prmap(rdl, map);
107 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
112 char path[MAXPATHLEN];
113 char last[MAXPATHLEN];
117 memset(last, 0, sizeof(last));
118 for (i = 0; i < p->nobjs; i++) {
120 proc_rdl2prmap(rdl, &map);
121 basename_r(rdl->rdl_path, path);
123 * We shouldn't call the callback twice with the same object.
124 * To do that we are assuming the fact that if there are
125 * repeated object names (i.e. different mappings for the
126 * same object) they occur next to each other.
128 if (strcmp(path, last) == 0)
130 (*func)(cd, &map, path);
131 strlcpy(last, path, sizeof(last));
138 proc_addr2map(struct proc_handle *p, uintptr_t addr)
144 struct kinfo_vmentry *kves, *kve;
147 * If we don't have a cache of listed objects, we need to query
151 if ((kves = kinfo_getvmmap(p->pid, &cnt)) == NULL)
153 for (i = 0; i < (size_t)cnt; i++) {
155 if (kve->kve_type == KVME_TYPE_VNODE)
157 if (addr >= kve->kve_start && addr < kve->kve_end) {
158 if ((map = malloc(sizeof(*map))) == NULL) {
162 map->pr_vaddr = kve->kve_start;
163 map->pr_size = kve->kve_end - kve->kve_start;
164 map->pr_offset = kve->kve_offset;
166 if (kve->kve_protection & KVME_PROT_READ)
167 map->pr_mflags |= MA_READ;
168 if (kve->kve_protection & KVME_PROT_WRITE)
169 map->pr_mflags |= MA_WRITE;
170 if (kve->kve_protection & KVME_PROT_EXEC)
171 map->pr_mflags |= MA_EXEC;
172 if (kve->kve_flags & KVME_FLAG_COW)
173 map->pr_mflags |= MA_COW;
174 if (kve->kve_flags & KVME_FLAG_NEEDS_COPY)
175 map->pr_mflags |= MA_NEEDS_COPY;
176 if (kve->kve_flags & KVME_FLAG_NOCOREDUMP)
177 map->pr_mflags |= MA_NOCOREDUMP;
178 strlcpy(map->pr_mapname, kves[lastvn].kve_path,
179 sizeof(map->pr_mapname));
188 for (i = 0; i < p->nobjs; i++) {
190 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
191 if ((map = malloc(sizeof(*map))) == NULL)
193 proc_rdl2prmap(rdl, map);
201 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
202 size_t namesz, GElf_Sym *symcopy)
205 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
215 unsigned long symtabstridx = 0, dynsymstridx = 0;
217 if ((map = proc_addr2map(p, addr)) == NULL)
219 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
220 DPRINTF("ERROR: open %s failed", map->pr_mapname);
223 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
224 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
227 if (gelf_getehdr(e, &ehdr) == NULL) {
228 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
232 * Find the index of the STRTAB and SYMTAB sections to locate
236 while ((scn = elf_nextscn(e, scn)) != NULL) {
237 gelf_getshdr(scn, &shdr);
238 switch (shdr.sh_type) {
241 symtabstridx = shdr.sh_link;
245 dynsymstridx = shdr.sh_link;
252 * Iterate over the Dynamic Symbols table to find the symbol.
253 * Then look up the string name in STRTAB (.dynstr)
255 if ((data = elf_getdata(dynsymscn, NULL)) == NULL) {
256 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
260 while (gelf_getsym(data, i++, &sym) != NULL) {
262 * Calculate the address mapped to the virtual memory
265 if (ehdr.e_type != ET_EXEC)
266 rsym = map->pr_vaddr + sym.st_value;
269 if (addr >= rsym && addr < rsym + sym.st_size) {
270 s = elf_strptr(e, dynsymstridx, sym.st_name);
272 strlcpy(name, s, namesz);
273 memcpy(symcopy, &sym, sizeof(sym));
275 * DTrace expects the st_value to contain
276 * only the address relative to the start of
279 symcopy->st_value = rsym;
287 * Iterate over the Symbols Table to find the symbol.
288 * Then look up the string name in STRTAB (.dynstr)
290 if ((data = elf_getdata(symtabscn, 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, symtabstridx, sym.st_name);
307 strlcpy(name, s, 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;
331 proc_name2map(struct proc_handle *p, const char *name)
336 char tmppath[MAXPATHLEN];
337 struct kinfo_vmentry *kves, *kve;
341 * If we haven't iterated over the list of loaded objects,
342 * librtld_db isn't yet initialized and it's very likely
343 * that librtld_db called us. We need to do the heavy
344 * lifting here to find the symbol librtld_db is looking for.
347 if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
349 for (i = 0; i < (size_t)cnt; i++) {
351 basename_r(kve->kve_path, tmppath);
352 if (strcmp(tmppath, name) == 0) {
353 map = proc_addr2map(p, kve->kve_start);
361 if (name == NULL || strcmp(name, "a.out") == 0) {
362 map = proc_addr2map(p, p->rdobjs[0].rdl_saddr);
365 for (i = 0; i < p->nobjs; i++) {
367 basename_r(rdl->rdl_path, tmppath);
368 if (strcmp(tmppath, name) == 0) {
369 if ((map = malloc(sizeof(*map))) == NULL)
371 proc_rdl2prmap(rdl, map);
380 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
384 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
393 unsigned long symtabstridx = 0, dynsymstridx = 0;
395 if ((map = proc_name2map(p, object)) == NULL) {
396 DPRINTFX("ERROR: couldn't find object %s", object);
399 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
400 DPRINTF("ERROR: open %s failed", map->pr_mapname);
403 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
404 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
407 if (gelf_getehdr(e, &ehdr) == NULL) {
408 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
412 * Find the index of the STRTAB and SYMTAB sections to locate
416 while ((scn = elf_nextscn(e, scn)) != NULL) {
417 gelf_getshdr(scn, &shdr);
418 switch (shdr.sh_type) {
421 symtabstridx = shdr.sh_link;
425 dynsymstridx = shdr.sh_link;
432 * Iterate over the Dynamic Symbols table to find the symbol.
433 * Then look up the string name in STRTAB (.dynstr)
435 if ((data = elf_getdata(dynsymscn, NULL))) {
437 while (gelf_getsym(data, i++, &sym) != NULL) {
438 s = elf_strptr(e, dynsymstridx, sym.st_name);
439 if (s && strcmp(s, symbol) == 0) {
440 memcpy(symcopy, &sym, sizeof(sym));
441 if (ehdr.e_type != ET_EXEC)
442 symcopy->st_value += map->pr_vaddr;
449 * Iterate over the Symbols Table to find the symbol.
450 * Then look up the string name in STRTAB (.dynstr)
452 if ((data = elf_getdata(symtabscn, NULL))) {
454 while (gelf_getsym(data, i++, &sym) != NULL) {
455 s = elf_strptr(e, symtabstridx, sym.st_name);
456 if (s && strcmp(s, symbol) == 0) {
457 memcpy(symcopy, &sym, sizeof(sym));
458 if (ehdr.e_type != ET_EXEC)
459 symcopy->st_value += map->pr_vaddr;
466 DPRINTFX("found addr 0x%lx for %s", symcopy->st_value, symbol);
479 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
480 int mask, proc_sym_f *func, void *cd)
485 Elf_Scn *scn, *foundscn = NULL;
490 unsigned long stridx = -1;
494 if ((map = proc_name2map(p, object)) == NULL)
496 if ((fd = open(map->pr_mapname, O_RDONLY)) < 0) {
497 DPRINTF("ERROR: open %s failed", map->pr_mapname);
500 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
501 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
504 if (gelf_getehdr(e, &ehdr) == NULL) {
505 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
509 * Find the section we are looking for.
512 while ((scn = elf_nextscn(e, scn)) != NULL) {
513 gelf_getshdr(scn, &shdr);
514 if (which == PR_SYMTAB &&
515 shdr.sh_type == SHT_SYMTAB) {
518 } else if (which == PR_DYNSYM &&
519 shdr.sh_type == SHT_DYNSYM) {
526 stridx = shdr.sh_link;
527 if ((data = elf_getdata(foundscn, NULL)) == NULL) {
528 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
532 while (gelf_getsym(data, i++, &sym) != NULL) {
533 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
534 (mask & BIND_LOCAL) == 0)
536 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
537 (mask & BIND_GLOBAL) == 0)
539 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
540 (mask & BIND_WEAK) == 0)
542 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
543 (mask & TYPE_NOTYPE) == 0)
545 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
546 (mask & TYPE_OBJECT) == 0)
548 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
549 (mask & TYPE_FUNC) == 0)
551 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
552 (mask & TYPE_SECTION) == 0)
554 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
555 (mask & TYPE_FILE) == 0)
557 s = elf_strptr(e, stridx, sym.st_name);
558 if (ehdr.e_type != ET_EXEC)
559 sym.st_value += map->pr_vaddr;
560 (*func)(cd, &sym, s);