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