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