agent/src/os/linux/libproc_impl.c

Wed, 14 Jan 2009 19:45:01 -0800

author
swamyv
date
Wed, 14 Jan 2009 19:45:01 -0800
changeset 964
8db2b3e46c38
parent 435
a61af66fc99e
child 1729
7de45b5044c3
permissions
-rw-r--r--

6786948: SA on core file fails on solaris-amd64 if vm started with -XX:+StartAttachListener
Reviewed-by: jjh, dcubed

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

mercurial