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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
46 static int _librtld_db_debug = 0;
47 #define DPRINTF(...) do { \
48 if (_librtld_db_debug) { \
49 fprintf(stderr, "librtld_db: DEBUG: "); \
50 fprintf(stderr, __VA_ARGS__); \
55 rd_delete(rd_agent_t *rdap)
62 rd_errstr(rd_err_e rderr)
67 return "generic error";
71 return "capability not supported";
73 return "database error";
79 return "unknown error";
84 rd_event_addr(rd_agent_t *rdap, rd_event_e event, rd_notify_t *notify)
88 DPRINTF("%s rdap %p event %d notify %p\n", __func__, rdap, event,
96 notify->type = RD_NOTIFY_BPT;
97 notify->u.bptaddr = rdap->rda_preinit_addr;
100 notify->type = RD_NOTIFY_BPT;
101 notify->u.bptaddr = rdap->rda_postinit_addr;
104 notify->type = RD_NOTIFY_BPT;
105 notify->u.bptaddr = rdap->rda_dlactivity_addr;
115 rd_event_enable(rd_agent_t *rdap __unused, int onoff)
117 DPRINTF("%s onoff %d\n", __func__, onoff);
123 rd_event_getmsg(rd_agent_t *rdap __unused, rd_event_msg_t *msg)
125 DPRINTF("%s\n", __func__);
127 msg->type = RD_POSTINIT;
128 msg->u.state = RD_CONSISTENT;
138 if (version == RD_VERSION) {
139 debug = getenv("LIBRTLD_DB_DEBUG");
140 _librtld_db_debug = debug ? atoi(debug) : 0;
147 rd_loadobj_iter(rd_agent_t *rdap, rl_iter_f *cb, void *clnt_data)
149 int cnt, i, lastvn = 0;
151 struct kinfo_vmentry *kves, *kve;
153 DPRINTF("%s\n", __func__);
155 if ((kves = kinfo_getvmmap(proc_getpid(rdap->rda_php), &cnt)) == NULL) {
156 warn("ERROR: kinfo_getvmmap() failed");
159 for (i = 0; i < cnt; i++) {
161 if (kve->kve_type == KVME_TYPE_VNODE)
163 memset(&rdl, 0, sizeof(rdl));
165 * Map the kinfo_vmentry struct to the rd_loadobj structure.
167 rdl.rdl_saddr = kve->kve_start;
168 rdl.rdl_eaddr = kve->kve_end;
169 rdl.rdl_offset = kve->kve_offset;
170 if (kve->kve_protection & KVME_PROT_READ)
171 rdl.rdl_prot |= RD_RDL_R;
172 if (kve->kve_protection & KVME_PROT_WRITE)
173 rdl.rdl_prot |= RD_RDL_W;
174 if (kve->kve_protection & KVME_PROT_EXEC)
175 rdl.rdl_prot |= RD_RDL_X;
176 strlcpy(rdl.rdl_path, kves[lastvn].kve_path,
177 sizeof(rdl.rdl_path));
178 (*cb)(&rdl, clnt_data);
186 rd_log(const int onoff)
188 DPRINTF("%s\n", __func__);
194 rd_new(struct proc_handle *php)
198 rdap = malloc(sizeof(rd_agent_t));
200 memset(rdap, 0, sizeof(rd_agent_t));
209 rd_objpad_enable(rd_agent_t *rdap, size_t padsize)
211 DPRINTF("%s\n", __func__);
220 rd_plt_resolution(rd_agent_t *rdap, uintptr_t pc, struct proc *proc,
221 uintptr_t plt_base, rd_plt_info_t *rpi)
223 DPRINTF("%s\n", __func__);
235 rd_reset(rd_agent_t *rdap)
239 if (proc_name2sym(rdap->rda_php, "ld-elf.so.1", "r_debug_state",
242 DPRINTF("found r_debug_state at 0x%lx\n", (unsigned long)sym.st_value);
243 rdap->rda_preinit_addr = sym.st_value;
244 rdap->rda_dlactivity_addr = sym.st_value;
246 if (proc_name2sym(rdap->rda_php, "ld-elf.so.1", "_r_debug_postinit",
249 DPRINTF("found _r_debug_postinit at 0x%lx\n",
250 (unsigned long)sym.st_value);
251 rdap->rda_postinit_addr = sym.st_value;