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 (!map->pr_mapname || (fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
220 warn("ERROR: open %s failed", map->pr_mapname);
223 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
224 warn("ERROR: elf_begin() failed");
227 if (gelf_getehdr(e, &ehdr) == NULL) {
228 warn("ERROR: gelf_getehdr() failed");
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 DPRINTF("ERROR: elf_getdata() failed");
260 while (gelf_getsym(data, i++, &sym) != NULL) {
262 * Calculate the address mapped to the virtual memory
265 rsym = map->pr_vaddr + sym.st_value;
266 if (addr >= rsym && addr <= (rsym + sym.st_size)) {
267 s = elf_strptr(e, dynsymstridx, sym.st_name);
269 strlcpy(name, s, namesz);
270 memcpy(symcopy, &sym, sizeof(sym));
272 * DTrace expects the st_value to contain
273 * only the address relative to the start of
276 symcopy->st_value = rsym;
283 * Iterate over the Symbols Table to find the symbol.
284 * Then look up the string name in STRTAB (.dynstr)
286 if (symtabscn == NULL)
288 if ((data = elf_getdata(symtabscn, NULL)) == NULL) {
289 DPRINTF("ERROR: elf_getdata() failed");
293 while (gelf_getsym(data, i++, &sym) != NULL) {
295 * Calculate the address mapped to the virtual memory
298 if (ehdr.e_type != ET_EXEC)
299 rsym = map->pr_vaddr + sym.st_value;
302 if (addr >= rsym && addr <= (rsym + sym.st_size)) {
303 s = elf_strptr(e, symtabstridx, sym.st_name);
305 strlcpy(name, s, namesz);
306 memcpy(symcopy, &sym, sizeof(sym));
308 * DTrace expects the st_value to contain
309 * only the address relative to the start of
312 symcopy->st_value = rsym;
329 proc_name2map(struct proc_handle *p, const char *name)
334 char tmppath[MAXPATHLEN];
335 struct kinfo_vmentry *kves, *kve;
339 * If we haven't iterated over the list of loaded objects,
340 * librtld_db isn't yet initialized and it's very likely
341 * that librtld_db called us. We need to do the heavy
342 * lifting here to find the symbol librtld_db is looking for.
345 if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
347 for (i = 0; i < (size_t)cnt; i++) {
349 basename_r(kve->kve_path, tmppath);
350 if (strcmp(tmppath, name) == 0) {
351 map = proc_addr2map(p, kve->kve_start);
359 if (name == NULL || strcmp(name, "a.out") == 0) {
360 map = proc_addr2map(p, p->rdobjs[0].rdl_saddr);
363 for (i = 0; i < p->nobjs; i++) {
365 basename_r(rdl->rdl_path, tmppath);
366 if (strcmp(tmppath, name) == 0) {
367 if ((map = malloc(sizeof(*map))) == NULL)
369 proc_rdl2prmap(rdl, map);
378 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
382 Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
391 unsigned long symtabstridx = 0, dynsymstridx = 0;
393 if ((map = proc_name2map(p, object)) == NULL) {
394 DPRINTF("ERROR: couldn't find object %s", object);
397 if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
398 DPRINTF("ERROR: open %s failed", map->pr_mapname);
401 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
402 warn("ERROR: elf_begin() failed");
405 if (gelf_getehdr(e, &ehdr) == NULL) {
406 warn("ERROR: gelf_getehdr() failed");
410 * Find the index of the STRTAB and SYMTAB sections to locate
414 while ((scn = elf_nextscn(e, scn)) != NULL) {
415 gelf_getshdr(scn, &shdr);
416 switch (shdr.sh_type) {
419 symtabstridx = shdr.sh_link;
423 dynsymstridx = shdr.sh_link;
430 * Iterate over the Dynamic Symbols table to find the symbol.
431 * Then look up the string name in STRTAB (.dynstr)
433 if ((data = elf_getdata(dynsymscn, NULL)) == NULL) {
434 DPRINTF("ERROR: elf_getdata() failed");
438 while (gelf_getsym(data, i++, &sym) != NULL) {
439 s = elf_strptr(e, dynsymstridx, sym.st_name);
440 if (s && strcmp(s, symbol) == 0) {
441 memcpy(symcopy, &sym, sizeof(sym));
442 symcopy->st_value = map->pr_vaddr + sym.st_value;
448 * Iterate over the Symbols Table to find the symbol.
449 * Then look up the string name in STRTAB (.dynstr)
451 if (symtabscn == NULL)
453 if ((data = elf_getdata(symtabscn, NULL)) == NULL) {
454 DPRINTF("ERROR: elf_getdata() failed");
458 while (gelf_getsym(data, i++, &sym) != NULL) {
459 s = elf_strptr(e, symtabstridx, sym.st_name);
460 if (s && strcmp(s, symbol) == 0) {
461 memcpy(symcopy, &sym, sizeof(sym));
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;
489 unsigned long stridx = -1;
493 if ((map = proc_name2map(p, object)) == NULL)
495 if ((fd = open(map->pr_mapname, O_RDONLY)) < 0) {
496 warn("ERROR: open %s failed", map->pr_mapname);
499 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
500 warn("ERROR: elf_begin() failed");
504 * Find the section we are looking for.
507 while ((scn = elf_nextscn(e, scn)) != NULL) {
508 gelf_getshdr(scn, &shdr);
509 if (which == PR_SYMTAB &&
510 shdr.sh_type == SHT_SYMTAB) {
513 } else if (which == PR_DYNSYM &&
514 shdr.sh_type == SHT_DYNSYM) {
521 stridx = shdr.sh_link;
522 if ((data = elf_getdata(foundscn, NULL)) == NULL) {
523 DPRINTF("ERROR: elf_getdata() failed");
527 while (gelf_getsym(data, i++, &sym) != NULL) {
528 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
529 (mask & BIND_LOCAL) == 0)
531 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
532 (mask & BIND_GLOBAL) == 0)
534 if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
535 (mask & BIND_WEAK) == 0)
537 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
538 (mask & TYPE_NOTYPE) == 0)
540 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
541 (mask & TYPE_OBJECT) == 0)
543 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
544 (mask & TYPE_FUNC) == 0)
546 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
547 (mask & TYPE_SECTION) == 0)
549 if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
550 (mask & TYPE_FILE) == 0)
552 s = elf_strptr(e, stridx, sym.st_name);
553 sym.st_value += map->pr_vaddr;
554 (*func)(cd, &sym, s);