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];
94 for (i = 0; i < p->nobjs; i++) {
95 basename_r(p->rdobjs[i].rdl_path, path);
96 if (strcmp(path, objname) == 0) {
102 if (strcmp(objname, "a.out") == 0 && p->rdexec != NULL)
108 if ((map = malloc(sizeof(*map))) == NULL)
110 proc_rdl2prmap(rdl, map);
115 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
120 char path[MAXPATHLEN];
121 char last[MAXPATHLEN];
125 memset(last, 0, sizeof(last));
126 for (i = 0; i < p->nobjs; i++) {
128 proc_rdl2prmap(rdl, &map);
129 basename_r(rdl->rdl_path, path);
131 * We shouldn't call the callback twice with the same object.
132 * To do that we are assuming the fact that if there are
133 * repeated object names (i.e. different mappings for the
134 * same object) they occur next to each other.
136 if (strcmp(path, last) == 0)
138 (*func)(cd, &map, path);
139 strlcpy(last, path, sizeof(last));
146 proc_addr2map(struct proc_handle *p, uintptr_t addr)
152 struct kinfo_vmentry *kves, *kve;
155 * If we don't have a cache of listed objects, we need to query
159 if ((kves = kinfo_getvmmap(p->pid, &cnt)) == NULL)
161 for (i = 0; i < (size_t)cnt; i++) {
163 if (kve->kve_type == KVME_TYPE_VNODE)
165 if (addr >= kve->kve_start && addr < kve->kve_end) {
166 if ((map = malloc(sizeof(*map))) == NULL) {
170 map->pr_vaddr = kve->kve_start;
171 map->pr_size = kve->kve_end - kve->kve_start;
172 map->pr_offset = kve->kve_offset;
174 if (kve->kve_protection & KVME_PROT_READ)
175 map->pr_mflags |= MA_READ;
176 if (kve->kve_protection & KVME_PROT_WRITE)
177 map->pr_mflags |= MA_WRITE;
178 if (kve->kve_protection & KVME_PROT_EXEC)
179 map->pr_mflags |= MA_EXEC;
180 if (kve->kve_flags & KVME_FLAG_COW)
181 map->pr_mflags |= MA_COW;
182 if (kve->kve_flags & KVME_FLAG_NEEDS_COPY)
183 map->pr_mflags |= MA_NEEDS_COPY;
184 if (kve->kve_flags & KVME_FLAG_NOCOREDUMP)
185 map->pr_mflags |= MA_NOCOREDUMP;
186 strlcpy(map->pr_mapname, kves[lastvn].kve_path,
187 sizeof(map->pr_mapname));
196 for (i = 0; i < p->nobjs; i++) {
198 if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
199 if ((map = malloc(sizeof(*map))) == NULL)
201 proc_rdl2prmap(rdl, map);
209 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
210 size_t namesz, GElf_Sym *symcopy)
213 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
223 unsigned long symtabstridx = 0, dynsymstridx = 0;
225 if ((map = proc_addr2map(p, addr)) == NULL)
227 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
228 DPRINTF("ERROR: open %s failed", map->pr_mapname);
231 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
232 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
235 if (gelf_getehdr(e, &ehdr) == NULL) {
236 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
240 * Find the index of the STRTAB and SYMTAB sections to locate
244 while ((scn = elf_nextscn(e, scn)) != NULL) {
245 gelf_getshdr(scn, &shdr);
246 switch (shdr.sh_type) {
249 symtabstridx = shdr.sh_link;
253 dynsymstridx = shdr.sh_link;
260 * Iterate over the Dynamic Symbols table to find the symbol.
261 * Then look up the string name in STRTAB (.dynstr)
263 if ((data = elf_getdata(dynsymscn, NULL)) == NULL) {
264 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
268 while (gelf_getsym(data, i++, &sym) != NULL) {
270 * Calculate the address mapped to the virtual memory
273 if (ehdr.e_type != ET_EXEC)
274 rsym = map->pr_vaddr + sym.st_value;
277 if (addr >= rsym && addr < rsym + sym.st_size) {
278 s = elf_strptr(e, dynsymstridx, sym.st_name);
280 strlcpy(name, s, namesz);
281 memcpy(symcopy, &sym, sizeof(sym));
283 * DTrace expects the st_value to contain
284 * only the address relative to the start of
287 symcopy->st_value = rsym;
295 * Iterate over the Symbols Table to find the symbol.
296 * Then look up the string name in STRTAB (.dynstr)
298 if ((data = elf_getdata(symtabscn, NULL)) == NULL) {
299 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
303 while (gelf_getsym(data, i++, &sym) != NULL) {
305 * Calculate the address mapped to the virtual memory
308 if (ehdr.e_type != ET_EXEC)
309 rsym = map->pr_vaddr + sym.st_value;
312 if (addr >= rsym && addr < rsym + sym.st_size) {
313 s = elf_strptr(e, symtabstridx, sym.st_name);
315 strlcpy(name, s, namesz);
316 memcpy(symcopy, &sym, sizeof(sym));
318 * DTrace expects the st_value to contain
319 * only the address relative to the start of
322 symcopy->st_value = rsym;
339 proc_name2map(struct proc_handle *p, const char *name)
344 char tmppath[MAXPATHLEN];
345 struct kinfo_vmentry *kves, *kve;
349 * If we haven't iterated over the list of loaded objects,
350 * librtld_db isn't yet initialized and it's very likely
351 * that librtld_db called us. We need to do the heavy
352 * lifting here to find the symbol librtld_db is looking for.
355 if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
357 for (i = 0; i < (size_t)cnt; i++) {
359 basename_r(kve->kve_path, tmppath);
360 if (strcmp(tmppath, name) == 0) {
361 map = proc_addr2map(p, kve->kve_start);
369 if ((name == NULL || strcmp(name, "a.out") == 0) &&
371 map = proc_addr2map(p, p->rdexec->rdl_saddr);
374 for (i = 0; i < p->nobjs; i++) {
376 basename_r(rdl->rdl_path, tmppath);
377 if (strcmp(tmppath, name) == 0) {
378 if ((map = malloc(sizeof(*map))) == NULL)
380 proc_rdl2prmap(rdl, map);
389 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
393 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
402 unsigned long symtabstridx = 0, dynsymstridx = 0;
404 if ((map = proc_name2map(p, object)) == NULL) {
405 DPRINTFX("ERROR: couldn't find object %s", object);
408 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
409 DPRINTF("ERROR: open %s failed", map->pr_mapname);
412 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
413 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
416 if (gelf_getehdr(e, &ehdr) == NULL) {
417 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
421 * Find the index of the STRTAB and SYMTAB sections to locate
425 while ((scn = elf_nextscn(e, scn)) != NULL) {
426 gelf_getshdr(scn, &shdr);
427 switch (shdr.sh_type) {
430 symtabstridx = shdr.sh_link;
434 dynsymstridx = shdr.sh_link;
441 * Iterate over the Dynamic Symbols table to find the symbol.
442 * Then look up the string name in STRTAB (.dynstr)
444 if ((data = elf_getdata(dynsymscn, NULL))) {
446 while (gelf_getsym(data, i++, &sym) != NULL) {
447 s = elf_strptr(e, dynsymstridx, sym.st_name);
448 if (s && strcmp(s, symbol) == 0) {
449 memcpy(symcopy, &sym, sizeof(sym));
450 if (ehdr.e_type != ET_EXEC)
451 symcopy->st_value += map->pr_vaddr;
458 * Iterate over the Symbols Table to find the symbol.
459 * Then look up the string name in STRTAB (.dynstr)
461 if ((data = elf_getdata(symtabscn, NULL))) {
463 while (gelf_getsym(data, i++, &sym) != NULL) {
464 s = elf_strptr(e, symtabstridx, sym.st_name);
465 if (s && strcmp(s, symbol) == 0) {
466 memcpy(symcopy, &sym, sizeof(sym));
467 if (ehdr.e_type != ET_EXEC)
468 symcopy->st_value += map->pr_vaddr;
475 DPRINTFX("found addr 0x%lx for %s", symcopy->st_value, symbol);
488 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
489 int mask, proc_sym_f *func, void *cd)
494 Elf_Scn *scn, *foundscn = NULL;
499 unsigned long stridx = -1;
503 if ((map = proc_name2map(p, object)) == NULL)
505 if ((fd = open(map->pr_mapname, O_RDONLY)) < 0) {
506 DPRINTF("ERROR: open %s failed", map->pr_mapname);
509 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
510 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
513 if (gelf_getehdr(e, &ehdr) == NULL) {
514 DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
518 * Find the section we are looking for.
521 while ((scn = elf_nextscn(e, scn)) != NULL) {
522 gelf_getshdr(scn, &shdr);
523 if (which == PR_SYMTAB &&
524 shdr.sh_type == SHT_SYMTAB) {
527 } else if (which == PR_DYNSYM &&
528 shdr.sh_type == SHT_DYNSYM) {
535 stridx = shdr.sh_link;
536 if ((data = elf_getdata(foundscn, NULL)) == NULL) {
537 DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
541 while (gelf_getsym(data, i++, &sym) != NULL) {
542 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
543 (mask & BIND_LOCAL) == 0)
545 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
546 (mask & BIND_GLOBAL) == 0)
548 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
549 (mask & BIND_WEAK) == 0)
551 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
552 (mask & TYPE_NOTYPE) == 0)
554 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
555 (mask & TYPE_OBJECT) == 0)
557 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
558 (mask & TYPE_FUNC) == 0)
560 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
561 (mask & TYPE_SECTION) == 0)
563 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
564 (mask & TYPE_FILE) == 0)
566 s = elf_strptr(e, stridx, sym.st_name);
567 if (ehdr.e_type != ET_EXEC)
568 sym.st_value += map->pr_vaddr;
569 (*func)(cd, &sym, s);