xref: /onnv-gate/usr/src/cmd/ptools/pmap/pmap.c (revision 5925)
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 /*
23*5925Ssp92102  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
301914Scasper #include <stdio_ext.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <dirent.h>
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate #include <link.h>
390Sstevel@tonic-gate #include <libelf.h>
400Sstevel@tonic-gate #include <sys/types.h>
412685Sakolb #include <signal.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <sys/mkdev.h>
442685Sakolb #include <sys/mman.h>
452685Sakolb #include <sys/lgrp_user.h>
460Sstevel@tonic-gate #include <libproc.h>
472712Snn35248 #include <libzonecfg.h>
480Sstevel@tonic-gate 
492685Sakolb #define	KILOBYTE	1024
502685Sakolb #define	MEGABYTE	(KILOBYTE * KILOBYTE)
512685Sakolb #define	GIGABYTE	(KILOBYTE * KILOBYTE * KILOBYTE)
522685Sakolb 
532685Sakolb /*
542685Sakolb  * Round up the value to the nearest kilobyte
552685Sakolb  */
562685Sakolb #define	ROUNDUP_KB(x)	(((x) + (KILOBYTE - 1)) / KILOBYTE)
572685Sakolb 
582685Sakolb /*
592685Sakolb  * The alignment should be a power of 2.
602685Sakolb  */
612685Sakolb #define	P2ALIGN(x, align)		((x) & -(align))
622685Sakolb 
632685Sakolb #define	INVALID_ADDRESS			(uintptr_t)(-1)
642685Sakolb 
650Sstevel@tonic-gate struct totals {
660Sstevel@tonic-gate 	ulong_t total_size;
670Sstevel@tonic-gate 	ulong_t total_swap;
680Sstevel@tonic-gate 	ulong_t total_rss;
690Sstevel@tonic-gate 	ulong_t total_anon;
700Sstevel@tonic-gate 	ulong_t total_locked;
710Sstevel@tonic-gate };
720Sstevel@tonic-gate 
732685Sakolb /*
742685Sakolb  * -L option requires per-page information. The information is presented in an
752685Sakolb  * array of page_descr structures.
762685Sakolb  */
772685Sakolb typedef struct page_descr {
782685Sakolb 	uintptr_t	pd_start;	/* start address of a page */
792685Sakolb 	size_t		pd_pagesize;	/* page size in bytes */
802685Sakolb 	lgrp_id_t	pd_lgrp;	/* lgroup of memory backing the page */
812685Sakolb 	int		pd_valid;	/* valid page description if non-zero */
822685Sakolb } page_descr_t;
832685Sakolb 
842685Sakolb /*
852685Sakolb  * Per-page information for a memory chunk.
862685Sakolb  * The meminfo(2) system call accepts up to MAX_MEMINFO_CNT pages at once.
872685Sakolb  * When we need to scan larger ranges we divide them in MAX_MEMINFO_CNT sized
882685Sakolb  * chunks. The chunk information is stored in the memory_chunk structure.
892685Sakolb  */
902685Sakolb typedef struct memory_chunk {
912685Sakolb 	page_descr_t	page_info[MAX_MEMINFO_CNT];
922685Sakolb 	uintptr_t	end_addr;
932685Sakolb 	uintptr_t	chunk_start;	/* Starting address */
942685Sakolb 	uintptr_t	chunk_end;	/* chunk_end is always <= end_addr */
952685Sakolb 	size_t		page_size;
962685Sakolb 	int		page_index;	/* Current page */
972685Sakolb 	int		page_count;	/* Number of pages */
982685Sakolb } memory_chunk_t;
992685Sakolb 
1002685Sakolb static volatile int interrupt;
1012685Sakolb 
1020Sstevel@tonic-gate typedef int proc_xmap_f(void *, const prxmap_t *, const char *, int, int);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static	int	xmapping_iter(struct ps_prochandle *, proc_xmap_f *, void *,
1050Sstevel@tonic-gate     int);
1060Sstevel@tonic-gate static	int	rmapping_iter(struct ps_prochandle *, proc_map_f *, void *);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static	int	look_map(void *, const prmap_t *, const char *);
1090Sstevel@tonic-gate static	int	look_smap(void *, const prxmap_t *, const char *, int, int);
1100Sstevel@tonic-gate static	int	look_xmap(void *, const prxmap_t *, const char *, int, int);
1110Sstevel@tonic-gate static	int	look_xmap_nopgsz(void *, const prxmap_t *, const char *,
1120Sstevel@tonic-gate     int, int);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static int gather_map(void *, const prmap_t *, const char *);
1150Sstevel@tonic-gate static int gather_xmap(void *, const prxmap_t *, const char *, int, int);
1160Sstevel@tonic-gate static int iter_map(proc_map_f *, void *);
1170Sstevel@tonic-gate static int iter_xmap(proc_xmap_f *, void *);
1182685Sakolb static int parse_addr_range(char *, uintptr_t *, uintptr_t *);
1192685Sakolb static void mem_chunk_init(memory_chunk_t *, uintptr_t, size_t);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate static	int	perr(char *);
1220Sstevel@tonic-gate static	void	printK(long, int);
1230Sstevel@tonic-gate static	char	*mflags(uint_t);
1240Sstevel@tonic-gate 
1252685Sakolb static size_t get_contiguous_region(memory_chunk_t *, uintptr_t,
1262685Sakolb     uintptr_t, size_t, lgrp_id_t *);
1272685Sakolb static void	mem_chunk_get(memory_chunk_t *, uintptr_t);
1282685Sakolb static lgrp_id_t addr_to_lgrp(memory_chunk_t *, uintptr_t, size_t *);
1292685Sakolb static char	*lgrp2str(lgrp_id_t);
1302685Sakolb 
1312685Sakolb static int	address_in_range(uintptr_t, uintptr_t, size_t);
1322685Sakolb static size_t	adjust_addr_range(uintptr_t, uintptr_t, size_t,
1332685Sakolb     uintptr_t *, uintptr_t *);
1342685Sakolb 
1350Sstevel@tonic-gate static	int	lflag = 0;
1362685Sakolb static	int	Lflag = 0;
1370Sstevel@tonic-gate static	int	aflag = 0;
1382685Sakolb 
1392685Sakolb /*
1402685Sakolb  * The -A address range is represented as a pair of addresses
1412685Sakolb  * <start_addr, end_addr>. Either one of these may be unspecified (set to
1422685Sakolb  * INVALID_ADDRESS). If both are unspecified, no address range restrictions are
1432685Sakolb  * in place.
1442685Sakolb  */
1452685Sakolb static  uintptr_t start_addr = INVALID_ADDRESS;
1462685Sakolb static	uintptr_t end_addr = INVALID_ADDRESS;
1472685Sakolb 
1480Sstevel@tonic-gate static	int	addr_width, size_width;
1490Sstevel@tonic-gate static	char	*command;
1500Sstevel@tonic-gate static	char	*procname;
1510Sstevel@tonic-gate static	struct ps_prochandle *Pr;
1520Sstevel@tonic-gate 
1532685Sakolb static void intr(int);
1542685Sakolb 
1550Sstevel@tonic-gate typedef struct lwpstack {
1560Sstevel@tonic-gate 	lwpid_t	lwps_lwpid;
1570Sstevel@tonic-gate 	stack_t	lwps_stack;
1580Sstevel@tonic-gate } lwpstack_t;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate typedef struct {
1610Sstevel@tonic-gate 	prxmap_t	md_xmap;
1620Sstevel@tonic-gate 	prmap_t		md_map;
1630Sstevel@tonic-gate 	char		*md_objname;
1642685Sakolb 	boolean_t	md_last;
1650Sstevel@tonic-gate 	int		md_doswap;
1660Sstevel@tonic-gate } mapdata_t;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static	mapdata_t	*maps;
1690Sstevel@tonic-gate static	int		map_count;
1700Sstevel@tonic-gate static	int		map_alloc;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate static	lwpstack_t *stacks = NULL;
1730Sstevel@tonic-gate static	uint_t	nstacks = 0;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate #define	MAX_TRIES	5
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static int
1780Sstevel@tonic-gate getstack(void *data, const lwpstatus_t *lsp)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	int *np = (int *)data;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (Plwp_alt_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
1830Sstevel@tonic-gate 		stacks[*np].lwps_stack.ss_flags |= SS_ONSTACK;
1840Sstevel@tonic-gate 		stacks[*np].lwps_lwpid = lsp->pr_lwpid;
1850Sstevel@tonic-gate 		(*np)++;
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (Plwp_main_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
1890Sstevel@tonic-gate 		stacks[*np].lwps_lwpid = lsp->pr_lwpid;
1900Sstevel@tonic-gate 		(*np)++;
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	return (0);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  * We compare the high memory addresses since stacks are faulted in from
1980Sstevel@tonic-gate  * high memory addresses to low memory addresses, and our prmap_t
1990Sstevel@tonic-gate  * structures identify only the range of addresses that have been faulted
2000Sstevel@tonic-gate  * in so far.
2010Sstevel@tonic-gate  */
2020Sstevel@tonic-gate static int
2030Sstevel@tonic-gate cmpstacks(const void *ap, const void *bp)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	const lwpstack_t *as = ap;
2060Sstevel@tonic-gate 	const lwpstack_t *bs = bp;
2070Sstevel@tonic-gate 	uintptr_t a = (uintptr_t)as->lwps_stack.ss_sp + as->lwps_stack.ss_size;
2080Sstevel@tonic-gate 	uintptr_t b = (uintptr_t)bs->lwps_stack.ss_sp + bs->lwps_stack.ss_size;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if (a < b)
2110Sstevel@tonic-gate 		return (1);
2120Sstevel@tonic-gate 	if (a > b)
2130Sstevel@tonic-gate 		return (-1);
2140Sstevel@tonic-gate 	return (0);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate int
2190Sstevel@tonic-gate main(int argc, char **argv)
2200Sstevel@tonic-gate {
2212685Sakolb 	int rflag = 0, sflag = 0, xflag = 0, Fflag = 0;
2220Sstevel@tonic-gate 	int errflg = 0, Sflag = 0;
2230Sstevel@tonic-gate 	int rc = 0;
2240Sstevel@tonic-gate 	int opt;
2250Sstevel@tonic-gate 	const char *bar8 = "-------";
2260Sstevel@tonic-gate 	const char *bar16 = "----------";
2270Sstevel@tonic-gate 	const char *bar;
2280Sstevel@tonic-gate 	struct rlimit rlim;
2290Sstevel@tonic-gate 	struct stat64 statbuf;
2300Sstevel@tonic-gate 	char buf[128];
2310Sstevel@tonic-gate 	int mapfd;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
2340Sstevel@tonic-gate 		command++;
2350Sstevel@tonic-gate 	else
2360Sstevel@tonic-gate 		command = argv[0];
2370Sstevel@tonic-gate 
2382685Sakolb 	while ((opt = getopt(argc, argv, "arsxSlLFA:")) != EOF) {
2390Sstevel@tonic-gate 		switch (opt) {
2400Sstevel@tonic-gate 		case 'a':		/* include shared mappings in -[xS] */
2410Sstevel@tonic-gate 			aflag = 1;
2420Sstevel@tonic-gate 			break;
2430Sstevel@tonic-gate 		case 'r':		/* show reserved mappings */
2440Sstevel@tonic-gate 			rflag = 1;
2450Sstevel@tonic-gate 			break;
2460Sstevel@tonic-gate 		case 's':		/* show hardware page sizes */
2470Sstevel@tonic-gate 			sflag = 1;
2480Sstevel@tonic-gate 			break;
2490Sstevel@tonic-gate 		case 'S':		/* show swap reservations */
2500Sstevel@tonic-gate 			Sflag = 1;
2510Sstevel@tonic-gate 			break;
2520Sstevel@tonic-gate 		case 'x':		/* show extended mappings */
2530Sstevel@tonic-gate 			xflag = 1;
2540Sstevel@tonic-gate 			break;
2550Sstevel@tonic-gate 		case 'l':		/* show unresolved link map names */
2560Sstevel@tonic-gate 			lflag = 1;
2570Sstevel@tonic-gate 			break;
2582685Sakolb 		case 'L':		/* show lgroup information */
2592685Sakolb 			Lflag = 1;
2602685Sakolb 			break;
2612685Sakolb 		case 'F':		/* force grabbing (no O_EXCL) */
2622685Sakolb 			Fflag = PGRAB_FORCE;
2632685Sakolb 			break;
2642685Sakolb 		case 'A':
2652685Sakolb 			if (parse_addr_range(optarg, &start_addr, &end_addr)
2662685Sakolb 			    != 0)
2672685Sakolb 				errflg++;
2680Sstevel@tonic-gate 			break;
2690Sstevel@tonic-gate 		default:
2700Sstevel@tonic-gate 			errflg = 1;
2710Sstevel@tonic-gate 			break;
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	argc -= optind;
2760Sstevel@tonic-gate 	argv += optind;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if ((Sflag && (xflag || rflag || sflag)) || (xflag && rflag) ||
2792685Sakolb 	    (aflag && (!xflag && !Sflag)) ||
2802685Sakolb 	    (Lflag && (xflag || Sflag))) {
2810Sstevel@tonic-gate 		errflg = 1;
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if (errflg || argc <= 0) {
2850Sstevel@tonic-gate 		(void) fprintf(stderr,
2862685Sakolb 		    "usage:\t%s [-rslF] [-A start[,end]] { pid | core } ...\n",
2872685Sakolb 		    command);
2880Sstevel@tonic-gate 		(void) fprintf(stderr,
2890Sstevel@tonic-gate 		    "\t\t(report process address maps)\n");
2900Sstevel@tonic-gate 		(void) fprintf(stderr,
2912685Sakolb 		    "\t%s -L [-rslF] [-A start[,end]] pid ...\n", command);
2922685Sakolb 		(void) fprintf(stderr,
2932685Sakolb 		    "\t\t(report process address maps lgroups mappings)\n");
2942685Sakolb 		(void) fprintf(stderr,
2952685Sakolb 		    "\t%s -x [-aslF] [-A start[,end]] pid ...\n", command);
2960Sstevel@tonic-gate 		(void) fprintf(stderr,
2970Sstevel@tonic-gate 		    "\t\t(show resident/anon/locked mapping details)\n");
2980Sstevel@tonic-gate 		(void) fprintf(stderr,
2992685Sakolb 		    "\t%s -S [-alF] [-A start[,end]] { pid | core } ...\n",
3002685Sakolb 		    command);
3010Sstevel@tonic-gate 		(void) fprintf(stderr,
3020Sstevel@tonic-gate 		    "\t\t(show swap reservations)\n\n");
3030Sstevel@tonic-gate 		(void) fprintf(stderr,
3040Sstevel@tonic-gate 		    "\t-a: include shared mappings in -[xS] summary\n");
3050Sstevel@tonic-gate 		(void) fprintf(stderr,
3060Sstevel@tonic-gate 		    "\t-r: show reserved address maps\n");
3070Sstevel@tonic-gate 		(void) fprintf(stderr,
3080Sstevel@tonic-gate 		    "\t-s: show hardware page sizes\n");
3090Sstevel@tonic-gate 		(void) fprintf(stderr,
3100Sstevel@tonic-gate 		    "\t-l: show unresolved dynamic linker map names\n");
3110Sstevel@tonic-gate 		(void) fprintf(stderr,
3120Sstevel@tonic-gate 		    "\t-F: force grabbing of the target process\n");
3132685Sakolb 		(void) fprintf(stderr,
3142685Sakolb 		    "\t-L: show lgroup mappings\n");
3152685Sakolb 		(void) fprintf(stderr,
3162685Sakolb 		    "\t-A start,end: limit output to the specified range\n");
3170Sstevel@tonic-gate 		return (2);
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * Make sure we'll have enough file descriptors to handle a target
3220Sstevel@tonic-gate 	 * that has many many mappings.
3230Sstevel@tonic-gate 	 */
3240Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
3250Sstevel@tonic-gate 		rlim.rlim_cur = rlim.rlim_max;
3260Sstevel@tonic-gate 		(void) setrlimit(RLIMIT_NOFILE, &rlim);
3271914Scasper 		(void) enable_extended_FILE_stdio(-1, -1);
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	while (argc-- > 0) {
3310Sstevel@tonic-gate 		char *arg;
3320Sstevel@tonic-gate 		int gcode;
3330Sstevel@tonic-gate 		psinfo_t psinfo;
3340Sstevel@tonic-gate 		int tries = 0;
3352685Sakolb 		int prg_gflags = PGRAB_RDONLY;
3362685Sakolb 		int prr_flags = 0;
3372685Sakolb 
3382685Sakolb 		if (Lflag) {
3392685Sakolb 			prg_gflags = PGRAB_RETAIN | Fflag;
3402685Sakolb 			prr_flags = PRELEASE_RETAIN;
3412685Sakolb 		}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
3442685Sakolb 		    prg_gflags, &gcode)) == NULL) {
3450Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
3460Sstevel@tonic-gate 			    command, arg, Pgrab_error(gcode));
3470Sstevel@tonic-gate 			rc++;
3480Sstevel@tonic-gate 			continue;
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		procname = arg;		/* for perr() */
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		addr_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 16 : 8;
3540Sstevel@tonic-gate 		size_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 11 : 8;
3550Sstevel@tonic-gate 		bar = addr_width == 8 ? bar8 : bar16;
3560Sstevel@tonic-gate 		(void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
3570Sstevel@tonic-gate 		proc_unctrl_psinfo(&psinfo);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 		if (Pstate(Pr) != PS_DEAD) {
3600Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf),
3610Sstevel@tonic-gate 			    "/proc/%d/map", (int)psinfo.pr_pid);
3620Sstevel@tonic-gate 			if ((mapfd = open(buf, O_RDONLY)) < 0) {
3630Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: cannot "
3640Sstevel@tonic-gate 				    "examine %s: lost control of "
3650Sstevel@tonic-gate 				    "process\n", command, arg);
3660Sstevel@tonic-gate 				rc++;
3672685Sakolb 				Prelease(Pr, prr_flags);
3680Sstevel@tonic-gate 				continue;
3690Sstevel@tonic-gate 			}
3700Sstevel@tonic-gate 		} else {
3710Sstevel@tonic-gate 			mapfd = -1;
3720Sstevel@tonic-gate 		}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate again:
3750Sstevel@tonic-gate 		map_count = 0;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		if (Pstate(Pr) == PS_DEAD) {
3780Sstevel@tonic-gate 			(void) printf("core '%s' of %d:\t%.70s\n",
3790Sstevel@tonic-gate 			    arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
3800Sstevel@tonic-gate 
3812685Sakolb 			if (rflag || sflag || xflag || Sflag || Lflag) {
3820Sstevel@tonic-gate 				(void) printf("  -%c option is not compatible "
3830Sstevel@tonic-gate 				    "with core files\n", xflag ? 'x' :
3842685Sakolb 				    sflag ? 's' : rflag ? 'r' :
3852685Sakolb 				    Lflag ? 'L' : 'S');
3862685Sakolb 				Prelease(Pr, prr_flags);
3870Sstevel@tonic-gate 				rc++;
3880Sstevel@tonic-gate 				continue;
3890Sstevel@tonic-gate 			}
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 		} else {
3920Sstevel@tonic-gate 			(void) printf("%d:\t%.70s\n",
3930Sstevel@tonic-gate 			    (int)psinfo.pr_pid, psinfo.pr_psargs);
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 
3962685Sakolb 		if (Lflag) {
3972685Sakolb 			/*
3982685Sakolb 			 * The implementation of -L option creates an agent LWP
3992685Sakolb 			 * in the target process address space. The agent LWP
4002685Sakolb 			 * issues meminfo(2) system calls on behalf of the
4012685Sakolb 			 * target process. If we are interrupted prematurely,
4022685Sakolb 			 * the target process remains in the stopped state with
4032685Sakolb 			 * the agent still attached to it. To prevent such
4042685Sakolb 			 * situation we catch signals from terminal and
4052685Sakolb 			 * terminate gracefully.
4062685Sakolb 			 */
4072685Sakolb 			if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
4082685Sakolb 				(void) sigset(SIGHUP, intr);
4092685Sakolb 			if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
4102685Sakolb 				(void) sigset(SIGINT, intr);
4112685Sakolb 			if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
4122685Sakolb 				(void) sigset(SIGQUIT, intr);
4132685Sakolb 			(void) sigset(SIGPIPE, intr);
4142685Sakolb 			(void) sigset(SIGTERM, intr);
4152685Sakolb 		}
4162685Sakolb 
4170Sstevel@tonic-gate 		if (!(Pstatus(Pr)->pr_flags & PR_ISSYS)) {
4180Sstevel@tonic-gate 			struct totals t;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 			/*
4210Sstevel@tonic-gate 			 * Since we're grabbing the process readonly, we need
4220Sstevel@tonic-gate 			 * to make sure the address space doesn't change during
4230Sstevel@tonic-gate 			 * execution.
4240Sstevel@tonic-gate 			 */
4250Sstevel@tonic-gate 			if (Pstate(Pr) != PS_DEAD) {
4260Sstevel@tonic-gate 				if (tries++ == MAX_TRIES) {
4272685Sakolb 					Prelease(Pr, prr_flags);
4280Sstevel@tonic-gate 					(void) close(mapfd);
4290Sstevel@tonic-gate 					(void) fprintf(stderr, "%s: cannot "
4300Sstevel@tonic-gate 					    "examine %s: address space is "
4310Sstevel@tonic-gate 					    "changing\n", command, arg);
4320Sstevel@tonic-gate 					continue;
4330Sstevel@tonic-gate 				}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 				if (fstat64(mapfd, &statbuf) != 0) {
4362685Sakolb 					Prelease(Pr, prr_flags);
4370Sstevel@tonic-gate 					(void) close(mapfd);
4380Sstevel@tonic-gate 					(void) fprintf(stderr, "%s: cannot "
4390Sstevel@tonic-gate 					    "examine %s: lost control of "
4400Sstevel@tonic-gate 					    "process\n", command, arg);
4410Sstevel@tonic-gate 					continue;
4420Sstevel@tonic-gate 				}
4430Sstevel@tonic-gate 			}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 			nstacks = psinfo.pr_nlwp * 2;
4460Sstevel@tonic-gate 			stacks = calloc(nstacks, sizeof (stacks[0]));
4470Sstevel@tonic-gate 			if (stacks != NULL) {
4480Sstevel@tonic-gate 				int n = 0;
4490Sstevel@tonic-gate 				(void) Plwp_iter(Pr, getstack, &n);
4500Sstevel@tonic-gate 				qsort(stacks, nstacks, sizeof (stacks[0]),
4510Sstevel@tonic-gate 				    cmpstacks);
4520Sstevel@tonic-gate 			}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 			(void) memset(&t, 0, sizeof (t));
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 			if (Pgetauxval(Pr, AT_BASE) != -1L &&
4570Sstevel@tonic-gate 			    Prd_agent(Pr) == NULL) {
4580Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: warning: "
4590Sstevel@tonic-gate 				    "librtld_db failed to initialize; "
4600Sstevel@tonic-gate 				    "shared library information will not be "
4610Sstevel@tonic-gate 				    "available\n", command);
4620Sstevel@tonic-gate 			}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 			/*
4650Sstevel@tonic-gate 			 * Gather data
4660Sstevel@tonic-gate 			 */
4670Sstevel@tonic-gate 			if (xflag)
4680Sstevel@tonic-gate 				rc += xmapping_iter(Pr, gather_xmap, NULL, 0);
4690Sstevel@tonic-gate 			else if (Sflag)
4700Sstevel@tonic-gate 				rc += xmapping_iter(Pr, gather_xmap, NULL, 1);
4710Sstevel@tonic-gate 			else {
4720Sstevel@tonic-gate 				if (rflag)
4730Sstevel@tonic-gate 					rc += rmapping_iter(Pr, gather_map,
4740Sstevel@tonic-gate 					    NULL);
4750Sstevel@tonic-gate 				else if (sflag)
4760Sstevel@tonic-gate 					rc += xmapping_iter(Pr, gather_xmap,
4770Sstevel@tonic-gate 					    NULL, 0);
4780Sstevel@tonic-gate 				else
4790Sstevel@tonic-gate 					rc += Pmapping_iter(Pr, gather_map,
4800Sstevel@tonic-gate 					    NULL);
4810Sstevel@tonic-gate 			}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 			/*
4840Sstevel@tonic-gate 			 * Ensure mappings are consistent.
4850Sstevel@tonic-gate 			 */
4860Sstevel@tonic-gate 			if (Pstate(Pr) != PS_DEAD) {
4870Sstevel@tonic-gate 				struct stat64 newbuf;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 				if (fstat64(mapfd, &newbuf) != 0 ||
4900Sstevel@tonic-gate 				    memcmp(&newbuf.st_mtim, &statbuf.st_mtim,
4910Sstevel@tonic-gate 				    sizeof (newbuf.st_mtim)) != 0) {
4920Sstevel@tonic-gate 					if (stacks != NULL) {
4930Sstevel@tonic-gate 						free(stacks);
4940Sstevel@tonic-gate 						stacks = NULL;
4950Sstevel@tonic-gate 					}
4960Sstevel@tonic-gate 					goto again;
4970Sstevel@tonic-gate 				}
4980Sstevel@tonic-gate 			}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 			/*
5010Sstevel@tonic-gate 			 * Display data.
5020Sstevel@tonic-gate 			 */
5030Sstevel@tonic-gate 			if (xflag) {
5040Sstevel@tonic-gate 				(void) printf("%*s%*s%*s%*s%*s "
5050Sstevel@tonic-gate 				    "%sMode   Mapped File\n",
5060Sstevel@tonic-gate 				    addr_width, "Address",
5070Sstevel@tonic-gate 				    size_width, "Kbytes",
5080Sstevel@tonic-gate 				    size_width, "RSS",
5090Sstevel@tonic-gate 				    size_width, "Anon",
5100Sstevel@tonic-gate 				    size_width, "Locked",
5110Sstevel@tonic-gate 				    sflag ? "Pgsz " : "");
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 				rc += iter_xmap(sflag ?  look_xmap :
5140Sstevel@tonic-gate 				    look_xmap_nopgsz, &t);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 				(void) printf("%s%s %s %s %s %s\n",
5170Sstevel@tonic-gate 				    addr_width == 8 ? "-" : "------",
5180Sstevel@tonic-gate 				    bar, bar, bar, bar, bar);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 				(void) printf("%stotal Kb", addr_width == 16 ?
5210Sstevel@tonic-gate 				    "        " : "");
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 				printK(t.total_size, size_width);
5240Sstevel@tonic-gate 				printK(t.total_rss, size_width);
5250Sstevel@tonic-gate 				printK(t.total_anon, size_width);
5260Sstevel@tonic-gate 				printK(t.total_locked, size_width);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 				(void) printf("\n");
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 			} else if (Sflag) {
5312685Sakolb 				(void) printf("%*s%*s%*s Mode"
5322685Sakolb 				    " Mapped File\n",
5330Sstevel@tonic-gate 				    addr_width, "Address",
5340Sstevel@tonic-gate 				    size_width, "Kbytes",
5350Sstevel@tonic-gate 				    size_width, "Swap");
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 				rc += iter_xmap(look_xmap_nopgsz, &t);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 				(void) printf("%s%s %s %s\n",
5400Sstevel@tonic-gate 				    addr_width == 8 ? "-" : "------",
5410Sstevel@tonic-gate 				    bar, bar, bar);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 				(void) printf("%stotal Kb", addr_width == 16 ?
5440Sstevel@tonic-gate 				    "        " : "");
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 				printK(t.total_size, size_width);
5470Sstevel@tonic-gate 				printK(t.total_swap, size_width);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 				(void) printf("\n");
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 			} else {
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 				if (rflag) {
5540Sstevel@tonic-gate 					rc += iter_map(look_map, &t);
5550Sstevel@tonic-gate 				} else if (sflag) {
5562685Sakolb 					if (Lflag) {
5572685Sakolb 						(void) printf("%*s %*s %4s"
5582685Sakolb 						    " %-6s %s %s\n",
5592685Sakolb 						    addr_width, "Address",
5602685Sakolb 						    size_width,
5612685Sakolb 						    "Bytes", "Pgsz", "Mode ",
5622685Sakolb 						    "Lgrp", "Mapped File");
5632685Sakolb 						rc += iter_xmap(look_smap, &t);
5642685Sakolb 					} else {
5652685Sakolb 						(void) printf("%*s %*s %4s"
5662685Sakolb 						    " %-6s %s\n",
5672685Sakolb 						    addr_width, "Address",
5682685Sakolb 						    size_width,
5692685Sakolb 						    "Bytes", "Pgsz", "Mode ",
5702685Sakolb 						    "Mapped File");
5712685Sakolb 						rc += iter_xmap(look_smap, &t);
5722685Sakolb 					}
5730Sstevel@tonic-gate 				} else {
5740Sstevel@tonic-gate 					rc += iter_map(look_map, &t);
5750Sstevel@tonic-gate 				}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 				(void) printf(" %stotal  %*luK\n",
5780Sstevel@tonic-gate 				    addr_width == 16 ?
5790Sstevel@tonic-gate 				    "        " : "",
5800Sstevel@tonic-gate 				    size_width, t.total_size);
5810Sstevel@tonic-gate 			}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 			if (stacks != NULL) {
5840Sstevel@tonic-gate 				free(stacks);
5850Sstevel@tonic-gate 				stacks = NULL;
5860Sstevel@tonic-gate 			}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 		}
5890Sstevel@tonic-gate 
5902685Sakolb 		Prelease(Pr, prr_flags);
5910Sstevel@tonic-gate 		if (mapfd != -1)
5920Sstevel@tonic-gate 			(void) close(mapfd);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	return (rc);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate static char *
5990Sstevel@tonic-gate make_name(struct ps_prochandle *Pr, uintptr_t addr, const char *mapname,
6000Sstevel@tonic-gate 	char *buf, size_t bufsz)
6010Sstevel@tonic-gate {
6022712Snn35248 	const pstatus_t		*Psp = Pstatus(Pr);
6032712Snn35248 	const psinfo_t		*pi = Ppsinfo(Pr);
6042712Snn35248 	char			fname[100];
6052712Snn35248 	struct stat		statb;
6062712Snn35248 	int			len;
6072712Snn35248 	char			zname[ZONENAME_MAX];
6082712Snn35248 	char			zpath[PATH_MAX];
6092712Snn35248 	char			objname[PATH_MAX];
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	if (!lflag && strcmp(mapname, "a.out") == 0 &&
6120Sstevel@tonic-gate 	    Pexecname(Pr, buf, bufsz) != NULL)
6130Sstevel@tonic-gate 		return (buf);
6140Sstevel@tonic-gate 
6152712Snn35248 	if (Pobjname(Pr, addr, objname, sizeof (objname)) != NULL) {
6162712Snn35248 		(void) strncpy(buf, objname, bufsz);
6172712Snn35248 
6180Sstevel@tonic-gate 		if (lflag)
6190Sstevel@tonic-gate 			return (buf);
6202712Snn35248 
6212712Snn35248 		if ((len = resolvepath(buf, buf, bufsz)) > 0) {
6222712Snn35248 			buf[len] = '\0';
6232712Snn35248 			return (buf);
6242712Snn35248 		}
6252712Snn35248 
6262712Snn35248 		/*
6272712Snn35248 		 * If the target is in a non-global zone, attempt to prepend
6282712Snn35248 		 * the zone path in order to give the global-zone caller the
6292712Snn35248 		 * real path to the file.
6302712Snn35248 		 */
6312712Snn35248 		if (getzonenamebyid(pi->pr_zoneid, zname,
632*5925Ssp92102 		    sizeof (zname)) != -1 && strcmp(zname, "global") != 0) {
633*5925Ssp92102 			typedef int  (*fptr)(char *, char *, size_t);
634*5925Ssp92102 			fptr zone_get_zonepath;
635*5925Ssp92102 			void *dlhdl;
6362712Snn35248 
637*5925Ssp92102 			if (((dlhdl =
638*5925Ssp92102 			    dlopen(LIBZONECFG_PATH, RTLD_LAZY)) == NULL) ||
639*5925Ssp92102 			    ((zone_get_zonepath =
640*5925Ssp92102 			    (fptr) dlsym(dlhdl, "zone_get_zonepath")) == NULL))
6412712Snn35248 				return (NULL);
6422712Snn35248 
643*5925Ssp92102 			if ((*zone_get_zonepath)(zname, zpath, sizeof (zpath))
644*5925Ssp92102 			    == Z_OK) {
645*5925Ssp92102 				(void) strncat(zpath, "/root",
646*5925Ssp92102 				    MAXPATHLEN - strlen(zpath));
647*5925Ssp92102 
648*5925Ssp92102 				if (bufsz <= strlen(zpath)) {
649*5925Ssp92102 					dlclose(dlhdl);
650*5925Ssp92102 					return (NULL);
651*5925Ssp92102 				}
652*5925Ssp92102 
653*5925Ssp92102 				(void) strncpy(buf, zpath, bufsz);
654*5925Ssp92102 				(void) strncat(buf, objname,
655*5925Ssp92102 				    bufsz - strlen(zpath));
656*5925Ssp92102 			}
657*5925Ssp92102 			dlclose(dlhdl);
6582712Snn35248 		}
6592712Snn35248 
6600Sstevel@tonic-gate 		if ((len = resolvepath(buf, buf, bufsz)) > 0) {
6610Sstevel@tonic-gate 			buf[len] = '\0';
6620Sstevel@tonic-gate 			return (buf);
6630Sstevel@tonic-gate 		}
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	if (Pstate(Pr) != PS_DEAD && *mapname != '\0') {
6674450Seh208807 		(void) snprintf(fname, sizeof (fname), "/proc/%d/path/%s",
6684450Seh208807 		    (int)Psp->pr_pid, mapname);
6694450Seh208807 		len = readlink(fname, buf, bufsz - 1);
6704450Seh208807 		if (len >= 0) {
6714450Seh208807 			buf[len] = '\0';
6720Sstevel@tonic-gate 			return (buf);
6734450Seh208807 		} else { /* there is no path and readlink() error */
6744450Seh208807 			(void) snprintf(fname, sizeof (fname),
6754450Seh208807 			    "/proc/%d/object/%s", (int)Psp->pr_pid, mapname);
6764450Seh208807 			if (stat(fname, &statb) == 0) {
6774450Seh208807 				dev_t dev = statb.st_dev;
6784450Seh208807 				ino_t ino = statb.st_ino;
6794450Seh208807 				(void) snprintf(buf, bufsz,
6804450Seh208807 				    "dev:%lu,%lu ino:%lu",
6814450Seh208807 				    (ulong_t)major(dev),
6824450Seh208807 				    (ulong_t)minor(dev), ino);
6834450Seh208807 				return (buf);
6844450Seh208807 			}
6850Sstevel@tonic-gate 		}
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	return (NULL);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate static char *
6920Sstevel@tonic-gate anon_name(char *name, const pstatus_t *Psp,
6930Sstevel@tonic-gate     uintptr_t vaddr, size_t size, int mflags, int shmid)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	if (mflags & MA_ISM) {
6960Sstevel@tonic-gate 		if (shmid == -1)
6970Sstevel@tonic-gate 			(void) snprintf(name, PATH_MAX, "  [ %s shmid=null ]",
6980Sstevel@tonic-gate 			    (mflags & MA_NORESERVE) ? "ism" : "dism");
6990Sstevel@tonic-gate 		else
7000Sstevel@tonic-gate 			(void) snprintf(name, PATH_MAX, "  [ %s shmid=0x%x ]",
7010Sstevel@tonic-gate 			    (mflags & MA_NORESERVE) ? "ism" : "dism", shmid);
7020Sstevel@tonic-gate 	} else if (mflags & MA_SHM) {
7030Sstevel@tonic-gate 		if (shmid == -1)
7040Sstevel@tonic-gate 			(void) sprintf(name, "  [ shmid=null ]");
7050Sstevel@tonic-gate 		else
7060Sstevel@tonic-gate 			(void) sprintf(name, "  [ shmid=0x%x ]", shmid);
7070Sstevel@tonic-gate 	} else if (vaddr + size > Psp->pr_stkbase &&
7080Sstevel@tonic-gate 	    vaddr < Psp->pr_stkbase + Psp->pr_stksize) {
7090Sstevel@tonic-gate 		(void) strcpy(name, "  [ stack ]");
7100Sstevel@tonic-gate 	} else if ((mflags & MA_ANON) &&
7110Sstevel@tonic-gate 	    vaddr + size > Psp->pr_brkbase &&
7120Sstevel@tonic-gate 	    vaddr < Psp->pr_brkbase + Psp->pr_brksize) {
7130Sstevel@tonic-gate 		(void) strcpy(name, "  [ heap ]");
7140Sstevel@tonic-gate 	} else {
7150Sstevel@tonic-gate 		lwpstack_t key, *stk;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 		key.lwps_stack.ss_sp = (void *)vaddr;
7180Sstevel@tonic-gate 		key.lwps_stack.ss_size = size;
7190Sstevel@tonic-gate 		if (nstacks > 0 &&
7200Sstevel@tonic-gate 		    (stk = bsearch(&key, stacks, nstacks, sizeof (stacks[0]),
7210Sstevel@tonic-gate 		    cmpstacks)) != NULL) {
7220Sstevel@tonic-gate 			(void) snprintf(name, PATH_MAX, "  [ %s tid=%d ]",
7230Sstevel@tonic-gate 			    (stk->lwps_stack.ss_flags & SS_ONSTACK) ?
7240Sstevel@tonic-gate 			    "altstack" : "stack",
7250Sstevel@tonic-gate 			    stk->lwps_lwpid);
7260Sstevel@tonic-gate 		} else if (Pstate(Pr) != PS_DEAD) {
7270Sstevel@tonic-gate 			(void) strcpy(name, "  [ anon ]");
7280Sstevel@tonic-gate 		} else {
7290Sstevel@tonic-gate 			return (NULL);
7300Sstevel@tonic-gate 		}
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	return (name);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate static int
7370Sstevel@tonic-gate rmapping_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	char mapname[PATH_MAX];
7400Sstevel@tonic-gate 	int mapfd, nmap, i, rc;
7410Sstevel@tonic-gate 	struct stat st;
7420Sstevel@tonic-gate 	prmap_t *prmapp, *pmp;
7430Sstevel@tonic-gate 	ssize_t n;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	(void) snprintf(mapname, sizeof (mapname),
7460Sstevel@tonic-gate 	    "/proc/%d/rmap", (int)Pstatus(Pr)->pr_pid);
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
7490Sstevel@tonic-gate 		if (mapfd >= 0)
7500Sstevel@tonic-gate 			(void) close(mapfd);
7510Sstevel@tonic-gate 		return (perr(mapname));
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	nmap = st.st_size / sizeof (prmap_t);
7550Sstevel@tonic-gate 	prmapp = malloc((nmap + 1) * sizeof (prmap_t));
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prmap_t), 0L)) < 0) {
7580Sstevel@tonic-gate 		(void) close(mapfd);
7590Sstevel@tonic-gate 		free(prmapp);
7600Sstevel@tonic-gate 		return (perr("read rmap"));
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	(void) close(mapfd);
7640Sstevel@tonic-gate 	nmap = n / sizeof (prmap_t);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
7670Sstevel@tonic-gate 		if ((rc = func(cd, pmp, NULL)) != 0) {
7680Sstevel@tonic-gate 			free(prmapp);
7690Sstevel@tonic-gate 			return (rc);
7700Sstevel@tonic-gate 		}
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	free(prmapp);
7740Sstevel@tonic-gate 	return (0);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate static int
7780Sstevel@tonic-gate xmapping_iter(struct ps_prochandle *Pr, proc_xmap_f *func, void *cd, int doswap)
7790Sstevel@tonic-gate {
7800Sstevel@tonic-gate 	char mapname[PATH_MAX];
7810Sstevel@tonic-gate 	int mapfd, nmap, i, rc;
7820Sstevel@tonic-gate 	struct stat st;
7830Sstevel@tonic-gate 	prxmap_t *prmapp, *pmp;
7840Sstevel@tonic-gate 	ssize_t n;
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	(void) snprintf(mapname, sizeof (mapname),
7870Sstevel@tonic-gate 	    "/proc/%d/xmap", (int)Pstatus(Pr)->pr_pid);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
7900Sstevel@tonic-gate 		if (mapfd >= 0)
7910Sstevel@tonic-gate 			(void) close(mapfd);
7920Sstevel@tonic-gate 		return (perr(mapname));
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	nmap = st.st_size / sizeof (prxmap_t);
7960Sstevel@tonic-gate 	nmap *= 2;
7970Sstevel@tonic-gate again:
7980Sstevel@tonic-gate 	prmapp = malloc((nmap + 1) * sizeof (prxmap_t));
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prxmap_t), 0)) < 0) {
8010Sstevel@tonic-gate 		(void) close(mapfd);
8020Sstevel@tonic-gate 		free(prmapp);
8030Sstevel@tonic-gate 		return (perr("read xmap"));
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	if (nmap < n / sizeof (prxmap_t)) {
8070Sstevel@tonic-gate 		free(prmapp);
8080Sstevel@tonic-gate 		nmap *= 2;
8090Sstevel@tonic-gate 		goto again;
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	(void) close(mapfd);
8130Sstevel@tonic-gate 	nmap = n / sizeof (prxmap_t);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
8160Sstevel@tonic-gate 		if ((rc = func(cd, pmp, NULL, i == nmap - 1, doswap)) != 0) {
8170Sstevel@tonic-gate 			free(prmapp);
8180Sstevel@tonic-gate 			return (rc);
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8222685Sakolb 	/*
8232685Sakolb 	 * Mark the last element.
8242685Sakolb 	 */
8252685Sakolb 	if (map_count > 0)
8262685Sakolb 		maps[map_count - 1].md_last = B_TRUE;
8272685Sakolb 
8280Sstevel@tonic-gate 	free(prmapp);
8290Sstevel@tonic-gate 	return (0);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate /*ARGSUSED*/
8330Sstevel@tonic-gate static int
8340Sstevel@tonic-gate look_map(void *data, const prmap_t *pmp, const char *object_name)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	struct totals *t = data;
8370Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Pr);
8382685Sakolb 	size_t size;
8390Sstevel@tonic-gate 	char mname[PATH_MAX];
8400Sstevel@tonic-gate 	char *lname = NULL;
8412685Sakolb 	size_t	psz = pmp->pr_pagesize;
8422685Sakolb 	uintptr_t vaddr = pmp->pr_vaddr;
8432685Sakolb 	uintptr_t segment_end = vaddr + pmp->pr_size;
8442685Sakolb 	lgrp_id_t lgrp;
8452685Sakolb 	memory_chunk_t mchunk;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	/*
8480Sstevel@tonic-gate 	 * If the mapping is not anon or not part of the heap, make a name
8490Sstevel@tonic-gate 	 * for it.  We don't want to report the heap as a.out's data.
8500Sstevel@tonic-gate 	 */
8510Sstevel@tonic-gate 	if (!(pmp->pr_mflags & MA_ANON) ||
8522685Sakolb 	    segment_end <= Psp->pr_brkbase ||
8530Sstevel@tonic-gate 	    pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
8540Sstevel@tonic-gate 		lname = make_name(Pr, pmp->pr_vaddr, pmp->pr_mapname,
8550Sstevel@tonic-gate 		    mname, sizeof (mname));
8560Sstevel@tonic-gate 	}
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	if (lname == NULL &&
8590Sstevel@tonic-gate 	    ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD)) {
8600Sstevel@tonic-gate 		lname = anon_name(mname, Psp, pmp->pr_vaddr,
8610Sstevel@tonic-gate 		    pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid);
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 
8642685Sakolb 	/*
8652685Sakolb 	 * Adjust the address range if -A is specified.
8662685Sakolb 	 */
8672685Sakolb 	size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz,
8682685Sakolb 	    &vaddr, &segment_end);
8692685Sakolb 
8702685Sakolb 	if (size == 0)
8712685Sakolb 		return (0);
8722685Sakolb 
8732685Sakolb 	if (!Lflag) {
8742685Sakolb 		/*
8752685Sakolb 		 * Display the whole mapping
8762685Sakolb 		 */
8772685Sakolb 		size = ROUNDUP_KB(size);
8782685Sakolb 
8792685Sakolb 		(void) printf(lname ?
8802685Sakolb 		    "%.*lX %*luK %-6s %s\n" :
8812685Sakolb 		    "%.*lX %*luK %s\n",
8822685Sakolb 		    addr_width, vaddr,
8832685Sakolb 		    size_width - 1, size, mflags(pmp->pr_mflags), lname);
8842685Sakolb 
8852685Sakolb 		t->total_size += size;
8862685Sakolb 		return (0);
8872685Sakolb 	}
8882685Sakolb 
8892685Sakolb 	/*
8902685Sakolb 	 * We need to display lgroups backing physical memory, so we break the
8912685Sakolb 	 * segment into individual pages and coalesce pages with the same lgroup
8922685Sakolb 	 * into one "segment".
8932685Sakolb 	 */
8940Sstevel@tonic-gate 
8952685Sakolb 	/*
8962685Sakolb 	 * Initialize address descriptions for the mapping.
8972685Sakolb 	 */
8982685Sakolb 	mem_chunk_init(&mchunk, segment_end, psz);
8992685Sakolb 	size = 0;
9002685Sakolb 
9012685Sakolb 	/*
9022685Sakolb 	 * Walk mapping (page by page) and display contiguous ranges of memory
9032685Sakolb 	 * allocated to same lgroup.
9042685Sakolb 	 */
9052685Sakolb 	do {
9062685Sakolb 		size_t		size_contig;
9072685Sakolb 
9082685Sakolb 		/*
9092685Sakolb 		 * Get contiguous region of memory starting from vaddr allocated
9102685Sakolb 		 * from the same lgroup.
9112685Sakolb 		 */
9122685Sakolb 		size_contig = get_contiguous_region(&mchunk, vaddr,
9132685Sakolb 		    segment_end, pmp->pr_pagesize, &lgrp);
9142685Sakolb 
9152685Sakolb 		(void) printf(lname ? "%.*lX %*luK %-6s%s %s\n" :
9162685Sakolb 		    "%.*lX %*luK %s %s\n",
9172685Sakolb 		    addr_width, vaddr,
9182685Sakolb 		    size_width - 1, size_contig / KILOBYTE,
9192685Sakolb 		    mflags(pmp->pr_mflags),
9202685Sakolb 		    lgrp2str(lgrp), lname);
9212685Sakolb 
9222685Sakolb 		vaddr += size_contig;
9232685Sakolb 		size += size_contig;
9242685Sakolb 	} while (vaddr < segment_end && !interrupt);
9252685Sakolb 
9262685Sakolb 	/* Update the total size */
9272685Sakolb 	t->total_size += ROUNDUP_KB(size);
9280Sstevel@tonic-gate 	return (0);
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate static void
9320Sstevel@tonic-gate printK(long value, int width)
9330Sstevel@tonic-gate {
9340Sstevel@tonic-gate 	if (value == 0)
9350Sstevel@tonic-gate 		(void) printf(width == 8 ? "       -" : "          -");
9360Sstevel@tonic-gate 	else
9370Sstevel@tonic-gate 		(void) printf(" %*lu", width - 1, value);
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate static const char *
9410Sstevel@tonic-gate pagesize(const prxmap_t *pmp)
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate 	int pagesize = pmp->pr_hatpagesize;
9440Sstevel@tonic-gate 	static char buf[32];
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	if (pagesize == 0) {
9470Sstevel@tonic-gate 		return ("-"); /* no underlying HAT mapping */
9480Sstevel@tonic-gate 	}
9490Sstevel@tonic-gate 
9502685Sakolb 	if (pagesize >= KILOBYTE && (pagesize % KILOBYTE) == 0) {
9512685Sakolb 		if ((pagesize % GIGABYTE) == 0)
9520Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%dG",
9532685Sakolb 			    pagesize / GIGABYTE);
9542685Sakolb 		else if ((pagesize % MEGABYTE) == 0)
9550Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%dM",
9562685Sakolb 			    pagesize / MEGABYTE);
9570Sstevel@tonic-gate 		else
9580Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%dK",
9592685Sakolb 			    pagesize / KILOBYTE);
9600Sstevel@tonic-gate 	} else
9610Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%db", pagesize);
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	return (buf);
9640Sstevel@tonic-gate }
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate /*ARGSUSED*/
9670Sstevel@tonic-gate static int
9680Sstevel@tonic-gate look_smap(void *data,
9690Sstevel@tonic-gate 	const prxmap_t *pmp,
9700Sstevel@tonic-gate 	const char *object_name,
9710Sstevel@tonic-gate 	int last, int doswap)
9720Sstevel@tonic-gate {
9730Sstevel@tonic-gate 	struct totals *t = data;
9740Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Pr);
9752685Sakolb 	size_t size;
9760Sstevel@tonic-gate 	char mname[PATH_MAX];
9770Sstevel@tonic-gate 	char *lname = NULL;
9780Sstevel@tonic-gate 	const char *format;
9792685Sakolb 	size_t	psz = pmp->pr_pagesize;
9802685Sakolb 	uintptr_t vaddr = pmp->pr_vaddr;
9812685Sakolb 	uintptr_t segment_end = vaddr + pmp->pr_size;
9822685Sakolb 	lgrp_id_t lgrp;
9832685Sakolb 	memory_chunk_t mchunk;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	/*
9860Sstevel@tonic-gate 	 * If the mapping is not anon or not part of the heap, make a name
9870Sstevel@tonic-gate 	 * for it.  We don't want to report the heap as a.out's data.
9880Sstevel@tonic-gate 	 */
9890Sstevel@tonic-gate 	if (!(pmp->pr_mflags & MA_ANON) ||
9900Sstevel@tonic-gate 	    pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
9910Sstevel@tonic-gate 	    pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
9920Sstevel@tonic-gate 		lname = make_name(Pr, pmp->pr_vaddr, pmp->pr_mapname,
9930Sstevel@tonic-gate 		    mname, sizeof (mname));
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	if (lname == NULL &&
9970Sstevel@tonic-gate 	    ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD)) {
9980Sstevel@tonic-gate 		lname = anon_name(mname, Psp, pmp->pr_vaddr,
9990Sstevel@tonic-gate 		    pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid);
10000Sstevel@tonic-gate 	}
10010Sstevel@tonic-gate 
10022685Sakolb 	/*
10032685Sakolb 	 * Adjust the address range if -A is specified.
10042685Sakolb 	 */
10052685Sakolb 	size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz,
10062685Sakolb 	    &vaddr, &segment_end);
10072685Sakolb 
10082685Sakolb 	if (size == 0)
10092685Sakolb 		return (0);
10102685Sakolb 
10112685Sakolb 	if (!Lflag) {
10122685Sakolb 		/*
10132685Sakolb 		 * Display the whole mapping
10142685Sakolb 		 */
10152685Sakolb 		if (lname != NULL)
10162685Sakolb 			format = "%.*lX %*luK %4s %-6s %s\n";
10172685Sakolb 		else
10182685Sakolb 			format = "%.*lX %*luK %4s %s\n";
10192685Sakolb 
10202685Sakolb 		size = ROUNDUP_KB(size);
10212685Sakolb 
10222685Sakolb 		(void) printf(format, addr_width, vaddr, size_width - 1, size,
10232685Sakolb 		    pagesize(pmp), mflags(pmp->pr_mflags), lname);
10242685Sakolb 
10252685Sakolb 		t->total_size += size;
10262685Sakolb 		return (0);
10272685Sakolb 	}
10282685Sakolb 
10290Sstevel@tonic-gate 	if (lname != NULL)
10302685Sakolb 		format = "%.*lX %*luK %4s %-6s%s %s\n";
10310Sstevel@tonic-gate 	else
10322685Sakolb 		format = "%.*lX %*luK %4s%s %s\n";
10330Sstevel@tonic-gate 
10342685Sakolb 	/*
10352685Sakolb 	 * We need to display lgroups backing physical memory, so we break the
10362685Sakolb 	 * segment into individual pages and coalesce pages with the same lgroup
10372685Sakolb 	 * into one "segment".
10382685Sakolb 	 */
10392685Sakolb 
10402685Sakolb 	/*
10412685Sakolb 	 * Initialize address descriptions for the mapping.
10422685Sakolb 	 */
10432685Sakolb 	mem_chunk_init(&mchunk, segment_end, psz);
10442685Sakolb 	size = 0;
10450Sstevel@tonic-gate 
10462685Sakolb 	/*
10472685Sakolb 	 * Walk mapping (page by page) and display contiguous ranges of memory
10482685Sakolb 	 * allocated to same lgroup.
10492685Sakolb 	 */
10502685Sakolb 	do {
10512685Sakolb 		size_t		size_contig;
10522685Sakolb 
10532685Sakolb 		/*
10542685Sakolb 		 * Get contiguous region of memory starting from vaddr allocated
10552685Sakolb 		 * from the same lgroup.
10562685Sakolb 		 */
10572685Sakolb 		size_contig = get_contiguous_region(&mchunk, vaddr,
10582685Sakolb 		    segment_end, pmp->pr_pagesize, &lgrp);
10592685Sakolb 
10602685Sakolb 		(void) printf(format, addr_width, vaddr,
10612685Sakolb 		    size_width - 1, size_contig / KILOBYTE,
10622685Sakolb 		    pagesize(pmp), mflags(pmp->pr_mflags),
10632685Sakolb 		    lgrp2str(lgrp), lname);
10642685Sakolb 
10652685Sakolb 		vaddr += size_contig;
10662685Sakolb 		size += size_contig;
10672685Sakolb 	} while (vaddr < segment_end && !interrupt);
10682685Sakolb 
10692685Sakolb 	t->total_size += ROUNDUP_KB(size);
10700Sstevel@tonic-gate 	return (0);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate #define	ANON(x)	((aflag || (((x)->pr_mflags & MA_SHARED) == 0)) ? \
10740Sstevel@tonic-gate 	    ((x)->pr_anon) : 0)
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate /*ARGSUSED*/
10770Sstevel@tonic-gate static int
10780Sstevel@tonic-gate look_xmap(void *data,
10790Sstevel@tonic-gate 	const prxmap_t *pmp,
10800Sstevel@tonic-gate 	const char *object_name,
10810Sstevel@tonic-gate 	int last, int doswap)
10820Sstevel@tonic-gate {
10830Sstevel@tonic-gate 	struct totals *t = data;
10840Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Pr);
10850Sstevel@tonic-gate 	char mname[PATH_MAX];
10860Sstevel@tonic-gate 	char *lname = NULL;
10870Sstevel@tonic-gate 	char *ln;
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	/*
10900Sstevel@tonic-gate 	 * If the mapping is not anon or not part of the heap, make a name
10910Sstevel@tonic-gate 	 * for it.  We don't want to report the heap as a.out's data.
10920Sstevel@tonic-gate 	 */
10930Sstevel@tonic-gate 	if (!(pmp->pr_mflags & MA_ANON) ||
10940Sstevel@tonic-gate 	    pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
10950Sstevel@tonic-gate 	    pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
10960Sstevel@tonic-gate 		lname = make_name(Pr, pmp->pr_vaddr, pmp->pr_mapname,
10970Sstevel@tonic-gate 		    mname, sizeof (mname));
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	if (lname != NULL) {
11010Sstevel@tonic-gate 		if ((ln = strrchr(lname, '/')) != NULL)
11020Sstevel@tonic-gate 			lname = ln + 1;
11030Sstevel@tonic-gate 	} else if ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD) {
11040Sstevel@tonic-gate 		lname = anon_name(mname, Psp, pmp->pr_vaddr,
11050Sstevel@tonic-gate 		    pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid);
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	(void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr);
11090Sstevel@tonic-gate 
11102685Sakolb 	printK(ROUNDUP_KB(pmp->pr_size), size_width);
11112685Sakolb 	printK(pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE), size_width);
11122685Sakolb 	printK(ANON(pmp) * (pmp->pr_pagesize / KILOBYTE), size_width);
11132685Sakolb 	printK(pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE), size_width);
11140Sstevel@tonic-gate 	(void) printf(lname ? " %4s %-6s %s\n" : " %4s %s\n",
11150Sstevel@tonic-gate 	    pagesize(pmp), mflags(pmp->pr_mflags), lname);
11160Sstevel@tonic-gate 
11172685Sakolb 	t->total_size += ROUNDUP_KB(pmp->pr_size);
11182685Sakolb 	t->total_rss += pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE);
11192685Sakolb 	t->total_anon += ANON(pmp) * (pmp->pr_pagesize / KILOBYTE);
11202685Sakolb 	t->total_locked += (pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE));
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	return (0);
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate /*ARGSUSED*/
11260Sstevel@tonic-gate static int
11270Sstevel@tonic-gate look_xmap_nopgsz(void *data,
11280Sstevel@tonic-gate 	const prxmap_t *pmp,
11290Sstevel@tonic-gate 	const char *object_name,
11300Sstevel@tonic-gate 	int last, int doswap)
11310Sstevel@tonic-gate {
11320Sstevel@tonic-gate 	struct totals *t = data;
11330Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Pr);
11340Sstevel@tonic-gate 	char mname[PATH_MAX];
11350Sstevel@tonic-gate 	char *lname = NULL;
11360Sstevel@tonic-gate 	char *ln;
11370Sstevel@tonic-gate 	static uintptr_t prev_vaddr;
11380Sstevel@tonic-gate 	static size_t prev_size;
11390Sstevel@tonic-gate 	static offset_t prev_offset;
11400Sstevel@tonic-gate 	static int prev_mflags;
11410Sstevel@tonic-gate 	static char *prev_lname;
11420Sstevel@tonic-gate 	static char prev_mname[PATH_MAX];
11430Sstevel@tonic-gate 	static ulong_t prev_rss;
11440Sstevel@tonic-gate 	static ulong_t prev_anon;
11450Sstevel@tonic-gate 	static ulong_t prev_locked;
11460Sstevel@tonic-gate 	static ulong_t prev_swap;
11470Sstevel@tonic-gate 	int merged = 0;
11480Sstevel@tonic-gate 	static int first = 1;
11490Sstevel@tonic-gate 	ulong_t swap = 0;
11500Sstevel@tonic-gate 	int kperpage;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	/*
11530Sstevel@tonic-gate 	 * Calculate swap reservations
11540Sstevel@tonic-gate 	 */
11550Sstevel@tonic-gate 	if (pmp->pr_mflags & MA_SHARED) {
11560Sstevel@tonic-gate 		if (aflag && (pmp->pr_mflags & MA_NORESERVE) == 0) {
11570Sstevel@tonic-gate 			/* Swap reserved for entire non-ism SHM */
11580Sstevel@tonic-gate 			swap = pmp->pr_size / pmp->pr_pagesize;
11590Sstevel@tonic-gate 		}
11600Sstevel@tonic-gate 	} else if (pmp->pr_mflags & MA_NORESERVE) {
11610Sstevel@tonic-gate 		/* Swap reserved on fault for each anon page */
11620Sstevel@tonic-gate 		swap = pmp->pr_anon;
11630Sstevel@tonic-gate 	} else if (pmp->pr_mflags & MA_WRITE) {
11640Sstevel@tonic-gate 		/* Swap reserve for entire writable segment */
11650Sstevel@tonic-gate 		swap = pmp->pr_size / pmp->pr_pagesize;
11660Sstevel@tonic-gate 	}
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	/*
11690Sstevel@tonic-gate 	 * If the mapping is not anon or not part of the heap, make a name
11700Sstevel@tonic-gate 	 * for it.  We don't want to report the heap as a.out's data.
11710Sstevel@tonic-gate 	 */
11720Sstevel@tonic-gate 	if (!(pmp->pr_mflags & MA_ANON) ||
11730Sstevel@tonic-gate 	    pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
11740Sstevel@tonic-gate 	    pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
11750Sstevel@tonic-gate 		lname = make_name(Pr, pmp->pr_vaddr, pmp->pr_mapname,
11760Sstevel@tonic-gate 		    mname, sizeof (mname));
11770Sstevel@tonic-gate 	}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	if (lname != NULL) {
11800Sstevel@tonic-gate 		if ((ln = strrchr(lname, '/')) != NULL)
11810Sstevel@tonic-gate 			lname = ln + 1;
11820Sstevel@tonic-gate 	} else if ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD) {
11830Sstevel@tonic-gate 		lname = anon_name(mname, Psp, pmp->pr_vaddr,
11840Sstevel@tonic-gate 		    pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid);
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 
11872685Sakolb 	kperpage = pmp->pr_pagesize / KILOBYTE;
11880Sstevel@tonic-gate 
11892685Sakolb 	t->total_size += ROUNDUP_KB(pmp->pr_size);
11900Sstevel@tonic-gate 	t->total_rss += pmp->pr_rss * kperpage;
11910Sstevel@tonic-gate 	t->total_anon += ANON(pmp) * kperpage;
11920Sstevel@tonic-gate 	t->total_locked += pmp->pr_locked * kperpage;
11930Sstevel@tonic-gate 	t->total_swap += swap * kperpage;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	if (first == 1) {
11960Sstevel@tonic-gate 		first = 0;
11970Sstevel@tonic-gate 		prev_vaddr = pmp->pr_vaddr;
11980Sstevel@tonic-gate 		prev_size = pmp->pr_size;
11990Sstevel@tonic-gate 		prev_offset = pmp->pr_offset;
12000Sstevel@tonic-gate 		prev_mflags = pmp->pr_mflags;
12010Sstevel@tonic-gate 		if (lname == NULL) {
12020Sstevel@tonic-gate 			prev_lname = NULL;
12030Sstevel@tonic-gate 		} else {
12040Sstevel@tonic-gate 			(void) strcpy(prev_mname, lname);
12050Sstevel@tonic-gate 			prev_lname = prev_mname;
12060Sstevel@tonic-gate 		}
12070Sstevel@tonic-gate 		prev_rss = pmp->pr_rss * kperpage;
12080Sstevel@tonic-gate 		prev_anon = ANON(pmp) * kperpage;
12090Sstevel@tonic-gate 		prev_locked = pmp->pr_locked * kperpage;
12100Sstevel@tonic-gate 		prev_swap = swap * kperpage;
12110Sstevel@tonic-gate 		if (last == 0) {
12120Sstevel@tonic-gate 			return (0);
12130Sstevel@tonic-gate 		}
12140Sstevel@tonic-gate 		merged = 1;
12150Sstevel@tonic-gate 	} else if (prev_vaddr + prev_size == pmp->pr_vaddr &&
12160Sstevel@tonic-gate 	    prev_mflags == pmp->pr_mflags &&
12170Sstevel@tonic-gate 	    ((prev_mflags & MA_ISM) ||
12184450Seh208807 	    prev_offset + prev_size == pmp->pr_offset) &&
12190Sstevel@tonic-gate 	    ((lname == NULL && prev_lname == NULL) ||
12204450Seh208807 	    (lname != NULL && prev_lname != NULL &&
12214450Seh208807 	    strcmp(lname, prev_lname) == 0))) {
12220Sstevel@tonic-gate 		prev_size += pmp->pr_size;
12230Sstevel@tonic-gate 		prev_rss += pmp->pr_rss * kperpage;
12240Sstevel@tonic-gate 		prev_anon += ANON(pmp) * kperpage;
12250Sstevel@tonic-gate 		prev_locked += pmp->pr_locked * kperpage;
12260Sstevel@tonic-gate 		prev_swap += swap * kperpage;
12270Sstevel@tonic-gate 		if (last == 0) {
12280Sstevel@tonic-gate 			return (0);
12290Sstevel@tonic-gate 		}
12300Sstevel@tonic-gate 		merged = 1;
12310Sstevel@tonic-gate 	}
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	(void) printf("%.*lX", addr_width, (ulong_t)prev_vaddr);
12342685Sakolb 	printK(ROUNDUP_KB(prev_size), size_width);
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	if (doswap)
12370Sstevel@tonic-gate 		printK(prev_swap, size_width);
12380Sstevel@tonic-gate 	else {
12390Sstevel@tonic-gate 		printK(prev_rss, size_width);
12400Sstevel@tonic-gate 		printK(prev_anon, size_width);
12410Sstevel@tonic-gate 		printK(prev_locked, size_width);
12420Sstevel@tonic-gate 	}
12432685Sakolb 	(void) printf(prev_lname ? " %-6s %s\n" : "%s\n",
12440Sstevel@tonic-gate 	    mflags(prev_mflags), prev_lname);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	if (last == 0) {
12470Sstevel@tonic-gate 		prev_vaddr = pmp->pr_vaddr;
12480Sstevel@tonic-gate 		prev_size = pmp->pr_size;
12490Sstevel@tonic-gate 		prev_offset = pmp->pr_offset;
12500Sstevel@tonic-gate 		prev_mflags = pmp->pr_mflags;
12510Sstevel@tonic-gate 		if (lname == NULL) {
12520Sstevel@tonic-gate 			prev_lname = NULL;
12530Sstevel@tonic-gate 		} else {
12540Sstevel@tonic-gate 			(void) strcpy(prev_mname, lname);
12550Sstevel@tonic-gate 			prev_lname = prev_mname;
12560Sstevel@tonic-gate 		}
12570Sstevel@tonic-gate 		prev_rss = pmp->pr_rss * kperpage;
12580Sstevel@tonic-gate 		prev_anon = ANON(pmp) * kperpage;
12590Sstevel@tonic-gate 		prev_locked = pmp->pr_locked * kperpage;
12600Sstevel@tonic-gate 		prev_swap = swap * kperpage;
12610Sstevel@tonic-gate 	} else if (merged == 0) {
12620Sstevel@tonic-gate 		(void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr);
12632685Sakolb 		printK(ROUNDUP_KB(pmp->pr_size), size_width);
12640Sstevel@tonic-gate 		if (doswap)
12650Sstevel@tonic-gate 			printK(swap * kperpage, size_width);
12660Sstevel@tonic-gate 		else {
12670Sstevel@tonic-gate 			printK(pmp->pr_rss * kperpage, size_width);
12680Sstevel@tonic-gate 			printK(ANON(pmp) * kperpage, size_width);
12690Sstevel@tonic-gate 			printK(pmp->pr_locked * kperpage, size_width);
12700Sstevel@tonic-gate 		}
12710Sstevel@tonic-gate 		(void) printf(lname ? " %-6s %s\n" : " %s\n",
12720Sstevel@tonic-gate 		    mflags(pmp->pr_mflags), lname);
12730Sstevel@tonic-gate 	}
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	if (last != 0)
12760Sstevel@tonic-gate 		first = 1;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	return (0);
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate static int
12820Sstevel@tonic-gate perr(char *s)
12830Sstevel@tonic-gate {
12840Sstevel@tonic-gate 	if (s)
12850Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", procname);
12860Sstevel@tonic-gate 	else
12870Sstevel@tonic-gate 		s = procname;
12880Sstevel@tonic-gate 	perror(s);
12890Sstevel@tonic-gate 	return (1);
12900Sstevel@tonic-gate }
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate static char *
12930Sstevel@tonic-gate mflags(uint_t arg)
12940Sstevel@tonic-gate {
12950Sstevel@tonic-gate 	static char code_buf[80];
12960Sstevel@tonic-gate 	char *str = code_buf;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	/*
12990Sstevel@tonic-gate 	 * rwxsR
13000Sstevel@tonic-gate 	 *
13010Sstevel@tonic-gate 	 * r - segment is readable
13020Sstevel@tonic-gate 	 * w - segment is writable
13030Sstevel@tonic-gate 	 * x - segment is executable
13040Sstevel@tonic-gate 	 * s - segment is shared
13050Sstevel@tonic-gate 	 * R - segment is mapped MAP_NORESERVE
13060Sstevel@tonic-gate 	 *
13070Sstevel@tonic-gate 	 */
13080Sstevel@tonic-gate 	(void) sprintf(str, "%c%c%c%c%c%c",
13090Sstevel@tonic-gate 	    arg & MA_READ ? 'r' : '-',
13100Sstevel@tonic-gate 	    arg & MA_WRITE ? 'w' : '-',
13110Sstevel@tonic-gate 	    arg & MA_EXEC ? 'x' : '-',
13120Sstevel@tonic-gate 	    arg & MA_SHARED ? 's' : '-',
13130Sstevel@tonic-gate 	    arg & MA_NORESERVE ? 'R' : '-',
13140Sstevel@tonic-gate 	    arg & MA_RESERVED1 ? '*' : ' ');
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	return (str);
13170Sstevel@tonic-gate }
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate static mapdata_t *
13200Sstevel@tonic-gate nextmap(void)
13210Sstevel@tonic-gate {
13220Sstevel@tonic-gate 	mapdata_t *newmaps;
13230Sstevel@tonic-gate 	int next;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	if (map_count == map_alloc) {
13260Sstevel@tonic-gate 		if (map_alloc == 0)
13270Sstevel@tonic-gate 			next = 16;
13280Sstevel@tonic-gate 		else
13290Sstevel@tonic-gate 			next = map_alloc * 2;
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 		newmaps = realloc(maps, next * sizeof (mapdata_t));
13320Sstevel@tonic-gate 		if (newmaps == NULL) {
13330Sstevel@tonic-gate 			(void) perr("failed to allocate maps");
13340Sstevel@tonic-gate 			exit(1);
13350Sstevel@tonic-gate 		}
13360Sstevel@tonic-gate 		(void) memset(newmaps + map_alloc, '\0',
13370Sstevel@tonic-gate 		    (next - map_alloc) * sizeof (mapdata_t));
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 		map_alloc = next;
13400Sstevel@tonic-gate 		maps = newmaps;
13410Sstevel@tonic-gate 	}
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	return (&maps[map_count++]);
13440Sstevel@tonic-gate }
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate /*ARGSUSED*/
13470Sstevel@tonic-gate static int
13480Sstevel@tonic-gate gather_map(void *ignored, const prmap_t *map, const char *objname)
13490Sstevel@tonic-gate {
13502685Sakolb 	mapdata_t *data;
13510Sstevel@tonic-gate 
13522685Sakolb 	/* Skip mappings which are outside the range specified by -A */
13532685Sakolb 	if (!address_in_range(map->pr_vaddr,
13544450Seh208807 	    map->pr_vaddr + map->pr_size, map->pr_pagesize))
13552685Sakolb 		return (0);
13562685Sakolb 
13572685Sakolb 	data = nextmap();
13580Sstevel@tonic-gate 	data->md_map = *map;
13590Sstevel@tonic-gate 	if (data->md_objname != NULL)
13600Sstevel@tonic-gate 		free(data->md_objname);
13610Sstevel@tonic-gate 	data->md_objname = objname ? strdup(objname) : NULL;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	return (0);
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate /*ARGSUSED*/
13670Sstevel@tonic-gate static int
13680Sstevel@tonic-gate gather_xmap(void *ignored, const prxmap_t *xmap, const char *objname,
13690Sstevel@tonic-gate     int last, int doswap)
13700Sstevel@tonic-gate {
13712685Sakolb 	mapdata_t *data;
13720Sstevel@tonic-gate 
13732685Sakolb 	/* Skip mappings which are outside the range specified by -A */
13742685Sakolb 	if (!address_in_range(xmap->pr_vaddr,
13754450Seh208807 	    xmap->pr_vaddr + xmap->pr_size, xmap->pr_pagesize))
13762685Sakolb 		return (0);
13772685Sakolb 
13782685Sakolb 	data = nextmap();
13790Sstevel@tonic-gate 	data->md_xmap = *xmap;
13800Sstevel@tonic-gate 	if (data->md_objname != NULL)
13810Sstevel@tonic-gate 		free(data->md_objname);
13820Sstevel@tonic-gate 	data->md_objname = objname ? strdup(objname) : NULL;
13830Sstevel@tonic-gate 	data->md_last = last;
13840Sstevel@tonic-gate 	data->md_doswap = doswap;
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	return (0);
13870Sstevel@tonic-gate }
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate static int
13900Sstevel@tonic-gate iter_map(proc_map_f *func, void *data)
13910Sstevel@tonic-gate {
13920Sstevel@tonic-gate 	int i;
13930Sstevel@tonic-gate 	int ret;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	for (i = 0; i < map_count; i++) {
13962685Sakolb 		if (interrupt)
13972685Sakolb 			break;
13980Sstevel@tonic-gate 		if ((ret = func(data, &maps[i].md_map,
13990Sstevel@tonic-gate 		    maps[i].md_objname)) != 0)
14000Sstevel@tonic-gate 			return (ret);
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	return (0);
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate static int
14070Sstevel@tonic-gate iter_xmap(proc_xmap_f *func, void *data)
14080Sstevel@tonic-gate {
14090Sstevel@tonic-gate 	int i;
14100Sstevel@tonic-gate 	int ret;
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	for (i = 0; i < map_count; i++) {
14132685Sakolb 		if (interrupt)
14142685Sakolb 			break;
14150Sstevel@tonic-gate 		if ((ret = func(data, &maps[i].md_xmap, maps[i].md_objname,
14160Sstevel@tonic-gate 		    maps[i].md_last, maps[i].md_doswap)) != 0)
14170Sstevel@tonic-gate 			return (ret);
14180Sstevel@tonic-gate 	}
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	return (0);
14210Sstevel@tonic-gate }
14222685Sakolb 
14232685Sakolb /*
14242685Sakolb  * Convert lgroup ID to string.
14252685Sakolb  * returns dash when lgroup ID is invalid.
14262685Sakolb  */
14272685Sakolb static char *
14282685Sakolb lgrp2str(lgrp_id_t lgrp)
14292685Sakolb {
14302685Sakolb 	static char lgrp_buf[20];
14312685Sakolb 	char *str = lgrp_buf;
14322685Sakolb 
14332685Sakolb 	(void) sprintf(str, lgrp == LGRP_NONE ? "   -" : "%4d", lgrp);
14342685Sakolb 	return (str);
14352685Sakolb }
14362685Sakolb 
14372685Sakolb /*
14382685Sakolb  * Parse address range specification for -A option.
14392685Sakolb  * The address range may have the following forms:
14402685Sakolb  *
14412685Sakolb  * address
14422685Sakolb  *	start and end is set to address
14432685Sakolb  * address,
14442685Sakolb  *	start is set to address, end is set to INVALID_ADDRESS
14452685Sakolb  * ,address
14462685Sakolb  *	start is set to 0, end is set to address
14472685Sakolb  * address1,address2
14482685Sakolb  *	start is set to address1, end is set to address2
14492685Sakolb  *
14502685Sakolb  */
14512685Sakolb static int
14522685Sakolb parse_addr_range(char *input_str, uintptr_t *start, uintptr_t *end)
14532685Sakolb {
14542685Sakolb 	char *startp = input_str;
14552685Sakolb 	char *endp = strchr(input_str, ',');
14562685Sakolb 	ulong_t	s = (ulong_t)INVALID_ADDRESS;
14572685Sakolb 	ulong_t e = (ulong_t)INVALID_ADDRESS;
14582685Sakolb 
14592685Sakolb 	if (endp != NULL) {
14602685Sakolb 		/*
14612685Sakolb 		 * Comma is present. If there is nothing after comma, the end
14622685Sakolb 		 * remains set at INVALID_ADDRESS. Otherwise it is set to the
14632685Sakolb 		 * value after comma.
14642685Sakolb 		 */
14652685Sakolb 		*endp = '\0';
14662685Sakolb 		endp++;
14672685Sakolb 
14682685Sakolb 		if ((*endp != '\0') && sscanf(endp, "%lx", &e) != 1)
14692685Sakolb 			return (1);
14702685Sakolb 	}
14712685Sakolb 
14722685Sakolb 	if (startp != NULL) {
14732685Sakolb 		/*
14742685Sakolb 		 * Read the start address, if it is specified. If the address is
14752685Sakolb 		 * missing, start will be set to INVALID_ADDRESS.
14762685Sakolb 		 */
14772685Sakolb 		if ((*startp != '\0') && sscanf(startp, "%lx", &s) != 1)
14782685Sakolb 			return (1);
14792685Sakolb 	}
14802685Sakolb 
14812685Sakolb 	/* If there is no comma, end becomes equal to start */
14822685Sakolb 	if (endp == NULL)
14832685Sakolb 		e = s;
14842685Sakolb 
14852685Sakolb 	/*
14862685Sakolb 	 * ,end implies 0..end range
14872685Sakolb 	 */
14882685Sakolb 	if (e != INVALID_ADDRESS && s == INVALID_ADDRESS)
14892685Sakolb 		s = 0;
14902685Sakolb 
14912685Sakolb 	*start = (uintptr_t)s;
14922685Sakolb 	*end = (uintptr_t)e;
14932685Sakolb 
14942685Sakolb 	/* Return error if neither start nor end address were specified */
14952685Sakolb 	return (! (s != INVALID_ADDRESS || e != INVALID_ADDRESS));
14962685Sakolb }
14972685Sakolb 
14982685Sakolb /*
14992685Sakolb  * Check whether any portion of [start, end] segment is within the
15002685Sakolb  * [start_addr, end_addr] range.
15012685Sakolb  *
15022685Sakolb  * Return values:
15032685Sakolb  *   0 - address is outside the range
15042685Sakolb  *   1 - address is within the range
15052685Sakolb  */
15062685Sakolb static int
15072685Sakolb address_in_range(uintptr_t start, uintptr_t end, size_t psz)
15082685Sakolb {
15092685Sakolb 	int rc = 1;
15102685Sakolb 
15112685Sakolb 	/*
15122685Sakolb 	 *  Nothing to do if there is no address range specified with -A
15132685Sakolb 	 */
15142685Sakolb 	if (start_addr != INVALID_ADDRESS || end_addr != INVALID_ADDRESS) {
15152685Sakolb 		/* The segment end is below the range start */
15162685Sakolb 		if ((start_addr != INVALID_ADDRESS) &&
15172685Sakolb 		    (end < P2ALIGN(start_addr, psz)))
15182685Sakolb 			rc = 0;
15192685Sakolb 
15202685Sakolb 		/* The segment start is above the range end */
15212685Sakolb 		if ((end_addr != INVALID_ADDRESS) &&
15222685Sakolb 		    (start > P2ALIGN(end_addr + psz, psz)))
15232685Sakolb 			rc = 0;
15242685Sakolb 	}
15252685Sakolb 	return (rc);
15262685Sakolb }
15272685Sakolb 
15282685Sakolb /*
15292685Sakolb  * Returns an intersection of the [start, end] interval and the range specified
15302685Sakolb  * by -A flag [start_addr, end_addr]. Unspecified parts of the address range
15312685Sakolb  * have value INVALID_ADDRESS.
15322685Sakolb  *
15332685Sakolb  * The start_addr address is rounded down to the beginning of page and end_addr
15342685Sakolb  * is rounded up to the end of page.
15352685Sakolb  *
15362685Sakolb  * Returns the size of the resulting interval or zero if the interval is empty
15372685Sakolb  * or invalid.
15382685Sakolb  */
15392685Sakolb static size_t
15402685Sakolb adjust_addr_range(uintptr_t start, uintptr_t end, size_t psz,
15412685Sakolb     uintptr_t *new_start, uintptr_t *new_end)
15422685Sakolb {
15432685Sakolb 	uintptr_t from;		/* start_addr rounded down */
15442685Sakolb 	uintptr_t to;		/* end_addr rounded up */
15452685Sakolb 
15462685Sakolb 	/*
15472685Sakolb 	 * Round down the lower address of the range to the beginning of page.
15482685Sakolb 	 */
15492685Sakolb 	if (start_addr == INVALID_ADDRESS) {
15502685Sakolb 		/*
15512685Sakolb 		 * No start_addr specified by -A, the lower part of the interval
15522685Sakolb 		 * does not change.
15532685Sakolb 		 */
15542685Sakolb 		*new_start = start;
15552685Sakolb 	} else {
15562685Sakolb 		from = P2ALIGN(start_addr, psz);
15572685Sakolb 		/*
15582685Sakolb 		 * If end address is outside the range, return an empty
15592685Sakolb 		 * interval
15602685Sakolb 		 */
15612685Sakolb 		if (end <  from) {
15622685Sakolb 			*new_start = *new_end = 0;
15632685Sakolb 			return (0);
15642685Sakolb 		}
15652685Sakolb 		/*
15662685Sakolb 		 * The adjusted start address is the maximum of requested start
15672685Sakolb 		 * and the aligned start_addr of the -A range.
15682685Sakolb 		 */
15692685Sakolb 		*new_start = start < from ? from : start;
15702685Sakolb 	}
15712685Sakolb 
15722685Sakolb 	/*
15732685Sakolb 	 * Round up the higher address of the range to the end of page.
15742685Sakolb 	 */
15752685Sakolb 	if (end_addr == INVALID_ADDRESS) {
15762685Sakolb 		/*
15772685Sakolb 		 * No end_addr specified by -A, the upper part of the interval
15782685Sakolb 		 * does not change.
15792685Sakolb 		 */
15802685Sakolb 		*new_end = end;
15812685Sakolb 	} else {
15822685Sakolb 		/*
15832685Sakolb 		 * If only one address is specified and it is the beginning of a
15842685Sakolb 		 * segment, get information about the whole segment. This
15852685Sakolb 		 * function is called once per segment and the 'end' argument is
15862685Sakolb 		 * always the end of a segment, so just use the 'end' value.
15872685Sakolb 		 */
15882685Sakolb 		to = (end_addr == start_addr && start == start_addr) ?
15892685Sakolb 		    end :
15902685Sakolb 		    P2ALIGN(end_addr + psz, psz);
15912685Sakolb 		/*
15922685Sakolb 		 * If start address is outside the range, return an empty
15932685Sakolb 		 * interval
15942685Sakolb 		 */
15952685Sakolb 		if (start > to) {
15962685Sakolb 			*new_start = *new_end = 0;
15972685Sakolb 			return (0);
15982685Sakolb 		}
15992685Sakolb 		/*
16002685Sakolb 		 * The adjusted end address is the minimum of requested end
16012685Sakolb 		 * and the aligned end_addr of the -A range.
16022685Sakolb 		 */
16032685Sakolb 		*new_end = end > to ? to : end;
16042685Sakolb 	}
16052685Sakolb 
16062685Sakolb 	/*
16072685Sakolb 	 * Make sure that the resulting interval is legal.
16082685Sakolb 	 */
16092685Sakolb 	if (*new_end < *new_start)
16102685Sakolb 			*new_start = *new_end = 0;
16112685Sakolb 
16122685Sakolb 	/* Return the size of the interval */
16132685Sakolb 	return (*new_end - *new_start);
16142685Sakolb }
16152685Sakolb 
16162685Sakolb /*
16172685Sakolb  * Initialize memory_info data structure with information about a new segment.
16182685Sakolb  */
16192685Sakolb static void
16202685Sakolb mem_chunk_init(memory_chunk_t *chunk, uintptr_t end, size_t psz)
16212685Sakolb {
16222685Sakolb 	chunk->end_addr = end;
16232685Sakolb 	chunk->page_size = psz;
16242685Sakolb 	chunk->page_index = 0;
16252685Sakolb 	chunk->chunk_start = chunk->chunk_end = 0;
16262685Sakolb }
16272685Sakolb 
16282685Sakolb /*
16292685Sakolb  * Create a new chunk of addresses starting from vaddr.
16302685Sakolb  * Pass the whole chunk to pr_meminfo to collect lgroup and page size
16312685Sakolb  * information for each page in the chunk.
16322685Sakolb  */
16332685Sakolb static void
16342685Sakolb mem_chunk_get(memory_chunk_t *chunk, uintptr_t vaddr)
16352685Sakolb {
16362685Sakolb 	page_descr_t	*pdp = chunk->page_info;
16372685Sakolb 	size_t		psz = chunk->page_size;
16382685Sakolb 	uintptr_t	addr = vaddr;
16392685Sakolb 	uint64_t	inaddr[MAX_MEMINFO_CNT];
16402685Sakolb 	uint64_t	outdata[2 * MAX_MEMINFO_CNT];
16412685Sakolb 	uint_t		info[2] = { MEMINFO_VLGRP, MEMINFO_VPAGESIZE };
16422685Sakolb 	uint_t		validity[MAX_MEMINFO_CNT];
16432685Sakolb 	uint64_t	*dataptr = inaddr;
16442685Sakolb 	uint64_t	*outptr = outdata;
16452685Sakolb 	uint_t		*valptr = validity;
16462685Sakolb 	int 		i, j, rc;
16472685Sakolb 
16482685Sakolb 	chunk->chunk_start = vaddr;
16492685Sakolb 	chunk->page_index = 0;	/* reset index for the new chunk */
16502685Sakolb 
16512685Sakolb 	/*
16522685Sakolb 	 * Fill in MAX_MEMINFO_CNT wotrh of pages starting from vaddr. Also,
16532685Sakolb 	 * copy starting address of each page to inaddr array for pr_meminfo.
16542685Sakolb 	 */
16552685Sakolb 	for (i = 0, pdp = chunk->page_info;
16562685Sakolb 	    (i < MAX_MEMINFO_CNT) && (addr <= chunk->end_addr);
16572685Sakolb 	    i++, pdp++, dataptr++, addr += psz) {
16582685Sakolb 		*dataptr = (uint64_t)addr;
16592685Sakolb 		pdp->pd_start = addr;
16602685Sakolb 		pdp->pd_lgrp = LGRP_NONE;
16612685Sakolb 		pdp->pd_valid = 0;
16622685Sakolb 		pdp->pd_pagesize = 0;
16632685Sakolb 	}
16642685Sakolb 
16652685Sakolb 	/* Mark the number of entries in the chunk and the last address */
16662685Sakolb 	chunk->page_count = i;
16672685Sakolb 	chunk->chunk_end = addr - psz;
16682685Sakolb 
16692685Sakolb 	if (interrupt)
16702685Sakolb 		return;
16712685Sakolb 
16722685Sakolb 	/* Call meminfo for all collected addresses */
16732685Sakolb 	rc = pr_meminfo(Pr, inaddr, i, info, 2, outdata, validity);
16742685Sakolb 	if (rc < 0) {
16752685Sakolb 		(void) perr("can not get memory information");
16762685Sakolb 		return;
16772685Sakolb 	}
16782685Sakolb 
16792685Sakolb 	/* Verify validity of each result and fill in the addrs array */
16802685Sakolb 	pdp = chunk->page_info;
16812685Sakolb 	for (j = 0; j < i; j++, pdp++, valptr++, outptr += 2) {
16822685Sakolb 		/* Skip invalid address pointers */
16832685Sakolb 		if ((*valptr & 1) == 0) {
16842685Sakolb 			continue;
16852685Sakolb 		}
16862685Sakolb 
16872685Sakolb 		/* Is lgroup information available? */
16882685Sakolb 		if ((*valptr & 2) != 0) {
16892685Sakolb 			pdp->pd_lgrp = (lgrp_id_t)*outptr;
16902685Sakolb 			pdp->pd_valid = 1;
16912685Sakolb 		}
16922685Sakolb 
16932685Sakolb 		/* Is page size informaion available? */
16942685Sakolb 		if ((*valptr & 4) != 0) {
16952685Sakolb 			pdp->pd_pagesize = *(outptr + 1);
16962685Sakolb 		}
16972685Sakolb 	}
16982685Sakolb }
16992685Sakolb 
17002685Sakolb /*
17012685Sakolb  * Starting from address 'vaddr' find the region with pages allocated from the
17022685Sakolb  * same lgroup.
17032685Sakolb  *
17042685Sakolb  * Arguments:
17052685Sakolb  *	mchunk		Initialized memory chunk structure
17062685Sakolb  *	vaddr		Starting address of the region
17072685Sakolb  *	maxaddr		Upper bound of the region
17082685Sakolb  *	pagesize	Default page size to use
17092685Sakolb  *	ret_lgrp	On exit contains the lgroup ID of all pages in the
17102685Sakolb  *			region.
17112685Sakolb  *
17122685Sakolb  * Returns:
17132685Sakolb  *	Size of the contiguous region in bytes
17142685Sakolb  *	The lgroup ID of all pages in the region in ret_lgrp argument.
17152685Sakolb  */
17162685Sakolb static size_t
17172685Sakolb get_contiguous_region(memory_chunk_t *mchunk, uintptr_t vaddr,
17182685Sakolb     uintptr_t maxaddr, size_t pagesize, lgrp_id_t *ret_lgrp)
17192685Sakolb {
17202685Sakolb 	size_t		size_contig = 0;
17212685Sakolb 	lgrp_id_t	lgrp;		/* Lgroup of the region start */
17222685Sakolb 	lgrp_id_t	curr_lgrp;	/* Lgroup of the current page */
17232685Sakolb 	size_t		psz = pagesize;	/* Pagesize to use */
17242685Sakolb 
17252685Sakolb 	/* Set both lgroup IDs to the lgroup of the first page */
17262685Sakolb 	curr_lgrp = lgrp = addr_to_lgrp(mchunk, vaddr, &psz);
17272685Sakolb 
17282685Sakolb 	/*
17292685Sakolb 	 * Starting from vaddr, walk page by page until either the end
17302685Sakolb 	 * of the segment is reached or a page is allocated from a different
17312685Sakolb 	 * lgroup. Also stop if interrupted from keyboard.
17322685Sakolb 	 */
17332685Sakolb 	while ((vaddr < maxaddr) && (curr_lgrp == lgrp) && !interrupt) {
17342685Sakolb 		/*
17352685Sakolb 		 * Get lgroup ID and the page size of the current page.
17362685Sakolb 		 */
17372685Sakolb 		curr_lgrp = addr_to_lgrp(mchunk, vaddr, &psz);
17382685Sakolb 		/* If there is no page size information, use the default */
17392685Sakolb 		if (psz == 0)
17402685Sakolb 			psz = pagesize;
17412685Sakolb 
17422685Sakolb 		if (curr_lgrp == lgrp) {
17432685Sakolb 			/*
17442685Sakolb 			 * This page belongs to the contiguous region.
17452685Sakolb 			 * Increase the region size and advance to the new page.
17462685Sakolb 			 */
17472685Sakolb 			size_contig += psz;
17482685Sakolb 			vaddr += psz;
17492685Sakolb 		}
17502685Sakolb 	}
17512685Sakolb 
17522685Sakolb 	/* Return the region lgroup ID and the size */
17532685Sakolb 	*ret_lgrp = lgrp;
17542685Sakolb 	return (size_contig);
17552685Sakolb }
17562685Sakolb 
17572685Sakolb /*
17582685Sakolb  * Given a virtual address, return its lgroup and page size. If there is meminfo
17592685Sakolb  * information for an address, use it, otherwise shift the chunk window to the
17602685Sakolb  * vaddr and create a new chunk with known meminfo information.
17612685Sakolb  */
17622685Sakolb static lgrp_id_t
17632685Sakolb addr_to_lgrp(memory_chunk_t *chunk, uintptr_t vaddr, size_t *psz)
17642685Sakolb {
17652685Sakolb 	page_descr_t *pdp;
17662685Sakolb 	lgrp_id_t lgrp = LGRP_NONE;
17672685Sakolb 	int i;
17682685Sakolb 
17692685Sakolb 	*psz = chunk->page_size;
17702685Sakolb 
17712685Sakolb 	if (interrupt)
17722685Sakolb 		return (0);
17732685Sakolb 
17742685Sakolb 	/*
17752685Sakolb 	 * Is there information about this address? If not, create a new chunk
17762685Sakolb 	 * starting from vaddr and apply pr_meminfo() to the whole chunk.
17772685Sakolb 	 */
17782685Sakolb 	if (vaddr < chunk->chunk_start || vaddr > chunk->chunk_end) {
17792685Sakolb 		/*
17802685Sakolb 		 * This address is outside the chunk, get the new chunk and
17812685Sakolb 		 * collect meminfo information for it.
17822685Sakolb 		 */
17832685Sakolb 		mem_chunk_get(chunk, vaddr);
17842685Sakolb 	}
17852685Sakolb 
17862685Sakolb 	/*
17872685Sakolb 	 * Find information about the address.
17882685Sakolb 	 */
17892685Sakolb 	pdp = &chunk->page_info[chunk->page_index];
17902685Sakolb 	for (i = chunk->page_index; i < chunk->page_count; i++, pdp++) {
17912685Sakolb 		if (pdp->pd_start == vaddr) {
17922685Sakolb 			if (pdp->pd_valid) {
17932685Sakolb 				lgrp = pdp->pd_lgrp;
17942685Sakolb 				/*
17952685Sakolb 				 * Override page size information if it is
17962685Sakolb 				 * present.
17972685Sakolb 				 */
17982685Sakolb 				if (pdp->pd_pagesize > 0)
17992685Sakolb 					*psz = pdp->pd_pagesize;
18002685Sakolb 			}
18012685Sakolb 			break;
18022685Sakolb 		}
18032685Sakolb 	}
18042685Sakolb 	/*
18052685Sakolb 	 * Remember where we ended - the next search will start here.
18062685Sakolb 	 * We can query for the lgrp for the same address again, so do not
18072685Sakolb 	 * advance index past the current value.
18082685Sakolb 	 */
18092685Sakolb 	chunk->page_index = i;
18102685Sakolb 
18112685Sakolb 	return (lgrp);
18122685Sakolb }
18132685Sakolb 
18142685Sakolb /* ARGSUSED */
18152685Sakolb static void
18162685Sakolb intr(int sig)
18172685Sakolb {
18182685Sakolb 	interrupt = 1;
18192685Sakolb }
1820