1 /* 2 * Copyright (c) 2010 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Rui Paulo under sponsorship from the 6 * FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <machine/_inttypes.h> 33 #include <sys/types.h> 34 #include <sys/user.h> 35 36 #include <err.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <limits.h> 41 #include <libproc.h> 42 #include <libutil.h> 43 44 #include "rtld_db.h" 45 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__); \ 51 } \ 52 } while (0) 53 54 void 55 rd_delete(rd_agent_t *rdap) 56 { 57 58 free(rdap); 59 } 60 61 const char * 62 rd_errstr(rd_err_e rderr) 63 { 64 65 switch (rderr) { 66 case RD_ERR: 67 return "generic error"; 68 case RD_OK: 69 return "no error"; 70 case RD_NOCAPAB: 71 return "capability not supported"; 72 case RD_DBERR: 73 return "database error"; 74 case RD_NOBASE: 75 return "NOBASE"; 76 case RD_NOMAPS: 77 return "NOMAPS"; 78 default: 79 return "unknown error"; 80 } 81 } 82 83 rd_err_e 84 rd_event_addr(rd_agent_t *rdap, rd_event_e event __unused, rd_notify_t *notify) 85 { 86 DPRINTF("%s rdap %p notify %p\n", __func__, rdap, notify); 87 88 notify->type = RD_NOTIFY_BPT; 89 notify->u.bptaddr = rdap->rda_addr; 90 91 return (RD_OK); 92 } 93 94 rd_err_e 95 rd_event_enable(rd_agent_t *rdap __unused, int onoff) 96 { 97 DPRINTF("%s onoff %d\n", __func__, onoff); 98 99 return (RD_OK); 100 } 101 102 rd_err_e 103 rd_event_getmsg(rd_agent_t *rdap __unused, rd_event_msg_t *msg) 104 { 105 DPRINTF("%s\n", __func__); 106 107 msg->type = RD_POSTINIT; 108 msg->u.state = RD_CONSISTENT; 109 110 return (RD_OK); 111 } 112 113 rd_err_e 114 rd_init(int version) 115 { 116 char *debug = NULL; 117 118 if (version == RD_VERSION) { 119 debug = getenv("LIBRTLD_DB_DEBUG"); 120 _librtld_db_debug = debug ? atoi(debug) : 0; 121 return (RD_OK); 122 } else 123 return (RD_NOCAPAB); 124 } 125 126 rd_err_e 127 rd_loadobj_iter(rd_agent_t *rdap, rl_iter_f *cb, void *clnt_data) 128 { 129 int cnt, i, lastvn = 0; 130 rd_loadobj_t rdl; 131 struct kinfo_vmentry *kves, *kve; 132 133 DPRINTF("%s\n", __func__); 134 135 if ((kves = kinfo_getvmmap(proc_getpid(rdap->rda_php), &cnt)) == NULL) { 136 warn("ERROR: kinfo_getvmmap() failed"); 137 return (RD_ERR); 138 } 139 for (i = 0; i < cnt; i++) { 140 kve = kves + i; 141 if (kve->kve_type == KVME_TYPE_VNODE) 142 lastvn = i; 143 memset(&rdl, 0, sizeof(rdl)); 144 /* 145 * Map the kinfo_vmentry struct to the rd_loadobj structure. 146 */ 147 rdl.rdl_saddr = kve->kve_start; 148 rdl.rdl_eaddr = kve->kve_end; 149 rdl.rdl_offset = kve->kve_offset; 150 if (kve->kve_protection & KVME_PROT_READ) 151 rdl.rdl_prot |= RD_RDL_R; 152 if (kve->kve_protection & KVME_PROT_WRITE) 153 rdl.rdl_prot |= RD_RDL_W; 154 if (kve->kve_protection & KVME_PROT_EXEC) 155 rdl.rdl_prot |= RD_RDL_X; 156 strlcpy(rdl.rdl_path, kves[lastvn].kve_path, 157 sizeof(rdl.rdl_path)); 158 (*cb)(&rdl, clnt_data); 159 } 160 free(kves); 161 162 return (RD_OK); 163 } 164 165 void 166 rd_log(const int onoff) 167 { 168 DPRINTF("%s\n", __func__); 169 170 (void)onoff; 171 } 172 173 rd_agent_t * 174 rd_new(struct proc_handle *php) 175 { 176 rd_agent_t *rdap; 177 178 rdap = malloc(sizeof(rd_agent_t)); 179 if (rdap) { 180 memset(rdap, 0, sizeof(rd_agent_t)); 181 rdap->rda_php = php; 182 rd_reset(rdap); 183 } 184 185 return (rdap); 186 } 187 188 rd_err_e 189 rd_objpad_enable(rd_agent_t *rdap, size_t padsize) 190 { 191 DPRINTF("%s\n", __func__); 192 193 (void)rdap; 194 (void)padsize; 195 196 return (RD_ERR); 197 } 198 199 rd_err_e 200 rd_plt_resolution(rd_agent_t *rdap, uintptr_t pc, struct proc *proc, 201 uintptr_t plt_base, rd_plt_info_t *rpi) 202 { 203 DPRINTF("%s\n", __func__); 204 205 (void)rdap; 206 (void)pc; 207 (void)proc; 208 (void)plt_base; 209 (void)rpi; 210 211 return (RD_ERR); 212 } 213 214 rd_err_e 215 rd_reset(rd_agent_t *rdap) 216 { 217 GElf_Sym sym; 218 219 if (proc_name2sym(rdap->rda_php, "ld-elf.so.1", "r_debug_state", 220 &sym) < 0) 221 return (RD_ERR); 222 DPRINTF("found r_debug_state at 0x%lx\n", (unsigned long)sym.st_value); 223 rdap->rda_addr = sym.st_value; 224 225 return (RD_OK); 226 } 227