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