agent/src/os/linux/libproc_impl.c

Wed, 13 Feb 2013 09:46:19 +0100

author
rbackman
date
Wed, 13 Feb 2013 09:46:19 +0100
changeset 4599
2394a89e89f4
parent 3546
54d3535a6dd3
child 6876
710a3c8b516e
child 9627
d626acee4654
permissions
-rw-r--r--

8008088: SA can hang the VM
Reviewed-by: mgronlun, sla, dholmes

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

mercurial