< prev index next >

src/jdk.hotspot.agent/linux/native/libsaproc/libproc_impl.c

Print this page


   1 /*
   2  * Copyright (c) 2003, 2018, 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, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * 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 #define SA_ALTROOT "SA_ALTROOT"
  33 
  34 int pathmap_open(const char* name) {
  35   static const char *alt_root = NULL;
  36   static int alt_root_initialized = 0;
  37 
  38   int fd;
  39   char alt_path[PATH_MAX + 1], *alt_path_end;
  40   const char *s;
  41   int free_space;
  42 
  43   if (!alt_root_initialized) {
  44     alt_root_initialized = -1;
  45     alt_root = getenv(SA_ALTROOT);
  46   }
  47 
  48   if (alt_root == NULL) {
  49     return open(name, O_RDONLY);
  50   }


  99    }
 100 }
 101 
 102 void print_error(const char* format,...) {
 103   va_list alist;
 104   va_start(alist, format);
 105   fputs("ERROR: ", stderr);
 106   vfprintf(stderr, format, alist);
 107   va_end(alist);
 108 }
 109 
 110 bool is_debug() {
 111    return _libsaproc_debug;
 112 }
 113 
 114 // initialize libproc
 115 JNIEXPORT bool JNICALL
 116 init_libproc(bool debug) {
 117    // init debug mode
 118    _libsaproc_debug = debug;
 119 
 120    // initialize the thread_db library
 121    if (td_init() != TD_OK) {
 122      print_debug("libthread_db's td_init failed\n");
 123      return false;
 124    }
 125 
 126    return true;
 127 }
 128 
 129 static void destroy_lib_info(struct ps_prochandle* ph) {
 130    lib_info* lib = ph->libs;
 131    while (lib) {
 132      lib_info *next = lib->next;
 133      if (lib->symtab) {
 134         destroy_symtab(lib->symtab);
 135      }
 136      free(lib);
 137      lib = next;
 138    }
 139 }
 140 
 141 static void destroy_thread_info(struct ps_prochandle* ph) {
 142    thread_info* thr = ph->threads;
 143    while (thr) {
 144      thread_info *next = thr->next;
 145      free(thr);


 239    print_debug("lookup failed for symbol '%s' in obj '%s'\n",
 240                           sym_name, object_name);
 241    return (uintptr_t) NULL;
 242 }
 243 
 244 
 245 const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {
 246    const char* res = NULL;
 247    lib_info* lib = ph->libs;
 248    while (lib) {
 249       if (lib->symtab && addr >= lib->base) {
 250          res = nearest_symbol(lib->symtab, addr - lib->base, poffset);
 251          if (res) return res;
 252       }
 253       lib = lib->next;
 254    }
 255    return NULL;
 256 }
 257 
 258 // add a thread to ps_prochandle
 259 thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
 260    thread_info* newthr;
 261    if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) {
 262       print_debug("can't allocate memory for thread_info\n");
 263       return NULL;
 264    }
 265 
 266    // initialize thread info
 267    newthr->pthread_id = pthread_id;
 268    newthr->lwp_id = lwp_id;
 269 
 270    // add new thread to the list
 271    newthr->next = ph->threads;
 272    ph->threads = newthr;
 273    ph->num_threads++;
 274    return newthr;
 275 }
 276 
 277 void delete_thread_info(struct ps_prochandle* ph, thread_info* thr_to_be_removed) {
 278     thread_info* current_thr = ph->threads;
 279 
 280     if (thr_to_be_removed == ph->threads) {
 281       ph->threads = ph->threads->next;
 282     } else {
 283       thread_info* previous_thr;
 284       while (current_thr && current_thr != thr_to_be_removed) {
 285         previous_thr = current_thr;
 286         current_thr = current_thr->next;
 287       }
 288       if (current_thr == NULL) {
 289         print_error("Could not find the thread to be removed");
 290         return;
 291       }
 292       previous_thr->next = current_thr->next;
 293     }
 294     ph->num_threads--;
 295     free(current_thr);
 296 }
 297 
 298 // struct used for client data from thread_db callback
 299 struct thread_db_client_data {
 300    struct ps_prochandle* ph;
 301    thread_info_callback callback;
 302 };
 303 
 304 // callback function for libthread_db
 305 static int thread_db_callback(const td_thrhandle_t *th_p, void *data) {
 306   struct thread_db_client_data* ptr = (struct thread_db_client_data*) data;
 307   td_thrinfo_t ti;
 308   td_err_e err;
 309 
 310   memset(&ti, 0, sizeof(ti));
 311   err = td_thr_get_info(th_p, &ti);
 312   if (err != TD_OK) {
 313     print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n");
 314     return err;
 315   }
 316 
 317   print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);
 318 
 319   if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) {
 320     print_debug("Skipping pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);
 321     return TD_OK;
 322   }
 323 
 324   if (ptr->callback(ptr->ph, ti.ti_tid, ti.ti_lid) != true)
 325     return TD_ERR;
 326 
 327   return TD_OK;
 328 }
 329 
 330 // read thread_info using libthread_db
 331 bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) {
 332   struct thread_db_client_data mydata;
 333   td_thragent_t* thread_agent = NULL;
 334   if (td_ta_new(ph, &thread_agent) != TD_OK) {
 335      print_debug("can't create libthread_db agent\n");
 336      return false;
 337   }
 338 
 339   mydata.ph = ph;
 340   mydata.callback = cb;
 341 
 342   // we use libthread_db iterator to iterate thru list of threads.
 343   if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata,
 344                  TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
 345                  TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) {
 346      td_ta_delete(thread_agent);
 347      return false;
 348   }
 349 
 350   // delete thread agent
 351   td_ta_delete(thread_agent);
 352   return true;
 353 }
 354 
 355 
 356 // get number of threads
 357 int get_num_threads(struct ps_prochandle* ph) {
 358    return ph->num_threads;
 359 }
 360 
 361 // get lwp_id of n'th thread
 362 lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
 363    int count = 0;
 364    thread_info* thr = ph->threads;
 365    while (thr) {
 366       if (count == index) {
 367          return thr->lwp_id;
 368       }
 369       count++;
 370       thr = thr->next;
 371    }
 372    return -1;
 373 }
 374 
 375 // get regs for a given lwp


 467 }
 468 
 469 JNIEXPORT ps_err_e JNICALL
 470 ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) {
 471   print_debug("ps_lsetregs not implemented\n");
 472   return PS_OK;
 473 }
 474 
 475 JNIEXPORT ps_err_e  JNICALL
 476 ps_lgetfpregs(struct  ps_prochandle  *ph,  lwpid_t lid, prfpregset_t *fpregs) {
 477   print_debug("ps_lgetfpregs not implemented\n");
 478   return PS_OK;
 479 }
 480 
 481 JNIEXPORT ps_err_e JNICALL
 482 ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) {
 483   print_debug("ps_lgetfpregs not implemented\n");
 484   return PS_OK;
 485 }
 486 
 487 // new libthread_db of NPTL seem to require this symbol
 488 JNIEXPORT ps_err_e JNICALL
 489 ps_get_thread_area() {
 490   print_debug("ps_get_thread_area not implemented\n");
 491   return PS_OK;
 492 }
   1 /*
   2  * Copyright (c) 2003, 2019, 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, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * 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 <sys/procfs.h>
  30 #include "libproc_impl.h"
  31 #include "proc_service.h"
  32 
  33 #define SA_ALTROOT "SA_ALTROOT"
  34 
  35 int pathmap_open(const char* name) {
  36   static const char *alt_root = NULL;
  37   static int alt_root_initialized = 0;
  38 
  39   int fd;
  40   char alt_path[PATH_MAX + 1], *alt_path_end;
  41   const char *s;
  42   int free_space;
  43 
  44   if (!alt_root_initialized) {
  45     alt_root_initialized = -1;
  46     alt_root = getenv(SA_ALTROOT);
  47   }
  48 
  49   if (alt_root == NULL) {
  50     return open(name, O_RDONLY);
  51   }


 100    }
 101 }
 102 
 103 void print_error(const char* format,...) {
 104   va_list alist;
 105   va_start(alist, format);
 106   fputs("ERROR: ", stderr);
 107   vfprintf(stderr, format, alist);
 108   va_end(alist);
 109 }
 110 
 111 bool is_debug() {
 112    return _libsaproc_debug;
 113 }
 114 
 115 // initialize libproc
 116 JNIEXPORT bool JNICALL
 117 init_libproc(bool debug) {
 118    // init debug mode
 119    _libsaproc_debug = debug;







 120    return true;
 121 }
 122 
 123 static void destroy_lib_info(struct ps_prochandle* ph) {
 124    lib_info* lib = ph->libs;
 125    while (lib) {
 126      lib_info *next = lib->next;
 127      if (lib->symtab) {
 128         destroy_symtab(lib->symtab);
 129      }
 130      free(lib);
 131      lib = next;
 132    }
 133 }
 134 
 135 static void destroy_thread_info(struct ps_prochandle* ph) {
 136    thread_info* thr = ph->threads;
 137    while (thr) {
 138      thread_info *next = thr->next;
 139      free(thr);


 233    print_debug("lookup failed for symbol '%s' in obj '%s'\n",
 234                           sym_name, object_name);
 235    return (uintptr_t) NULL;
 236 }
 237 
 238 
 239 const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {
 240    const char* res = NULL;
 241    lib_info* lib = ph->libs;
 242    while (lib) {
 243       if (lib->symtab && addr >= lib->base) {
 244          res = nearest_symbol(lib->symtab, addr - lib->base, poffset);
 245          if (res) return res;
 246       }
 247       lib = lib->next;
 248    }
 249    return NULL;
 250 }
 251 
 252 // add a thread to ps_prochandle
 253 thread_info* add_thread_info(struct ps_prochandle* ph, lwpid_t lwp_id) {
 254    thread_info* newthr;
 255    if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) {
 256       print_debug("can't allocate memory for thread_info\n");
 257       return NULL;
 258    }
 259 
 260    // initialize thread info

 261    newthr->lwp_id = lwp_id;
 262 
 263    // add new thread to the list
 264    newthr->next = ph->threads;
 265    ph->threads = newthr;
 266    ph->num_threads++;
 267    return newthr;
 268 }
 269 
 270 void delete_thread_info(struct ps_prochandle* ph, thread_info* thr_to_be_removed) {
 271     thread_info* current_thr = ph->threads;
 272 
 273     if (thr_to_be_removed == ph->threads) {
 274       ph->threads = ph->threads->next;
 275     } else {
 276       thread_info* previous_thr;
 277       while (current_thr && current_thr != thr_to_be_removed) {
 278         previous_thr = current_thr;
 279         current_thr = current_thr->next;
 280       }
 281       if (current_thr == NULL) {
 282         print_error("Could not find the thread to be removed");
 283         return;
 284       }
 285       previous_thr->next = current_thr->next;
 286     }
 287     ph->num_threads--;
 288     free(current_thr);
 289 }
 290 


























































 291 // get number of threads
 292 int get_num_threads(struct ps_prochandle* ph) {
 293    return ph->num_threads;
 294 }
 295 
 296 // get lwp_id of n'th thread
 297 lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
 298    int count = 0;
 299    thread_info* thr = ph->threads;
 300    while (thr) {
 301       if (count == index) {
 302          return thr->lwp_id;
 303       }
 304       count++;
 305       thr = thr->next;
 306    }
 307    return -1;
 308 }
 309 
 310 // get regs for a given lwp


 402 }
 403 
 404 JNIEXPORT ps_err_e JNICALL
 405 ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) {
 406   print_debug("ps_lsetregs not implemented\n");
 407   return PS_OK;
 408 }
 409 
 410 JNIEXPORT ps_err_e  JNICALL
 411 ps_lgetfpregs(struct  ps_prochandle  *ph,  lwpid_t lid, prfpregset_t *fpregs) {
 412   print_debug("ps_lgetfpregs not implemented\n");
 413   return PS_OK;
 414 }
 415 
 416 JNIEXPORT ps_err_e JNICALL
 417 ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) {
 418   print_debug("ps_lgetfpregs not implemented\n");
 419   return PS_OK;
 420 }
 421 






< prev index next >