agent/src/os/linux/libproc_impl.c

Fri, 25 Jan 2013 16:50:33 -0800

author
morris
date
Fri, 25 Jan 2013 16:50:33 -0800
changeset 4535
9fae07c31641
parent 3546
54d3535a6dd3
child 4599
2394a89e89f4
permissions
-rw-r--r--

6518907: cleanup IA64 specific code in Hotspot
Summary: removed unused IA64 specific code
Reviewed-by: twisti, kvn, dholmes

duke@435 1 /*
poonam@3546 2 * Copyright (c) 2003, 2012, 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
duke@435 95 bool is_debug() {
duke@435 96 return _libsaproc_debug;
duke@435 97 }
duke@435 98
duke@435 99 // initialize libproc
duke@435 100 bool init_libproc(bool debug) {
duke@435 101 // init debug mode
duke@435 102 _libsaproc_debug = debug;
duke@435 103
duke@435 104 // initialize the thread_db library
duke@435 105 if (td_init() != TD_OK) {
duke@435 106 print_debug("libthread_db's td_init failed\n");
duke@435 107 return false;
duke@435 108 }
duke@435 109
duke@435 110 return true;
duke@435 111 }
duke@435 112
duke@435 113 static void destroy_lib_info(struct ps_prochandle* ph) {
duke@435 114 lib_info* lib = ph->libs;
duke@435 115 while (lib) {
duke@435 116 lib_info *next = lib->next;
duke@435 117 if (lib->symtab) {
duke@435 118 destroy_symtab(lib->symtab);
duke@435 119 }
duke@435 120 free(lib);
duke@435 121 lib = next;
duke@435 122 }
duke@435 123 }
duke@435 124
duke@435 125 static void destroy_thread_info(struct ps_prochandle* ph) {
duke@435 126 thread_info* thr = ph->threads;
duke@435 127 while (thr) {
duke@435 128 thread_info *next = thr->next;
duke@435 129 free(thr);
duke@435 130 thr = next;
duke@435 131 }
duke@435 132 }
duke@435 133
duke@435 134 // ps_prochandle cleanup
duke@435 135
duke@435 136 // ps_prochandle cleanup
duke@435 137 void Prelease(struct ps_prochandle* ph) {
duke@435 138 // do the "derived class" clean-up first
duke@435 139 ph->ops->release(ph);
duke@435 140 destroy_lib_info(ph);
duke@435 141 destroy_thread_info(ph);
duke@435 142 free(ph);
duke@435 143 }
duke@435 144
duke@435 145 lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) {
duke@435 146 return add_lib_info_fd(ph, libname, -1, base);
duke@435 147 }
duke@435 148
duke@435 149 lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {
duke@435 150 lib_info* newlib;
duke@435 151
duke@435 152 if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {
duke@435 153 print_debug("can't allocate memory for lib_info\n");
duke@435 154 return NULL;
duke@435 155 }
duke@435 156
duke@435 157 strncpy(newlib->name, libname, sizeof(newlib->name));
duke@435 158 newlib->base = base;
duke@435 159
duke@435 160 if (fd == -1) {
duke@435 161 if ( (newlib->fd = pathmap_open(newlib->name)) < 0) {
duke@435 162 print_debug("can't open shared object %s\n", newlib->name);
duke@435 163 free(newlib);
duke@435 164 return NULL;
duke@435 165 }
duke@435 166 } else {
duke@435 167 newlib->fd = fd;
duke@435 168 }
duke@435 169
duke@435 170 // check whether we have got an ELF file. /proc/<pid>/map
duke@435 171 // gives out all file mappings and not just shared objects
duke@435 172 if (is_elf_file(newlib->fd) == false) {
duke@435 173 close(newlib->fd);
duke@435 174 free(newlib);
duke@435 175 return NULL;
duke@435 176 }
duke@435 177
never@1729 178 newlib->symtab = build_symtab(newlib->fd, libname);
duke@435 179 if (newlib->symtab == NULL) {
duke@435 180 print_debug("symbol table build failed for %s\n", newlib->name);
duke@435 181 }
duke@435 182
duke@435 183 // even if symbol table building fails, we add the lib_info.
duke@435 184 // This is because we may need to read from the ELF file for core file
duke@435 185 // address read functionality. lookup_symbol checks for NULL symtab.
duke@435 186 if (ph->libs) {
duke@435 187 ph->lib_tail->next = newlib;
duke@435 188 ph->lib_tail = newlib;
duke@435 189 } else {
duke@435 190 ph->libs = ph->lib_tail = newlib;
duke@435 191 }
duke@435 192 ph->num_libs++;
duke@435 193
duke@435 194 return newlib;
duke@435 195 }
duke@435 196
duke@435 197 // lookup for a specific symbol
duke@435 198 uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name,
duke@435 199 const char* sym_name) {
duke@435 200 // ignore object_name. search in all libraries
duke@435 201 // FIXME: what should we do with object_name?? The library names are obtained
duke@435 202 // by parsing /proc/<pid>/maps, which may not be the same as object_name.
duke@435 203 // What we need is a utility to map object_name to real file name, something
duke@435 204 // dlopen() does by looking at LD_LIBRARY_PATH and /etc/ld.so.cache. For
duke@435 205 // now, we just ignore object_name and do a global search for the symbol.
duke@435 206
duke@435 207 lib_info* lib = ph->libs;
duke@435 208 while (lib) {
duke@435 209 if (lib->symtab) {
duke@435 210 uintptr_t res = search_symbol(lib->symtab, lib->base, sym_name, NULL);
duke@435 211 if (res) return res;
duke@435 212 }
duke@435 213 lib = lib->next;
duke@435 214 }
duke@435 215
duke@435 216 print_debug("lookup failed for symbol '%s' in obj '%s'\n",
duke@435 217 sym_name, object_name);
duke@435 218 return (uintptr_t) NULL;
duke@435 219 }
duke@435 220
duke@435 221
duke@435 222 const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {
duke@435 223 const char* res = NULL;
duke@435 224 lib_info* lib = ph->libs;
duke@435 225 while (lib) {
duke@435 226 if (lib->symtab && addr >= lib->base) {
duke@435 227 res = nearest_symbol(lib->symtab, addr - lib->base, poffset);
duke@435 228 if (res) return res;
duke@435 229 }
duke@435 230 lib = lib->next;
duke@435 231 }
duke@435 232 return NULL;
duke@435 233 }
duke@435 234
duke@435 235 // add a thread to ps_prochandle
duke@435 236 thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
duke@435 237 thread_info* newthr;
duke@435 238 if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) {
duke@435 239 print_debug("can't allocate memory for thread_info\n");
duke@435 240 return NULL;
duke@435 241 }
duke@435 242
duke@435 243 // initialize thread info
duke@435 244 newthr->pthread_id = pthread_id;
duke@435 245 newthr->lwp_id = lwp_id;
duke@435 246
duke@435 247 // add new thread to the list
duke@435 248 newthr->next = ph->threads;
duke@435 249 ph->threads = newthr;
duke@435 250 ph->num_threads++;
duke@435 251 return newthr;
duke@435 252 }
duke@435 253
duke@435 254
duke@435 255 // struct used for client data from thread_db callback
duke@435 256 struct thread_db_client_data {
duke@435 257 struct ps_prochandle* ph;
duke@435 258 thread_info_callback callback;
duke@435 259 };
duke@435 260
duke@435 261 // callback function for libthread_db
duke@435 262 static int thread_db_callback(const td_thrhandle_t *th_p, void *data) {
duke@435 263 struct thread_db_client_data* ptr = (struct thread_db_client_data*) data;
duke@435 264 td_thrinfo_t ti;
duke@435 265 td_err_e err;
duke@435 266
duke@435 267 memset(&ti, 0, sizeof(ti));
duke@435 268 err = td_thr_get_info(th_p, &ti);
duke@435 269 if (err != TD_OK) {
duke@435 270 print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n");
duke@435 271 return err;
duke@435 272 }
duke@435 273
duke@435 274 print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);
duke@435 275
duke@435 276 if (ptr->callback(ptr->ph, ti.ti_tid, ti.ti_lid) != true)
duke@435 277 return TD_ERR;
duke@435 278
duke@435 279 return TD_OK;
duke@435 280 }
duke@435 281
duke@435 282 // read thread_info using libthread_db
duke@435 283 bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) {
duke@435 284 struct thread_db_client_data mydata;
duke@435 285 td_thragent_t* thread_agent = NULL;
duke@435 286 if (td_ta_new(ph, &thread_agent) != TD_OK) {
duke@435 287 print_debug("can't create libthread_db agent\n");
duke@435 288 return false;
duke@435 289 }
duke@435 290
duke@435 291 mydata.ph = ph;
duke@435 292 mydata.callback = cb;
duke@435 293
duke@435 294 // we use libthread_db iterator to iterate thru list of threads.
duke@435 295 if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata,
duke@435 296 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
duke@435 297 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) {
duke@435 298 td_ta_delete(thread_agent);
duke@435 299 return false;
duke@435 300 }
duke@435 301
duke@435 302 // delete thread agent
duke@435 303 td_ta_delete(thread_agent);
duke@435 304 return true;
duke@435 305 }
duke@435 306
duke@435 307
duke@435 308 // get number of threads
duke@435 309 int get_num_threads(struct ps_prochandle* ph) {
duke@435 310 return ph->num_threads;
duke@435 311 }
duke@435 312
duke@435 313 // get lwp_id of n'th thread
duke@435 314 lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
duke@435 315 int count = 0;
duke@435 316 thread_info* thr = ph->threads;
duke@435 317 while (thr) {
duke@435 318 if (count == index) {
duke@435 319 return thr->lwp_id;
duke@435 320 }
duke@435 321 count++;
duke@435 322 thr = thr->next;
duke@435 323 }
duke@435 324 return -1;
duke@435 325 }
duke@435 326
duke@435 327 // get regs for a given lwp
duke@435 328 bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct user_regs_struct* regs) {
duke@435 329 return ph->ops->get_lwp_regs(ph, lwp_id, regs);
duke@435 330 }
duke@435 331
duke@435 332 // get number of shared objects
duke@435 333 int get_num_libs(struct ps_prochandle* ph) {
duke@435 334 return ph->num_libs;
duke@435 335 }
duke@435 336
duke@435 337 // get name of n'th solib
duke@435 338 const char* get_lib_name(struct ps_prochandle* ph, int index) {
duke@435 339 int count = 0;
duke@435 340 lib_info* lib = ph->libs;
duke@435 341 while (lib) {
duke@435 342 if (count == index) {
duke@435 343 return lib->name;
duke@435 344 }
duke@435 345 count++;
duke@435 346 lib = lib->next;
duke@435 347 }
duke@435 348 return NULL;
duke@435 349 }
duke@435 350
duke@435 351 // get base address of a lib
duke@435 352 uintptr_t get_lib_base(struct ps_prochandle* ph, int index) {
duke@435 353 int count = 0;
duke@435 354 lib_info* lib = ph->libs;
duke@435 355 while (lib) {
duke@435 356 if (count == index) {
duke@435 357 return lib->base;
duke@435 358 }
duke@435 359 count++;
duke@435 360 lib = lib->next;
duke@435 361 }
duke@435 362 return (uintptr_t)NULL;
duke@435 363 }
duke@435 364
duke@435 365 bool find_lib(struct ps_prochandle* ph, const char *lib_name) {
duke@435 366 lib_info *p = ph->libs;
duke@435 367 while (p) {
duke@435 368 if (strcmp(p->name, lib_name) == 0) {
duke@435 369 return true;
duke@435 370 }
duke@435 371 p = p->next;
duke@435 372 }
duke@435 373 return false;
duke@435 374 }
duke@435 375
duke@435 376 //--------------------------------------------------------------------------
duke@435 377 // proc service functions
duke@435 378
duke@435 379 // get process id
duke@435 380 pid_t ps_getpid(struct ps_prochandle *ph) {
duke@435 381 return ph->pid;
duke@435 382 }
duke@435 383
duke@435 384 // ps_pglobal_lookup() looks up the symbol sym_name in the symbol table
duke@435 385 // of the load object object_name in the target process identified by ph.
duke@435 386 // It returns the symbol's value as an address in the target process in
duke@435 387 // *sym_addr.
duke@435 388
duke@435 389 ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,
duke@435 390 const char *sym_name, psaddr_t *sym_addr) {
duke@435 391 *sym_addr = (psaddr_t) lookup_symbol(ph, object_name, sym_name);
duke@435 392 return (*sym_addr ? PS_OK : PS_NOSYM);
duke@435 393 }
duke@435 394
duke@435 395 // read "size" bytes info "buf" from address "addr"
duke@435 396 ps_err_e ps_pdread(struct ps_prochandle *ph, psaddr_t addr,
duke@435 397 void *buf, size_t size) {
duke@435 398 return ph->ops->p_pread(ph, (uintptr_t) addr, buf, size)? PS_OK: PS_ERR;
duke@435 399 }
duke@435 400
duke@435 401 // write "size" bytes of data to debuggee at address "addr"
duke@435 402 ps_err_e ps_pdwrite(struct ps_prochandle *ph, psaddr_t addr,
duke@435 403 const void *buf, size_t size) {
duke@435 404 return ph->ops->p_pwrite(ph, (uintptr_t)addr, buf, size)? PS_OK: PS_ERR;
duke@435 405 }
duke@435 406
duke@435 407 // ------------------------------------------------------------------------
duke@435 408 // Functions below this point are not yet implemented. They are here only
duke@435 409 // to make the linker happy.
duke@435 410
duke@435 411 ps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) {
duke@435 412 print_debug("ps_lsetfpregs not implemented\n");
duke@435 413 return PS_OK;
duke@435 414 }
duke@435 415
duke@435 416 ps_err_e ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) {
duke@435 417 print_debug("ps_lsetregs not implemented\n");
duke@435 418 return PS_OK;
duke@435 419 }
duke@435 420
duke@435 421 ps_err_e ps_lgetfpregs(struct ps_prochandle *ph, lwpid_t lid, prfpregset_t *fpregs) {
duke@435 422 print_debug("ps_lgetfpregs not implemented\n");
duke@435 423 return PS_OK;
duke@435 424 }
duke@435 425
duke@435 426 ps_err_e ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) {
duke@435 427 print_debug("ps_lgetfpregs not implemented\n");
duke@435 428 return PS_OK;
duke@435 429 }
duke@435 430
duke@435 431 // new libthread_db of NPTL seem to require this symbol
duke@435 432 ps_err_e ps_get_thread_area() {
duke@435 433 print_debug("ps_get_thread_area not implemented\n");
duke@435 434 return PS_OK;
duke@435 435 }

mercurial