2 * Copyright (c) 2010 The FreeBSD Foundation
5 * This software was developed by Rui Paulo under sponsorship from the
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/sysctl.h>
47 #include <machine/elf.h>
51 #include <libprocstat.h>
56 static int _librtld_db_debug = 0;
57 #define DPRINTF(...) do { \
58 if (_librtld_db_debug) { \
59 fprintf(stderr, "librtld_db: DEBUG: "); \
60 fprintf(stderr, __VA_ARGS__); \
65 rd_delete(rd_agent_t *rdap)
68 if (rdap->rda_procstat != NULL)
69 procstat_close(rdap->rda_procstat);
74 rd_errstr(rd_err_e rderr)
79 return "generic error";
83 return "capability not supported";
85 return "database error";
91 return "unknown error";
96 rd_event_addr(rd_agent_t *rdap, rd_event_e event, rd_notify_t *notify)
100 DPRINTF("%s rdap %p event %d notify %p\n", __func__, rdap, event,
108 notify->type = RD_NOTIFY_BPT;
109 notify->u.bptaddr = rdap->rda_preinit_addr;
112 notify->type = RD_NOTIFY_BPT;
113 notify->u.bptaddr = rdap->rda_postinit_addr;
116 notify->type = RD_NOTIFY_BPT;
117 notify->u.bptaddr = rdap->rda_dlactivity_addr;
127 rd_event_enable(rd_agent_t *rdap __unused, int onoff)
129 DPRINTF("%s onoff %d\n", __func__, onoff);
135 rd_event_getmsg(rd_agent_t *rdap __unused, rd_event_msg_t *msg)
137 DPRINTF("%s\n", __func__);
139 msg->type = RD_POSTINIT;
140 msg->u.state = RD_CONSISTENT;
150 if (version == RD_VERSION) {
151 debug = getenv("LIBRTLD_DB_DEBUG");
152 _librtld_db_debug = debug ? atoi(debug) : 0;
159 rd_loadobj_iter(rd_agent_t *rdap, rl_iter_f *cb, void *clnt_data)
161 struct kinfo_vmentry *kves, *kve;
166 DPRINTF("%s\n", __func__);
168 if ((kves = kinfo_getvmmap(proc_getpid(rdap->rda_php), &cnt)) == NULL) {
169 warn("ERROR: kinfo_getvmmap() failed");
175 for (i = 0; i < cnt; i++) {
177 if (kve->kve_type == KVME_TYPE_VNODE)
179 memset(&rdl, 0, sizeof(rdl));
181 * Map the kinfo_vmentry struct to the rd_loadobj structure.
183 rdl.rdl_saddr = kve->kve_start;
184 rdl.rdl_eaddr = kve->kve_end;
185 rdl.rdl_offset = kve->kve_offset;
186 if (kve->kve_protection & KVME_PROT_READ)
187 rdl.rdl_prot |= RD_RDL_R;
188 if (kve->kve_protection & KVME_PROT_WRITE)
189 rdl.rdl_prot |= RD_RDL_W;
190 if (kve->kve_protection & KVME_PROT_EXEC)
191 rdl.rdl_prot |= RD_RDL_X;
192 strlcpy(rdl.rdl_path, kves[lastvn].kve_path,
193 sizeof(rdl.rdl_path));
194 if ((*cb)(&rdl, clnt_data) != 0) {
204 rd_log(const int onoff)
206 DPRINTF("%s\n", __func__);
212 rd_new(struct proc_handle *php)
216 rdap = malloc(sizeof(*rdap));
220 memset(rdap, 0, sizeof(rd_agent_t));
222 rdap->rda_procstat = procstat_open_sysctl();
224 if (rd_reset(rdap) != RD_OK) {
232 rd_objpad_enable(rd_agent_t *rdap, size_t padsize)
234 DPRINTF("%s\n", __func__);
243 rd_plt_resolution(rd_agent_t *rdap, uintptr_t pc, struct proc *proc,
244 uintptr_t plt_base, rd_plt_info_t *rpi)
246 DPRINTF("%s\n", __func__);
258 rtld_syms(rd_agent_t *rdap, const char *rtldpath, u_long base)
266 Elf64_Word strscnidx;
272 fd = open(rtldpath, O_RDONLY);
276 if (elf_version(EV_CURRENT) == EV_NONE)
278 e = elf_begin(fd, ELF_C_READ, NULL);
285 while ((scn = elf_nextscn(e, scn)) != NULL) {
286 gelf_getshdr(scn, &shdr);
287 if (shdr.sh_type == SHT_DYNSYM)
293 strscnidx = shdr.sh_link;
294 data = elf_getdata(scn, NULL);
298 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
299 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
300 GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
302 symname = elf_strptr(e, strscnidx, sym.st_name);
306 if (strcmp(symname, "r_debug_state") == 0) {
307 rdap->rda_preinit_addr = sym.st_value + base;
308 rdap->rda_dlactivity_addr = sym.st_value + base;
309 } else if (strcmp(symname, "_r_debug_postinit") == 0) {
310 rdap->rda_postinit_addr = sym.st_value + base;
314 if (rdap->rda_preinit_addr != 0 &&
315 rdap->rda_postinit_addr != 0 &&
316 rdap->rda_dlactivity_addr != 0)
328 rd_reset(rd_agent_t *rdap)
330 struct kinfo_proc *kp;
331 struct kinfo_vmentry *kve;
333 const char *rtldpath;
343 kp = procstat_getprocs(rdap->rda_procstat, KERN_PROC_PID,
344 proc_getpid(rdap->rda_php), &count);
349 auxv = procstat_getauxv(rdap->rda_procstat, kp, &count);
354 for (i = 0; i < count; i++) {
355 if (auxv[i].a_type == AT_BASE) {
356 base = auxv[i].a_un.a_val;
364 kve = procstat_getvmmap(rdap->rda_procstat, kp, &count);
367 for (i = 0; i < count; i++) {
368 if (kve[i].kve_start == base) {
369 rtldpath = kve[i].kve_path;
376 if (rtld_syms(rdap, rtldpath, base) != 0)
383 procstat_freevmmap(rdap->rda_procstat, kve);
385 procstat_freeauxv(rdap->rda_procstat, auxv);
387 procstat_freeprocs(rdap->rda_procstat, kp);