agent/src/os/bsd/libproc_impl.c

Sun, 25 Sep 2011 16:03:29 -0700

author
never
date
Sun, 25 Sep 2011 16:03:29 -0700
changeset 3156
f08d439fab8c
child 4599
2394a89e89f4
permissions
-rw-r--r--

7089790: integrate bsd-port changes
Reviewed-by: kvn, twisti, jrose
Contributed-by: Kurt Miller <kurt@intricatesoftware.com>, Greg Lewis <glewis@eyesbeyond.com>, Jung-uk Kim <jkim@freebsd.org>, Christos Zoulas <christos@zoulas.com>, Landon Fuller <landonf@plausible.coop>, The FreeBSD Foundation <board@freebsdfoundation.org>, Michael Franz <mvfranz@gmail.com>, Roger Hoover <rhoover@apple.com>, Alexander Strange <astrange@apple.com>

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

mercurial