10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51914Scasper * Common Development and Distribution License (the "License").
61914Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
212685Sakolb
220Sstevel@tonic-gate /*
235925Ssp92102 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <stdio.h>
281914Scasper #include <stdio_ext.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <fcntl.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <dirent.h>
350Sstevel@tonic-gate #include <limits.h>
360Sstevel@tonic-gate #include <link.h>
370Sstevel@tonic-gate #include <libelf.h>
380Sstevel@tonic-gate #include <sys/types.h>
392685Sakolb #include <signal.h>
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate #include <sys/mkdev.h>
422685Sakolb #include <sys/mman.h>
432685Sakolb #include <sys/lgrp_user.h>
440Sstevel@tonic-gate #include <libproc.h>
45*7675SEdward.Pilatowicz@Sun.COM
46*7675SEdward.Pilatowicz@Sun.COM #include "pmap_common.h"
470Sstevel@tonic-gate
482685Sakolb #define KILOBYTE 1024
492685Sakolb #define MEGABYTE (KILOBYTE * KILOBYTE)
502685Sakolb #define GIGABYTE (KILOBYTE * KILOBYTE * KILOBYTE)
512685Sakolb
522685Sakolb /*
532685Sakolb * Round up the value to the nearest kilobyte
542685Sakolb */
552685Sakolb #define ROUNDUP_KB(x) (((x) + (KILOBYTE - 1)) / KILOBYTE)
562685Sakolb
572685Sakolb /*
582685Sakolb * The alignment should be a power of 2.
592685Sakolb */
602685Sakolb #define P2ALIGN(x, align) ((x) & -(align))
612685Sakolb
622685Sakolb #define INVALID_ADDRESS (uintptr_t)(-1)
632685Sakolb
640Sstevel@tonic-gate struct totals {
650Sstevel@tonic-gate ulong_t total_size;
660Sstevel@tonic-gate ulong_t total_swap;
670Sstevel@tonic-gate ulong_t total_rss;
680Sstevel@tonic-gate ulong_t total_anon;
690Sstevel@tonic-gate ulong_t total_locked;
700Sstevel@tonic-gate };
710Sstevel@tonic-gate
722685Sakolb /*
732685Sakolb * -L option requires per-page information. The information is presented in an
742685Sakolb * array of page_descr structures.
752685Sakolb */
762685Sakolb typedef struct page_descr {
772685Sakolb uintptr_t pd_start; /* start address of a page */
782685Sakolb size_t pd_pagesize; /* page size in bytes */
792685Sakolb lgrp_id_t pd_lgrp; /* lgroup of memory backing the page */
802685Sakolb int pd_valid; /* valid page description if non-zero */
812685Sakolb } page_descr_t;
822685Sakolb
832685Sakolb /*
842685Sakolb * Per-page information for a memory chunk.
852685Sakolb * The meminfo(2) system call accepts up to MAX_MEMINFO_CNT pages at once.
862685Sakolb * When we need to scan larger ranges we divide them in MAX_MEMINFO_CNT sized
872685Sakolb * chunks. The chunk information is stored in the memory_chunk structure.
882685Sakolb */
892685Sakolb typedef struct memory_chunk {
902685Sakolb page_descr_t page_info[MAX_MEMINFO_CNT];
912685Sakolb uintptr_t end_addr;
922685Sakolb uintptr_t chunk_start; /* Starting address */
932685Sakolb uintptr_t chunk_end; /* chunk_end is always <= end_addr */
942685Sakolb size_t page_size;
952685Sakolb int page_index; /* Current page */
962685Sakolb int page_count; /* Number of pages */
972685Sakolb } memory_chunk_t;
982685Sakolb
992685Sakolb static volatile int interrupt;
1002685Sakolb
1010Sstevel@tonic-gate typedef int proc_xmap_f(void *, const prxmap_t *, const char *, int, int);
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate static int xmapping_iter(struct ps_prochandle *, proc_xmap_f *, void *,
1040Sstevel@tonic-gate int);
1050Sstevel@tonic-gate static int rmapping_iter(struct ps_prochandle *, proc_map_f *, void *);
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate static int look_map(void *, const prmap_t *, const char *);
1080Sstevel@tonic-gate static int look_smap(void *, const prxmap_t *, const char *, int, int);
1090Sstevel@tonic-gate static int look_xmap(void *, const prxmap_t *, const char *, int, int);
1100Sstevel@tonic-gate static int look_xmap_nopgsz(void *, const prxmap_t *, const char *,
1110Sstevel@tonic-gate int, int);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate static int gather_map(void *, const prmap_t *, const char *);
1140Sstevel@tonic-gate static int gather_xmap(void *, const prxmap_t *, const char *, int, int);
1150Sstevel@tonic-gate static int iter_map(proc_map_f *, void *);
1160Sstevel@tonic-gate static int iter_xmap(proc_xmap_f *, void *);
1172685Sakolb static int parse_addr_range(char *, uintptr_t *, uintptr_t *);
1182685Sakolb static void mem_chunk_init(memory_chunk_t *, uintptr_t, size_t);
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate static int perr(char *);
1210Sstevel@tonic-gate static void printK(long, int);
1220Sstevel@tonic-gate static char *mflags(uint_t);
1230Sstevel@tonic-gate
1242685Sakolb static size_t get_contiguous_region(memory_chunk_t *, uintptr_t,
1252685Sakolb uintptr_t, size_t, lgrp_id_t *);
1262685Sakolb static void mem_chunk_get(memory_chunk_t *, uintptr_t);
1272685Sakolb static lgrp_id_t addr_to_lgrp(memory_chunk_t *, uintptr_t, size_t *);
1282685Sakolb static char *lgrp2str(lgrp_id_t);
1292685Sakolb
1302685Sakolb static int address_in_range(uintptr_t, uintptr_t, size_t);
1312685Sakolb static size_t adjust_addr_range(uintptr_t, uintptr_t, size_t,
1322685Sakolb uintptr_t *, uintptr_t *);
1332685Sakolb
1340Sstevel@tonic-gate static int lflag = 0;
1352685Sakolb static int Lflag = 0;
1360Sstevel@tonic-gate static int aflag = 0;
1372685Sakolb
1382685Sakolb /*
1392685Sakolb * The -A address range is represented as a pair of addresses
1402685Sakolb * <start_addr, end_addr>. Either one of these may be unspecified (set to
1412685Sakolb * INVALID_ADDRESS). If both are unspecified, no address range restrictions are
1422685Sakolb * in place.
1432685Sakolb */
1442685Sakolb static uintptr_t start_addr = INVALID_ADDRESS;
1452685Sakolb static uintptr_t end_addr = INVALID_ADDRESS;
1462685Sakolb
1470Sstevel@tonic-gate static int addr_width, size_width;
1480Sstevel@tonic-gate static char *command;
1490Sstevel@tonic-gate static char *procname;
1500Sstevel@tonic-gate static struct ps_prochandle *Pr;
1510Sstevel@tonic-gate
1522685Sakolb static void intr(int);
1532685Sakolb
1540Sstevel@tonic-gate typedef struct {
1550Sstevel@tonic-gate prxmap_t md_xmap;
1560Sstevel@tonic-gate prmap_t md_map;
1570Sstevel@tonic-gate char *md_objname;
1582685Sakolb boolean_t md_last;
1590Sstevel@tonic-gate int md_doswap;
1600Sstevel@tonic-gate } mapdata_t;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate static mapdata_t *maps;
1630Sstevel@tonic-gate static int map_count;
1640Sstevel@tonic-gate static int map_alloc;
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate static lwpstack_t *stacks = NULL;
1670Sstevel@tonic-gate static uint_t nstacks = 0;
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate #define MAX_TRIES 5
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate static int
getstack(void * data,const lwpstatus_t * lsp)1720Sstevel@tonic-gate getstack(void *data, const lwpstatus_t *lsp)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate int *np = (int *)data;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate if (Plwp_alt_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
1770Sstevel@tonic-gate stacks[*np].lwps_stack.ss_flags |= SS_ONSTACK;
1780Sstevel@tonic-gate stacks[*np].lwps_lwpid = lsp->pr_lwpid;
1790Sstevel@tonic-gate (*np)++;
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate if (Plwp_main_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
1830Sstevel@tonic-gate stacks[*np].lwps_lwpid = lsp->pr_lwpid;
1840Sstevel@tonic-gate (*np)++;
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate return (0);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate int
main(int argc,char ** argv)1910Sstevel@tonic-gate main(int argc, char **argv)
1920Sstevel@tonic-gate {
1932685Sakolb int rflag = 0, sflag = 0, xflag = 0, Fflag = 0;
1940Sstevel@tonic-gate int errflg = 0, Sflag = 0;
1950Sstevel@tonic-gate int rc = 0;
1960Sstevel@tonic-gate int opt;
1970Sstevel@tonic-gate const char *bar8 = "-------";
1980Sstevel@tonic-gate const char *bar16 = "----------";
1990Sstevel@tonic-gate const char *bar;
2000Sstevel@tonic-gate struct rlimit rlim;
2010Sstevel@tonic-gate struct stat64 statbuf;
2020Sstevel@tonic-gate char buf[128];
2030Sstevel@tonic-gate int mapfd;
2047032Sakolb int prg_gflags = PGRAB_RDONLY;
2057032Sakolb int prr_flags = 0;
2067032Sakolb boolean_t use_agent_lwp = B_FALSE;
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL)
2090Sstevel@tonic-gate command++;
2100Sstevel@tonic-gate else
2110Sstevel@tonic-gate command = argv[0];
2120Sstevel@tonic-gate
2132685Sakolb while ((opt = getopt(argc, argv, "arsxSlLFA:")) != EOF) {
2140Sstevel@tonic-gate switch (opt) {
2150Sstevel@tonic-gate case 'a': /* include shared mappings in -[xS] */
2160Sstevel@tonic-gate aflag = 1;
2170Sstevel@tonic-gate break;
2180Sstevel@tonic-gate case 'r': /* show reserved mappings */
2190Sstevel@tonic-gate rflag = 1;
2200Sstevel@tonic-gate break;
2210Sstevel@tonic-gate case 's': /* show hardware page sizes */
2220Sstevel@tonic-gate sflag = 1;
2230Sstevel@tonic-gate break;
2240Sstevel@tonic-gate case 'S': /* show swap reservations */
2250Sstevel@tonic-gate Sflag = 1;
2260Sstevel@tonic-gate break;
2270Sstevel@tonic-gate case 'x': /* show extended mappings */
2280Sstevel@tonic-gate xflag = 1;
2290Sstevel@tonic-gate break;
2300Sstevel@tonic-gate case 'l': /* show unresolved link map names */
2310Sstevel@tonic-gate lflag = 1;
2320Sstevel@tonic-gate break;
2332685Sakolb case 'L': /* show lgroup information */
2342685Sakolb Lflag = 1;
2357032Sakolb use_agent_lwp = B_TRUE;
2362685Sakolb break;
2372685Sakolb case 'F': /* force grabbing (no O_EXCL) */
2382685Sakolb Fflag = PGRAB_FORCE;
2392685Sakolb break;
2402685Sakolb case 'A':
2412685Sakolb if (parse_addr_range(optarg, &start_addr, &end_addr)
2422685Sakolb != 0)
2432685Sakolb errflg++;
2440Sstevel@tonic-gate break;
2450Sstevel@tonic-gate default:
2460Sstevel@tonic-gate errflg = 1;
2470Sstevel@tonic-gate break;
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate argc -= optind;
2520Sstevel@tonic-gate argv += optind;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if ((Sflag && (xflag || rflag || sflag)) || (xflag && rflag) ||
2552685Sakolb (aflag && (!xflag && !Sflag)) ||
2562685Sakolb (Lflag && (xflag || Sflag))) {
2570Sstevel@tonic-gate errflg = 1;
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate if (errflg || argc <= 0) {
2610Sstevel@tonic-gate (void) fprintf(stderr,
2622685Sakolb "usage:\t%s [-rslF] [-A start[,end]] { pid | core } ...\n",
2632685Sakolb command);
2640Sstevel@tonic-gate (void) fprintf(stderr,
2650Sstevel@tonic-gate "\t\t(report process address maps)\n");
2660Sstevel@tonic-gate (void) fprintf(stderr,
2672685Sakolb "\t%s -L [-rslF] [-A start[,end]] pid ...\n", command);
2682685Sakolb (void) fprintf(stderr,
2692685Sakolb "\t\t(report process address maps lgroups mappings)\n");
2702685Sakolb (void) fprintf(stderr,
2712685Sakolb "\t%s -x [-aslF] [-A start[,end]] pid ...\n", command);
2720Sstevel@tonic-gate (void) fprintf(stderr,
2730Sstevel@tonic-gate "\t\t(show resident/anon/locked mapping details)\n");
2740Sstevel@tonic-gate (void) fprintf(stderr,
2752685Sakolb "\t%s -S [-alF] [-A start[,end]] { pid | core } ...\n",
2762685Sakolb command);
2770Sstevel@tonic-gate (void) fprintf(stderr,
2780Sstevel@tonic-gate "\t\t(show swap reservations)\n\n");
2790Sstevel@tonic-gate (void) fprintf(stderr,
2800Sstevel@tonic-gate "\t-a: include shared mappings in -[xS] summary\n");
2810Sstevel@tonic-gate (void) fprintf(stderr,
2820Sstevel@tonic-gate "\t-r: show reserved address maps\n");
2830Sstevel@tonic-gate (void) fprintf(stderr,
2840Sstevel@tonic-gate "\t-s: show hardware page sizes\n");
2850Sstevel@tonic-gate (void) fprintf(stderr,
2860Sstevel@tonic-gate "\t-l: show unresolved dynamic linker map names\n");
2870Sstevel@tonic-gate (void) fprintf(stderr,
2880Sstevel@tonic-gate "\t-F: force grabbing of the target process\n");
2892685Sakolb (void) fprintf(stderr,
2902685Sakolb "\t-L: show lgroup mappings\n");
2912685Sakolb (void) fprintf(stderr,
2922685Sakolb "\t-A start,end: limit output to the specified range\n");
2930Sstevel@tonic-gate return (2);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate * Make sure we'll have enough file descriptors to handle a target
2980Sstevel@tonic-gate * that has many many mappings.
2990Sstevel@tonic-gate */
3000Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
3010Sstevel@tonic-gate rlim.rlim_cur = rlim.rlim_max;
3020Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &rlim);
3031914Scasper (void) enable_extended_FILE_stdio(-1, -1);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate
3067032Sakolb /*
3077032Sakolb * The implementation of -L option creates an agent LWP in the target
3087032Sakolb * process address space. The agent LWP issues meminfo(2) system calls
3097032Sakolb * on behalf of the target process. If we are interrupted prematurely,
3107032Sakolb * the target process remains in the stopped state with the agent still
3117032Sakolb * attached to it. To prevent such situation we catch signals from
3127032Sakolb * terminal and terminate gracefully.
3137032Sakolb */
3147032Sakolb if (use_agent_lwp) {
3157032Sakolb /*
3167032Sakolb * Buffer output to stdout, stderr while process is grabbed.
3177032Sakolb * Prevents infamous deadlocks due to pmap `pgrep xterm` and
3187032Sakolb * other variants.
3197032Sakolb */
3207032Sakolb (void) proc_initstdio();
3217032Sakolb
3227032Sakolb prg_gflags = PGRAB_RETAIN | Fflag;
3237032Sakolb prr_flags = PRELEASE_RETAIN;
3247032Sakolb
3257032Sakolb if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
3267032Sakolb (void) sigset(SIGHUP, intr);
3277032Sakolb if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
3287032Sakolb (void) sigset(SIGINT, intr);
3297032Sakolb if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
3307032Sakolb (void) sigset(SIGQUIT, intr);
3317032Sakolb (void) sigset(SIGPIPE, intr);
3327032Sakolb (void) sigset(SIGTERM, intr);
3337032Sakolb }
3347032Sakolb
3350Sstevel@tonic-gate while (argc-- > 0) {
3360Sstevel@tonic-gate char *arg;
3370Sstevel@tonic-gate int gcode;
3380Sstevel@tonic-gate psinfo_t psinfo;
3390Sstevel@tonic-gate int tries = 0;
3402685Sakolb
3417032Sakolb if (use_agent_lwp)
3427032Sakolb (void) proc_flushstdio();
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
3452685Sakolb prg_gflags, &gcode)) == NULL) {
3460Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
3470Sstevel@tonic-gate command, arg, Pgrab_error(gcode));
3480Sstevel@tonic-gate rc++;
3490Sstevel@tonic-gate continue;
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate procname = arg; /* for perr() */
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate addr_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 16 : 8;
3550Sstevel@tonic-gate size_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 11 : 8;
3560Sstevel@tonic-gate bar = addr_width == 8 ? bar8 : bar16;
3570Sstevel@tonic-gate (void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
3580Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo);
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate if (Pstate(Pr) != PS_DEAD) {
3610Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf),
3620Sstevel@tonic-gate "/proc/%d/map", (int)psinfo.pr_pid);
3630Sstevel@tonic-gate if ((mapfd = open(buf, O_RDONLY)) < 0) {
3640Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot "
3650Sstevel@tonic-gate "examine %s: lost control of "
3660Sstevel@tonic-gate "process\n", command, arg);
3670Sstevel@tonic-gate rc++;
3682685Sakolb Prelease(Pr, prr_flags);
3690Sstevel@tonic-gate continue;
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate } else {
3720Sstevel@tonic-gate mapfd = -1;
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate again:
3760Sstevel@tonic-gate map_count = 0;
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate if (Pstate(Pr) == PS_DEAD) {
3790Sstevel@tonic-gate (void) printf("core '%s' of %d:\t%.70s\n",
3800Sstevel@tonic-gate arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
3810Sstevel@tonic-gate
3822685Sakolb if (rflag || sflag || xflag || Sflag || Lflag) {
3830Sstevel@tonic-gate (void) printf(" -%c option is not compatible "
3840Sstevel@tonic-gate "with core files\n", xflag ? 'x' :
3852685Sakolb sflag ? 's' : rflag ? 'r' :
3862685Sakolb Lflag ? 'L' : 'S');
3872685Sakolb Prelease(Pr, prr_flags);
3880Sstevel@tonic-gate rc++;
3890Sstevel@tonic-gate continue;
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate } else {
3930Sstevel@tonic-gate (void) printf("%d:\t%.70s\n",
3940Sstevel@tonic-gate (int)psinfo.pr_pid, psinfo.pr_psargs);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate if (!(Pstatus(Pr)->pr_flags & PR_ISSYS)) {
3980Sstevel@tonic-gate struct totals t;
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate * Since we're grabbing the process readonly, we need
4020Sstevel@tonic-gate * to make sure the address space doesn't change during
4030Sstevel@tonic-gate * execution.
4040Sstevel@tonic-gate */
4050Sstevel@tonic-gate if (Pstate(Pr) != PS_DEAD) {
4060Sstevel@tonic-gate if (tries++ == MAX_TRIES) {
4072685Sakolb Prelease(Pr, prr_flags);
4080Sstevel@tonic-gate (void) close(mapfd);
4090Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot "
4100Sstevel@tonic-gate "examine %s: address space is "
4110Sstevel@tonic-gate "changing\n", command, arg);
4120Sstevel@tonic-gate continue;
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate if (fstat64(mapfd, &statbuf) != 0) {
4162685Sakolb Prelease(Pr, prr_flags);
4170Sstevel@tonic-gate (void) close(mapfd);
4180Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot "
4190Sstevel@tonic-gate "examine %s: lost control of "
4200Sstevel@tonic-gate "process\n", command, arg);
4210Sstevel@tonic-gate continue;
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate nstacks = psinfo.pr_nlwp * 2;
4260Sstevel@tonic-gate stacks = calloc(nstacks, sizeof (stacks[0]));
4270Sstevel@tonic-gate if (stacks != NULL) {
4280Sstevel@tonic-gate int n = 0;
4290Sstevel@tonic-gate (void) Plwp_iter(Pr, getstack, &n);
4300Sstevel@tonic-gate qsort(stacks, nstacks, sizeof (stacks[0]),
4310Sstevel@tonic-gate cmpstacks);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate (void) memset(&t, 0, sizeof (t));
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate if (Pgetauxval(Pr, AT_BASE) != -1L &&
4370Sstevel@tonic-gate Prd_agent(Pr) == NULL) {
4380Sstevel@tonic-gate (void) fprintf(stderr, "%s: warning: "
4390Sstevel@tonic-gate "librtld_db failed to initialize; "
4400Sstevel@tonic-gate "shared library information will not be "
4410Sstevel@tonic-gate "available\n", command);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate /*
4450Sstevel@tonic-gate * Gather data
4460Sstevel@tonic-gate */
4470Sstevel@tonic-gate if (xflag)
4480Sstevel@tonic-gate rc += xmapping_iter(Pr, gather_xmap, NULL, 0);
4490Sstevel@tonic-gate else if (Sflag)
4500Sstevel@tonic-gate rc += xmapping_iter(Pr, gather_xmap, NULL, 1);
4510Sstevel@tonic-gate else {
4520Sstevel@tonic-gate if (rflag)
4530Sstevel@tonic-gate rc += rmapping_iter(Pr, gather_map,
4540Sstevel@tonic-gate NULL);
4550Sstevel@tonic-gate else if (sflag)
4560Sstevel@tonic-gate rc += xmapping_iter(Pr, gather_xmap,
4570Sstevel@tonic-gate NULL, 0);
458*7675SEdward.Pilatowicz@Sun.COM else if (lflag)
459*7675SEdward.Pilatowicz@Sun.COM rc += Pmapping_iter(Pr,
460*7675SEdward.Pilatowicz@Sun.COM gather_map, NULL);
4610Sstevel@tonic-gate else
462*7675SEdward.Pilatowicz@Sun.COM rc += Pmapping_iter_resolved(Pr,
463*7675SEdward.Pilatowicz@Sun.COM gather_map, NULL);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate * Ensure mappings are consistent.
4680Sstevel@tonic-gate */
4690Sstevel@tonic-gate if (Pstate(Pr) != PS_DEAD) {
4700Sstevel@tonic-gate struct stat64 newbuf;
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate if (fstat64(mapfd, &newbuf) != 0 ||
4730Sstevel@tonic-gate memcmp(&newbuf.st_mtim, &statbuf.st_mtim,
4740Sstevel@tonic-gate sizeof (newbuf.st_mtim)) != 0) {
4750Sstevel@tonic-gate if (stacks != NULL) {
4760Sstevel@tonic-gate free(stacks);
4770Sstevel@tonic-gate stacks = NULL;
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate goto again;
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate /*
4840Sstevel@tonic-gate * Display data.
4850Sstevel@tonic-gate */
4860Sstevel@tonic-gate if (xflag) {
4870Sstevel@tonic-gate (void) printf("%*s%*s%*s%*s%*s "
4880Sstevel@tonic-gate "%sMode Mapped File\n",
4890Sstevel@tonic-gate addr_width, "Address",
4900Sstevel@tonic-gate size_width, "Kbytes",
4910Sstevel@tonic-gate size_width, "RSS",
4920Sstevel@tonic-gate size_width, "Anon",
4930Sstevel@tonic-gate size_width, "Locked",
4940Sstevel@tonic-gate sflag ? "Pgsz " : "");
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate rc += iter_xmap(sflag ? look_xmap :
4970Sstevel@tonic-gate look_xmap_nopgsz, &t);
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate (void) printf("%s%s %s %s %s %s\n",
5000Sstevel@tonic-gate addr_width == 8 ? "-" : "------",
5010Sstevel@tonic-gate bar, bar, bar, bar, bar);
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate (void) printf("%stotal Kb", addr_width == 16 ?
5040Sstevel@tonic-gate " " : "");
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate printK(t.total_size, size_width);
5070Sstevel@tonic-gate printK(t.total_rss, size_width);
5080Sstevel@tonic-gate printK(t.total_anon, size_width);
5090Sstevel@tonic-gate printK(t.total_locked, size_width);
5100Sstevel@tonic-gate
5110Sstevel@tonic-gate (void) printf("\n");
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate } else if (Sflag) {
5142685Sakolb (void) printf("%*s%*s%*s Mode"
5152685Sakolb " Mapped File\n",
5160Sstevel@tonic-gate addr_width, "Address",
5170Sstevel@tonic-gate size_width, "Kbytes",
5180Sstevel@tonic-gate size_width, "Swap");
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate rc += iter_xmap(look_xmap_nopgsz, &t);
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate (void) printf("%s%s %s %s\n",
5230Sstevel@tonic-gate addr_width == 8 ? "-" : "------",
5240Sstevel@tonic-gate bar, bar, bar);
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate (void) printf("%stotal Kb", addr_width == 16 ?
5270Sstevel@tonic-gate " " : "");
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate printK(t.total_size, size_width);
5300Sstevel@tonic-gate printK(t.total_swap, size_width);
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate (void) printf("\n");
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate } else {
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate if (rflag) {
5370Sstevel@tonic-gate rc += iter_map(look_map, &t);
5380Sstevel@tonic-gate } else if (sflag) {
5392685Sakolb if (Lflag) {
5402685Sakolb (void) printf("%*s %*s %4s"
5412685Sakolb " %-6s %s %s\n",
5422685Sakolb addr_width, "Address",
5432685Sakolb size_width,
5442685Sakolb "Bytes", "Pgsz", "Mode ",
5452685Sakolb "Lgrp", "Mapped File");
5462685Sakolb rc += iter_xmap(look_smap, &t);
5472685Sakolb } else {
5482685Sakolb (void) printf("%*s %*s %4s"
5492685Sakolb " %-6s %s\n",
5502685Sakolb addr_width, "Address",
5512685Sakolb size_width,
5522685Sakolb "Bytes", "Pgsz", "Mode ",
5532685Sakolb "Mapped File");
5542685Sakolb rc += iter_xmap(look_smap, &t);
5552685Sakolb }
5560Sstevel@tonic-gate } else {
5570Sstevel@tonic-gate rc += iter_map(look_map, &t);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate (void) printf(" %stotal %*luK\n",
5610Sstevel@tonic-gate addr_width == 16 ?
5620Sstevel@tonic-gate " " : "",
5630Sstevel@tonic-gate size_width, t.total_size);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate if (stacks != NULL) {
5670Sstevel@tonic-gate free(stacks);
5680Sstevel@tonic-gate stacks = NULL;
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate
5732685Sakolb Prelease(Pr, prr_flags);
5740Sstevel@tonic-gate if (mapfd != -1)
5750Sstevel@tonic-gate (void) close(mapfd);
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate
5787032Sakolb if (use_agent_lwp)
5797032Sakolb (void) proc_finistdio();
5807032Sakolb
5810Sstevel@tonic-gate return (rc);
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate static int
rmapping_iter(struct ps_prochandle * Pr,proc_map_f * func,void * cd)5850Sstevel@tonic-gate rmapping_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate char mapname[PATH_MAX];
5880Sstevel@tonic-gate int mapfd, nmap, i, rc;
5890Sstevel@tonic-gate struct stat st;
5900Sstevel@tonic-gate prmap_t *prmapp, *pmp;
5910Sstevel@tonic-gate ssize_t n;
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate (void) snprintf(mapname, sizeof (mapname),
5940Sstevel@tonic-gate "/proc/%d/rmap", (int)Pstatus(Pr)->pr_pid);
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
5970Sstevel@tonic-gate if (mapfd >= 0)
5980Sstevel@tonic-gate (void) close(mapfd);
5990Sstevel@tonic-gate return (perr(mapname));
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate nmap = st.st_size / sizeof (prmap_t);
6030Sstevel@tonic-gate prmapp = malloc((nmap + 1) * sizeof (prmap_t));
6040Sstevel@tonic-gate
6050Sstevel@tonic-gate if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prmap_t), 0L)) < 0) {
6060Sstevel@tonic-gate (void) close(mapfd);
6070Sstevel@tonic-gate free(prmapp);
6080Sstevel@tonic-gate return (perr("read rmap"));
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate (void) close(mapfd);
6120Sstevel@tonic-gate nmap = n / sizeof (prmap_t);
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
6150Sstevel@tonic-gate if ((rc = func(cd, pmp, NULL)) != 0) {
6160Sstevel@tonic-gate free(prmapp);
6170Sstevel@tonic-gate return (rc);
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate free(prmapp);
6220Sstevel@tonic-gate return (0);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate static int
xmapping_iter(struct ps_prochandle * Pr,proc_xmap_f * func,void * cd,int doswap)6260Sstevel@tonic-gate xmapping_iter(struct ps_prochandle *Pr, proc_xmap_f *func, void *cd, int doswap)
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate char mapname[PATH_MAX];
6290Sstevel@tonic-gate int mapfd, nmap, i, rc;
6300Sstevel@tonic-gate struct stat st;
6310Sstevel@tonic-gate prxmap_t *prmapp, *pmp;
6320Sstevel@tonic-gate ssize_t n;
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate (void) snprintf(mapname, sizeof (mapname),
6350Sstevel@tonic-gate "/proc/%d/xmap", (int)Pstatus(Pr)->pr_pid);
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
6380Sstevel@tonic-gate if (mapfd >= 0)
6390Sstevel@tonic-gate (void) close(mapfd);
6400Sstevel@tonic-gate return (perr(mapname));
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate nmap = st.st_size / sizeof (prxmap_t);
6440Sstevel@tonic-gate nmap *= 2;
6450Sstevel@tonic-gate again:
6460Sstevel@tonic-gate prmapp = malloc((nmap + 1) * sizeof (prxmap_t));
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prxmap_t), 0)) < 0) {
6490Sstevel@tonic-gate (void) close(mapfd);
6500Sstevel@tonic-gate free(prmapp);
6510Sstevel@tonic-gate return (perr("read xmap"));
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate
6540Sstevel@tonic-gate if (nmap < n / sizeof (prxmap_t)) {
6550Sstevel@tonic-gate free(prmapp);
6560Sstevel@tonic-gate nmap *= 2;
6570Sstevel@tonic-gate goto again;
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate (void) close(mapfd);
6610Sstevel@tonic-gate nmap = n / sizeof (prxmap_t);
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
6640Sstevel@tonic-gate if ((rc = func(cd, pmp, NULL, i == nmap - 1, doswap)) != 0) {
6650Sstevel@tonic-gate free(prmapp);
6660Sstevel@tonic-gate return (rc);
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate
6702685Sakolb /*
6712685Sakolb * Mark the last element.
6722685Sakolb */
6732685Sakolb if (map_count > 0)
6742685Sakolb maps[map_count - 1].md_last = B_TRUE;
6752685Sakolb
6760Sstevel@tonic-gate free(prmapp);
6770Sstevel@tonic-gate return (0);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate /*ARGSUSED*/
6810Sstevel@tonic-gate static int
look_map(void * data,const prmap_t * pmp,const char * object_name)6820Sstevel@tonic-gate look_map(void *data, const prmap_t *pmp, const char *object_name)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate struct totals *t = data;
6850Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Pr);
6862685Sakolb size_t size;
6870Sstevel@tonic-gate char mname[PATH_MAX];
6880Sstevel@tonic-gate char *lname = NULL;
6892685Sakolb size_t psz = pmp->pr_pagesize;
6902685Sakolb uintptr_t vaddr = pmp->pr_vaddr;
6912685Sakolb uintptr_t segment_end = vaddr + pmp->pr_size;
6922685Sakolb lgrp_id_t lgrp;
6932685Sakolb memory_chunk_t mchunk;
6940Sstevel@tonic-gate
6950Sstevel@tonic-gate /*
6960Sstevel@tonic-gate * If the mapping is not anon or not part of the heap, make a name
6970Sstevel@tonic-gate * for it. We don't want to report the heap as a.out's data.
6980Sstevel@tonic-gate */
6990Sstevel@tonic-gate if (!(pmp->pr_mflags & MA_ANON) ||
7002685Sakolb segment_end <= Psp->pr_brkbase ||
7010Sstevel@tonic-gate pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
702*7675SEdward.Pilatowicz@Sun.COM lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
7030Sstevel@tonic-gate mname, sizeof (mname));
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate
7060Sstevel@tonic-gate if (lname == NULL &&
7070Sstevel@tonic-gate ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD)) {
708*7675SEdward.Pilatowicz@Sun.COM lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
709*7675SEdward.Pilatowicz@Sun.COM pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate
7122685Sakolb /*
7132685Sakolb * Adjust the address range if -A is specified.
7142685Sakolb */
7152685Sakolb size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz,
7162685Sakolb &vaddr, &segment_end);
7172685Sakolb
7182685Sakolb if (size == 0)
7192685Sakolb return (0);
7202685Sakolb
7212685Sakolb if (!Lflag) {
7222685Sakolb /*
7232685Sakolb * Display the whole mapping
7242685Sakolb */
7252685Sakolb size = ROUNDUP_KB(size);
7262685Sakolb
7272685Sakolb (void) printf(lname ?
7282685Sakolb "%.*lX %*luK %-6s %s\n" :
7292685Sakolb "%.*lX %*luK %s\n",
7302685Sakolb addr_width, vaddr,
7312685Sakolb size_width - 1, size, mflags(pmp->pr_mflags), lname);
7322685Sakolb
7332685Sakolb t->total_size += size;
7342685Sakolb return (0);
7352685Sakolb }
7362685Sakolb
7372685Sakolb /*
7382685Sakolb * We need to display lgroups backing physical memory, so we break the
7392685Sakolb * segment into individual pages and coalesce pages with the same lgroup
7402685Sakolb * into one "segment".
7412685Sakolb */
7420Sstevel@tonic-gate
7432685Sakolb /*
7442685Sakolb * Initialize address descriptions for the mapping.
7452685Sakolb */
7462685Sakolb mem_chunk_init(&mchunk, segment_end, psz);
7472685Sakolb size = 0;
7482685Sakolb
7492685Sakolb /*
7502685Sakolb * Walk mapping (page by page) and display contiguous ranges of memory
7512685Sakolb * allocated to same lgroup.
7522685Sakolb */
7532685Sakolb do {
7542685Sakolb size_t size_contig;
7552685Sakolb
7562685Sakolb /*
7572685Sakolb * Get contiguous region of memory starting from vaddr allocated
7582685Sakolb * from the same lgroup.
7592685Sakolb */
7602685Sakolb size_contig = get_contiguous_region(&mchunk, vaddr,
7612685Sakolb segment_end, pmp->pr_pagesize, &lgrp);
7622685Sakolb
7632685Sakolb (void) printf(lname ? "%.*lX %*luK %-6s%s %s\n" :
7642685Sakolb "%.*lX %*luK %s %s\n",
7652685Sakolb addr_width, vaddr,
7662685Sakolb size_width - 1, size_contig / KILOBYTE,
7672685Sakolb mflags(pmp->pr_mflags),
7682685Sakolb lgrp2str(lgrp), lname);
7692685Sakolb
7702685Sakolb vaddr += size_contig;
7712685Sakolb size += size_contig;
7722685Sakolb } while (vaddr < segment_end && !interrupt);
7732685Sakolb
7742685Sakolb /* Update the total size */
7752685Sakolb t->total_size += ROUNDUP_KB(size);
7760Sstevel@tonic-gate return (0);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate static void
printK(long value,int width)7800Sstevel@tonic-gate printK(long value, int width)
7810Sstevel@tonic-gate {
7820Sstevel@tonic-gate if (value == 0)
7830Sstevel@tonic-gate (void) printf(width == 8 ? " -" : " -");
7840Sstevel@tonic-gate else
7850Sstevel@tonic-gate (void) printf(" %*lu", width - 1, value);
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate
7880Sstevel@tonic-gate static const char *
pagesize(const prxmap_t * pmp)7890Sstevel@tonic-gate pagesize(const prxmap_t *pmp)
7900Sstevel@tonic-gate {
7910Sstevel@tonic-gate int pagesize = pmp->pr_hatpagesize;
7920Sstevel@tonic-gate static char buf[32];
7930Sstevel@tonic-gate
7940Sstevel@tonic-gate if (pagesize == 0) {
7950Sstevel@tonic-gate return ("-"); /* no underlying HAT mapping */
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate
7982685Sakolb if (pagesize >= KILOBYTE && (pagesize % KILOBYTE) == 0) {
7992685Sakolb if ((pagesize % GIGABYTE) == 0)
8000Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%dG",
8012685Sakolb pagesize / GIGABYTE);
8022685Sakolb else if ((pagesize % MEGABYTE) == 0)
8030Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%dM",
8042685Sakolb pagesize / MEGABYTE);
8050Sstevel@tonic-gate else
8060Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%dK",
8072685Sakolb pagesize / KILOBYTE);
8080Sstevel@tonic-gate } else
8090Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%db", pagesize);
8100Sstevel@tonic-gate
8110Sstevel@tonic-gate return (buf);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate /*ARGSUSED*/
8150Sstevel@tonic-gate static int
look_smap(void * data,const prxmap_t * pmp,const char * object_name,int last,int doswap)8160Sstevel@tonic-gate look_smap(void *data,
8170Sstevel@tonic-gate const prxmap_t *pmp,
8180Sstevel@tonic-gate const char *object_name,
8190Sstevel@tonic-gate int last, int doswap)
8200Sstevel@tonic-gate {
8210Sstevel@tonic-gate struct totals *t = data;
8220Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Pr);
8232685Sakolb size_t size;
8240Sstevel@tonic-gate char mname[PATH_MAX];
8250Sstevel@tonic-gate char *lname = NULL;
8260Sstevel@tonic-gate const char *format;
8272685Sakolb size_t psz = pmp->pr_pagesize;
8282685Sakolb uintptr_t vaddr = pmp->pr_vaddr;
8292685Sakolb uintptr_t segment_end = vaddr + pmp->pr_size;
8302685Sakolb lgrp_id_t lgrp;
8312685Sakolb memory_chunk_t mchunk;
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate /*
8340Sstevel@tonic-gate * If the mapping is not anon or not part of the heap, make a name
8350Sstevel@tonic-gate * for it. We don't want to report the heap as a.out's data.
8360Sstevel@tonic-gate */
8370Sstevel@tonic-gate if (!(pmp->pr_mflags & MA_ANON) ||
8380Sstevel@tonic-gate pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
8390Sstevel@tonic-gate pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
840*7675SEdward.Pilatowicz@Sun.COM lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
8410Sstevel@tonic-gate mname, sizeof (mname));
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate
8440Sstevel@tonic-gate if (lname == NULL &&
8450Sstevel@tonic-gate ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD)) {
846*7675SEdward.Pilatowicz@Sun.COM lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
847*7675SEdward.Pilatowicz@Sun.COM pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate
8502685Sakolb /*
8512685Sakolb * Adjust the address range if -A is specified.
8522685Sakolb */
8532685Sakolb size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz,
8542685Sakolb &vaddr, &segment_end);
8552685Sakolb
8562685Sakolb if (size == 0)
8572685Sakolb return (0);
8582685Sakolb
8592685Sakolb if (!Lflag) {
8602685Sakolb /*
8612685Sakolb * Display the whole mapping
8622685Sakolb */
8632685Sakolb if (lname != NULL)
8642685Sakolb format = "%.*lX %*luK %4s %-6s %s\n";
8652685Sakolb else
8662685Sakolb format = "%.*lX %*luK %4s %s\n";
8672685Sakolb
8682685Sakolb size = ROUNDUP_KB(size);
8692685Sakolb
8702685Sakolb (void) printf(format, addr_width, vaddr, size_width - 1, size,
8712685Sakolb pagesize(pmp), mflags(pmp->pr_mflags), lname);
8722685Sakolb
8732685Sakolb t->total_size += size;
8742685Sakolb return (0);
8752685Sakolb }
8762685Sakolb
8770Sstevel@tonic-gate if (lname != NULL)
8782685Sakolb format = "%.*lX %*luK %4s %-6s%s %s\n";
8790Sstevel@tonic-gate else
8802685Sakolb format = "%.*lX %*luK %4s%s %s\n";
8810Sstevel@tonic-gate
8822685Sakolb /*
8832685Sakolb * We need to display lgroups backing physical memory, so we break the
8842685Sakolb * segment into individual pages and coalesce pages with the same lgroup
8852685Sakolb * into one "segment".
8862685Sakolb */
8872685Sakolb
8882685Sakolb /*
8892685Sakolb * Initialize address descriptions for the mapping.
8902685Sakolb */
8912685Sakolb mem_chunk_init(&mchunk, segment_end, psz);
8922685Sakolb size = 0;
8930Sstevel@tonic-gate
8942685Sakolb /*
8952685Sakolb * Walk mapping (page by page) and display contiguous ranges of memory
8962685Sakolb * allocated to same lgroup.
8972685Sakolb */
8982685Sakolb do {
8992685Sakolb size_t size_contig;
9002685Sakolb
9012685Sakolb /*
9022685Sakolb * Get contiguous region of memory starting from vaddr allocated
9032685Sakolb * from the same lgroup.
9042685Sakolb */
9052685Sakolb size_contig = get_contiguous_region(&mchunk, vaddr,
9062685Sakolb segment_end, pmp->pr_pagesize, &lgrp);
9072685Sakolb
9082685Sakolb (void) printf(format, addr_width, vaddr,
9092685Sakolb size_width - 1, size_contig / KILOBYTE,
9102685Sakolb pagesize(pmp), mflags(pmp->pr_mflags),
9112685Sakolb lgrp2str(lgrp), lname);
9122685Sakolb
9132685Sakolb vaddr += size_contig;
9142685Sakolb size += size_contig;
9152685Sakolb } while (vaddr < segment_end && !interrupt);
9162685Sakolb
9172685Sakolb t->total_size += ROUNDUP_KB(size);
9180Sstevel@tonic-gate return (0);
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate
9210Sstevel@tonic-gate #define ANON(x) ((aflag || (((x)->pr_mflags & MA_SHARED) == 0)) ? \
9220Sstevel@tonic-gate ((x)->pr_anon) : 0)
9230Sstevel@tonic-gate
9240Sstevel@tonic-gate /*ARGSUSED*/
9250Sstevel@tonic-gate static int
look_xmap(void * data,const prxmap_t * pmp,const char * object_name,int last,int doswap)9260Sstevel@tonic-gate look_xmap(void *data,
9270Sstevel@tonic-gate const prxmap_t *pmp,
9280Sstevel@tonic-gate const char *object_name,
9290Sstevel@tonic-gate int last, int doswap)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate struct totals *t = data;
9320Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Pr);
9330Sstevel@tonic-gate char mname[PATH_MAX];
9340Sstevel@tonic-gate char *lname = NULL;
9350Sstevel@tonic-gate char *ln;
9360Sstevel@tonic-gate
9370Sstevel@tonic-gate /*
9380Sstevel@tonic-gate * If the mapping is not anon or not part of the heap, make a name
9390Sstevel@tonic-gate * for it. We don't want to report the heap as a.out's data.
9400Sstevel@tonic-gate */
9410Sstevel@tonic-gate if (!(pmp->pr_mflags & MA_ANON) ||
9420Sstevel@tonic-gate pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
9430Sstevel@tonic-gate pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
944*7675SEdward.Pilatowicz@Sun.COM lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
9450Sstevel@tonic-gate mname, sizeof (mname));
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate if (lname != NULL) {
9490Sstevel@tonic-gate if ((ln = strrchr(lname, '/')) != NULL)
9500Sstevel@tonic-gate lname = ln + 1;
9510Sstevel@tonic-gate } else if ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD) {
952*7675SEdward.Pilatowicz@Sun.COM lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
953*7675SEdward.Pilatowicz@Sun.COM pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
9540Sstevel@tonic-gate }
9550Sstevel@tonic-gate
9560Sstevel@tonic-gate (void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr);
9570Sstevel@tonic-gate
9582685Sakolb printK(ROUNDUP_KB(pmp->pr_size), size_width);
9592685Sakolb printK(pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE), size_width);
9602685Sakolb printK(ANON(pmp) * (pmp->pr_pagesize / KILOBYTE), size_width);
9612685Sakolb printK(pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE), size_width);
9620Sstevel@tonic-gate (void) printf(lname ? " %4s %-6s %s\n" : " %4s %s\n",
9630Sstevel@tonic-gate pagesize(pmp), mflags(pmp->pr_mflags), lname);
9640Sstevel@tonic-gate
9652685Sakolb t->total_size += ROUNDUP_KB(pmp->pr_size);
9662685Sakolb t->total_rss += pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE);
9672685Sakolb t->total_anon += ANON(pmp) * (pmp->pr_pagesize / KILOBYTE);
9682685Sakolb t->total_locked += (pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE));
9690Sstevel@tonic-gate
9700Sstevel@tonic-gate return (0);
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate
9730Sstevel@tonic-gate /*ARGSUSED*/
9740Sstevel@tonic-gate static int
look_xmap_nopgsz(void * data,const prxmap_t * pmp,const char * object_name,int last,int doswap)9750Sstevel@tonic-gate look_xmap_nopgsz(void *data,
9760Sstevel@tonic-gate const prxmap_t *pmp,
9770Sstevel@tonic-gate const char *object_name,
9780Sstevel@tonic-gate int last, int doswap)
9790Sstevel@tonic-gate {
9800Sstevel@tonic-gate struct totals *t = data;
9810Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Pr);
9820Sstevel@tonic-gate char mname[PATH_MAX];
9830Sstevel@tonic-gate char *lname = NULL;
9840Sstevel@tonic-gate char *ln;
9850Sstevel@tonic-gate static uintptr_t prev_vaddr;
9860Sstevel@tonic-gate static size_t prev_size;
9870Sstevel@tonic-gate static offset_t prev_offset;
9880Sstevel@tonic-gate static int prev_mflags;
9890Sstevel@tonic-gate static char *prev_lname;
9900Sstevel@tonic-gate static char prev_mname[PATH_MAX];
9910Sstevel@tonic-gate static ulong_t prev_rss;
9920Sstevel@tonic-gate static ulong_t prev_anon;
9930Sstevel@tonic-gate static ulong_t prev_locked;
9940Sstevel@tonic-gate static ulong_t prev_swap;
9950Sstevel@tonic-gate int merged = 0;
9960Sstevel@tonic-gate static int first = 1;
9970Sstevel@tonic-gate ulong_t swap = 0;
9980Sstevel@tonic-gate int kperpage;
9990Sstevel@tonic-gate
10000Sstevel@tonic-gate /*
10010Sstevel@tonic-gate * Calculate swap reservations
10020Sstevel@tonic-gate */
10030Sstevel@tonic-gate if (pmp->pr_mflags & MA_SHARED) {
10040Sstevel@tonic-gate if (aflag && (pmp->pr_mflags & MA_NORESERVE) == 0) {
10050Sstevel@tonic-gate /* Swap reserved for entire non-ism SHM */
10060Sstevel@tonic-gate swap = pmp->pr_size / pmp->pr_pagesize;
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate } else if (pmp->pr_mflags & MA_NORESERVE) {
10090Sstevel@tonic-gate /* Swap reserved on fault for each anon page */
10100Sstevel@tonic-gate swap = pmp->pr_anon;
10110Sstevel@tonic-gate } else if (pmp->pr_mflags & MA_WRITE) {
10120Sstevel@tonic-gate /* Swap reserve for entire writable segment */
10130Sstevel@tonic-gate swap = pmp->pr_size / pmp->pr_pagesize;
10140Sstevel@tonic-gate }
10150Sstevel@tonic-gate
10160Sstevel@tonic-gate /*
10170Sstevel@tonic-gate * If the mapping is not anon or not part of the heap, make a name
10180Sstevel@tonic-gate * for it. We don't want to report the heap as a.out's data.
10190Sstevel@tonic-gate */
10200Sstevel@tonic-gate if (!(pmp->pr_mflags & MA_ANON) ||
10210Sstevel@tonic-gate pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
10220Sstevel@tonic-gate pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
1023*7675SEdward.Pilatowicz@Sun.COM lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
10240Sstevel@tonic-gate mname, sizeof (mname));
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate if (lname != NULL) {
10280Sstevel@tonic-gate if ((ln = strrchr(lname, '/')) != NULL)
10290Sstevel@tonic-gate lname = ln + 1;
10300Sstevel@tonic-gate } else if ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD) {
1031*7675SEdward.Pilatowicz@Sun.COM lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
1032*7675SEdward.Pilatowicz@Sun.COM pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
10330Sstevel@tonic-gate }
10340Sstevel@tonic-gate
10352685Sakolb kperpage = pmp->pr_pagesize / KILOBYTE;
10360Sstevel@tonic-gate
10372685Sakolb t->total_size += ROUNDUP_KB(pmp->pr_size);
10380Sstevel@tonic-gate t->total_rss += pmp->pr_rss * kperpage;
10390Sstevel@tonic-gate t->total_anon += ANON(pmp) * kperpage;
10400Sstevel@tonic-gate t->total_locked += pmp->pr_locked * kperpage;
10410Sstevel@tonic-gate t->total_swap += swap * kperpage;
10420Sstevel@tonic-gate
10430Sstevel@tonic-gate if (first == 1) {
10440Sstevel@tonic-gate first = 0;
10450Sstevel@tonic-gate prev_vaddr = pmp->pr_vaddr;
10460Sstevel@tonic-gate prev_size = pmp->pr_size;
10470Sstevel@tonic-gate prev_offset = pmp->pr_offset;
10480Sstevel@tonic-gate prev_mflags = pmp->pr_mflags;
10490Sstevel@tonic-gate if (lname == NULL) {
10500Sstevel@tonic-gate prev_lname = NULL;
10510Sstevel@tonic-gate } else {
10520Sstevel@tonic-gate (void) strcpy(prev_mname, lname);
10530Sstevel@tonic-gate prev_lname = prev_mname;
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate prev_rss = pmp->pr_rss * kperpage;
10560Sstevel@tonic-gate prev_anon = ANON(pmp) * kperpage;
10570Sstevel@tonic-gate prev_locked = pmp->pr_locked * kperpage;
10580Sstevel@tonic-gate prev_swap = swap * kperpage;
10590Sstevel@tonic-gate if (last == 0) {
10600Sstevel@tonic-gate return (0);
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate merged = 1;
10630Sstevel@tonic-gate } else if (prev_vaddr + prev_size == pmp->pr_vaddr &&
10640Sstevel@tonic-gate prev_mflags == pmp->pr_mflags &&
10650Sstevel@tonic-gate ((prev_mflags & MA_ISM) ||
10664450Seh208807 prev_offset + prev_size == pmp->pr_offset) &&
10670Sstevel@tonic-gate ((lname == NULL && prev_lname == NULL) ||
10684450Seh208807 (lname != NULL && prev_lname != NULL &&
10694450Seh208807 strcmp(lname, prev_lname) == 0))) {
10700Sstevel@tonic-gate prev_size += pmp->pr_size;
10710Sstevel@tonic-gate prev_rss += pmp->pr_rss * kperpage;
10720Sstevel@tonic-gate prev_anon += ANON(pmp) * kperpage;
10730Sstevel@tonic-gate prev_locked += pmp->pr_locked * kperpage;
10740Sstevel@tonic-gate prev_swap += swap * kperpage;
10750Sstevel@tonic-gate if (last == 0) {
10760Sstevel@tonic-gate return (0);
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate merged = 1;
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate
10810Sstevel@tonic-gate (void) printf("%.*lX", addr_width, (ulong_t)prev_vaddr);
10822685Sakolb printK(ROUNDUP_KB(prev_size), size_width);
10830Sstevel@tonic-gate
10840Sstevel@tonic-gate if (doswap)
10850Sstevel@tonic-gate printK(prev_swap, size_width);
10860Sstevel@tonic-gate else {
10870Sstevel@tonic-gate printK(prev_rss, size_width);
10880Sstevel@tonic-gate printK(prev_anon, size_width);
10890Sstevel@tonic-gate printK(prev_locked, size_width);
10900Sstevel@tonic-gate }
10912685Sakolb (void) printf(prev_lname ? " %-6s %s\n" : "%s\n",
10920Sstevel@tonic-gate mflags(prev_mflags), prev_lname);
10930Sstevel@tonic-gate
10940Sstevel@tonic-gate if (last == 0) {
10950Sstevel@tonic-gate prev_vaddr = pmp->pr_vaddr;
10960Sstevel@tonic-gate prev_size = pmp->pr_size;
10970Sstevel@tonic-gate prev_offset = pmp->pr_offset;
10980Sstevel@tonic-gate prev_mflags = pmp->pr_mflags;
10990Sstevel@tonic-gate if (lname == NULL) {
11000Sstevel@tonic-gate prev_lname = NULL;
11010Sstevel@tonic-gate } else {
11020Sstevel@tonic-gate (void) strcpy(prev_mname, lname);
11030Sstevel@tonic-gate prev_lname = prev_mname;
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate prev_rss = pmp->pr_rss * kperpage;
11060Sstevel@tonic-gate prev_anon = ANON(pmp) * kperpage;
11070Sstevel@tonic-gate prev_locked = pmp->pr_locked * kperpage;
11080Sstevel@tonic-gate prev_swap = swap * kperpage;
11090Sstevel@tonic-gate } else if (merged == 0) {
11100Sstevel@tonic-gate (void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr);
11112685Sakolb printK(ROUNDUP_KB(pmp->pr_size), size_width);
11120Sstevel@tonic-gate if (doswap)
11130Sstevel@tonic-gate printK(swap * kperpage, size_width);
11140Sstevel@tonic-gate else {
11150Sstevel@tonic-gate printK(pmp->pr_rss * kperpage, size_width);
11160Sstevel@tonic-gate printK(ANON(pmp) * kperpage, size_width);
11170Sstevel@tonic-gate printK(pmp->pr_locked * kperpage, size_width);
11180Sstevel@tonic-gate }
11190Sstevel@tonic-gate (void) printf(lname ? " %-6s %s\n" : " %s\n",
11200Sstevel@tonic-gate mflags(pmp->pr_mflags), lname);
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate
11230Sstevel@tonic-gate if (last != 0)
11240Sstevel@tonic-gate first = 1;
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate return (0);
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate
11290Sstevel@tonic-gate static int
perr(char * s)11300Sstevel@tonic-gate perr(char *s)
11310Sstevel@tonic-gate {
11320Sstevel@tonic-gate if (s)
11330Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", procname);
11340Sstevel@tonic-gate else
11350Sstevel@tonic-gate s = procname;
11360Sstevel@tonic-gate perror(s);
11370Sstevel@tonic-gate return (1);
11380Sstevel@tonic-gate }
11390Sstevel@tonic-gate
11400Sstevel@tonic-gate static char *
mflags(uint_t arg)11410Sstevel@tonic-gate mflags(uint_t arg)
11420Sstevel@tonic-gate {
11430Sstevel@tonic-gate static char code_buf[80];
11440Sstevel@tonic-gate char *str = code_buf;
11450Sstevel@tonic-gate
11460Sstevel@tonic-gate /*
11470Sstevel@tonic-gate * rwxsR
11480Sstevel@tonic-gate *
11490Sstevel@tonic-gate * r - segment is readable
11500Sstevel@tonic-gate * w - segment is writable
11510Sstevel@tonic-gate * x - segment is executable
11520Sstevel@tonic-gate * s - segment is shared
11530Sstevel@tonic-gate * R - segment is mapped MAP_NORESERVE
11540Sstevel@tonic-gate *
11550Sstevel@tonic-gate */
11560Sstevel@tonic-gate (void) sprintf(str, "%c%c%c%c%c%c",
11570Sstevel@tonic-gate arg & MA_READ ? 'r' : '-',
11580Sstevel@tonic-gate arg & MA_WRITE ? 'w' : '-',
11590Sstevel@tonic-gate arg & MA_EXEC ? 'x' : '-',
11600Sstevel@tonic-gate arg & MA_SHARED ? 's' : '-',
11610Sstevel@tonic-gate arg & MA_NORESERVE ? 'R' : '-',
11620Sstevel@tonic-gate arg & MA_RESERVED1 ? '*' : ' ');
11630Sstevel@tonic-gate
11640Sstevel@tonic-gate return (str);
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate
11670Sstevel@tonic-gate static mapdata_t *
nextmap(void)11680Sstevel@tonic-gate nextmap(void)
11690Sstevel@tonic-gate {
11700Sstevel@tonic-gate mapdata_t *newmaps;
11710Sstevel@tonic-gate int next;
11720Sstevel@tonic-gate
11730Sstevel@tonic-gate if (map_count == map_alloc) {
11740Sstevel@tonic-gate if (map_alloc == 0)
11750Sstevel@tonic-gate next = 16;
11760Sstevel@tonic-gate else
11770Sstevel@tonic-gate next = map_alloc * 2;
11780Sstevel@tonic-gate
11790Sstevel@tonic-gate newmaps = realloc(maps, next * sizeof (mapdata_t));
11800Sstevel@tonic-gate if (newmaps == NULL) {
11810Sstevel@tonic-gate (void) perr("failed to allocate maps");
11820Sstevel@tonic-gate exit(1);
11830Sstevel@tonic-gate }
11840Sstevel@tonic-gate (void) memset(newmaps + map_alloc, '\0',
11850Sstevel@tonic-gate (next - map_alloc) * sizeof (mapdata_t));
11860Sstevel@tonic-gate
11870Sstevel@tonic-gate map_alloc = next;
11880Sstevel@tonic-gate maps = newmaps;
11890Sstevel@tonic-gate }
11900Sstevel@tonic-gate
11910Sstevel@tonic-gate return (&maps[map_count++]);
11920Sstevel@tonic-gate }
11930Sstevel@tonic-gate
11940Sstevel@tonic-gate /*ARGSUSED*/
11950Sstevel@tonic-gate static int
gather_map(void * ignored,const prmap_t * map,const char * objname)11960Sstevel@tonic-gate gather_map(void *ignored, const prmap_t *map, const char *objname)
11970Sstevel@tonic-gate {
11982685Sakolb mapdata_t *data;
11990Sstevel@tonic-gate
12002685Sakolb /* Skip mappings which are outside the range specified by -A */
12012685Sakolb if (!address_in_range(map->pr_vaddr,
12024450Seh208807 map->pr_vaddr + map->pr_size, map->pr_pagesize))
12032685Sakolb return (0);
12042685Sakolb
12052685Sakolb data = nextmap();
12060Sstevel@tonic-gate data->md_map = *map;
12070Sstevel@tonic-gate if (data->md_objname != NULL)
12080Sstevel@tonic-gate free(data->md_objname);
12090Sstevel@tonic-gate data->md_objname = objname ? strdup(objname) : NULL;
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate return (0);
12120Sstevel@tonic-gate }
12130Sstevel@tonic-gate
12140Sstevel@tonic-gate /*ARGSUSED*/
12150Sstevel@tonic-gate static int
gather_xmap(void * ignored,const prxmap_t * xmap,const char * objname,int last,int doswap)12160Sstevel@tonic-gate gather_xmap(void *ignored, const prxmap_t *xmap, const char *objname,
12170Sstevel@tonic-gate int last, int doswap)
12180Sstevel@tonic-gate {
12192685Sakolb mapdata_t *data;
12200Sstevel@tonic-gate
12212685Sakolb /* Skip mappings which are outside the range specified by -A */
12222685Sakolb if (!address_in_range(xmap->pr_vaddr,
12234450Seh208807 xmap->pr_vaddr + xmap->pr_size, xmap->pr_pagesize))
12242685Sakolb return (0);
12252685Sakolb
12262685Sakolb data = nextmap();
12270Sstevel@tonic-gate data->md_xmap = *xmap;
12280Sstevel@tonic-gate if (data->md_objname != NULL)
12290Sstevel@tonic-gate free(data->md_objname);
12300Sstevel@tonic-gate data->md_objname = objname ? strdup(objname) : NULL;
12310Sstevel@tonic-gate data->md_last = last;
12320Sstevel@tonic-gate data->md_doswap = doswap;
12330Sstevel@tonic-gate
12340Sstevel@tonic-gate return (0);
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate
12370Sstevel@tonic-gate static int
iter_map(proc_map_f * func,void * data)12380Sstevel@tonic-gate iter_map(proc_map_f *func, void *data)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate int i;
12410Sstevel@tonic-gate int ret;
12420Sstevel@tonic-gate
12430Sstevel@tonic-gate for (i = 0; i < map_count; i++) {
12442685Sakolb if (interrupt)
12452685Sakolb break;
12460Sstevel@tonic-gate if ((ret = func(data, &maps[i].md_map,
12470Sstevel@tonic-gate maps[i].md_objname)) != 0)
12480Sstevel@tonic-gate return (ret);
12490Sstevel@tonic-gate }
12500Sstevel@tonic-gate
12510Sstevel@tonic-gate return (0);
12520Sstevel@tonic-gate }
12530Sstevel@tonic-gate
12540Sstevel@tonic-gate static int
iter_xmap(proc_xmap_f * func,void * data)12550Sstevel@tonic-gate iter_xmap(proc_xmap_f *func, void *data)
12560Sstevel@tonic-gate {
12570Sstevel@tonic-gate int i;
12580Sstevel@tonic-gate int ret;
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate for (i = 0; i < map_count; i++) {
12612685Sakolb if (interrupt)
12622685Sakolb break;
12630Sstevel@tonic-gate if ((ret = func(data, &maps[i].md_xmap, maps[i].md_objname,
12640Sstevel@tonic-gate maps[i].md_last, maps[i].md_doswap)) != 0)
12650Sstevel@tonic-gate return (ret);
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate
12680Sstevel@tonic-gate return (0);
12690Sstevel@tonic-gate }
12702685Sakolb
12712685Sakolb /*
12722685Sakolb * Convert lgroup ID to string.
12732685Sakolb * returns dash when lgroup ID is invalid.
12742685Sakolb */
12752685Sakolb static char *
lgrp2str(lgrp_id_t lgrp)12762685Sakolb lgrp2str(lgrp_id_t lgrp)
12772685Sakolb {
12782685Sakolb static char lgrp_buf[20];
12792685Sakolb char *str = lgrp_buf;
12802685Sakolb
12812685Sakolb (void) sprintf(str, lgrp == LGRP_NONE ? " -" : "%4d", lgrp);
12822685Sakolb return (str);
12832685Sakolb }
12842685Sakolb
12852685Sakolb /*
12862685Sakolb * Parse address range specification for -A option.
12872685Sakolb * The address range may have the following forms:
12882685Sakolb *
12892685Sakolb * address
12902685Sakolb * start and end is set to address
12912685Sakolb * address,
12922685Sakolb * start is set to address, end is set to INVALID_ADDRESS
12932685Sakolb * ,address
12942685Sakolb * start is set to 0, end is set to address
12952685Sakolb * address1,address2
12962685Sakolb * start is set to address1, end is set to address2
12972685Sakolb *
12982685Sakolb */
12992685Sakolb static int
parse_addr_range(char * input_str,uintptr_t * start,uintptr_t * end)13002685Sakolb parse_addr_range(char *input_str, uintptr_t *start, uintptr_t *end)
13012685Sakolb {
13022685Sakolb char *startp = input_str;
13032685Sakolb char *endp = strchr(input_str, ',');
13042685Sakolb ulong_t s = (ulong_t)INVALID_ADDRESS;
13052685Sakolb ulong_t e = (ulong_t)INVALID_ADDRESS;
13062685Sakolb
13072685Sakolb if (endp != NULL) {
13082685Sakolb /*
13092685Sakolb * Comma is present. If there is nothing after comma, the end
13102685Sakolb * remains set at INVALID_ADDRESS. Otherwise it is set to the
13112685Sakolb * value after comma.
13122685Sakolb */
13132685Sakolb *endp = '\0';
13142685Sakolb endp++;
13152685Sakolb
13162685Sakolb if ((*endp != '\0') && sscanf(endp, "%lx", &e) != 1)
13172685Sakolb return (1);
13182685Sakolb }
13192685Sakolb
13202685Sakolb if (startp != NULL) {
13212685Sakolb /*
13222685Sakolb * Read the start address, if it is specified. If the address is
13232685Sakolb * missing, start will be set to INVALID_ADDRESS.
13242685Sakolb */
13252685Sakolb if ((*startp != '\0') && sscanf(startp, "%lx", &s) != 1)
13262685Sakolb return (1);
13272685Sakolb }
13282685Sakolb
13292685Sakolb /* If there is no comma, end becomes equal to start */
13302685Sakolb if (endp == NULL)
13312685Sakolb e = s;
13322685Sakolb
13332685Sakolb /*
13342685Sakolb * ,end implies 0..end range
13352685Sakolb */
13362685Sakolb if (e != INVALID_ADDRESS && s == INVALID_ADDRESS)
13372685Sakolb s = 0;
13382685Sakolb
13392685Sakolb *start = (uintptr_t)s;
13402685Sakolb *end = (uintptr_t)e;
13412685Sakolb
13422685Sakolb /* Return error if neither start nor end address were specified */
13432685Sakolb return (! (s != INVALID_ADDRESS || e != INVALID_ADDRESS));
13442685Sakolb }
13452685Sakolb
13462685Sakolb /*
13472685Sakolb * Check whether any portion of [start, end] segment is within the
13482685Sakolb * [start_addr, end_addr] range.
13492685Sakolb *
13502685Sakolb * Return values:
13512685Sakolb * 0 - address is outside the range
13522685Sakolb * 1 - address is within the range
13532685Sakolb */
13542685Sakolb static int
address_in_range(uintptr_t start,uintptr_t end,size_t psz)13552685Sakolb address_in_range(uintptr_t start, uintptr_t end, size_t psz)
13562685Sakolb {
13572685Sakolb int rc = 1;
13582685Sakolb
13592685Sakolb /*
13602685Sakolb * Nothing to do if there is no address range specified with -A
13612685Sakolb */
13622685Sakolb if (start_addr != INVALID_ADDRESS || end_addr != INVALID_ADDRESS) {
13632685Sakolb /* The segment end is below the range start */
13642685Sakolb if ((start_addr != INVALID_ADDRESS) &&
13652685Sakolb (end < P2ALIGN(start_addr, psz)))
13662685Sakolb rc = 0;
13672685Sakolb
13682685Sakolb /* The segment start is above the range end */
13692685Sakolb if ((end_addr != INVALID_ADDRESS) &&
13702685Sakolb (start > P2ALIGN(end_addr + psz, psz)))
13712685Sakolb rc = 0;
13722685Sakolb }
13732685Sakolb return (rc);
13742685Sakolb }
13752685Sakolb
13762685Sakolb /*
13772685Sakolb * Returns an intersection of the [start, end] interval and the range specified
13782685Sakolb * by -A flag [start_addr, end_addr]. Unspecified parts of the address range
13792685Sakolb * have value INVALID_ADDRESS.
13802685Sakolb *
13812685Sakolb * The start_addr address is rounded down to the beginning of page and end_addr
13822685Sakolb * is rounded up to the end of page.
13832685Sakolb *
13842685Sakolb * Returns the size of the resulting interval or zero if the interval is empty
13852685Sakolb * or invalid.
13862685Sakolb */
13872685Sakolb static size_t
adjust_addr_range(uintptr_t start,uintptr_t end,size_t psz,uintptr_t * new_start,uintptr_t * new_end)13882685Sakolb adjust_addr_range(uintptr_t start, uintptr_t end, size_t psz,
13892685Sakolb uintptr_t *new_start, uintptr_t *new_end)
13902685Sakolb {
13912685Sakolb uintptr_t from; /* start_addr rounded down */
13922685Sakolb uintptr_t to; /* end_addr rounded up */
13932685Sakolb
13942685Sakolb /*
13952685Sakolb * Round down the lower address of the range to the beginning of page.
13962685Sakolb */
13972685Sakolb if (start_addr == INVALID_ADDRESS) {
13982685Sakolb /*
13992685Sakolb * No start_addr specified by -A, the lower part of the interval
14002685Sakolb * does not change.
14012685Sakolb */
14022685Sakolb *new_start = start;
14032685Sakolb } else {
14042685Sakolb from = P2ALIGN(start_addr, psz);
14052685Sakolb /*
14062685Sakolb * If end address is outside the range, return an empty
14072685Sakolb * interval
14082685Sakolb */
14092685Sakolb if (end < from) {
14102685Sakolb *new_start = *new_end = 0;
14112685Sakolb return (0);
14122685Sakolb }
14132685Sakolb /*
14142685Sakolb * The adjusted start address is the maximum of requested start
14152685Sakolb * and the aligned start_addr of the -A range.
14162685Sakolb */
14172685Sakolb *new_start = start < from ? from : start;
14182685Sakolb }
14192685Sakolb
14202685Sakolb /*
14212685Sakolb * Round up the higher address of the range to the end of page.
14222685Sakolb */
14232685Sakolb if (end_addr == INVALID_ADDRESS) {
14242685Sakolb /*
14252685Sakolb * No end_addr specified by -A, the upper part of the interval
14262685Sakolb * does not change.
14272685Sakolb */
14282685Sakolb *new_end = end;
14292685Sakolb } else {
14302685Sakolb /*
14312685Sakolb * If only one address is specified and it is the beginning of a
14322685Sakolb * segment, get information about the whole segment. This
14332685Sakolb * function is called once per segment and the 'end' argument is
14342685Sakolb * always the end of a segment, so just use the 'end' value.
14352685Sakolb */
14362685Sakolb to = (end_addr == start_addr && start == start_addr) ?
14372685Sakolb end :
14382685Sakolb P2ALIGN(end_addr + psz, psz);
14392685Sakolb /*
14402685Sakolb * If start address is outside the range, return an empty
14412685Sakolb * interval
14422685Sakolb */
14432685Sakolb if (start > to) {
14442685Sakolb *new_start = *new_end = 0;
14452685Sakolb return (0);
14462685Sakolb }
14472685Sakolb /*
14482685Sakolb * The adjusted end address is the minimum of requested end
14492685Sakolb * and the aligned end_addr of the -A range.
14502685Sakolb */
14512685Sakolb *new_end = end > to ? to : end;
14522685Sakolb }
14532685Sakolb
14542685Sakolb /*
14552685Sakolb * Make sure that the resulting interval is legal.
14562685Sakolb */
14572685Sakolb if (*new_end < *new_start)
14582685Sakolb *new_start = *new_end = 0;
14592685Sakolb
14602685Sakolb /* Return the size of the interval */
14612685Sakolb return (*new_end - *new_start);
14622685Sakolb }
14632685Sakolb
14642685Sakolb /*
14652685Sakolb * Initialize memory_info data structure with information about a new segment.
14662685Sakolb */
14672685Sakolb static void
mem_chunk_init(memory_chunk_t * chunk,uintptr_t end,size_t psz)14682685Sakolb mem_chunk_init(memory_chunk_t *chunk, uintptr_t end, size_t psz)
14692685Sakolb {
14702685Sakolb chunk->end_addr = end;
14712685Sakolb chunk->page_size = psz;
14722685Sakolb chunk->page_index = 0;
14732685Sakolb chunk->chunk_start = chunk->chunk_end = 0;
14742685Sakolb }
14752685Sakolb
14762685Sakolb /*
14772685Sakolb * Create a new chunk of addresses starting from vaddr.
14782685Sakolb * Pass the whole chunk to pr_meminfo to collect lgroup and page size
14792685Sakolb * information for each page in the chunk.
14802685Sakolb */
14812685Sakolb static void
mem_chunk_get(memory_chunk_t * chunk,uintptr_t vaddr)14822685Sakolb mem_chunk_get(memory_chunk_t *chunk, uintptr_t vaddr)
14832685Sakolb {
14842685Sakolb page_descr_t *pdp = chunk->page_info;
14852685Sakolb size_t psz = chunk->page_size;
14862685Sakolb uintptr_t addr = vaddr;
14872685Sakolb uint64_t inaddr[MAX_MEMINFO_CNT];
14882685Sakolb uint64_t outdata[2 * MAX_MEMINFO_CNT];
14892685Sakolb uint_t info[2] = { MEMINFO_VLGRP, MEMINFO_VPAGESIZE };
14902685Sakolb uint_t validity[MAX_MEMINFO_CNT];
14912685Sakolb uint64_t *dataptr = inaddr;
14922685Sakolb uint64_t *outptr = outdata;
14932685Sakolb uint_t *valptr = validity;
14942685Sakolb int i, j, rc;
14952685Sakolb
14962685Sakolb chunk->chunk_start = vaddr;
14972685Sakolb chunk->page_index = 0; /* reset index for the new chunk */
14982685Sakolb
14992685Sakolb /*
15002685Sakolb * Fill in MAX_MEMINFO_CNT wotrh of pages starting from vaddr. Also,
15012685Sakolb * copy starting address of each page to inaddr array for pr_meminfo.
15022685Sakolb */
15032685Sakolb for (i = 0, pdp = chunk->page_info;
15042685Sakolb (i < MAX_MEMINFO_CNT) && (addr <= chunk->end_addr);
15052685Sakolb i++, pdp++, dataptr++, addr += psz) {
15062685Sakolb *dataptr = (uint64_t)addr;
15072685Sakolb pdp->pd_start = addr;
15082685Sakolb pdp->pd_lgrp = LGRP_NONE;
15092685Sakolb pdp->pd_valid = 0;
15102685Sakolb pdp->pd_pagesize = 0;
15112685Sakolb }
15122685Sakolb
15132685Sakolb /* Mark the number of entries in the chunk and the last address */
15142685Sakolb chunk->page_count = i;
15152685Sakolb chunk->chunk_end = addr - psz;
15162685Sakolb
15172685Sakolb if (interrupt)
15182685Sakolb return;
15192685Sakolb
15202685Sakolb /* Call meminfo for all collected addresses */
15212685Sakolb rc = pr_meminfo(Pr, inaddr, i, info, 2, outdata, validity);
15222685Sakolb if (rc < 0) {
15232685Sakolb (void) perr("can not get memory information");
15242685Sakolb return;
15252685Sakolb }
15262685Sakolb
15272685Sakolb /* Verify validity of each result and fill in the addrs array */
15282685Sakolb pdp = chunk->page_info;
15292685Sakolb for (j = 0; j < i; j++, pdp++, valptr++, outptr += 2) {
15302685Sakolb /* Skip invalid address pointers */
15312685Sakolb if ((*valptr & 1) == 0) {
15322685Sakolb continue;
15332685Sakolb }
15342685Sakolb
15352685Sakolb /* Is lgroup information available? */
15362685Sakolb if ((*valptr & 2) != 0) {
15372685Sakolb pdp->pd_lgrp = (lgrp_id_t)*outptr;
15382685Sakolb pdp->pd_valid = 1;
15392685Sakolb }
15402685Sakolb
15412685Sakolb /* Is page size informaion available? */
15422685Sakolb if ((*valptr & 4) != 0) {
15432685Sakolb pdp->pd_pagesize = *(outptr + 1);
15442685Sakolb }
15452685Sakolb }
15462685Sakolb }
15472685Sakolb
15482685Sakolb /*
15492685Sakolb * Starting from address 'vaddr' find the region with pages allocated from the
15502685Sakolb * same lgroup.
15512685Sakolb *
15522685Sakolb * Arguments:
15532685Sakolb * mchunk Initialized memory chunk structure
15542685Sakolb * vaddr Starting address of the region
15552685Sakolb * maxaddr Upper bound of the region
15562685Sakolb * pagesize Default page size to use
15572685Sakolb * ret_lgrp On exit contains the lgroup ID of all pages in the
15582685Sakolb * region.
15592685Sakolb *
15602685Sakolb * Returns:
15612685Sakolb * Size of the contiguous region in bytes
15622685Sakolb * The lgroup ID of all pages in the region in ret_lgrp argument.
15632685Sakolb */
15642685Sakolb static size_t
get_contiguous_region(memory_chunk_t * mchunk,uintptr_t vaddr,uintptr_t maxaddr,size_t pagesize,lgrp_id_t * ret_lgrp)15652685Sakolb get_contiguous_region(memory_chunk_t *mchunk, uintptr_t vaddr,
15662685Sakolb uintptr_t maxaddr, size_t pagesize, lgrp_id_t *ret_lgrp)
15672685Sakolb {
15682685Sakolb size_t size_contig = 0;
15692685Sakolb lgrp_id_t lgrp; /* Lgroup of the region start */
15702685Sakolb lgrp_id_t curr_lgrp; /* Lgroup of the current page */
15712685Sakolb size_t psz = pagesize; /* Pagesize to use */
15722685Sakolb
15732685Sakolb /* Set both lgroup IDs to the lgroup of the first page */
15742685Sakolb curr_lgrp = lgrp = addr_to_lgrp(mchunk, vaddr, &psz);
15752685Sakolb
15762685Sakolb /*
15772685Sakolb * Starting from vaddr, walk page by page until either the end
15782685Sakolb * of the segment is reached or a page is allocated from a different
15792685Sakolb * lgroup. Also stop if interrupted from keyboard.
15802685Sakolb */
15812685Sakolb while ((vaddr < maxaddr) && (curr_lgrp == lgrp) && !interrupt) {
15822685Sakolb /*
15832685Sakolb * Get lgroup ID and the page size of the current page.
15842685Sakolb */
15852685Sakolb curr_lgrp = addr_to_lgrp(mchunk, vaddr, &psz);
15862685Sakolb /* If there is no page size information, use the default */
15872685Sakolb if (psz == 0)
15882685Sakolb psz = pagesize;
15892685Sakolb
15902685Sakolb if (curr_lgrp == lgrp) {
15912685Sakolb /*
15922685Sakolb * This page belongs to the contiguous region.
15932685Sakolb * Increase the region size and advance to the new page.
15942685Sakolb */
15952685Sakolb size_contig += psz;
15962685Sakolb vaddr += psz;
15972685Sakolb }
15982685Sakolb }
15992685Sakolb
16002685Sakolb /* Return the region lgroup ID and the size */
16012685Sakolb *ret_lgrp = lgrp;
16022685Sakolb return (size_contig);
16032685Sakolb }
16042685Sakolb
16052685Sakolb /*
16062685Sakolb * Given a virtual address, return its lgroup and page size. If there is meminfo
16072685Sakolb * information for an address, use it, otherwise shift the chunk window to the
16082685Sakolb * vaddr and create a new chunk with known meminfo information.
16092685Sakolb */
16102685Sakolb static lgrp_id_t
addr_to_lgrp(memory_chunk_t * chunk,uintptr_t vaddr,size_t * psz)16112685Sakolb addr_to_lgrp(memory_chunk_t *chunk, uintptr_t vaddr, size_t *psz)
16122685Sakolb {
16132685Sakolb page_descr_t *pdp;
16142685Sakolb lgrp_id_t lgrp = LGRP_NONE;
16152685Sakolb int i;
16162685Sakolb
16172685Sakolb *psz = chunk->page_size;
16182685Sakolb
16192685Sakolb if (interrupt)
16202685Sakolb return (0);
16212685Sakolb
16222685Sakolb /*
16232685Sakolb * Is there information about this address? If not, create a new chunk
16242685Sakolb * starting from vaddr and apply pr_meminfo() to the whole chunk.
16252685Sakolb */
16262685Sakolb if (vaddr < chunk->chunk_start || vaddr > chunk->chunk_end) {
16272685Sakolb /*
16282685Sakolb * This address is outside the chunk, get the new chunk and
16292685Sakolb * collect meminfo information for it.
16302685Sakolb */
16312685Sakolb mem_chunk_get(chunk, vaddr);
16322685Sakolb }
16332685Sakolb
16342685Sakolb /*
16352685Sakolb * Find information about the address.
16362685Sakolb */
16372685Sakolb pdp = &chunk->page_info[chunk->page_index];
16382685Sakolb for (i = chunk->page_index; i < chunk->page_count; i++, pdp++) {
16392685Sakolb if (pdp->pd_start == vaddr) {
16402685Sakolb if (pdp->pd_valid) {
16412685Sakolb lgrp = pdp->pd_lgrp;
16422685Sakolb /*
16432685Sakolb * Override page size information if it is
16442685Sakolb * present.
16452685Sakolb */
16462685Sakolb if (pdp->pd_pagesize > 0)
16472685Sakolb *psz = pdp->pd_pagesize;
16482685Sakolb }
16492685Sakolb break;
16502685Sakolb }
16512685Sakolb }
16522685Sakolb /*
16532685Sakolb * Remember where we ended - the next search will start here.
16542685Sakolb * We can query for the lgrp for the same address again, so do not
16552685Sakolb * advance index past the current value.
16562685Sakolb */
16572685Sakolb chunk->page_index = i;
16582685Sakolb
16592685Sakolb return (lgrp);
16602685Sakolb }
16612685Sakolb
16622685Sakolb /* ARGSUSED */
16632685Sakolb static void
intr(int sig)16642685Sakolb intr(int sig)
16652685Sakolb {
16662685Sakolb interrupt = 1;
16672685Sakolb }
1668