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>


   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.

  25  */
  26 
  27 #include <stdio.h>
  28 #include <stdio_ext.h>
  29 #include <stdlib.h>
  30 #include <unistd.h>
  31 #include <ctype.h>
  32 #include <fcntl.h>
  33 #include <string.h>
  34 #include <dirent.h>
  35 #include <limits.h>
  36 #include <link.h>
  37 #include <libelf.h>
  38 #include <sys/types.h>
  39 #include <signal.h>
  40 #include <sys/stat.h>
  41 #include <sys/mkdev.h>
  42 #include <sys/mman.h>
  43 #include <sys/lgrp_user.h>
  44 #include <libproc.h>

  45 
  46 #include "pmap_common.h"
  47 
  48 #define KILOBYTE        1024
  49 #define MEGABYTE        (KILOBYTE * KILOBYTE)
  50 #define GIGABYTE        (KILOBYTE * KILOBYTE * KILOBYTE)
  51 
  52 /*
  53  * Round up the value to the nearest kilobyte
  54  */
  55 #define ROUNDUP_KB(x)   (((x) + (KILOBYTE - 1)) / KILOBYTE)
  56 
  57 /*
  58  * The alignment should be a power of 2.
  59  */
  60 #define P2ALIGN(x, align)               ((x) & -(align))
  61 
  62 #define INVALID_ADDRESS                 (uintptr_t)(-1)
  63 
  64 struct totals {


 182         if (Plwp_main_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
 183                 stacks[*np].lwps_lwpid = lsp->pr_lwpid;
 184                 (*np)++;
 185         }
 186 
 187         return (0);
 188 }
 189 
 190 int
 191 main(int argc, char **argv)
 192 {
 193         int rflag = 0, sflag = 0, xflag = 0, Fflag = 0;
 194         int errflg = 0, Sflag = 0;
 195         int rc = 0;
 196         int opt;
 197         const char *bar8 = "-------";
 198         const char *bar16 = "----------";
 199         const char *bar;
 200         struct rlimit rlim;
 201         struct stat64 statbuf;
 202         char buf[128];
 203         int mapfd;
 204         int prg_gflags = PGRAB_RDONLY;
 205         int prr_flags = 0;
 206         boolean_t use_agent_lwp = B_FALSE;
 207 
 208         if ((command = strrchr(argv[0], '/')) != NULL)
 209                 command++;
 210         else
 211                 command = argv[0];
 212 
 213         while ((opt = getopt(argc, argv, "arsxSlLFA:")) != EOF) {
 214                 switch (opt) {
 215                 case 'a':               /* include shared mappings in -[xS] */
 216                         aflag = 1;
 217                         break;
 218                 case 'r':               /* show reserved mappings */
 219                         rflag = 1;
 220                         break;
 221                 case 's':               /* show hardware page sizes */
 222                         sflag = 1;


 341                 if (use_agent_lwp)
 342                         (void) proc_flushstdio();
 343 
 344                 if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
 345                     prg_gflags, &gcode)) == NULL) {
 346                         (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
 347                             command, arg, Pgrab_error(gcode));
 348                         rc++;
 349                         continue;
 350                 }
 351 
 352                 procname = arg;         /* for perr() */
 353 
 354                 addr_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 16 : 8;
 355                 size_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 11 : 8;
 356                 bar = addr_width == 8 ? bar8 : bar16;
 357                 (void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
 358                 proc_unctrl_psinfo(&psinfo);
 359 
 360                 if (Pstate(Pr) != PS_DEAD) {
 361                         (void) snprintf(buf, sizeof (buf),
 362                             "/proc/%d/map", (int)psinfo.pr_pid);
 363                         if ((mapfd = open(buf, O_RDONLY)) < 0) {
 364                                 (void) fprintf(stderr, "%s: cannot "
 365                                     "examine %s: lost control of "
 366                                     "process\n", command, arg);
 367                                 rc++;
 368                                 Prelease(Pr, prr_flags);
 369                                 continue;
 370                         }
 371                 } else {
 372                         mapfd = -1;
 373                 }
 374 
 375 again:
 376                 map_count = 0;
 377 
 378                 if (Pstate(Pr) == PS_DEAD) {
 379                         (void) printf("core '%s' of %d:\t%.70s\n",
 380                             arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
 381 


 573                 Prelease(Pr, prr_flags);
 574                 if (mapfd != -1)
 575                         (void) close(mapfd);
 576         }
 577 
 578         if (use_agent_lwp)
 579                 (void) proc_finistdio();
 580 
 581         return (rc);
 582 }
 583 
 584 static int
 585 rmapping_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd)
 586 {
 587         char mapname[PATH_MAX];
 588         int mapfd, nmap, i, rc;
 589         struct stat st;
 590         prmap_t *prmapp, *pmp;
 591         ssize_t n;
 592 
 593         (void) snprintf(mapname, sizeof (mapname),
 594             "/proc/%d/rmap", (int)Pstatus(Pr)->pr_pid);
 595 
 596         if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
 597                 if (mapfd >= 0)
 598                         (void) close(mapfd);
 599                 return (perr(mapname));
 600         }
 601 
 602         nmap = st.st_size / sizeof (prmap_t);
 603         prmapp = malloc((nmap + 1) * sizeof (prmap_t));
 604 
 605         if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prmap_t), 0L)) < 0) {
 606                 (void) close(mapfd);
 607                 free(prmapp);
 608                 return (perr("read rmap"));
 609         }
 610 
 611         (void) close(mapfd);
 612         nmap = n / sizeof (prmap_t);
 613 
 614         for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
 615                 if ((rc = func(cd, pmp, NULL)) != 0) {
 616                         free(prmapp);
 617                         return (rc);
 618                 }
 619         }
 620 
 621         free(prmapp);
 622         return (0);
 623 }
 624 
 625 static int
 626 xmapping_iter(struct ps_prochandle *Pr, proc_xmap_f *func, void *cd, int doswap)
 627 {
 628         char mapname[PATH_MAX];
 629         int mapfd, nmap, i, rc;
 630         struct stat st;
 631         prxmap_t *prmapp, *pmp;
 632         ssize_t n;
 633 
 634         (void) snprintf(mapname, sizeof (mapname),
 635             "/proc/%d/xmap", (int)Pstatus(Pr)->pr_pid);
 636 
 637         if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
 638                 if (mapfd >= 0)
 639                         (void) close(mapfd);
 640                 return (perr(mapname));
 641         }
 642 
 643         nmap = st.st_size / sizeof (prxmap_t);
 644         nmap *= 2;
 645 again:
 646         prmapp = malloc((nmap + 1) * sizeof (prxmap_t));
 647 
 648         if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prxmap_t), 0)) < 0) {
 649                 (void) close(mapfd);
 650                 free(prmapp);
 651                 return (perr("read xmap"));
 652         }
 653 
 654         if (nmap < n / sizeof (prxmap_t)) {




   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
  26  */
  27 
  28 #include <stdio.h>
  29 #include <stdio_ext.h>
  30 #include <stdlib.h>
  31 #include <unistd.h>
  32 #include <ctype.h>
  33 #include <fcntl.h>
  34 #include <string.h>
  35 #include <dirent.h>
  36 #include <limits.h>
  37 #include <link.h>
  38 #include <libelf.h>
  39 #include <sys/types.h>
  40 #include <signal.h>
  41 #include <sys/stat.h>
  42 #include <sys/mkdev.h>
  43 #include <sys/mman.h>
  44 #include <sys/lgrp_user.h>
  45 #include <libproc.h>
  46 #include "ptools_common.h"
  47 
  48 #include "pmap_common.h"
  49 
  50 #define KILOBYTE        1024
  51 #define MEGABYTE        (KILOBYTE * KILOBYTE)
  52 #define GIGABYTE        (KILOBYTE * KILOBYTE * KILOBYTE)
  53 
  54 /*
  55  * Round up the value to the nearest kilobyte
  56  */
  57 #define ROUNDUP_KB(x)   (((x) + (KILOBYTE - 1)) / KILOBYTE)
  58 
  59 /*
  60  * The alignment should be a power of 2.
  61  */
  62 #define P2ALIGN(x, align)               ((x) & -(align))
  63 
  64 #define INVALID_ADDRESS                 (uintptr_t)(-1)
  65 
  66 struct totals {


 184         if (Plwp_main_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
 185                 stacks[*np].lwps_lwpid = lsp->pr_lwpid;
 186                 (*np)++;
 187         }
 188 
 189         return (0);
 190 }
 191 
 192 int
 193 main(int argc, char **argv)
 194 {
 195         int rflag = 0, sflag = 0, xflag = 0, Fflag = 0;
 196         int errflg = 0, Sflag = 0;
 197         int rc = 0;
 198         int opt;
 199         const char *bar8 = "-------";
 200         const char *bar16 = "----------";
 201         const char *bar;
 202         struct rlimit rlim;
 203         struct stat64 statbuf;
 204         char buf[PATH_MAX];
 205         int mapfd;
 206         int prg_gflags = PGRAB_RDONLY;
 207         int prr_flags = 0;
 208         boolean_t use_agent_lwp = B_FALSE;
 209 
 210         if ((command = strrchr(argv[0], '/')) != NULL)
 211                 command++;
 212         else
 213                 command = argv[0];
 214 
 215         while ((opt = getopt(argc, argv, "arsxSlLFA:")) != EOF) {
 216                 switch (opt) {
 217                 case 'a':               /* include shared mappings in -[xS] */
 218                         aflag = 1;
 219                         break;
 220                 case 'r':               /* show reserved mappings */
 221                         rflag = 1;
 222                         break;
 223                 case 's':               /* show hardware page sizes */
 224                         sflag = 1;


 343                 if (use_agent_lwp)
 344                         (void) proc_flushstdio();
 345 
 346                 if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
 347                     prg_gflags, &gcode)) == NULL) {
 348                         (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
 349                             command, arg, Pgrab_error(gcode));
 350                         rc++;
 351                         continue;
 352                 }
 353 
 354                 procname = arg;         /* for perr() */
 355 
 356                 addr_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 16 : 8;
 357                 size_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 11 : 8;
 358                 bar = addr_width == 8 ? bar8 : bar16;
 359                 (void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
 360                 proc_unctrl_psinfo(&psinfo);
 361 
 362                 if (Pstate(Pr) != PS_DEAD) {
 363                         (void) proc_snprintf(buf, sizeof (buf),
 364                             "/proc/%d/map", (int)psinfo.pr_pid);
 365                         if ((mapfd = open(buf, O_RDONLY)) < 0) {
 366                                 (void) fprintf(stderr, "%s: cannot "
 367                                     "examine %s: lost control of "
 368                                     "process\n", command, arg);
 369                                 rc++;
 370                                 Prelease(Pr, prr_flags);
 371                                 continue;
 372                         }
 373                 } else {
 374                         mapfd = -1;
 375                 }
 376 
 377 again:
 378                 map_count = 0;
 379 
 380                 if (Pstate(Pr) == PS_DEAD) {
 381                         (void) printf("core '%s' of %d:\t%.70s\n",
 382                             arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
 383 


 575                 Prelease(Pr, prr_flags);
 576                 if (mapfd != -1)
 577                         (void) close(mapfd);
 578         }
 579 
 580         if (use_agent_lwp)
 581                 (void) proc_finistdio();
 582 
 583         return (rc);
 584 }
 585 
 586 static int
 587 rmapping_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd)
 588 {
 589         char mapname[PATH_MAX];
 590         int mapfd, nmap, i, rc;
 591         struct stat st;
 592         prmap_t *prmapp, *pmp;
 593         ssize_t n;
 594 
 595         (void) proc_snprintf(mapname, sizeof (mapname),
 596             "/proc/%d/rmap", (int)Pstatus(Pr)->pr_pid);
 597 
 598         if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
 599                 if (mapfd >= 0)
 600                         (void) close(mapfd);
 601                 return (perr(mapname));
 602         }
 603 
 604         nmap = st.st_size / sizeof (prmap_t);
 605         prmapp = malloc((nmap + 1) * sizeof (prmap_t));
 606 
 607         if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prmap_t), 0L)) < 0) {
 608                 (void) close(mapfd);
 609                 free(prmapp);
 610                 return (perr("read rmap"));
 611         }
 612 
 613         (void) close(mapfd);
 614         nmap = n / sizeof (prmap_t);
 615 
 616         for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
 617                 if ((rc = func(cd, pmp, NULL)) != 0) {
 618                         free(prmapp);
 619                         return (rc);
 620                 }
 621         }
 622 
 623         free(prmapp);
 624         return (0);
 625 }
 626 
 627 static int
 628 xmapping_iter(struct ps_prochandle *Pr, proc_xmap_f *func, void *cd, int doswap)
 629 {
 630         char mapname[PATH_MAX];
 631         int mapfd, nmap, i, rc;
 632         struct stat st;
 633         prxmap_t *prmapp, *pmp;
 634         ssize_t n;
 635 
 636         (void) proc_snprintf(mapname, sizeof (mapname),
 637             "/proc/%d/xmap", (int)Pstatus(Pr)->pr_pid);
 638 
 639         if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
 640                 if (mapfd >= 0)
 641                         (void) close(mapfd);
 642                 return (perr(mapname));
 643         }
 644 
 645         nmap = st.st_size / sizeof (prxmap_t);
 646         nmap *= 2;
 647 again:
 648         prmapp = malloc((nmap + 1) * sizeof (prxmap_t));
 649 
 650         if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prxmap_t), 0)) < 0) {
 651                 (void) close(mapfd);
 652                 free(prmapp);
 653                 return (perr("read xmap"));
 654         }
 655 
 656         if (nmap < n / sizeof (prxmap_t)) {