Print this page
    
OS-3280 need a way to specify the root of a native system in the lx brand
OS-3279 lx brand should allow delegated datasets
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/cmd/ptools/pmap/pmap.c
          +++ new/usr/src/cmd/ptools/pmap/pmap.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  /*
  23   23   * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
       25 + * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
  25   26   */
  26   27  
  27   28  #include <stdio.h>
  28   29  #include <stdio_ext.h>
  29   30  #include <stdlib.h>
  30   31  #include <unistd.h>
  31   32  #include <ctype.h>
  32   33  #include <fcntl.h>
  33   34  #include <string.h>
  34   35  #include <dirent.h>
  35   36  #include <limits.h>
  36   37  #include <link.h>
  37   38  #include <libelf.h>
  38   39  #include <sys/types.h>
  39   40  #include <signal.h>
  40   41  #include <sys/stat.h>
  41   42  #include <sys/mkdev.h>
  42   43  #include <sys/mman.h>
  43   44  #include <sys/lgrp_user.h>
  44   45  #include <libproc.h>
       46 +#include "ptools_common.h"
  45   47  
  46   48  #include "pmap_common.h"
  47   49  
  48   50  #define KILOBYTE        1024
  49   51  #define MEGABYTE        (KILOBYTE * KILOBYTE)
  50   52  #define GIGABYTE        (KILOBYTE * KILOBYTE * KILOBYTE)
  51   53  
  52   54  /*
  53   55   * Round up the value to the nearest kilobyte
  54   56   */
  55   57  #define ROUNDUP_KB(x)   (((x) + (KILOBYTE - 1)) / KILOBYTE)
  56   58  
  57   59  /*
  58   60   * The alignment should be a power of 2.
  59   61   */
  60   62  #define P2ALIGN(x, align)               ((x) & -(align))
  61   63  
  62   64  #define INVALID_ADDRESS                 (uintptr_t)(-1)
  63   65  
  64   66  struct totals {
  65   67          ulong_t total_size;
  66   68          ulong_t total_swap;
  67   69          ulong_t total_rss;
  68   70          ulong_t total_anon;
  69   71          ulong_t total_locked;
  70   72  };
  71   73  
  72   74  /*
  73   75   * -L option requires per-page information. The information is presented in an
  74   76   * array of page_descr structures.
  75   77   */
  76   78  typedef struct page_descr {
  77   79          uintptr_t       pd_start;       /* start address of a page */
  78   80          size_t          pd_pagesize;    /* page size in bytes */
  79   81          lgrp_id_t       pd_lgrp;        /* lgroup of memory backing the page */
  80   82          int             pd_valid;       /* valid page description if non-zero */
  81   83  } page_descr_t;
  82   84  
  83   85  /*
  84   86   * Per-page information for a memory chunk.
  85   87   * The meminfo(2) system call accepts up to MAX_MEMINFO_CNT pages at once.
  86   88   * When we need to scan larger ranges we divide them in MAX_MEMINFO_CNT sized
  87   89   * chunks. The chunk information is stored in the memory_chunk structure.
  88   90   */
  89   91  typedef struct memory_chunk {
  90   92          page_descr_t    page_info[MAX_MEMINFO_CNT];
  91   93          uintptr_t       end_addr;
  92   94          uintptr_t       chunk_start;    /* Starting address */
  93   95          uintptr_t       chunk_end;      /* chunk_end is always <= end_addr */
  94   96          size_t          page_size;
  95   97          int             page_index;     /* Current page */
  96   98          int             page_count;     /* Number of pages */
  97   99  } memory_chunk_t;
  98  100  
  99  101  static volatile int interrupt;
 100  102  
 101  103  typedef int proc_xmap_f(void *, const prxmap_t *, const char *, int, int);
 102  104  
 103  105  static  int     xmapping_iter(struct ps_prochandle *, proc_xmap_f *, void *,
 104  106      int);
 105  107  static  int     rmapping_iter(struct ps_prochandle *, proc_map_f *, void *);
 106  108  
 107  109  static  int     look_map(void *, const prmap_t *, const char *);
 108  110  static  int     look_smap(void *, const prxmap_t *, const char *, int, int);
 109  111  static  int     look_xmap(void *, const prxmap_t *, const char *, int, int);
 110  112  static  int     look_xmap_nopgsz(void *, const prxmap_t *, const char *,
 111  113      int, int);
 112  114  
 113  115  static int gather_map(void *, const prmap_t *, const char *);
 114  116  static int gather_xmap(void *, const prxmap_t *, const char *, int, int);
 115  117  static int iter_map(proc_map_f *, void *);
 116  118  static int iter_xmap(proc_xmap_f *, void *);
 117  119  static int parse_addr_range(char *, uintptr_t *, uintptr_t *);
 118  120  static void mem_chunk_init(memory_chunk_t *, uintptr_t, size_t);
 119  121  
 120  122  static  int     perr(char *);
 121  123  static  void    printK(long, int);
 122  124  static  char    *mflags(uint_t);
 123  125  
 124  126  static size_t get_contiguous_region(memory_chunk_t *, uintptr_t,
 125  127      uintptr_t, size_t, lgrp_id_t *);
 126  128  static void     mem_chunk_get(memory_chunk_t *, uintptr_t);
 127  129  static lgrp_id_t addr_to_lgrp(memory_chunk_t *, uintptr_t, size_t *);
 128  130  static char     *lgrp2str(lgrp_id_t);
 129  131  
 130  132  static int      address_in_range(uintptr_t, uintptr_t, size_t);
 131  133  static size_t   adjust_addr_range(uintptr_t, uintptr_t, size_t,
 132  134      uintptr_t *, uintptr_t *);
 133  135  
 134  136  static  int     lflag = 0;
 135  137  static  int     Lflag = 0;
 136  138  static  int     aflag = 0;
 137  139  
 138  140  /*
 139  141   * The -A address range is represented as a pair of addresses
 140  142   * <start_addr, end_addr>. Either one of these may be unspecified (set to
 141  143   * INVALID_ADDRESS). If both are unspecified, no address range restrictions are
 142  144   * in place.
 143  145   */
 144  146  static  uintptr_t start_addr = INVALID_ADDRESS;
 145  147  static  uintptr_t end_addr = INVALID_ADDRESS;
 146  148  
 147  149  static  int     addr_width, size_width;
 148  150  static  char    *command;
 149  151  static  char    *procname;
 150  152  static  struct ps_prochandle *Pr;
 151  153  
 152  154  static void intr(int);
 153  155  
 154  156  typedef struct {
 155  157          prxmap_t        md_xmap;
 156  158          prmap_t         md_map;
 157  159          char            *md_objname;
 158  160          boolean_t       md_last;
 159  161          int             md_doswap;
 160  162  } mapdata_t;
 161  163  
 162  164  static  mapdata_t       *maps;
 163  165  static  int             map_count;
 164  166  static  int             map_alloc;
 165  167  
 166  168  static  lwpstack_t *stacks = NULL;
 167  169  static  uint_t  nstacks = 0;
 168  170  
 169  171  #define MAX_TRIES       5
 170  172  
 171  173  static int
 172  174  getstack(void *data, const lwpstatus_t *lsp)
 173  175  {
 174  176          int *np = (int *)data;
 175  177  
 176  178          if (Plwp_alt_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
 177  179                  stacks[*np].lwps_stack.ss_flags |= SS_ONSTACK;
 178  180                  stacks[*np].lwps_lwpid = lsp->pr_lwpid;
 179  181                  (*np)++;
 180  182          }
 181  183  
 182  184          if (Plwp_main_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
 183  185                  stacks[*np].lwps_lwpid = lsp->pr_lwpid;
 184  186                  (*np)++;
 185  187          }
 186  188  
 187  189          return (0);
 188  190  }
 189  191  
 190  192  int
 191  193  main(int argc, char **argv)
  
    | 
      ↓ open down ↓ | 
    137 lines elided | 
    
      ↑ open up ↑ | 
  
 192  194  {
 193  195          int rflag = 0, sflag = 0, xflag = 0, Fflag = 0;
 194  196          int errflg = 0, Sflag = 0;
 195  197          int rc = 0;
 196  198          int opt;
 197  199          const char *bar8 = "-------";
 198  200          const char *bar16 = "----------";
 199  201          const char *bar;
 200  202          struct rlimit rlim;
 201  203          struct stat64 statbuf;
 202      -        char buf[128];
      204 +        char buf[PATH_MAX];
 203  205          int mapfd;
 204  206          int prg_gflags = PGRAB_RDONLY;
 205  207          int prr_flags = 0;
 206  208          boolean_t use_agent_lwp = B_FALSE;
 207  209  
 208  210          if ((command = strrchr(argv[0], '/')) != NULL)
 209  211                  command++;
 210  212          else
 211  213                  command = argv[0];
 212  214  
 213  215          while ((opt = getopt(argc, argv, "arsxSlLFA:")) != EOF) {
 214  216                  switch (opt) {
 215  217                  case 'a':               /* include shared mappings in -[xS] */
 216  218                          aflag = 1;
 217  219                          break;
 218  220                  case 'r':               /* show reserved mappings */
 219  221                          rflag = 1;
 220  222                          break;
 221  223                  case 's':               /* show hardware page sizes */
 222  224                          sflag = 1;
 223  225                          break;
 224  226                  case 'S':               /* show swap reservations */
 225  227                          Sflag = 1;
 226  228                          break;
 227  229                  case 'x':               /* show extended mappings */
 228  230                          xflag = 1;
 229  231                          break;
 230  232                  case 'l':               /* show unresolved link map names */
 231  233                          lflag = 1;
 232  234                          break;
 233  235                  case 'L':               /* show lgroup information */
 234  236                          Lflag = 1;
 235  237                          use_agent_lwp = B_TRUE;
 236  238                          break;
 237  239                  case 'F':               /* force grabbing (no O_EXCL) */
 238  240                          Fflag = PGRAB_FORCE;
 239  241                          break;
 240  242                  case 'A':
 241  243                          if (parse_addr_range(optarg, &start_addr, &end_addr)
 242  244                              != 0)
 243  245                                  errflg++;
 244  246                          break;
 245  247                  default:
 246  248                          errflg = 1;
 247  249                          break;
 248  250                  }
 249  251          }
 250  252  
 251  253          argc -= optind;
 252  254          argv += optind;
 253  255  
 254  256          if ((Sflag && (xflag || rflag || sflag)) || (xflag && rflag) ||
 255  257              (aflag && (!xflag && !Sflag)) ||
 256  258              (Lflag && (xflag || Sflag))) {
 257  259                  errflg = 1;
 258  260          }
 259  261  
 260  262          if (errflg || argc <= 0) {
 261  263                  (void) fprintf(stderr,
 262  264                      "usage:\t%s [-rslF] [-A start[,end]] { pid | core } ...\n",
 263  265                      command);
 264  266                  (void) fprintf(stderr,
 265  267                      "\t\t(report process address maps)\n");
 266  268                  (void) fprintf(stderr,
 267  269                      "\t%s -L [-rslF] [-A start[,end]] pid ...\n", command);
 268  270                  (void) fprintf(stderr,
 269  271                      "\t\t(report process address maps lgroups mappings)\n");
 270  272                  (void) fprintf(stderr,
 271  273                      "\t%s -x [-aslF] [-A start[,end]] pid ...\n", command);
 272  274                  (void) fprintf(stderr,
 273  275                      "\t\t(show resident/anon/locked mapping details)\n");
 274  276                  (void) fprintf(stderr,
 275  277                      "\t%s -S [-alF] [-A start[,end]] { pid | core } ...\n",
 276  278                      command);
 277  279                  (void) fprintf(stderr,
 278  280                      "\t\t(show swap reservations)\n\n");
 279  281                  (void) fprintf(stderr,
 280  282                      "\t-a: include shared mappings in -[xS] summary\n");
 281  283                  (void) fprintf(stderr,
 282  284                      "\t-r: show reserved address maps\n");
 283  285                  (void) fprintf(stderr,
 284  286                      "\t-s: show hardware page sizes\n");
 285  287                  (void) fprintf(stderr,
 286  288                      "\t-l: show unresolved dynamic linker map names\n");
 287  289                  (void) fprintf(stderr,
 288  290                      "\t-F: force grabbing of the target process\n");
 289  291                  (void) fprintf(stderr,
 290  292                      "\t-L: show lgroup mappings\n");
 291  293                  (void) fprintf(stderr,
 292  294                      "\t-A start,end: limit output to the specified range\n");
 293  295                  return (2);
 294  296          }
 295  297  
 296  298          /*
 297  299           * Make sure we'll have enough file descriptors to handle a target
 298  300           * that has many many mappings.
 299  301           */
 300  302          if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
 301  303                  rlim.rlim_cur = rlim.rlim_max;
 302  304                  (void) setrlimit(RLIMIT_NOFILE, &rlim);
 303  305                  (void) enable_extended_FILE_stdio(-1, -1);
 304  306          }
 305  307  
 306  308          /*
 307  309           * The implementation of -L option creates an agent LWP in the target
 308  310           * process address space. The agent LWP issues meminfo(2) system calls
 309  311           * on behalf of the target process. If we are interrupted prematurely,
 310  312           * the target process remains in the stopped state with the agent still
 311  313           * attached to it. To prevent such situation we catch signals from
 312  314           * terminal and terminate gracefully.
 313  315           */
 314  316          if (use_agent_lwp) {
 315  317                  /*
 316  318                   * Buffer output to stdout, stderr while process is grabbed.
 317  319                   * Prevents infamous deadlocks due to pmap `pgrep xterm` and
 318  320                   * other variants.
 319  321                   */
 320  322                  (void) proc_initstdio();
 321  323  
 322  324                  prg_gflags = PGRAB_RETAIN | Fflag;
 323  325                  prr_flags = PRELEASE_RETAIN;
 324  326  
 325  327                  if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
 326  328                          (void) sigset(SIGHUP, intr);
 327  329                  if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
 328  330                          (void) sigset(SIGINT, intr);
 329  331                  if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
 330  332                          (void) sigset(SIGQUIT, intr);
 331  333                  (void) sigset(SIGPIPE, intr);
 332  334                  (void) sigset(SIGTERM, intr);
 333  335          }
 334  336  
 335  337          while (argc-- > 0) {
 336  338                  char *arg;
 337  339                  int gcode;
 338  340                  psinfo_t psinfo;
 339  341                  int tries = 0;
 340  342  
 341  343                  if (use_agent_lwp)
 342  344                          (void) proc_flushstdio();
 343  345  
 344  346                  if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
 345  347                      prg_gflags, &gcode)) == NULL) {
 346  348                          (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
 347  349                              command, arg, Pgrab_error(gcode));
 348  350                          rc++;
 349  351                          continue;
 350  352                  }
  
    | 
      ↓ open down ↓ | 
    138 lines elided | 
    
      ↑ open up ↑ | 
  
 351  353  
 352  354                  procname = arg;         /* for perr() */
 353  355  
 354  356                  addr_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 16 : 8;
 355  357                  size_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 11 : 8;
 356  358                  bar = addr_width == 8 ? bar8 : bar16;
 357  359                  (void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
 358  360                  proc_unctrl_psinfo(&psinfo);
 359  361  
 360  362                  if (Pstate(Pr) != PS_DEAD) {
 361      -                        (void) snprintf(buf, sizeof (buf),
      363 +                        (void) proc_snprintf(buf, sizeof (buf),
 362  364                              "/proc/%d/map", (int)psinfo.pr_pid);
 363  365                          if ((mapfd = open(buf, O_RDONLY)) < 0) {
 364  366                                  (void) fprintf(stderr, "%s: cannot "
 365  367                                      "examine %s: lost control of "
 366  368                                      "process\n", command, arg);
 367  369                                  rc++;
 368  370                                  Prelease(Pr, prr_flags);
 369  371                                  continue;
 370  372                          }
 371  373                  } else {
 372  374                          mapfd = -1;
 373  375                  }
 374  376  
 375  377  again:
 376  378                  map_count = 0;
 377  379  
 378  380                  if (Pstate(Pr) == PS_DEAD) {
 379  381                          (void) printf("core '%s' of %d:\t%.70s\n",
 380  382                              arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
 381  383  
 382  384                          if (rflag || sflag || xflag || Sflag || Lflag) {
 383  385                                  (void) printf("  -%c option is not compatible "
 384  386                                      "with core files\n", xflag ? 'x' :
 385  387                                      sflag ? 's' : rflag ? 'r' :
 386  388                                      Lflag ? 'L' : 'S');
 387  389                                  Prelease(Pr, prr_flags);
 388  390                                  rc++;
 389  391                                  continue;
 390  392                          }
 391  393  
 392  394                  } else {
 393  395                          (void) printf("%d:\t%.70s\n",
 394  396                              (int)psinfo.pr_pid, psinfo.pr_psargs);
 395  397                  }
 396  398  
 397  399                  if (!(Pstatus(Pr)->pr_flags & PR_ISSYS)) {
 398  400                          struct totals t;
 399  401  
 400  402                          /*
 401  403                           * Since we're grabbing the process readonly, we need
 402  404                           * to make sure the address space doesn't change during
 403  405                           * execution.
 404  406                           */
 405  407                          if (Pstate(Pr) != PS_DEAD) {
 406  408                                  if (tries++ == MAX_TRIES) {
 407  409                                          Prelease(Pr, prr_flags);
 408  410                                          (void) close(mapfd);
 409  411                                          (void) fprintf(stderr, "%s: cannot "
 410  412                                              "examine %s: address space is "
 411  413                                              "changing\n", command, arg);
 412  414                                          continue;
 413  415                                  }
 414  416  
 415  417                                  if (fstat64(mapfd, &statbuf) != 0) {
 416  418                                          Prelease(Pr, prr_flags);
 417  419                                          (void) close(mapfd);
 418  420                                          (void) fprintf(stderr, "%s: cannot "
 419  421                                              "examine %s: lost control of "
 420  422                                              "process\n", command, arg);
 421  423                                          continue;
 422  424                                  }
 423  425                          }
 424  426  
 425  427                          nstacks = psinfo.pr_nlwp * 2;
 426  428                          stacks = calloc(nstacks, sizeof (stacks[0]));
 427  429                          if (stacks != NULL) {
 428  430                                  int n = 0;
 429  431                                  (void) Plwp_iter(Pr, getstack, &n);
 430  432                                  qsort(stacks, nstacks, sizeof (stacks[0]),
 431  433                                      cmpstacks);
 432  434                          }
 433  435  
 434  436                          (void) memset(&t, 0, sizeof (t));
 435  437  
 436  438                          if (Pgetauxval(Pr, AT_BASE) != -1L &&
 437  439                              Prd_agent(Pr) == NULL) {
 438  440                                  (void) fprintf(stderr, "%s: warning: "
 439  441                                      "librtld_db failed to initialize; "
 440  442                                      "shared library information will not be "
 441  443                                      "available\n", command);
 442  444                          }
 443  445  
 444  446                          /*
 445  447                           * Gather data
 446  448                           */
 447  449                          if (xflag)
 448  450                                  rc += xmapping_iter(Pr, gather_xmap, NULL, 0);
 449  451                          else if (Sflag)
 450  452                                  rc += xmapping_iter(Pr, gather_xmap, NULL, 1);
 451  453                          else {
 452  454                                  if (rflag)
 453  455                                          rc += rmapping_iter(Pr, gather_map,
 454  456                                              NULL);
 455  457                                  else if (sflag)
 456  458                                          rc += xmapping_iter(Pr, gather_xmap,
 457  459                                              NULL, 0);
 458  460                                  else if (lflag)
 459  461                                          rc += Pmapping_iter(Pr,
 460  462                                              gather_map, NULL);
 461  463                                  else
 462  464                                          rc += Pmapping_iter_resolved(Pr,
 463  465                                              gather_map, NULL);
 464  466                          }
 465  467  
 466  468                          /*
 467  469                           * Ensure mappings are consistent.
 468  470                           */
 469  471                          if (Pstate(Pr) != PS_DEAD) {
 470  472                                  struct stat64 newbuf;
 471  473  
 472  474                                  if (fstat64(mapfd, &newbuf) != 0 ||
 473  475                                      memcmp(&newbuf.st_mtim, &statbuf.st_mtim,
 474  476                                      sizeof (newbuf.st_mtim)) != 0) {
 475  477                                          if (stacks != NULL) {
 476  478                                                  free(stacks);
 477  479                                                  stacks = NULL;
 478  480                                          }
 479  481                                          goto again;
 480  482                                  }
 481  483                          }
 482  484  
 483  485                          /*
 484  486                           * Display data.
 485  487                           */
 486  488                          if (xflag) {
 487  489                                  (void) printf("%*s%*s%*s%*s%*s "
 488  490                                      "%sMode   Mapped File\n",
 489  491                                      addr_width, "Address",
 490  492                                      size_width, "Kbytes",
 491  493                                      size_width, "RSS",
 492  494                                      size_width, "Anon",
 493  495                                      size_width, "Locked",
 494  496                                      sflag ? "Pgsz " : "");
 495  497  
 496  498                                  rc += iter_xmap(sflag ?  look_xmap :
 497  499                                      look_xmap_nopgsz, &t);
 498  500  
 499  501                                  (void) printf("%s%s %s %s %s %s\n",
 500  502                                      addr_width == 8 ? "-" : "------",
 501  503                                      bar, bar, bar, bar, bar);
 502  504  
 503  505                                  (void) printf("%stotal Kb", addr_width == 16 ?
 504  506                                      "        " : "");
 505  507  
 506  508                                  printK(t.total_size, size_width);
 507  509                                  printK(t.total_rss, size_width);
 508  510                                  printK(t.total_anon, size_width);
 509  511                                  printK(t.total_locked, size_width);
 510  512  
 511  513                                  (void) printf("\n");
 512  514  
 513  515                          } else if (Sflag) {
 514  516                                  (void) printf("%*s%*s%*s Mode"
 515  517                                      " Mapped File\n",
 516  518                                      addr_width, "Address",
 517  519                                      size_width, "Kbytes",
 518  520                                      size_width, "Swap");
 519  521  
 520  522                                  rc += iter_xmap(look_xmap_nopgsz, &t);
 521  523  
 522  524                                  (void) printf("%s%s %s %s\n",
 523  525                                      addr_width == 8 ? "-" : "------",
 524  526                                      bar, bar, bar);
 525  527  
 526  528                                  (void) printf("%stotal Kb", addr_width == 16 ?
 527  529                                      "        " : "");
 528  530  
 529  531                                  printK(t.total_size, size_width);
 530  532                                  printK(t.total_swap, size_width);
 531  533  
 532  534                                  (void) printf("\n");
 533  535  
 534  536                          } else {
 535  537  
 536  538                                  if (rflag) {
 537  539                                          rc += iter_map(look_map, &t);
 538  540                                  } else if (sflag) {
 539  541                                          if (Lflag) {
 540  542                                                  (void) printf("%*s %*s %4s"
 541  543                                                      " %-6s %s %s\n",
 542  544                                                      addr_width, "Address",
 543  545                                                      size_width,
 544  546                                                      "Bytes", "Pgsz", "Mode ",
 545  547                                                      "Lgrp", "Mapped File");
 546  548                                                  rc += iter_xmap(look_smap, &t);
 547  549                                          } else {
 548  550                                                  (void) printf("%*s %*s %4s"
 549  551                                                      " %-6s %s\n",
 550  552                                                      addr_width, "Address",
 551  553                                                      size_width,
 552  554                                                      "Bytes", "Pgsz", "Mode ",
 553  555                                                      "Mapped File");
 554  556                                                  rc += iter_xmap(look_smap, &t);
 555  557                                          }
 556  558                                  } else {
 557  559                                          rc += iter_map(look_map, &t);
 558  560                                  }
 559  561  
 560  562                                  (void) printf(" %stotal  %*luK\n",
 561  563                                      addr_width == 16 ?
 562  564                                      "        " : "",
 563  565                                      size_width, t.total_size);
 564  566                          }
 565  567  
 566  568                          if (stacks != NULL) {
 567  569                                  free(stacks);
 568  570                                  stacks = NULL;
 569  571                          }
 570  572  
 571  573                  }
 572  574  
 573  575                  Prelease(Pr, prr_flags);
 574  576                  if (mapfd != -1)
 575  577                          (void) close(mapfd);
 576  578          }
 577  579  
 578  580          if (use_agent_lwp)
 579  581                  (void) proc_finistdio();
 580  582  
 581  583          return (rc);
 582  584  }
  
    | 
      ↓ open down ↓ | 
    211 lines elided | 
    
      ↑ open up ↑ | 
  
 583  585  
 584  586  static int
 585  587  rmapping_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd)
 586  588  {
 587  589          char mapname[PATH_MAX];
 588  590          int mapfd, nmap, i, rc;
 589  591          struct stat st;
 590  592          prmap_t *prmapp, *pmp;
 591  593          ssize_t n;
 592  594  
 593      -        (void) snprintf(mapname, sizeof (mapname),
      595 +        (void) proc_snprintf(mapname, sizeof (mapname),
 594  596              "/proc/%d/rmap", (int)Pstatus(Pr)->pr_pid);
 595  597  
 596  598          if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
 597  599                  if (mapfd >= 0)
 598  600                          (void) close(mapfd);
 599  601                  return (perr(mapname));
 600  602          }
 601  603  
 602  604          nmap = st.st_size / sizeof (prmap_t);
 603  605          prmapp = malloc((nmap + 1) * sizeof (prmap_t));
 604  606  
 605  607          if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prmap_t), 0L)) < 0) {
 606  608                  (void) close(mapfd);
 607  609                  free(prmapp);
 608  610                  return (perr("read rmap"));
 609  611          }
 610  612  
 611  613          (void) close(mapfd);
 612  614          nmap = n / sizeof (prmap_t);
 613  615  
 614  616          for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
 615  617                  if ((rc = func(cd, pmp, NULL)) != 0) {
 616  618                          free(prmapp);
 617  619                          return (rc);
 618  620                  }
 619  621          }
 620  622  
 621  623          free(prmapp);
 622  624          return (0);
 623  625  }
  
    | 
      ↓ open down ↓ | 
    20 lines elided | 
    
      ↑ open up ↑ | 
  
 624  626  
 625  627  static int
 626  628  xmapping_iter(struct ps_prochandle *Pr, proc_xmap_f *func, void *cd, int doswap)
 627  629  {
 628  630          char mapname[PATH_MAX];
 629  631          int mapfd, nmap, i, rc;
 630  632          struct stat st;
 631  633          prxmap_t *prmapp, *pmp;
 632  634          ssize_t n;
 633  635  
 634      -        (void) snprintf(mapname, sizeof (mapname),
      636 +        (void) proc_snprintf(mapname, sizeof (mapname),
 635  637              "/proc/%d/xmap", (int)Pstatus(Pr)->pr_pid);
 636  638  
 637  639          if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
 638  640                  if (mapfd >= 0)
 639  641                          (void) close(mapfd);
 640  642                  return (perr(mapname));
 641  643          }
 642  644  
 643  645          nmap = st.st_size / sizeof (prxmap_t);
 644  646          nmap *= 2;
 645  647  again:
 646  648          prmapp = malloc((nmap + 1) * sizeof (prxmap_t));
 647  649  
 648  650          if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prxmap_t), 0)) < 0) {
 649  651                  (void) close(mapfd);
 650  652                  free(prmapp);
 651  653                  return (perr("read xmap"));
 652  654          }
 653  655  
 654  656          if (nmap < n / sizeof (prxmap_t)) {
 655  657                  free(prmapp);
 656  658                  nmap *= 2;
 657  659                  goto again;
 658  660          }
 659  661  
 660  662          (void) close(mapfd);
 661  663          nmap = n / sizeof (prxmap_t);
 662  664  
 663  665          for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
 664  666                  if ((rc = func(cd, pmp, NULL, i == nmap - 1, doswap)) != 0) {
 665  667                          free(prmapp);
 666  668                          return (rc);
 667  669                  }
 668  670          }
 669  671  
 670  672          /*
 671  673           * Mark the last element.
 672  674           */
 673  675          if (map_count > 0)
 674  676                  maps[map_count - 1].md_last = B_TRUE;
 675  677  
 676  678          free(prmapp);
 677  679          return (0);
 678  680  }
 679  681  
 680  682  /*ARGSUSED*/
 681  683  static int
 682  684  look_map(void *data, const prmap_t *pmp, const char *object_name)
 683  685  {
 684  686          struct totals *t = data;
 685  687          const pstatus_t *Psp = Pstatus(Pr);
 686  688          size_t size;
 687  689          char mname[PATH_MAX];
 688  690          char *lname = NULL;
 689  691          size_t  psz = pmp->pr_pagesize;
 690  692          uintptr_t vaddr = pmp->pr_vaddr;
 691  693          uintptr_t segment_end = vaddr + pmp->pr_size;
 692  694          lgrp_id_t lgrp;
 693  695          memory_chunk_t mchunk;
 694  696  
 695  697          /*
 696  698           * If the mapping is not anon or not part of the heap, make a name
 697  699           * for it.  We don't want to report the heap as a.out's data.
 698  700           */
 699  701          if (!(pmp->pr_mflags & MA_ANON) ||
 700  702              segment_end <= Psp->pr_brkbase ||
 701  703              pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
 702  704                  lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
 703  705                      mname, sizeof (mname));
 704  706          }
 705  707  
 706  708          if (lname == NULL &&
 707  709              ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD)) {
 708  710                  lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
 709  711                      pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
 710  712          }
 711  713  
 712  714          /*
 713  715           * Adjust the address range if -A is specified.
 714  716           */
 715  717          size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz,
 716  718              &vaddr, &segment_end);
 717  719  
 718  720          if (size == 0)
 719  721                  return (0);
 720  722  
 721  723          if (!Lflag) {
 722  724                  /*
 723  725                   * Display the whole mapping
 724  726                   */
 725  727                  size = ROUNDUP_KB(size);
 726  728  
 727  729                  (void) printf(lname ?
 728  730                      "%.*lX %*luK %-6s %s\n" :
 729  731                      "%.*lX %*luK %s\n",
 730  732                      addr_width, vaddr,
 731  733                      size_width - 1, size, mflags(pmp->pr_mflags), lname);
 732  734  
 733  735                  t->total_size += size;
 734  736                  return (0);
 735  737          }
 736  738  
 737  739          /*
 738  740           * We need to display lgroups backing physical memory, so we break the
 739  741           * segment into individual pages and coalesce pages with the same lgroup
 740  742           * into one "segment".
 741  743           */
 742  744  
 743  745          /*
 744  746           * Initialize address descriptions for the mapping.
 745  747           */
 746  748          mem_chunk_init(&mchunk, segment_end, psz);
 747  749          size = 0;
 748  750  
 749  751          /*
 750  752           * Walk mapping (page by page) and display contiguous ranges of memory
 751  753           * allocated to same lgroup.
 752  754           */
 753  755          do {
 754  756                  size_t          size_contig;
 755  757  
 756  758                  /*
 757  759                   * Get contiguous region of memory starting from vaddr allocated
 758  760                   * from the same lgroup.
 759  761                   */
 760  762                  size_contig = get_contiguous_region(&mchunk, vaddr,
 761  763                      segment_end, pmp->pr_pagesize, &lgrp);
 762  764  
 763  765                  (void) printf(lname ? "%.*lX %*luK %-6s%s %s\n" :
 764  766                      "%.*lX %*luK %s %s\n",
 765  767                      addr_width, vaddr,
 766  768                      size_width - 1, size_contig / KILOBYTE,
 767  769                      mflags(pmp->pr_mflags),
 768  770                      lgrp2str(lgrp), lname);
 769  771  
 770  772                  vaddr += size_contig;
 771  773                  size += size_contig;
 772  774          } while (vaddr < segment_end && !interrupt);
 773  775  
 774  776          /* Update the total size */
 775  777          t->total_size += ROUNDUP_KB(size);
 776  778          return (0);
 777  779  }
 778  780  
 779  781  static void
 780  782  printK(long value, int width)
 781  783  {
 782  784          if (value == 0)
 783  785                  (void) printf(width == 8 ? "       -" : "          -");
 784  786          else
 785  787                  (void) printf(" %*lu", width - 1, value);
 786  788  }
 787  789  
 788  790  static const char *
 789  791  pagesize(const prxmap_t *pmp)
 790  792  {
 791  793          int pagesize = pmp->pr_hatpagesize;
 792  794          static char buf[32];
 793  795  
 794  796          if (pagesize == 0) {
 795  797                  return ("-"); /* no underlying HAT mapping */
 796  798          }
 797  799  
 798  800          if (pagesize >= KILOBYTE && (pagesize % KILOBYTE) == 0) {
 799  801                  if ((pagesize % GIGABYTE) == 0)
 800  802                          (void) snprintf(buf, sizeof (buf), "%dG",
 801  803                              pagesize / GIGABYTE);
 802  804                  else if ((pagesize % MEGABYTE) == 0)
 803  805                          (void) snprintf(buf, sizeof (buf), "%dM",
 804  806                              pagesize / MEGABYTE);
 805  807                  else
 806  808                          (void) snprintf(buf, sizeof (buf), "%dK",
 807  809                              pagesize / KILOBYTE);
 808  810          } else
 809  811                  (void) snprintf(buf, sizeof (buf), "%db", pagesize);
 810  812  
 811  813          return (buf);
 812  814  }
 813  815  
 814  816  /*ARGSUSED*/
 815  817  static int
 816  818  look_smap(void *data,
 817  819          const prxmap_t *pmp,
 818  820          const char *object_name,
 819  821          int last, int doswap)
 820  822  {
 821  823          struct totals *t = data;
 822  824          const pstatus_t *Psp = Pstatus(Pr);
 823  825          size_t size;
 824  826          char mname[PATH_MAX];
 825  827          char *lname = NULL;
 826  828          const char *format;
 827  829          size_t  psz = pmp->pr_pagesize;
 828  830          uintptr_t vaddr = pmp->pr_vaddr;
 829  831          uintptr_t segment_end = vaddr + pmp->pr_size;
 830  832          lgrp_id_t lgrp;
 831  833          memory_chunk_t mchunk;
 832  834  
 833  835          /*
 834  836           * If the mapping is not anon or not part of the heap, make a name
 835  837           * for it.  We don't want to report the heap as a.out's data.
 836  838           */
 837  839          if (!(pmp->pr_mflags & MA_ANON) ||
 838  840              pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
 839  841              pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
 840  842                  lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
 841  843                      mname, sizeof (mname));
 842  844          }
 843  845  
 844  846          if (lname == NULL &&
 845  847              ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD)) {
 846  848                  lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
 847  849                      pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
 848  850          }
 849  851  
 850  852          /*
 851  853           * Adjust the address range if -A is specified.
 852  854           */
 853  855          size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz,
 854  856              &vaddr, &segment_end);
 855  857  
 856  858          if (size == 0)
 857  859                  return (0);
 858  860  
 859  861          if (!Lflag) {
 860  862                  /*
 861  863                   * Display the whole mapping
 862  864                   */
 863  865                  if (lname != NULL)
 864  866                          format = "%.*lX %*luK %4s %-6s %s\n";
 865  867                  else
 866  868                          format = "%.*lX %*luK %4s %s\n";
 867  869  
 868  870                  size = ROUNDUP_KB(size);
 869  871  
 870  872                  (void) printf(format, addr_width, vaddr, size_width - 1, size,
 871  873                      pagesize(pmp), mflags(pmp->pr_mflags), lname);
 872  874  
 873  875                  t->total_size += size;
 874  876                  return (0);
 875  877          }
 876  878  
 877  879          if (lname != NULL)
 878  880                  format = "%.*lX %*luK %4s %-6s%s %s\n";
 879  881          else
 880  882                  format = "%.*lX %*luK %4s%s %s\n";
 881  883  
 882  884          /*
 883  885           * We need to display lgroups backing physical memory, so we break the
 884  886           * segment into individual pages and coalesce pages with the same lgroup
 885  887           * into one "segment".
 886  888           */
 887  889  
 888  890          /*
 889  891           * Initialize address descriptions for the mapping.
 890  892           */
 891  893          mem_chunk_init(&mchunk, segment_end, psz);
 892  894          size = 0;
 893  895  
 894  896          /*
 895  897           * Walk mapping (page by page) and display contiguous ranges of memory
 896  898           * allocated to same lgroup.
 897  899           */
 898  900          do {
 899  901                  size_t          size_contig;
 900  902  
 901  903                  /*
 902  904                   * Get contiguous region of memory starting from vaddr allocated
 903  905                   * from the same lgroup.
 904  906                   */
 905  907                  size_contig = get_contiguous_region(&mchunk, vaddr,
 906  908                      segment_end, pmp->pr_pagesize, &lgrp);
 907  909  
 908  910                  (void) printf(format, addr_width, vaddr,
 909  911                      size_width - 1, size_contig / KILOBYTE,
 910  912                      pagesize(pmp), mflags(pmp->pr_mflags),
 911  913                      lgrp2str(lgrp), lname);
 912  914  
 913  915                  vaddr += size_contig;
 914  916                  size += size_contig;
 915  917          } while (vaddr < segment_end && !interrupt);
 916  918  
 917  919          t->total_size += ROUNDUP_KB(size);
 918  920          return (0);
 919  921  }
 920  922  
 921  923  #define ANON(x) ((aflag || (((x)->pr_mflags & MA_SHARED) == 0)) ? \
 922  924              ((x)->pr_anon) : 0)
 923  925  
 924  926  /*ARGSUSED*/
 925  927  static int
 926  928  look_xmap(void *data,
 927  929          const prxmap_t *pmp,
 928  930          const char *object_name,
 929  931          int last, int doswap)
 930  932  {
 931  933          struct totals *t = data;
 932  934          const pstatus_t *Psp = Pstatus(Pr);
 933  935          char mname[PATH_MAX];
 934  936          char *lname = NULL;
 935  937          char *ln;
 936  938  
 937  939          /*
 938  940           * If the mapping is not anon or not part of the heap, make a name
 939  941           * for it.  We don't want to report the heap as a.out's data.
 940  942           */
 941  943          if (!(pmp->pr_mflags & MA_ANON) ||
 942  944              pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
 943  945              pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
 944  946                  lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
 945  947                      mname, sizeof (mname));
 946  948          }
 947  949  
 948  950          if (lname != NULL) {
 949  951                  if ((ln = strrchr(lname, '/')) != NULL)
 950  952                          lname = ln + 1;
 951  953          } else if ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD) {
 952  954                  lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
 953  955                      pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
 954  956          }
 955  957  
 956  958          (void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr);
 957  959  
 958  960          printK(ROUNDUP_KB(pmp->pr_size), size_width);
 959  961          printK(pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE), size_width);
 960  962          printK(ANON(pmp) * (pmp->pr_pagesize / KILOBYTE), size_width);
 961  963          printK(pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE), size_width);
 962  964          (void) printf(lname ? " %4s %-6s %s\n" : " %4s %s\n",
 963  965              pagesize(pmp), mflags(pmp->pr_mflags), lname);
 964  966  
 965  967          t->total_size += ROUNDUP_KB(pmp->pr_size);
 966  968          t->total_rss += pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE);
 967  969          t->total_anon += ANON(pmp) * (pmp->pr_pagesize / KILOBYTE);
 968  970          t->total_locked += (pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE));
 969  971  
 970  972          return (0);
 971  973  }
 972  974  
 973  975  /*ARGSUSED*/
 974  976  static int
 975  977  look_xmap_nopgsz(void *data,
 976  978          const prxmap_t *pmp,
 977  979          const char *object_name,
 978  980          int last, int doswap)
 979  981  {
 980  982          struct totals *t = data;
 981  983          const pstatus_t *Psp = Pstatus(Pr);
 982  984          char mname[PATH_MAX];
 983  985          char *lname = NULL;
 984  986          char *ln;
 985  987          static uintptr_t prev_vaddr;
 986  988          static size_t prev_size;
 987  989          static offset_t prev_offset;
 988  990          static int prev_mflags;
 989  991          static char *prev_lname;
 990  992          static char prev_mname[PATH_MAX];
 991  993          static ulong_t prev_rss;
 992  994          static ulong_t prev_anon;
 993  995          static ulong_t prev_locked;
 994  996          static ulong_t prev_swap;
 995  997          int merged = 0;
 996  998          static int first = 1;
 997  999          ulong_t swap = 0;
 998 1000          int kperpage;
 999 1001  
1000 1002          /*
1001 1003           * Calculate swap reservations
1002 1004           */
1003 1005          if (pmp->pr_mflags & MA_SHARED) {
1004 1006                  if (aflag && (pmp->pr_mflags & MA_NORESERVE) == 0) {
1005 1007                          /* Swap reserved for entire non-ism SHM */
1006 1008                          swap = pmp->pr_size / pmp->pr_pagesize;
1007 1009                  }
1008 1010          } else if (pmp->pr_mflags & MA_NORESERVE) {
1009 1011                  /* Swap reserved on fault for each anon page */
1010 1012                  swap = pmp->pr_anon;
1011 1013          } else if (pmp->pr_mflags & MA_WRITE) {
1012 1014                  /* Swap reserve for entire writable segment */
1013 1015                  swap = pmp->pr_size / pmp->pr_pagesize;
1014 1016          }
1015 1017  
1016 1018          /*
1017 1019           * If the mapping is not anon or not part of the heap, make a name
1018 1020           * for it.  We don't want to report the heap as a.out's data.
1019 1021           */
1020 1022          if (!(pmp->pr_mflags & MA_ANON) ||
1021 1023              pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
1022 1024              pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
1023 1025                  lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
1024 1026                      mname, sizeof (mname));
1025 1027          }
1026 1028  
1027 1029          if (lname != NULL) {
1028 1030                  if ((ln = strrchr(lname, '/')) != NULL)
1029 1031                          lname = ln + 1;
1030 1032          } else if ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD) {
1031 1033                  lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
1032 1034                      pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
1033 1035          }
1034 1036  
1035 1037          kperpage = pmp->pr_pagesize / KILOBYTE;
1036 1038  
1037 1039          t->total_size += ROUNDUP_KB(pmp->pr_size);
1038 1040          t->total_rss += pmp->pr_rss * kperpage;
1039 1041          t->total_anon += ANON(pmp) * kperpage;
1040 1042          t->total_locked += pmp->pr_locked * kperpage;
1041 1043          t->total_swap += swap * kperpage;
1042 1044  
1043 1045          if (first == 1) {
1044 1046                  first = 0;
1045 1047                  prev_vaddr = pmp->pr_vaddr;
1046 1048                  prev_size = pmp->pr_size;
1047 1049                  prev_offset = pmp->pr_offset;
1048 1050                  prev_mflags = pmp->pr_mflags;
1049 1051                  if (lname == NULL) {
1050 1052                          prev_lname = NULL;
1051 1053                  } else {
1052 1054                          (void) strcpy(prev_mname, lname);
1053 1055                          prev_lname = prev_mname;
1054 1056                  }
1055 1057                  prev_rss = pmp->pr_rss * kperpage;
1056 1058                  prev_anon = ANON(pmp) * kperpage;
1057 1059                  prev_locked = pmp->pr_locked * kperpage;
1058 1060                  prev_swap = swap * kperpage;
1059 1061                  if (last == 0) {
1060 1062                          return (0);
1061 1063                  }
1062 1064                  merged = 1;
1063 1065          } else if (prev_vaddr + prev_size == pmp->pr_vaddr &&
1064 1066              prev_mflags == pmp->pr_mflags &&
1065 1067              ((prev_mflags & MA_ISM) ||
1066 1068              prev_offset + prev_size == pmp->pr_offset) &&
1067 1069              ((lname == NULL && prev_lname == NULL) ||
1068 1070              (lname != NULL && prev_lname != NULL &&
1069 1071              strcmp(lname, prev_lname) == 0))) {
1070 1072                  prev_size += pmp->pr_size;
1071 1073                  prev_rss += pmp->pr_rss * kperpage;
1072 1074                  prev_anon += ANON(pmp) * kperpage;
1073 1075                  prev_locked += pmp->pr_locked * kperpage;
1074 1076                  prev_swap += swap * kperpage;
1075 1077                  if (last == 0) {
1076 1078                          return (0);
1077 1079                  }
1078 1080                  merged = 1;
1079 1081          }
1080 1082  
1081 1083          (void) printf("%.*lX", addr_width, (ulong_t)prev_vaddr);
1082 1084          printK(ROUNDUP_KB(prev_size), size_width);
1083 1085  
1084 1086          if (doswap)
1085 1087                  printK(prev_swap, size_width);
1086 1088          else {
1087 1089                  printK(prev_rss, size_width);
1088 1090                  printK(prev_anon, size_width);
1089 1091                  printK(prev_locked, size_width);
1090 1092          }
1091 1093          (void) printf(prev_lname ? " %-6s %s\n" : "%s\n",
1092 1094              mflags(prev_mflags), prev_lname);
1093 1095  
1094 1096          if (last == 0) {
1095 1097                  prev_vaddr = pmp->pr_vaddr;
1096 1098                  prev_size = pmp->pr_size;
1097 1099                  prev_offset = pmp->pr_offset;
1098 1100                  prev_mflags = pmp->pr_mflags;
1099 1101                  if (lname == NULL) {
1100 1102                          prev_lname = NULL;
1101 1103                  } else {
1102 1104                          (void) strcpy(prev_mname, lname);
1103 1105                          prev_lname = prev_mname;
1104 1106                  }
1105 1107                  prev_rss = pmp->pr_rss * kperpage;
1106 1108                  prev_anon = ANON(pmp) * kperpage;
1107 1109                  prev_locked = pmp->pr_locked * kperpage;
1108 1110                  prev_swap = swap * kperpage;
1109 1111          } else if (merged == 0) {
1110 1112                  (void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr);
1111 1113                  printK(ROUNDUP_KB(pmp->pr_size), size_width);
1112 1114                  if (doswap)
1113 1115                          printK(swap * kperpage, size_width);
1114 1116                  else {
1115 1117                          printK(pmp->pr_rss * kperpage, size_width);
1116 1118                          printK(ANON(pmp) * kperpage, size_width);
1117 1119                          printK(pmp->pr_locked * kperpage, size_width);
1118 1120                  }
1119 1121                  (void) printf(lname ? " %-6s %s\n" : " %s\n",
1120 1122                      mflags(pmp->pr_mflags), lname);
1121 1123          }
1122 1124  
1123 1125          if (last != 0)
1124 1126                  first = 1;
1125 1127  
1126 1128          return (0);
1127 1129  }
1128 1130  
1129 1131  static int
1130 1132  perr(char *s)
1131 1133  {
1132 1134          if (s)
1133 1135                  (void) fprintf(stderr, "%s: ", procname);
1134 1136          else
1135 1137                  s = procname;
1136 1138          perror(s);
1137 1139          return (1);
1138 1140  }
1139 1141  
1140 1142  static char *
1141 1143  mflags(uint_t arg)
1142 1144  {
1143 1145          static char code_buf[80];
1144 1146          char *str = code_buf;
1145 1147  
1146 1148          /*
1147 1149           * rwxsR
1148 1150           *
1149 1151           * r - segment is readable
1150 1152           * w - segment is writable
1151 1153           * x - segment is executable
1152 1154           * s - segment is shared
1153 1155           * R - segment is mapped MAP_NORESERVE
1154 1156           *
1155 1157           */
1156 1158          (void) sprintf(str, "%c%c%c%c%c%c",
1157 1159              arg & MA_READ ? 'r' : '-',
1158 1160              arg & MA_WRITE ? 'w' : '-',
1159 1161              arg & MA_EXEC ? 'x' : '-',
1160 1162              arg & MA_SHARED ? 's' : '-',
1161 1163              arg & MA_NORESERVE ? 'R' : '-',
1162 1164              arg & MA_RESERVED1 ? '*' : ' ');
1163 1165  
1164 1166          return (str);
1165 1167  }
1166 1168  
1167 1169  static mapdata_t *
1168 1170  nextmap(void)
1169 1171  {
1170 1172          mapdata_t *newmaps;
1171 1173          int next;
1172 1174  
1173 1175          if (map_count == map_alloc) {
1174 1176                  if (map_alloc == 0)
1175 1177                          next = 16;
1176 1178                  else
1177 1179                          next = map_alloc * 2;
1178 1180  
1179 1181                  newmaps = realloc(maps, next * sizeof (mapdata_t));
1180 1182                  if (newmaps == NULL) {
1181 1183                          (void) perr("failed to allocate maps");
1182 1184                          exit(1);
1183 1185                  }
1184 1186                  (void) memset(newmaps + map_alloc, '\0',
1185 1187                      (next - map_alloc) * sizeof (mapdata_t));
1186 1188  
1187 1189                  map_alloc = next;
1188 1190                  maps = newmaps;
1189 1191          }
1190 1192  
1191 1193          return (&maps[map_count++]);
1192 1194  }
1193 1195  
1194 1196  /*ARGSUSED*/
1195 1197  static int
1196 1198  gather_map(void *ignored, const prmap_t *map, const char *objname)
1197 1199  {
1198 1200          mapdata_t *data;
1199 1201  
1200 1202          /* Skip mappings which are outside the range specified by -A */
1201 1203          if (!address_in_range(map->pr_vaddr,
1202 1204              map->pr_vaddr + map->pr_size, map->pr_pagesize))
1203 1205                  return (0);
1204 1206  
1205 1207          data = nextmap();
1206 1208          data->md_map = *map;
1207 1209          if (data->md_objname != NULL)
1208 1210                  free(data->md_objname);
1209 1211          data->md_objname = objname ? strdup(objname) : NULL;
1210 1212  
1211 1213          return (0);
1212 1214  }
1213 1215  
1214 1216  /*ARGSUSED*/
1215 1217  static int
1216 1218  gather_xmap(void *ignored, const prxmap_t *xmap, const char *objname,
1217 1219      int last, int doswap)
1218 1220  {
1219 1221          mapdata_t *data;
1220 1222  
1221 1223          /* Skip mappings which are outside the range specified by -A */
1222 1224          if (!address_in_range(xmap->pr_vaddr,
1223 1225              xmap->pr_vaddr + xmap->pr_size, xmap->pr_pagesize))
1224 1226                  return (0);
1225 1227  
1226 1228          data = nextmap();
1227 1229          data->md_xmap = *xmap;
1228 1230          if (data->md_objname != NULL)
1229 1231                  free(data->md_objname);
1230 1232          data->md_objname = objname ? strdup(objname) : NULL;
1231 1233          data->md_last = last;
1232 1234          data->md_doswap = doswap;
1233 1235  
1234 1236          return (0);
1235 1237  }
1236 1238  
1237 1239  static int
1238 1240  iter_map(proc_map_f *func, void *data)
1239 1241  {
1240 1242          int i;
1241 1243          int ret;
1242 1244  
1243 1245          for (i = 0; i < map_count; i++) {
1244 1246                  if (interrupt)
1245 1247                          break;
1246 1248                  if ((ret = func(data, &maps[i].md_map,
1247 1249                      maps[i].md_objname)) != 0)
1248 1250                          return (ret);
1249 1251          }
1250 1252  
1251 1253          return (0);
1252 1254  }
1253 1255  
1254 1256  static int
1255 1257  iter_xmap(proc_xmap_f *func, void *data)
1256 1258  {
1257 1259          int i;
1258 1260          int ret;
1259 1261  
1260 1262          for (i = 0; i < map_count; i++) {
1261 1263                  if (interrupt)
1262 1264                          break;
1263 1265                  if ((ret = func(data, &maps[i].md_xmap, maps[i].md_objname,
1264 1266                      maps[i].md_last, maps[i].md_doswap)) != 0)
1265 1267                          return (ret);
1266 1268          }
1267 1269  
1268 1270          return (0);
1269 1271  }
1270 1272  
1271 1273  /*
1272 1274   * Convert lgroup ID to string.
1273 1275   * returns dash when lgroup ID is invalid.
1274 1276   */
1275 1277  static char *
1276 1278  lgrp2str(lgrp_id_t lgrp)
1277 1279  {
1278 1280          static char lgrp_buf[20];
1279 1281          char *str = lgrp_buf;
1280 1282  
1281 1283          (void) sprintf(str, lgrp == LGRP_NONE ? "   -" : "%4d", lgrp);
1282 1284          return (str);
1283 1285  }
1284 1286  
1285 1287  /*
1286 1288   * Parse address range specification for -A option.
1287 1289   * The address range may have the following forms:
1288 1290   *
1289 1291   * address
1290 1292   *      start and end is set to address
1291 1293   * address,
1292 1294   *      start is set to address, end is set to INVALID_ADDRESS
1293 1295   * ,address
1294 1296   *      start is set to 0, end is set to address
1295 1297   * address1,address2
1296 1298   *      start is set to address1, end is set to address2
1297 1299   *
1298 1300   */
1299 1301  static int
1300 1302  parse_addr_range(char *input_str, uintptr_t *start, uintptr_t *end)
1301 1303  {
1302 1304          char *startp = input_str;
1303 1305          char *endp = strchr(input_str, ',');
1304 1306          ulong_t s = (ulong_t)INVALID_ADDRESS;
1305 1307          ulong_t e = (ulong_t)INVALID_ADDRESS;
1306 1308  
1307 1309          if (endp != NULL) {
1308 1310                  /*
1309 1311                   * Comma is present. If there is nothing after comma, the end
1310 1312                   * remains set at INVALID_ADDRESS. Otherwise it is set to the
1311 1313                   * value after comma.
1312 1314                   */
1313 1315                  *endp = '\0';
1314 1316                  endp++;
1315 1317  
1316 1318                  if ((*endp != '\0') && sscanf(endp, "%lx", &e) != 1)
1317 1319                          return (1);
1318 1320          }
1319 1321  
1320 1322          if (startp != NULL) {
1321 1323                  /*
1322 1324                   * Read the start address, if it is specified. If the address is
1323 1325                   * missing, start will be set to INVALID_ADDRESS.
1324 1326                   */
1325 1327                  if ((*startp != '\0') && sscanf(startp, "%lx", &s) != 1)
1326 1328                          return (1);
1327 1329          }
1328 1330  
1329 1331          /* If there is no comma, end becomes equal to start */
1330 1332          if (endp == NULL)
1331 1333                  e = s;
1332 1334  
1333 1335          /*
1334 1336           * ,end implies 0..end range
1335 1337           */
1336 1338          if (e != INVALID_ADDRESS && s == INVALID_ADDRESS)
1337 1339                  s = 0;
1338 1340  
1339 1341          *start = (uintptr_t)s;
1340 1342          *end = (uintptr_t)e;
1341 1343  
1342 1344          /* Return error if neither start nor end address were specified */
1343 1345          return (! (s != INVALID_ADDRESS || e != INVALID_ADDRESS));
1344 1346  }
1345 1347  
1346 1348  /*
1347 1349   * Check whether any portion of [start, end] segment is within the
1348 1350   * [start_addr, end_addr] range.
1349 1351   *
1350 1352   * Return values:
1351 1353   *   0 - address is outside the range
1352 1354   *   1 - address is within the range
1353 1355   */
1354 1356  static int
1355 1357  address_in_range(uintptr_t start, uintptr_t end, size_t psz)
1356 1358  {
1357 1359          int rc = 1;
1358 1360  
1359 1361          /*
1360 1362           *  Nothing to do if there is no address range specified with -A
1361 1363           */
1362 1364          if (start_addr != INVALID_ADDRESS || end_addr != INVALID_ADDRESS) {
1363 1365                  /* The segment end is below the range start */
1364 1366                  if ((start_addr != INVALID_ADDRESS) &&
1365 1367                      (end < P2ALIGN(start_addr, psz)))
1366 1368                          rc = 0;
1367 1369  
1368 1370                  /* The segment start is above the range end */
1369 1371                  if ((end_addr != INVALID_ADDRESS) &&
1370 1372                      (start > P2ALIGN(end_addr + psz, psz)))
1371 1373                          rc = 0;
1372 1374          }
1373 1375          return (rc);
1374 1376  }
1375 1377  
1376 1378  /*
1377 1379   * Returns an intersection of the [start, end] interval and the range specified
1378 1380   * by -A flag [start_addr, end_addr]. Unspecified parts of the address range
1379 1381   * have value INVALID_ADDRESS.
1380 1382   *
1381 1383   * The start_addr address is rounded down to the beginning of page and end_addr
1382 1384   * is rounded up to the end of page.
1383 1385   *
1384 1386   * Returns the size of the resulting interval or zero if the interval is empty
1385 1387   * or invalid.
1386 1388   */
1387 1389  static size_t
1388 1390  adjust_addr_range(uintptr_t start, uintptr_t end, size_t psz,
1389 1391      uintptr_t *new_start, uintptr_t *new_end)
1390 1392  {
1391 1393          uintptr_t from;         /* start_addr rounded down */
1392 1394          uintptr_t to;           /* end_addr rounded up */
1393 1395  
1394 1396          /*
1395 1397           * Round down the lower address of the range to the beginning of page.
1396 1398           */
1397 1399          if (start_addr == INVALID_ADDRESS) {
1398 1400                  /*
1399 1401                   * No start_addr specified by -A, the lower part of the interval
1400 1402                   * does not change.
1401 1403                   */
1402 1404                  *new_start = start;
1403 1405          } else {
1404 1406                  from = P2ALIGN(start_addr, psz);
1405 1407                  /*
1406 1408                   * If end address is outside the range, return an empty
1407 1409                   * interval
1408 1410                   */
1409 1411                  if (end <  from) {
1410 1412                          *new_start = *new_end = 0;
1411 1413                          return (0);
1412 1414                  }
1413 1415                  /*
1414 1416                   * The adjusted start address is the maximum of requested start
1415 1417                   * and the aligned start_addr of the -A range.
1416 1418                   */
1417 1419                  *new_start = start < from ? from : start;
1418 1420          }
1419 1421  
1420 1422          /*
1421 1423           * Round up the higher address of the range to the end of page.
1422 1424           */
1423 1425          if (end_addr == INVALID_ADDRESS) {
1424 1426                  /*
1425 1427                   * No end_addr specified by -A, the upper part of the interval
1426 1428                   * does not change.
1427 1429                   */
1428 1430                  *new_end = end;
1429 1431          } else {
1430 1432                  /*
1431 1433                   * If only one address is specified and it is the beginning of a
1432 1434                   * segment, get information about the whole segment. This
1433 1435                   * function is called once per segment and the 'end' argument is
1434 1436                   * always the end of a segment, so just use the 'end' value.
1435 1437                   */
1436 1438                  to = (end_addr == start_addr && start == start_addr) ?
1437 1439                      end :
1438 1440                      P2ALIGN(end_addr + psz, psz);
1439 1441                  /*
1440 1442                   * If start address is outside the range, return an empty
1441 1443                   * interval
1442 1444                   */
1443 1445                  if (start > to) {
1444 1446                          *new_start = *new_end = 0;
1445 1447                          return (0);
1446 1448                  }
1447 1449                  /*
1448 1450                   * The adjusted end address is the minimum of requested end
1449 1451                   * and the aligned end_addr of the -A range.
1450 1452                   */
1451 1453                  *new_end = end > to ? to : end;
1452 1454          }
1453 1455  
1454 1456          /*
1455 1457           * Make sure that the resulting interval is legal.
1456 1458           */
1457 1459          if (*new_end < *new_start)
1458 1460                          *new_start = *new_end = 0;
1459 1461  
1460 1462          /* Return the size of the interval */
1461 1463          return (*new_end - *new_start);
1462 1464  }
1463 1465  
1464 1466  /*
1465 1467   * Initialize memory_info data structure with information about a new segment.
1466 1468   */
1467 1469  static void
1468 1470  mem_chunk_init(memory_chunk_t *chunk, uintptr_t end, size_t psz)
1469 1471  {
1470 1472          chunk->end_addr = end;
1471 1473          chunk->page_size = psz;
1472 1474          chunk->page_index = 0;
1473 1475          chunk->chunk_start = chunk->chunk_end = 0;
1474 1476  }
1475 1477  
1476 1478  /*
1477 1479   * Create a new chunk of addresses starting from vaddr.
1478 1480   * Pass the whole chunk to pr_meminfo to collect lgroup and page size
1479 1481   * information for each page in the chunk.
1480 1482   */
1481 1483  static void
1482 1484  mem_chunk_get(memory_chunk_t *chunk, uintptr_t vaddr)
1483 1485  {
1484 1486          page_descr_t    *pdp = chunk->page_info;
1485 1487          size_t          psz = chunk->page_size;
1486 1488          uintptr_t       addr = vaddr;
1487 1489          uint64_t        inaddr[MAX_MEMINFO_CNT];
1488 1490          uint64_t        outdata[2 * MAX_MEMINFO_CNT];
1489 1491          uint_t          info[2] = { MEMINFO_VLGRP, MEMINFO_VPAGESIZE };
1490 1492          uint_t          validity[MAX_MEMINFO_CNT];
1491 1493          uint64_t        *dataptr = inaddr;
1492 1494          uint64_t        *outptr = outdata;
1493 1495          uint_t          *valptr = validity;
1494 1496          int             i, j, rc;
1495 1497  
1496 1498          chunk->chunk_start = vaddr;
1497 1499          chunk->page_index = 0;  /* reset index for the new chunk */
1498 1500  
1499 1501          /*
1500 1502           * Fill in MAX_MEMINFO_CNT wotrh of pages starting from vaddr. Also,
1501 1503           * copy starting address of each page to inaddr array for pr_meminfo.
1502 1504           */
1503 1505          for (i = 0, pdp = chunk->page_info;
1504 1506              (i < MAX_MEMINFO_CNT) && (addr <= chunk->end_addr);
1505 1507              i++, pdp++, dataptr++, addr += psz) {
1506 1508                  *dataptr = (uint64_t)addr;
1507 1509                  pdp->pd_start = addr;
1508 1510                  pdp->pd_lgrp = LGRP_NONE;
1509 1511                  pdp->pd_valid = 0;
1510 1512                  pdp->pd_pagesize = 0;
1511 1513          }
1512 1514  
1513 1515          /* Mark the number of entries in the chunk and the last address */
1514 1516          chunk->page_count = i;
1515 1517          chunk->chunk_end = addr - psz;
1516 1518  
1517 1519          if (interrupt)
1518 1520                  return;
1519 1521  
1520 1522          /* Call meminfo for all collected addresses */
1521 1523          rc = pr_meminfo(Pr, inaddr, i, info, 2, outdata, validity);
1522 1524          if (rc < 0) {
1523 1525                  (void) perr("can not get memory information");
1524 1526                  return;
1525 1527          }
1526 1528  
1527 1529          /* Verify validity of each result and fill in the addrs array */
1528 1530          pdp = chunk->page_info;
1529 1531          for (j = 0; j < i; j++, pdp++, valptr++, outptr += 2) {
1530 1532                  /* Skip invalid address pointers */
1531 1533                  if ((*valptr & 1) == 0) {
1532 1534                          continue;
1533 1535                  }
1534 1536  
1535 1537                  /* Is lgroup information available? */
1536 1538                  if ((*valptr & 2) != 0) {
1537 1539                          pdp->pd_lgrp = (lgrp_id_t)*outptr;
1538 1540                          pdp->pd_valid = 1;
1539 1541                  }
1540 1542  
1541 1543                  /* Is page size informaion available? */
1542 1544                  if ((*valptr & 4) != 0) {
1543 1545                          pdp->pd_pagesize = *(outptr + 1);
1544 1546                  }
1545 1547          }
1546 1548  }
1547 1549  
1548 1550  /*
1549 1551   * Starting from address 'vaddr' find the region with pages allocated from the
1550 1552   * same lgroup.
1551 1553   *
1552 1554   * Arguments:
1553 1555   *      mchunk          Initialized memory chunk structure
1554 1556   *      vaddr           Starting address of the region
1555 1557   *      maxaddr         Upper bound of the region
1556 1558   *      pagesize        Default page size to use
1557 1559   *      ret_lgrp        On exit contains the lgroup ID of all pages in the
1558 1560   *                      region.
1559 1561   *
1560 1562   * Returns:
1561 1563   *      Size of the contiguous region in bytes
1562 1564   *      The lgroup ID of all pages in the region in ret_lgrp argument.
1563 1565   */
1564 1566  static size_t
1565 1567  get_contiguous_region(memory_chunk_t *mchunk, uintptr_t vaddr,
1566 1568      uintptr_t maxaddr, size_t pagesize, lgrp_id_t *ret_lgrp)
1567 1569  {
1568 1570          size_t          size_contig = 0;
1569 1571          lgrp_id_t       lgrp;           /* Lgroup of the region start */
1570 1572          lgrp_id_t       curr_lgrp;      /* Lgroup of the current page */
1571 1573          size_t          psz = pagesize; /* Pagesize to use */
1572 1574  
1573 1575          /* Set both lgroup IDs to the lgroup of the first page */
1574 1576          curr_lgrp = lgrp = addr_to_lgrp(mchunk, vaddr, &psz);
1575 1577  
1576 1578          /*
1577 1579           * Starting from vaddr, walk page by page until either the end
1578 1580           * of the segment is reached or a page is allocated from a different
1579 1581           * lgroup. Also stop if interrupted from keyboard.
1580 1582           */
1581 1583          while ((vaddr < maxaddr) && (curr_lgrp == lgrp) && !interrupt) {
1582 1584                  /*
1583 1585                   * Get lgroup ID and the page size of the current page.
1584 1586                   */
1585 1587                  curr_lgrp = addr_to_lgrp(mchunk, vaddr, &psz);
1586 1588                  /* If there is no page size information, use the default */
1587 1589                  if (psz == 0)
1588 1590                          psz = pagesize;
1589 1591  
1590 1592                  if (curr_lgrp == lgrp) {
1591 1593                          /*
1592 1594                           * This page belongs to the contiguous region.
1593 1595                           * Increase the region size and advance to the new page.
1594 1596                           */
1595 1597                          size_contig += psz;
1596 1598                          vaddr += psz;
1597 1599                  }
1598 1600          }
1599 1601  
1600 1602          /* Return the region lgroup ID and the size */
1601 1603          *ret_lgrp = lgrp;
1602 1604          return (size_contig);
1603 1605  }
1604 1606  
1605 1607  /*
1606 1608   * Given a virtual address, return its lgroup and page size. If there is meminfo
1607 1609   * information for an address, use it, otherwise shift the chunk window to the
1608 1610   * vaddr and create a new chunk with known meminfo information.
1609 1611   */
1610 1612  static lgrp_id_t
1611 1613  addr_to_lgrp(memory_chunk_t *chunk, uintptr_t vaddr, size_t *psz)
1612 1614  {
1613 1615          page_descr_t *pdp;
1614 1616          lgrp_id_t lgrp = LGRP_NONE;
1615 1617          int i;
1616 1618  
1617 1619          *psz = chunk->page_size;
1618 1620  
1619 1621          if (interrupt)
1620 1622                  return (0);
1621 1623  
1622 1624          /*
1623 1625           * Is there information about this address? If not, create a new chunk
1624 1626           * starting from vaddr and apply pr_meminfo() to the whole chunk.
1625 1627           */
1626 1628          if (vaddr < chunk->chunk_start || vaddr > chunk->chunk_end) {
1627 1629                  /*
1628 1630                   * This address is outside the chunk, get the new chunk and
1629 1631                   * collect meminfo information for it.
1630 1632                   */
1631 1633                  mem_chunk_get(chunk, vaddr);
1632 1634          }
1633 1635  
1634 1636          /*
1635 1637           * Find information about the address.
1636 1638           */
1637 1639          pdp = &chunk->page_info[chunk->page_index];
1638 1640          for (i = chunk->page_index; i < chunk->page_count; i++, pdp++) {
1639 1641                  if (pdp->pd_start == vaddr) {
1640 1642                          if (pdp->pd_valid) {
1641 1643                                  lgrp = pdp->pd_lgrp;
1642 1644                                  /*
1643 1645                                   * Override page size information if it is
1644 1646                                   * present.
1645 1647                                   */
1646 1648                                  if (pdp->pd_pagesize > 0)
1647 1649                                          *psz = pdp->pd_pagesize;
1648 1650                          }
1649 1651                          break;
1650 1652                  }
1651 1653          }
1652 1654          /*
1653 1655           * Remember where we ended - the next search will start here.
1654 1656           * We can query for the lgrp for the same address again, so do not
1655 1657           * advance index past the current value.
1656 1658           */
1657 1659          chunk->page_index = i;
1658 1660  
1659 1661          return (lgrp);
1660 1662  }
1661 1663  
1662 1664  /* ARGSUSED */
1663 1665  static void
1664 1666  intr(int sig)
1665 1667  {
1666 1668          interrupt = 1;
1667 1669  }
  
    | 
      ↓ open down ↓ | 
    1023 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX