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/sysctl.h>
46 #include <machine/elf.h>
50 #include <libprocstat.h>
55 static int _librtld_db_debug = 0;
56 #define DPRINTF(...) do { \
57 if (_librtld_db_debug) { \
58 fprintf(stderr, "librtld_db: DEBUG: "); \
59 fprintf(stderr, __VA_ARGS__); \
64 rd_delete(rd_agent_t *rdap)
67 if (rdap->rda_procstat != NULL)
68 procstat_close(rdap->rda_procstat);
73 rd_errstr(rd_err_e rderr)
78 return "generic error";
82 return "capability not supported";
84 return "database error";
90 return "unknown error";
95 rd_event_addr(rd_agent_t *rdap, rd_event_e event, rd_notify_t *notify)
99 DPRINTF("%s rdap %p event %d notify %p\n", __func__, rdap, event,
107 notify->type = RD_NOTIFY_BPT;
108 notify->u.bptaddr = rdap->rda_preinit_addr;
111 notify->type = RD_NOTIFY_BPT;
112 notify->u.bptaddr = rdap->rda_postinit_addr;
115 notify->type = RD_NOTIFY_BPT;
116 notify->u.bptaddr = rdap->rda_dlactivity_addr;
126 rd_event_enable(rd_agent_t *rdap __unused, int onoff)
128 DPRINTF("%s onoff %d\n", __func__, onoff);
134 rd_event_getmsg(rd_agent_t *rdap __unused, rd_event_msg_t *msg)
136 DPRINTF("%s\n", __func__);
138 msg->type = RD_POSTINIT;
139 msg->u.state = RD_CONSISTENT;
149 if (version == RD_VERSION) {
150 debug = getenv("LIBRTLD_DB_DEBUG");
151 _librtld_db_debug = debug ? atoi(debug) : 0;
158 rd_loadobj_iter(rd_agent_t *rdap, rl_iter_f *cb, void *clnt_data)
160 struct kinfo_vmentry *kves, *kve;
165 DPRINTF("%s\n", __func__);
167 if ((kves = kinfo_getvmmap(proc_getpid(rdap->rda_php), &cnt)) == NULL) {
168 warn("ERROR: kinfo_getvmmap() failed");
174 for (i = 0; i < cnt; i++) {
176 if (kve->kve_type == KVME_TYPE_VNODE)
178 memset(&rdl, 0, sizeof(rdl));
180 * Map the kinfo_vmentry struct to the rd_loadobj structure.
182 rdl.rdl_saddr = kve->kve_start;
183 rdl.rdl_eaddr = kve->kve_end;
184 rdl.rdl_offset = kve->kve_offset;
185 if (kve->kve_protection & KVME_PROT_READ)
186 rdl.rdl_prot |= RD_RDL_R;
187 if (kve->kve_protection & KVME_PROT_WRITE)
188 rdl.rdl_prot |= RD_RDL_W;
189 if (kve->kve_protection & KVME_PROT_EXEC)
190 rdl.rdl_prot |= RD_RDL_X;
191 strlcpy(rdl.rdl_path, kves[lastvn].kve_path,
192 sizeof(rdl.rdl_path));
193 if ((*cb)(&rdl, clnt_data) != 0) {
203 rd_log(const int onoff)
205 DPRINTF("%s\n", __func__);
211 rd_new(struct proc_handle *php)
215 rdap = malloc(sizeof(*rdap));
219 memset(rdap, 0, sizeof(rd_agent_t));
221 rdap->rda_procstat = procstat_open_sysctl();
223 if (rd_reset(rdap) != RD_OK) {
231 rd_objpad_enable(rd_agent_t *rdap, size_t padsize)
233 DPRINTF("%s\n", __func__);
242 rd_plt_resolution(rd_agent_t *rdap, uintptr_t pc, struct proc *proc,
243 uintptr_t plt_base, rd_plt_info_t *rpi)
245 DPRINTF("%s\n", __func__);
257 rtld_syms(rd_agent_t *rdap, const char *rtldpath, u_long base)
265 Elf64_Word strscnidx;
271 fd = open(rtldpath, O_RDONLY);
275 if (elf_version(EV_CURRENT) == EV_NONE)
277 e = elf_begin(fd, ELF_C_READ, NULL);
282 while ((scn = elf_nextscn(e, scn)) != NULL) {
283 gelf_getshdr(scn, &shdr);
284 if (shdr.sh_type == SHT_DYNSYM)
290 strscnidx = shdr.sh_link;
291 data = elf_getdata(scn, NULL);
295 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
296 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
297 GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
299 symname = elf_strptr(e, strscnidx, sym.st_name);
303 if (strcmp(symname, "r_debug_state") == 0) {
304 rdap->rda_preinit_addr = sym.st_value + base;
305 rdap->rda_dlactivity_addr = sym.st_value + base;
306 } else if (strcmp(symname, "_r_debug_postinit") == 0) {
307 rdap->rda_postinit_addr = sym.st_value + base;
311 if (rdap->rda_preinit_addr != 0 &&
312 rdap->rda_postinit_addr != 0 &&
313 rdap->rda_dlactivity_addr != 0)
325 rd_reset(rd_agent_t *rdap)
327 struct kinfo_proc *kp;
328 struct kinfo_vmentry *kve;
330 const char *rtldpath;
340 kp = procstat_getprocs(rdap->rda_procstat, KERN_PROC_PID,
341 proc_getpid(rdap->rda_php), &count);
346 auxv = procstat_getauxv(rdap->rda_procstat, kp, &count);
351 for (i = 0; i < count; i++) {
352 if (auxv[i].a_type == AT_BASE) {
353 base = auxv[i].a_un.a_val;
361 kve = procstat_getvmmap(rdap->rda_procstat, kp, &count);
364 for (i = 0; i < count; i++) {
365 if (kve[i].kve_start == base) {
366 rtldpath = kve[i].kve_path;
373 if (rtld_syms(rdap, rtldpath, base) != 0)
380 procstat_freevmmap(rdap->rda_procstat, kve);
382 procstat_freeauxv(rdap->rda_procstat, auxv);
384 procstat_freeprocs(rdap->rda_procstat, kp);