Print this page
    
3946 ::gcore
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
    
      
        | Split | Close | 
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/lib/libproc/common/Pservice.c
          +++ new/usr/src/lib/libproc/common/Pservice.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  
    | ↓ open down ↓ | 14 lines elided | ↑ open up ↑ | 
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
       25 +/*
       26 + * Copyright (c) 2013 by Delphix. All rights reserved.
       27 + */
  25   28  
  26      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  27      -
  28   29  #include <stdarg.h>
  29   30  #include <string.h>
  30   31  #include "Pcontrol.h"
  31   32  
  32   33  /*
  33   34   * This file implements the process services declared in <proc_service.h>.
  34   35   * This enables libproc to be used in conjunction with libc_db and
  35   36   * librtld_db.  As most of these facilities are already provided by
  36   37   * (more elegant) interfaces in <libproc.h>, we can just call those.
  37   38   *
  38   39   * NOTE: We explicitly do *not* implement the functions ps_kill() and
  39   40   * ps_lrolltoaddr() in this library.  The very existence of these functions
  40   41   * causes libc_db to create an "agent thread" in the target process.
  41   42   * The only way to turn off this behavior is to omit these functions.
  42   43   */
  43   44  
  44   45  #pragma weak ps_pdread = ps_pread
  45   46  #pragma weak ps_ptread = ps_pread
  46   47  #pragma weak ps_pdwrite = ps_pwrite
  47   48  #pragma weak ps_ptwrite = ps_pwrite
  48   49  
  
    | ↓ open down ↓ | 11 lines elided | ↑ open up ↑ | 
  49   50  ps_err_e
  50   51  ps_pdmodel(struct ps_prochandle *P, int *modelp)
  51   52  {
  52   53          *modelp = P->status.pr_dmodel;
  53   54          return (PS_OK);
  54   55  }
  55   56  
  56   57  ps_err_e
  57   58  ps_pread(struct ps_prochandle *P, psaddr_t addr, void *buf, size_t size)
  58   59  {
  59      -        if (P->ops->p_pread(P, buf, size, addr) != size)
       60 +        if (P->ops.pop_pread(P, buf, size, addr, P->data) != size)
  60   61                  return (PS_BADADDR);
  61   62          return (PS_OK);
  62   63  }
  63   64  
  64   65  ps_err_e
  65   66  ps_pwrite(struct ps_prochandle *P, psaddr_t addr, const void *buf, size_t size)
  66   67  {
  67      -        if (P->ops->p_pwrite(P, buf, size, addr) != size)
       68 +        if (P->ops.pop_pwrite(P, buf, size, addr, P->data) != size)
  68   69                  return (PS_BADADDR);
  69   70          return (PS_OK);
  70   71  }
  71   72  
  72   73  /*
  73   74   * libc_db calls matched pairs of ps_pstop()/ps_pcontinue()
  74   75   * in the belief that the client may have left the process
  75   76   * running while calling in to the libc_db interfaces.
  76   77   *
  77   78   * We interpret the meaning of these functions to be an inquiry
  78   79   * as to whether the process is stopped, not an action to be
  79   80   * performed to make it stopped.  For similar reasons, we also
  80   81   * return PS_OK for core files in order to allow libc_db to
  81   82   * operate on these as well.
  82   83   */
  83   84  ps_err_e
  84   85  ps_pstop(struct ps_prochandle *P)
  85   86  {
  86   87          if (P->state != PS_STOP && P->state != PS_DEAD)
  87   88                  return (PS_ERR);
  88   89          return (PS_OK);
  89   90  }
  90   91  
  91   92  ps_err_e
  92   93  ps_pcontinue(struct ps_prochandle *P)
  93   94  {
  94   95          if (P->state != PS_STOP && P->state != PS_DEAD)
  95   96                  return (PS_ERR);
  96   97          return (PS_OK);
  97   98  }
  98   99  
  99  100  /*
 100  101   * ps_lstop() and ps_lcontinue() are not called by any code in libc_db
 101  102   * or librtld_db.  We make them behave like ps_pstop() and ps_pcontinue().
 102  103   */
 103  104  /* ARGSUSED1 */
 104  105  ps_err_e
 105  106  ps_lstop(struct ps_prochandle *P, lwpid_t lwpid)
 106  107  {
 107  108          if (P->state != PS_STOP && P->state != PS_DEAD)
 108  109                  return (PS_ERR);
 109  110          return (PS_OK);
 110  111  }
 111  112  
 112  113  /* ARGSUSED1 */
 113  114  ps_err_e
 114  115  ps_lcontinue(struct ps_prochandle *P, lwpid_t lwpid)
 115  116  {
 116  117          if (P->state != PS_STOP && P->state != PS_DEAD)
 117  118                  return (PS_ERR);
 118  119          return (PS_OK);
 119  120  }
 120  121  
 121  122  ps_err_e
 122  123  ps_lgetregs(struct ps_prochandle *P, lwpid_t lwpid, prgregset_t regs)
 123  124  {
 124  125          if (P->state != PS_STOP && P->state != PS_DEAD)
 125  126                  return (PS_ERR);
 126  127  
 127  128          if (Plwp_getregs(P, lwpid, regs) == 0)
 128  129                  return (PS_OK);
 129  130  
 130  131          return (PS_BADLID);
 131  132  }
 132  133  
 133  134  ps_err_e
 134  135  ps_lsetregs(struct ps_prochandle *P, lwpid_t lwpid, const prgregset_t regs)
 135  136  {
 136  137          if (P->state != PS_STOP)
 137  138                  return (PS_ERR);
 138  139  
 139  140          if (Plwp_setregs(P, lwpid, regs) == 0)
 140  141                  return (PS_OK);
 141  142  
 142  143          return (PS_BADLID);
 143  144  }
 144  145  
 145  146  ps_err_e
 146  147  ps_lgetfpregs(struct ps_prochandle *P, lwpid_t lwpid, prfpregset_t *regs)
 147  148  {
 148  149          if (P->state != PS_STOP && P->state != PS_DEAD)
 149  150                  return (PS_ERR);
 150  151  
 151  152          if (Plwp_getfpregs(P, lwpid, regs) == 0)
 152  153                  return (PS_OK);
 153  154  
 154  155          return (PS_BADLID);
 155  156  }
 156  157  
 157  158  ps_err_e
 158  159  ps_lsetfpregs(struct ps_prochandle *P, lwpid_t lwpid, const prfpregset_t *regs)
 159  160  {
 160  161          if (P->state != PS_STOP)
 161  162                  return (PS_ERR);
 162  163  
 163  164          if (Plwp_setfpregs(P, lwpid, regs) == 0)
 164  165                  return (PS_OK);
 165  166  
 166  167          return (PS_BADLID);
 167  168  }
 168  169  
 169  170  #if defined(sparc) || defined(__sparc)
 170  171  
 171  172  ps_err_e
 172  173  ps_lgetxregsize(struct ps_prochandle *P, lwpid_t lwpid, int *xrsize)
 173  174  {
 174  175          char fname[PATH_MAX];
 175  176          struct stat statb;
 176  177  
 177  178          if (P->state == PS_DEAD) {
 178  179                  lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
 179  180                  uint_t i;
 180  181  
 181  182                  for (i = 0; i < P->core->core_nlwp; i++, lwp = list_next(lwp)) {
 182  183                          if (lwp->lwp_id == lwpid) {
 183  184                                  if (lwp->lwp_xregs != NULL)
 184  185                                          *xrsize = sizeof (prxregset_t);
 185  186                                  else
 186  187                                          *xrsize = 0;
 187  188                                  return (PS_OK);
 188  189                          }
 189  190                  }
 190  191  
 191  192                  return (PS_BADLID);
 192  193          }
 193  194  
 194  195          (void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%d/xregs",
 195  196              procfs_path, (int)P->status.pr_pid, (int)lwpid);
 196  197  
 197  198          if (stat(fname, &statb) != 0)
 198  199                  return (PS_BADLID);
 199  200  
 200  201          *xrsize = (int)statb.st_size;
 201  202          return (PS_OK);
 202  203  }
 203  204  
 204  205  ps_err_e
 205  206  ps_lgetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
 206  207  {
 207  208          if (P->state != PS_STOP && P->state != PS_DEAD)
 208  209                  return (PS_ERR);
 209  210  
 210  211          /* LINTED - alignment */
 211  212          if (Plwp_getxregs(P, lwpid, (prxregset_t *)xregs) == 0)
 212  213                  return (PS_OK);
 213  214  
 214  215          return (PS_BADLID);
 215  216  }
 216  217  
 217  218  ps_err_e
 218  219  ps_lsetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
 219  220  {
 220  221          if (P->state != PS_STOP)
 221  222                  return (PS_ERR);
 222  223  
 223  224          /* LINTED - alignment */
 224  225          if (Plwp_setxregs(P, lwpid, (prxregset_t *)xregs) == 0)
 225  226                  return (PS_OK);
 226  227  
 227  228          return (PS_BADLID);
 228  229  }
 229  230  
 230  231  #endif  /* sparc */
 231  232  
 232  233  #if defined(__i386) || defined(__amd64)
 233  234  
 234  235  ps_err_e
 235  236  ps_lgetLDT(struct ps_prochandle *P, lwpid_t lwpid, struct ssd *ldt)
 236  237  {
 237  238  #if defined(__amd64) && defined(_LP64)
 238  239          if (P->status.pr_dmodel != PR_MODEL_NATIVE) {
 239  240  #endif
 240  241          prgregset_t regs;
 241  242          struct ssd *ldtarray;
 242  243          ps_err_e error;
 243  244          uint_t gs;
 244  245          int nldt;
 245  246          int i;
 246  247  
 247  248          if (P->state != PS_STOP && P->state != PS_DEAD)
 248  249                  return (PS_ERR);
 249  250  
 250  251          /*
 251  252           * We need to get the ldt entry that matches the
 252  253           * value in the lwp's GS register.
 253  254           */
 254  255          if ((error = ps_lgetregs(P, lwpid, regs)) != PS_OK)
 255  256                  return (error);
 256  257  
 257  258          gs = regs[GS];
 258  259  
 259  260          if ((nldt = Pldt(P, NULL, 0)) <= 0 ||
 260  261              (ldtarray = malloc(nldt * sizeof (struct ssd))) == NULL)
 261  262                  return (PS_ERR);
 262  263          if ((nldt = Pldt(P, ldtarray, nldt)) <= 0) {
 263  264                  free(ldtarray);
 264  265                  return (PS_ERR);
 265  266          }
 266  267  
 267  268          for (i = 0; i < nldt; i++) {
 268  269                  if (gs == ldtarray[i].sel) {
 269  270                          *ldt = ldtarray[i];
 270  271                          break;
 271  272                  }
 272  273          }
 273  274          free(ldtarray);
 274  275  
 275  276          if (i < nldt)
 276  277                  return (PS_OK);
 277  278  #if defined(__amd64) && defined(_LP64)
 278  279          }
 279  280  #endif
 280  281  
 281  282          return (PS_ERR);
 282  283  }
 283  284  
 284  285  #endif  /* __i386 || __amd64 */
 285  286  
 286  287  /*
 287  288   * Libthread_db doesn't use this function currently, but librtld_db uses
 288  289   * it for its debugging output.  We turn this on via rd_log if our debugging
 289  290   * switch is on, and then echo the messages sent to ps_plog to stderr.
 290  291   */
 291  292  void
 292  293  ps_plog(const char *fmt, ...)
 293  294  {
 294  295          va_list ap;
 295  296  
 296  297          if (_libproc_debug && fmt != NULL && *fmt != '\0') {
 297  298                  va_start(ap, fmt);
 298  299                  (void) vfprintf(stderr, fmt, ap);
 299  300                  va_end(ap);
 300  301                  if (fmt[strlen(fmt) - 1] != '\n')
 301  302                          (void) fputc('\n', stderr);
 302  303          }
 303  304  }
 304  305  
 305  306  /*
 306  307   * Store a pointer to our internal copy of the aux vector at the address
 307  308   * specified by the caller.  It should not hold on to this data for too long.
 308  309   */
 309  310  ps_err_e
 310  311  ps_pauxv(struct ps_prochandle *P, const auxv_t **aux)
 311  312  {
 312  313          if (P->auxv == NULL)
 313  314                  Preadauxvec(P);
 314  315  
 315  316          if (P->auxv == NULL)
 316  317                  return (PS_ERR);
 317  318  
 318  319          *aux = (const auxv_t *)P->auxv;
 319  320          return (PS_OK);
 320  321  }
 321  322  
 322  323  ps_err_e
 323  324  ps_pbrandname(struct ps_prochandle *P, char *buf, size_t len)
 324  325  {
 325  326          return (Pbrandname(P, buf, len) ? PS_OK : PS_ERR);
 326  327  }
 327  328  
 328  329  /*
 329  330   * Search for a symbol by name and return the corresponding address.
 330  331   */
 331  332  ps_err_e
 332  333  ps_pglobal_lookup(struct ps_prochandle *P, const char *object_name,
 333  334          const char *sym_name, psaddr_t *sym_addr)
 334  335  {
 335  336          GElf_Sym sym;
 336  337  
 337  338          if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
 338  339                  dprintf("pglobal_lookup <%s> -> %p\n",
 339  340                      sym_name, (void *)(uintptr_t)sym.st_value);
 340  341                  *sym_addr = (psaddr_t)sym.st_value;
 341  342                  return (PS_OK);
 342  343          }
 343  344  
 344  345          return (PS_NOSYM);
 345  346  }
 346  347  
 347  348  /*
 348  349   * Search for a symbol by name and return the corresponding symbol
 349  350   * information.  If we're compiled _LP64, we just call Plookup_by_name
 350  351   * and return because ps_sym_t is defined to be an Elf64_Sym, which
 351  352   * is the same as a GElf_Sym.  In the _ILP32 case, we have to convert
 352  353   * Plookup_by_name's result back to a ps_sym_t (which is an Elf32_Sym).
 353  354   */
 354  355  ps_err_e
 355  356  ps_pglobal_sym(struct ps_prochandle *P, const char *object_name,
 356  357          const char *sym_name, ps_sym_t *symp)
 357  358  {
 358  359  #if defined(_ILP32)
 359  360          GElf_Sym sym;
 360  361  
 361  362          if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
 362  363                  symp->st_name = (Elf32_Word)sym.st_name;
 363  364                  symp->st_value = (Elf32_Addr)sym.st_value;
 364  365                  symp->st_size = (Elf32_Word)sym.st_size;
 365  366                  symp->st_info = ELF32_ST_INFO(
 366  367                      GELF_ST_BIND(sym.st_info), GELF_ST_TYPE(sym.st_info));
 367  368                  symp->st_other = sym.st_other;
 368  369                  symp->st_shndx = sym.st_shndx;
 369  370                  return (PS_OK);
 370  371          }
 371  372  
 372  373  #elif defined(_LP64)
 373  374          if (Plookup_by_name(P, object_name, sym_name, symp) == 0)
 374  375                  return (PS_OK);
 375  376  #endif
 376  377          return (PS_NOSYM);
 377  378  }
  
    | ↓ open down ↓ | 300 lines elided | ↑ open up ↑ | 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX