1 /* 2 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, 20 * CA 94065 USA or visit www.oracle.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 #include <stdarg.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <fcntl.h> 29 #include <thread_db.h> 30 #include "libproc_impl.h" 31 32 static const char* alt_root = NULL; 33 static int alt_root_len = -1; 34 35 #define SA_ALTROOT "SA_ALTROOT" 36 37 static void init_alt_root() { 38 if (alt_root_len == -1) { 39 alt_root = getenv(SA_ALTROOT); 40 if (alt_root) { 41 alt_root_len = strlen(alt_root); 42 } else { 43 alt_root_len = 0; 44 } 45 } 46 } 47 48 int pathmap_open(const char* name) { 49 int fd; 50 char alt_path[PATH_MAX + 1]; 51 52 init_alt_root(); 53 fd = open(name, O_RDONLY); 54 if (fd >= 0) { 55 return fd; 56 } 57 58 if (alt_root_len > 0) { 59 strcpy(alt_path, alt_root); 60 strcat(alt_path, name); 61 fd = open(alt_path, O_RDONLY); 62 if (fd >= 0) { 63 print_debug("path %s substituted for %s\n", alt_path, name); 64 return fd; 65 } 66 67 if (strrchr(name, '/')) { 68 strcpy(alt_path, alt_root); 69 strcat(alt_path, strrchr(name, '/')); 70 fd = open(alt_path, O_RDONLY); 71 if (fd >= 0) { 72 print_debug("path %s substituted for %s\n", alt_path, name); 73 return fd; 74 } 75 } 76 } 77 78 return -1; 79 } 80 81 static bool _libsaproc_debug; 82 83 void print_debug(const char* format,...) { 84 if (_libsaproc_debug) { 85 va_list alist; 86 87 va_start(alist, format); 88 fputs("libsaproc DEBUG: ", stderr); 89 vfprintf(stderr, format, alist); 90 va_end(alist); 91 } 92 } 93 94 bool is_debug() { 95 return _libsaproc_debug; 96 } 97 98 // initialize libproc 99 bool init_libproc(bool debug) { 100 // init debug mode 101 _libsaproc_debug = debug; 102 103 // initialize the thread_db library 104 if (td_init() != TD_OK) { 105 print_debug("libthread_db's td_init failed\n"); 106 return false; 107 } 108 109 return true; 110 } 111 112 static void destroy_lib_info(struct ps_prochandle* ph) { 113 lib_info* lib = ph->libs; 114 while (lib) { 115 lib_info *next = lib->next; 116 if (lib->symtab) { 117 destroy_symtab(lib->symtab); 118 } 119 free(lib); 120 lib = next; 121 } 122 } 123 124 static void destroy_thread_info(struct ps_prochandle* ph) { 125 thread_info* thr = ph->threads; 126 while (thr) { 127 thread_info *next = thr->next; 128 free(thr); 129 thr = next; 130 } 131 } 132 133 // ps_prochandle cleanup 134 135 // ps_prochandle cleanup 136 void Prelease(struct ps_prochandle* ph) { 137 // do the "derived class" clean-up first 138 ph->ops->release(ph); 139 destroy_lib_info(ph); 140 destroy_thread_info(ph); 141 free(ph); 142 } 143 144 lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) { 145 return add_lib_info_fd(ph, libname, -1, base); 146 } 147 148 lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) { 149 lib_info* newlib; 150 151 if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) { 152 print_debug("can't allocate memory for lib_info\n"); 153 return NULL; 154 } 155 156 strncpy(newlib->name, libname, sizeof(newlib->name)); 157 newlib->base = base; 158 159 if (fd == -1) { 160 if ( (newlib->fd = pathmap_open(newlib->name)) < 0) { 161 print_debug("can't open shared object %s\n", newlib->name); 162 free(newlib); 163 return NULL; 164 } 165 } else { 166 newlib->fd = fd; 167 } 168 169 // check whether we have got an ELF file. /proc/<pid>/map 170 // gives out all file mappings and not just shared objects 171 if (is_elf_file(newlib->fd) == false) { 172 close(newlib->fd); 173 free(newlib); 174 return NULL; 175 } 176 177 newlib->symtab = build_symtab(newlib->fd); 178 if (newlib->symtab == NULL) { 179 print_debug("symbol table build failed for %s\n", newlib->name); 180 } 181 182 // even if symbol table building fails, we add the lib_info. 183 // This is because we may need to read from the ELF file for core file 184 // address read functionality. lookup_symbol checks for NULL symtab. 185 if (ph->libs) { 186 ph->lib_tail->next = newlib; 187 ph->lib_tail = newlib; 188 } else { 189 ph->libs = ph->lib_tail = newlib; 190 } 191 ph->num_libs++; 192 193 return newlib; 194 } 195 196 // lookup for a specific symbol 197 uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name, 198 const char* sym_name) { 199 // ignore object_name. search in all libraries 200 // FIXME: what should we do with object_name?? The library names are obtained 201 // by parsing /proc/<pid>/maps, which may not be the same as object_name. 202 // What we need is a utility to map object_name to real file name, something 203 // dlopen() does by looking at LD_LIBRARY_PATH and /etc/ld.so.cache. For 204 // now, we just ignore object_name and do a global search for the symbol. 205 206 lib_info* lib = ph->libs; 207 while (lib) { 208 if (lib->symtab) { 209 uintptr_t res = search_symbol(lib->symtab, lib->base, sym_name, NULL); 210 if (res) return res; 211 } 212 lib = lib->next; 213 } 214 215 print_debug("lookup failed for symbol '%s' in obj '%s'\n", 216 sym_name, object_name); 217 return (uintptr_t) NULL; 218 } 219 220 221 const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) { 222 const char* res = NULL; 223 lib_info* lib = ph->libs; 224 while (lib) { 225 if (lib->symtab && addr >= lib->base) { 226 res = nearest_symbol(lib->symtab, addr - lib->base, poffset); 227 if (res) return res; 228 } 229 lib = lib->next; 230 } 231 return NULL; 232 } 233 234 // add a thread to ps_prochandle 235 thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) { 236 thread_info* newthr; 237 if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) { 238 print_debug("can't allocate memory for thread_info\n"); 239 return NULL; 240 } 241 242 // initialize thread info 243 newthr->pthread_id = pthread_id; 244 newthr->lwp_id = lwp_id; 245 246 // add new thread to the list 247 newthr->next = ph->threads; 248 ph->threads = newthr; 249 ph->num_threads++; 250 return newthr; 251 } 252 253 254 // struct used for client data from thread_db callback 255 struct thread_db_client_data { 256 struct ps_prochandle* ph; 257 thread_info_callback callback; 258 }; 259 260 // callback function for libthread_db 261 static int thread_db_callback(const td_thrhandle_t *th_p, void *data) { 262 struct thread_db_client_data* ptr = (struct thread_db_client_data*) data; 263 td_thrinfo_t ti; 264 td_err_e err; 265 266 memset(&ti, 0, sizeof(ti)); 267 err = td_thr_get_info(th_p, &ti); 268 if (err != TD_OK) { 269 print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n"); 270 return err; 271 } 272 273 print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid); 274 275 if (ptr->callback(ptr->ph, ti.ti_tid, ti.ti_lid) != true) 276 return TD_ERR; 277 278 return TD_OK; 279 } 280 281 // read thread_info using libthread_db 282 bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) { 283 struct thread_db_client_data mydata; 284 td_thragent_t* thread_agent = NULL; 285 if (td_ta_new(ph, &thread_agent) != TD_OK) { 286 print_debug("can't create libthread_db agent\n"); 287 return false; 288 } 289 290 mydata.ph = ph; 291 mydata.callback = cb; 292 293 // we use libthread_db iterator to iterate thru list of threads. 294 if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata, 295 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, 296 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) { 297 td_ta_delete(thread_agent); 298 return false; 299 } 300 301 // delete thread agent 302 td_ta_delete(thread_agent); 303 return true; 304 } 305 306 307 // get number of threads 308 int get_num_threads(struct ps_prochandle* ph) { 309 return ph->num_threads; 310 } 311 312 // get lwp_id of n'th thread 313 lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) { 314 int count = 0; 315 thread_info* thr = ph->threads; 316 while (thr) { 317 if (count == index) { 318 return thr->lwp_id; 319 } 320 count++; 321 thr = thr->next; 322 } 323 return -1; 324 } 325 326 // get regs for a given lwp 327 bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct user_regs_struct* regs) { 328 return ph->ops->get_lwp_regs(ph, lwp_id, regs); 329 } 330 331 // get number of shared objects 332 int get_num_libs(struct ps_prochandle* ph) { 333 return ph->num_libs; 334 } 335 336 // get name of n'th solib 337 const char* get_lib_name(struct ps_prochandle* ph, int index) { 338 int count = 0; 339 lib_info* lib = ph->libs; 340 while (lib) { 341 if (count == index) { 342 return lib->name; 343 } 344 count++; 345 lib = lib->next; 346 } 347 return NULL; 348 } 349 350 // get base address of a lib 351 uintptr_t get_lib_base(struct ps_prochandle* ph, int index) { 352 int count = 0; 353 lib_info* lib = ph->libs; 354 while (lib) { 355 if (count == index) { 356 return lib->base; 357 } 358 count++; 359 lib = lib->next; 360 } 361 return (uintptr_t)NULL; 362 } 363 364 bool find_lib(struct ps_prochandle* ph, const char *lib_name) { 365 lib_info *p = ph->libs; 366 while (p) { 367 if (strcmp(p->name, lib_name) == 0) { 368 return true; 369 } 370 p = p->next; 371 } 372 return false; 373 } 374 375 //-------------------------------------------------------------------------- 376 // proc service functions 377 378 // get process id 379 pid_t ps_getpid(struct ps_prochandle *ph) { 380 return ph->pid; 381 } 382 383 // ps_pglobal_lookup() looks up the symbol sym_name in the symbol table 384 // of the load object object_name in the target process identified by ph. 385 // It returns the symbol's value as an address in the target process in 386 // *sym_addr. 387 388 ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name, 389 const char *sym_name, psaddr_t *sym_addr) { 390 *sym_addr = (psaddr_t) lookup_symbol(ph, object_name, sym_name); 391 return (*sym_addr ? PS_OK : PS_NOSYM); 392 } 393 394 // read "size" bytes info "buf" from address "addr" 395 ps_err_e ps_pdread(struct ps_prochandle *ph, psaddr_t addr, 396 void *buf, size_t size) { 397 return ph->ops->p_pread(ph, (uintptr_t) addr, buf, size)? PS_OK: PS_ERR; 398 } 399 400 // write "size" bytes of data to debuggee at address "addr" 401 ps_err_e ps_pdwrite(struct ps_prochandle *ph, psaddr_t addr, 402 const void *buf, size_t size) { 403 return ph->ops->p_pwrite(ph, (uintptr_t)addr, buf, size)? PS_OK: PS_ERR; 404 } 405 406 // ------------------------------------------------------------------------ 407 // Functions below this point are not yet implemented. They are here only 408 // to make the linker happy. 409 410 ps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) { 411 print_debug("ps_lsetfpregs not implemented\n"); 412 return PS_OK; 413 } 414 415 ps_err_e ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) { 416 print_debug("ps_lsetregs not implemented\n"); 417 return PS_OK; 418 } 419 420 ps_err_e ps_lgetfpregs(struct ps_prochandle *ph, lwpid_t lid, prfpregset_t *fpregs) { 421 print_debug("ps_lgetfpregs not implemented\n"); 422 return PS_OK; 423 } 424 425 ps_err_e ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) { 426 print_debug("ps_lgetfpregs not implemented\n"); 427 return PS_OK; 428 } 429 430 // new libthread_db of NPTL seem to require this symbol 431 ps_err_e ps_get_thread_area() { 432 print_debug("ps_get_thread_area not implemented\n"); 433 return PS_OK; 434 }