10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51914Scasper * Common Development and Distribution License (the "License"). 61914Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 212685Sakolb 220Sstevel@tonic-gate /* 235925Ssp92102 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #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; 232*7032Sakolb int prg_gflags = PGRAB_RDONLY; 233*7032Sakolb int prr_flags = 0; 234*7032Sakolb boolean_t use_agent_lwp = B_FALSE; 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL) 2370Sstevel@tonic-gate command++; 2380Sstevel@tonic-gate else 2390Sstevel@tonic-gate command = argv[0]; 2400Sstevel@tonic-gate 2412685Sakolb while ((opt = getopt(argc, argv, "arsxSlLFA:")) != EOF) { 2420Sstevel@tonic-gate switch (opt) { 2430Sstevel@tonic-gate case 'a': /* include shared mappings in -[xS] */ 2440Sstevel@tonic-gate aflag = 1; 2450Sstevel@tonic-gate break; 2460Sstevel@tonic-gate case 'r': /* show reserved mappings */ 2470Sstevel@tonic-gate rflag = 1; 2480Sstevel@tonic-gate break; 2490Sstevel@tonic-gate case 's': /* show hardware page sizes */ 2500Sstevel@tonic-gate sflag = 1; 2510Sstevel@tonic-gate break; 2520Sstevel@tonic-gate case 'S': /* show swap reservations */ 2530Sstevel@tonic-gate Sflag = 1; 2540Sstevel@tonic-gate break; 2550Sstevel@tonic-gate case 'x': /* show extended mappings */ 2560Sstevel@tonic-gate xflag = 1; 2570Sstevel@tonic-gate break; 2580Sstevel@tonic-gate case 'l': /* show unresolved link map names */ 2590Sstevel@tonic-gate lflag = 1; 2600Sstevel@tonic-gate break; 2612685Sakolb case 'L': /* show lgroup information */ 2622685Sakolb Lflag = 1; 263*7032Sakolb use_agent_lwp = B_TRUE; 2642685Sakolb break; 2652685Sakolb case 'F': /* force grabbing (no O_EXCL) */ 2662685Sakolb Fflag = PGRAB_FORCE; 2672685Sakolb break; 2682685Sakolb case 'A': 2692685Sakolb if (parse_addr_range(optarg, &start_addr, &end_addr) 2702685Sakolb != 0) 2712685Sakolb errflg++; 2720Sstevel@tonic-gate break; 2730Sstevel@tonic-gate default: 2740Sstevel@tonic-gate errflg = 1; 2750Sstevel@tonic-gate break; 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate argc -= optind; 2800Sstevel@tonic-gate argv += optind; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate if ((Sflag && (xflag || rflag || sflag)) || (xflag && rflag) || 2832685Sakolb (aflag && (!xflag && !Sflag)) || 2842685Sakolb (Lflag && (xflag || Sflag))) { 2850Sstevel@tonic-gate errflg = 1; 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate if (errflg || argc <= 0) { 2890Sstevel@tonic-gate (void) fprintf(stderr, 2902685Sakolb "usage:\t%s [-rslF] [-A start[,end]] { pid | core } ...\n", 2912685Sakolb command); 2920Sstevel@tonic-gate (void) fprintf(stderr, 2930Sstevel@tonic-gate "\t\t(report process address maps)\n"); 2940Sstevel@tonic-gate (void) fprintf(stderr, 2952685Sakolb "\t%s -L [-rslF] [-A start[,end]] pid ...\n", command); 2962685Sakolb (void) fprintf(stderr, 2972685Sakolb "\t\t(report process address maps lgroups mappings)\n"); 2982685Sakolb (void) fprintf(stderr, 2992685Sakolb "\t%s -x [-aslF] [-A start[,end]] pid ...\n", command); 3000Sstevel@tonic-gate (void) fprintf(stderr, 3010Sstevel@tonic-gate "\t\t(show resident/anon/locked mapping details)\n"); 3020Sstevel@tonic-gate (void) fprintf(stderr, 3032685Sakolb "\t%s -S [-alF] [-A start[,end]] { pid | core } ...\n", 3042685Sakolb command); 3050Sstevel@tonic-gate (void) fprintf(stderr, 3060Sstevel@tonic-gate "\t\t(show swap reservations)\n\n"); 3070Sstevel@tonic-gate (void) fprintf(stderr, 3080Sstevel@tonic-gate "\t-a: include shared mappings in -[xS] summary\n"); 3090Sstevel@tonic-gate (void) fprintf(stderr, 3100Sstevel@tonic-gate "\t-r: show reserved address maps\n"); 3110Sstevel@tonic-gate (void) fprintf(stderr, 3120Sstevel@tonic-gate "\t-s: show hardware page sizes\n"); 3130Sstevel@tonic-gate (void) fprintf(stderr, 3140Sstevel@tonic-gate "\t-l: show unresolved dynamic linker map names\n"); 3150Sstevel@tonic-gate (void) fprintf(stderr, 3160Sstevel@tonic-gate "\t-F: force grabbing of the target process\n"); 3172685Sakolb (void) fprintf(stderr, 3182685Sakolb "\t-L: show lgroup mappings\n"); 3192685Sakolb (void) fprintf(stderr, 3202685Sakolb "\t-A start,end: limit output to the specified range\n"); 3210Sstevel@tonic-gate return (2); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * Make sure we'll have enough file descriptors to handle a target 3260Sstevel@tonic-gate * that has many many mappings. 3270Sstevel@tonic-gate */ 3280Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) { 3290Sstevel@tonic-gate rlim.rlim_cur = rlim.rlim_max; 3300Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &rlim); 3311914Scasper (void) enable_extended_FILE_stdio(-1, -1); 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate 334*7032Sakolb /* 335*7032Sakolb * The implementation of -L option creates an agent LWP in the target 336*7032Sakolb * process address space. The agent LWP issues meminfo(2) system calls 337*7032Sakolb * on behalf of the target process. If we are interrupted prematurely, 338*7032Sakolb * the target process remains in the stopped state with the agent still 339*7032Sakolb * attached to it. To prevent such situation we catch signals from 340*7032Sakolb * terminal and terminate gracefully. 341*7032Sakolb */ 342*7032Sakolb if (use_agent_lwp) { 343*7032Sakolb /* 344*7032Sakolb * Buffer output to stdout, stderr while process is grabbed. 345*7032Sakolb * Prevents infamous deadlocks due to pmap `pgrep xterm` and 346*7032Sakolb * other variants. 347*7032Sakolb */ 348*7032Sakolb (void) proc_initstdio(); 349*7032Sakolb 350*7032Sakolb prg_gflags = PGRAB_RETAIN | Fflag; 351*7032Sakolb prr_flags = PRELEASE_RETAIN; 352*7032Sakolb 353*7032Sakolb if (sigset(SIGHUP, SIG_IGN) == SIG_DFL) 354*7032Sakolb (void) sigset(SIGHUP, intr); 355*7032Sakolb if (sigset(SIGINT, SIG_IGN) == SIG_DFL) 356*7032Sakolb (void) sigset(SIGINT, intr); 357*7032Sakolb if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL) 358*7032Sakolb (void) sigset(SIGQUIT, intr); 359*7032Sakolb (void) sigset(SIGPIPE, intr); 360*7032Sakolb (void) sigset(SIGTERM, intr); 361*7032Sakolb } 362*7032Sakolb 3630Sstevel@tonic-gate while (argc-- > 0) { 3640Sstevel@tonic-gate char *arg; 3650Sstevel@tonic-gate int gcode; 3660Sstevel@tonic-gate psinfo_t psinfo; 3670Sstevel@tonic-gate int tries = 0; 3682685Sakolb 369*7032Sakolb if (use_agent_lwp) 370*7032Sakolb (void) proc_flushstdio(); 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY, 3732685Sakolb prg_gflags, &gcode)) == NULL) { 3740Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %s: %s\n", 3750Sstevel@tonic-gate command, arg, Pgrab_error(gcode)); 3760Sstevel@tonic-gate rc++; 3770Sstevel@tonic-gate continue; 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate procname = arg; /* for perr() */ 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate addr_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 16 : 8; 3830Sstevel@tonic-gate size_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 11 : 8; 3840Sstevel@tonic-gate bar = addr_width == 8 ? bar8 : bar16; 3850Sstevel@tonic-gate (void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t)); 3860Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo); 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate if (Pstate(Pr) != PS_DEAD) { 3890Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 3900Sstevel@tonic-gate "/proc/%d/map", (int)psinfo.pr_pid); 3910Sstevel@tonic-gate if ((mapfd = open(buf, O_RDONLY)) < 0) { 3920Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot " 3930Sstevel@tonic-gate "examine %s: lost control of " 3940Sstevel@tonic-gate "process\n", command, arg); 3950Sstevel@tonic-gate rc++; 3962685Sakolb Prelease(Pr, prr_flags); 3970Sstevel@tonic-gate continue; 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate } else { 4000Sstevel@tonic-gate mapfd = -1; 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate again: 4040Sstevel@tonic-gate map_count = 0; 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate if (Pstate(Pr) == PS_DEAD) { 4070Sstevel@tonic-gate (void) printf("core '%s' of %d:\t%.70s\n", 4080Sstevel@tonic-gate arg, (int)psinfo.pr_pid, psinfo.pr_psargs); 4090Sstevel@tonic-gate 4102685Sakolb if (rflag || sflag || xflag || Sflag || Lflag) { 4110Sstevel@tonic-gate (void) printf(" -%c option is not compatible " 4120Sstevel@tonic-gate "with core files\n", xflag ? 'x' : 4132685Sakolb sflag ? 's' : rflag ? 'r' : 4142685Sakolb Lflag ? 'L' : 'S'); 4152685Sakolb Prelease(Pr, prr_flags); 4160Sstevel@tonic-gate rc++; 4170Sstevel@tonic-gate continue; 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate } else { 4210Sstevel@tonic-gate (void) printf("%d:\t%.70s\n", 4220Sstevel@tonic-gate (int)psinfo.pr_pid, psinfo.pr_psargs); 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate if (!(Pstatus(Pr)->pr_flags & PR_ISSYS)) { 4260Sstevel@tonic-gate struct totals t; 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate /* 4290Sstevel@tonic-gate * Since we're grabbing the process readonly, we need 4300Sstevel@tonic-gate * to make sure the address space doesn't change during 4310Sstevel@tonic-gate * execution. 4320Sstevel@tonic-gate */ 4330Sstevel@tonic-gate if (Pstate(Pr) != PS_DEAD) { 4340Sstevel@tonic-gate if (tries++ == MAX_TRIES) { 4352685Sakolb Prelease(Pr, prr_flags); 4360Sstevel@tonic-gate (void) close(mapfd); 4370Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot " 4380Sstevel@tonic-gate "examine %s: address space is " 4390Sstevel@tonic-gate "changing\n", command, arg); 4400Sstevel@tonic-gate continue; 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate if (fstat64(mapfd, &statbuf) != 0) { 4442685Sakolb Prelease(Pr, prr_flags); 4450Sstevel@tonic-gate (void) close(mapfd); 4460Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot " 4470Sstevel@tonic-gate "examine %s: lost control of " 4480Sstevel@tonic-gate "process\n", command, arg); 4490Sstevel@tonic-gate continue; 4500Sstevel@tonic-gate } 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate nstacks = psinfo.pr_nlwp * 2; 4540Sstevel@tonic-gate stacks = calloc(nstacks, sizeof (stacks[0])); 4550Sstevel@tonic-gate if (stacks != NULL) { 4560Sstevel@tonic-gate int n = 0; 4570Sstevel@tonic-gate (void) Plwp_iter(Pr, getstack, &n); 4580Sstevel@tonic-gate qsort(stacks, nstacks, sizeof (stacks[0]), 4590Sstevel@tonic-gate cmpstacks); 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate (void) memset(&t, 0, sizeof (t)); 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate if (Pgetauxval(Pr, AT_BASE) != -1L && 4650Sstevel@tonic-gate Prd_agent(Pr) == NULL) { 4660Sstevel@tonic-gate (void) fprintf(stderr, "%s: warning: " 4670Sstevel@tonic-gate "librtld_db failed to initialize; " 4680Sstevel@tonic-gate "shared library information will not be " 4690Sstevel@tonic-gate "available\n", command); 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate /* 4730Sstevel@tonic-gate * Gather data 4740Sstevel@tonic-gate */ 4750Sstevel@tonic-gate if (xflag) 4760Sstevel@tonic-gate rc += xmapping_iter(Pr, gather_xmap, NULL, 0); 4770Sstevel@tonic-gate else if (Sflag) 4780Sstevel@tonic-gate rc += xmapping_iter(Pr, gather_xmap, NULL, 1); 4790Sstevel@tonic-gate else { 4800Sstevel@tonic-gate if (rflag) 4810Sstevel@tonic-gate rc += rmapping_iter(Pr, gather_map, 4820Sstevel@tonic-gate NULL); 4830Sstevel@tonic-gate else if (sflag) 4840Sstevel@tonic-gate rc += xmapping_iter(Pr, gather_xmap, 4850Sstevel@tonic-gate NULL, 0); 4860Sstevel@tonic-gate else 4870Sstevel@tonic-gate rc += Pmapping_iter(Pr, gather_map, 4880Sstevel@tonic-gate NULL); 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate /* 4920Sstevel@tonic-gate * Ensure mappings are consistent. 4930Sstevel@tonic-gate */ 4940Sstevel@tonic-gate if (Pstate(Pr) != PS_DEAD) { 4950Sstevel@tonic-gate struct stat64 newbuf; 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate if (fstat64(mapfd, &newbuf) != 0 || 4980Sstevel@tonic-gate memcmp(&newbuf.st_mtim, &statbuf.st_mtim, 4990Sstevel@tonic-gate sizeof (newbuf.st_mtim)) != 0) { 5000Sstevel@tonic-gate if (stacks != NULL) { 5010Sstevel@tonic-gate free(stacks); 5020Sstevel@tonic-gate stacks = NULL; 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate goto again; 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate } 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate /* 5090Sstevel@tonic-gate * Display data. 5100Sstevel@tonic-gate */ 5110Sstevel@tonic-gate if (xflag) { 5120Sstevel@tonic-gate (void) printf("%*s%*s%*s%*s%*s " 5130Sstevel@tonic-gate "%sMode Mapped File\n", 5140Sstevel@tonic-gate addr_width, "Address", 5150Sstevel@tonic-gate size_width, "Kbytes", 5160Sstevel@tonic-gate size_width, "RSS", 5170Sstevel@tonic-gate size_width, "Anon", 5180Sstevel@tonic-gate size_width, "Locked", 5190Sstevel@tonic-gate sflag ? "Pgsz " : ""); 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate rc += iter_xmap(sflag ? look_xmap : 5220Sstevel@tonic-gate look_xmap_nopgsz, &t); 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate (void) printf("%s%s %s %s %s %s\n", 5250Sstevel@tonic-gate addr_width == 8 ? "-" : "------", 5260Sstevel@tonic-gate bar, bar, bar, bar, bar); 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate (void) printf("%stotal Kb", addr_width == 16 ? 5290Sstevel@tonic-gate " " : ""); 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate printK(t.total_size, size_width); 5320Sstevel@tonic-gate printK(t.total_rss, size_width); 5330Sstevel@tonic-gate printK(t.total_anon, size_width); 5340Sstevel@tonic-gate printK(t.total_locked, size_width); 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate (void) printf("\n"); 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate } else if (Sflag) { 5392685Sakolb (void) printf("%*s%*s%*s Mode" 5402685Sakolb " Mapped File\n", 5410Sstevel@tonic-gate addr_width, "Address", 5420Sstevel@tonic-gate size_width, "Kbytes", 5430Sstevel@tonic-gate size_width, "Swap"); 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate rc += iter_xmap(look_xmap_nopgsz, &t); 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate (void) printf("%s%s %s %s\n", 5480Sstevel@tonic-gate addr_width == 8 ? "-" : "------", 5490Sstevel@tonic-gate bar, bar, bar); 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate (void) printf("%stotal Kb", addr_width == 16 ? 5520Sstevel@tonic-gate " " : ""); 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate printK(t.total_size, size_width); 5550Sstevel@tonic-gate printK(t.total_swap, size_width); 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate (void) printf("\n"); 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate } else { 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate if (rflag) { 5620Sstevel@tonic-gate rc += iter_map(look_map, &t); 5630Sstevel@tonic-gate } else if (sflag) { 5642685Sakolb if (Lflag) { 5652685Sakolb (void) printf("%*s %*s %4s" 5662685Sakolb " %-6s %s %s\n", 5672685Sakolb addr_width, "Address", 5682685Sakolb size_width, 5692685Sakolb "Bytes", "Pgsz", "Mode ", 5702685Sakolb "Lgrp", "Mapped File"); 5712685Sakolb rc += iter_xmap(look_smap, &t); 5722685Sakolb } else { 5732685Sakolb (void) printf("%*s %*s %4s" 5742685Sakolb " %-6s %s\n", 5752685Sakolb addr_width, "Address", 5762685Sakolb size_width, 5772685Sakolb "Bytes", "Pgsz", "Mode ", 5782685Sakolb "Mapped File"); 5792685Sakolb rc += iter_xmap(look_smap, &t); 5802685Sakolb } 5810Sstevel@tonic-gate } else { 5820Sstevel@tonic-gate rc += iter_map(look_map, &t); 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate (void) printf(" %stotal %*luK\n", 5860Sstevel@tonic-gate addr_width == 16 ? 5870Sstevel@tonic-gate " " : "", 5880Sstevel@tonic-gate size_width, t.total_size); 5890Sstevel@tonic-gate } 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate if (stacks != NULL) { 5920Sstevel@tonic-gate free(stacks); 5930Sstevel@tonic-gate stacks = NULL; 5940Sstevel@tonic-gate } 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate 5982685Sakolb Prelease(Pr, prr_flags); 5990Sstevel@tonic-gate if (mapfd != -1) 6000Sstevel@tonic-gate (void) close(mapfd); 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate 603*7032Sakolb if (use_agent_lwp) 604*7032Sakolb (void) proc_finistdio(); 605*7032Sakolb 6060Sstevel@tonic-gate return (rc); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate static char * 6100Sstevel@tonic-gate make_name(struct ps_prochandle *Pr, uintptr_t addr, const char *mapname, 6110Sstevel@tonic-gate char *buf, size_t bufsz) 6120Sstevel@tonic-gate { 6132712Snn35248 const pstatus_t *Psp = Pstatus(Pr); 6142712Snn35248 const psinfo_t *pi = Ppsinfo(Pr); 6152712Snn35248 char fname[100]; 6162712Snn35248 struct stat statb; 6172712Snn35248 int len; 6182712Snn35248 char zname[ZONENAME_MAX]; 6192712Snn35248 char zpath[PATH_MAX]; 6202712Snn35248 char objname[PATH_MAX]; 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate if (!lflag && strcmp(mapname, "a.out") == 0 && 6230Sstevel@tonic-gate Pexecname(Pr, buf, bufsz) != NULL) 6240Sstevel@tonic-gate return (buf); 6250Sstevel@tonic-gate 6262712Snn35248 if (Pobjname(Pr, addr, objname, sizeof (objname)) != NULL) { 6272712Snn35248 (void) strncpy(buf, objname, bufsz); 6282712Snn35248 6290Sstevel@tonic-gate if (lflag) 6300Sstevel@tonic-gate return (buf); 6312712Snn35248 6322712Snn35248 if ((len = resolvepath(buf, buf, bufsz)) > 0) { 6332712Snn35248 buf[len] = '\0'; 6342712Snn35248 return (buf); 6352712Snn35248 } 6362712Snn35248 6372712Snn35248 /* 6382712Snn35248 * If the target is in a non-global zone, attempt to prepend 6392712Snn35248 * the zone path in order to give the global-zone caller the 6402712Snn35248 * real path to the file. 6412712Snn35248 */ 6422712Snn35248 if (getzonenamebyid(pi->pr_zoneid, zname, 6435925Ssp92102 sizeof (zname)) != -1 && strcmp(zname, "global") != 0) { 6445925Ssp92102 typedef int (*fptr)(char *, char *, size_t); 6455925Ssp92102 fptr zone_get_zonepath; 6465925Ssp92102 void *dlhdl; 6472712Snn35248 6485925Ssp92102 if (((dlhdl = 6495925Ssp92102 dlopen(LIBZONECFG_PATH, RTLD_LAZY)) == NULL) || 6505925Ssp92102 ((zone_get_zonepath = 6515925Ssp92102 (fptr) dlsym(dlhdl, "zone_get_zonepath")) == NULL)) 6522712Snn35248 return (NULL); 6532712Snn35248 6545925Ssp92102 if ((*zone_get_zonepath)(zname, zpath, sizeof (zpath)) 6555925Ssp92102 == Z_OK) { 6565925Ssp92102 (void) strncat(zpath, "/root", 6575925Ssp92102 MAXPATHLEN - strlen(zpath)); 6585925Ssp92102 6595925Ssp92102 if (bufsz <= strlen(zpath)) { 6605926Ssp92102 (void) dlclose(dlhdl); 6615925Ssp92102 return (NULL); 6625925Ssp92102 } 6635925Ssp92102 6645925Ssp92102 (void) strncpy(buf, zpath, bufsz); 6655925Ssp92102 (void) strncat(buf, objname, 6665925Ssp92102 bufsz - strlen(zpath)); 6675925Ssp92102 } 6685926Ssp92102 (void) dlclose(dlhdl); 6692712Snn35248 } 6702712Snn35248 6710Sstevel@tonic-gate if ((len = resolvepath(buf, buf, bufsz)) > 0) { 6720Sstevel@tonic-gate buf[len] = '\0'; 6730Sstevel@tonic-gate return (buf); 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate if (Pstate(Pr) != PS_DEAD && *mapname != '\0') { 6784450Seh208807 (void) snprintf(fname, sizeof (fname), "/proc/%d/path/%s", 6794450Seh208807 (int)Psp->pr_pid, mapname); 6804450Seh208807 len = readlink(fname, buf, bufsz - 1); 6814450Seh208807 if (len >= 0) { 6824450Seh208807 buf[len] = '\0'; 6830Sstevel@tonic-gate return (buf); 6844450Seh208807 } else { /* there is no path and readlink() error */ 6854450Seh208807 (void) snprintf(fname, sizeof (fname), 6864450Seh208807 "/proc/%d/object/%s", (int)Psp->pr_pid, mapname); 6874450Seh208807 if (stat(fname, &statb) == 0) { 6884450Seh208807 dev_t dev = statb.st_dev; 6894450Seh208807 ino_t ino = statb.st_ino; 6904450Seh208807 (void) snprintf(buf, bufsz, 6914450Seh208807 "dev:%lu,%lu ino:%lu", 6924450Seh208807 (ulong_t)major(dev), 6934450Seh208807 (ulong_t)minor(dev), ino); 6944450Seh208807 return (buf); 6954450Seh208807 } 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate return (NULL); 7000Sstevel@tonic-gate } 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate static char * 7030Sstevel@tonic-gate anon_name(char *name, const pstatus_t *Psp, 7040Sstevel@tonic-gate uintptr_t vaddr, size_t size, int mflags, int shmid) 7050Sstevel@tonic-gate { 7060Sstevel@tonic-gate if (mflags & MA_ISM) { 7070Sstevel@tonic-gate if (shmid == -1) 7080Sstevel@tonic-gate (void) snprintf(name, PATH_MAX, " [ %s shmid=null ]", 7090Sstevel@tonic-gate (mflags & MA_NORESERVE) ? "ism" : "dism"); 7100Sstevel@tonic-gate else 7110Sstevel@tonic-gate (void) snprintf(name, PATH_MAX, " [ %s shmid=0x%x ]", 7120Sstevel@tonic-gate (mflags & MA_NORESERVE) ? "ism" : "dism", shmid); 7130Sstevel@tonic-gate } else if (mflags & MA_SHM) { 7140Sstevel@tonic-gate if (shmid == -1) 7150Sstevel@tonic-gate (void) sprintf(name, " [ shmid=null ]"); 7160Sstevel@tonic-gate else 7170Sstevel@tonic-gate (void) sprintf(name, " [ shmid=0x%x ]", shmid); 7180Sstevel@tonic-gate } else if (vaddr + size > Psp->pr_stkbase && 7190Sstevel@tonic-gate vaddr < Psp->pr_stkbase + Psp->pr_stksize) { 7200Sstevel@tonic-gate (void) strcpy(name, " [ stack ]"); 7210Sstevel@tonic-gate } else if ((mflags & MA_ANON) && 7220Sstevel@tonic-gate vaddr + size > Psp->pr_brkbase && 7230Sstevel@tonic-gate vaddr < Psp->pr_brkbase + Psp->pr_brksize) { 7240Sstevel@tonic-gate (void) strcpy(name, " [ heap ]"); 7250Sstevel@tonic-gate } else { 7260Sstevel@tonic-gate lwpstack_t key, *stk; 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate key.lwps_stack.ss_sp = (void *)vaddr; 7290Sstevel@tonic-gate key.lwps_stack.ss_size = size; 7300Sstevel@tonic-gate if (nstacks > 0 && 7310Sstevel@tonic-gate (stk = bsearch(&key, stacks, nstacks, sizeof (stacks[0]), 7320Sstevel@tonic-gate cmpstacks)) != NULL) { 7330Sstevel@tonic-gate (void) snprintf(name, PATH_MAX, " [ %s tid=%d ]", 7340Sstevel@tonic-gate (stk->lwps_stack.ss_flags & SS_ONSTACK) ? 7350Sstevel@tonic-gate "altstack" : "stack", 7360Sstevel@tonic-gate stk->lwps_lwpid); 7370Sstevel@tonic-gate } else if (Pstate(Pr) != PS_DEAD) { 7380Sstevel@tonic-gate (void) strcpy(name, " [ anon ]"); 7390Sstevel@tonic-gate } else { 7400Sstevel@tonic-gate return (NULL); 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate return (name); 7450Sstevel@tonic-gate } 7460Sstevel@tonic-gate 7470Sstevel@tonic-gate static int 7480Sstevel@tonic-gate rmapping_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd) 7490Sstevel@tonic-gate { 7500Sstevel@tonic-gate char mapname[PATH_MAX]; 7510Sstevel@tonic-gate int mapfd, nmap, i, rc; 7520Sstevel@tonic-gate struct stat st; 7530Sstevel@tonic-gate prmap_t *prmapp, *pmp; 7540Sstevel@tonic-gate ssize_t n; 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate (void) snprintf(mapname, sizeof (mapname), 7570Sstevel@tonic-gate "/proc/%d/rmap", (int)Pstatus(Pr)->pr_pid); 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) { 7600Sstevel@tonic-gate if (mapfd >= 0) 7610Sstevel@tonic-gate (void) close(mapfd); 7620Sstevel@tonic-gate return (perr(mapname)); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate nmap = st.st_size / sizeof (prmap_t); 7660Sstevel@tonic-gate prmapp = malloc((nmap + 1) * sizeof (prmap_t)); 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prmap_t), 0L)) < 0) { 7690Sstevel@tonic-gate (void) close(mapfd); 7700Sstevel@tonic-gate free(prmapp); 7710Sstevel@tonic-gate return (perr("read rmap")); 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate (void) close(mapfd); 7750Sstevel@tonic-gate nmap = n / sizeof (prmap_t); 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) { 7780Sstevel@tonic-gate if ((rc = func(cd, pmp, NULL)) != 0) { 7790Sstevel@tonic-gate free(prmapp); 7800Sstevel@tonic-gate return (rc); 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate free(prmapp); 7850Sstevel@tonic-gate return (0); 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate 7880Sstevel@tonic-gate static int 7890Sstevel@tonic-gate xmapping_iter(struct ps_prochandle *Pr, proc_xmap_f *func, void *cd, int doswap) 7900Sstevel@tonic-gate { 7910Sstevel@tonic-gate char mapname[PATH_MAX]; 7920Sstevel@tonic-gate int mapfd, nmap, i, rc; 7930Sstevel@tonic-gate struct stat st; 7940Sstevel@tonic-gate prxmap_t *prmapp, *pmp; 7950Sstevel@tonic-gate ssize_t n; 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate (void) snprintf(mapname, sizeof (mapname), 7980Sstevel@tonic-gate "/proc/%d/xmap", (int)Pstatus(Pr)->pr_pid); 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) { 8010Sstevel@tonic-gate if (mapfd >= 0) 8020Sstevel@tonic-gate (void) close(mapfd); 8030Sstevel@tonic-gate return (perr(mapname)); 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate nmap = st.st_size / sizeof (prxmap_t); 8070Sstevel@tonic-gate nmap *= 2; 8080Sstevel@tonic-gate again: 8090Sstevel@tonic-gate prmapp = malloc((nmap + 1) * sizeof (prxmap_t)); 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prxmap_t), 0)) < 0) { 8120Sstevel@tonic-gate (void) close(mapfd); 8130Sstevel@tonic-gate free(prmapp); 8140Sstevel@tonic-gate return (perr("read xmap")); 8150Sstevel@tonic-gate } 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate if (nmap < n / sizeof (prxmap_t)) { 8180Sstevel@tonic-gate free(prmapp); 8190Sstevel@tonic-gate nmap *= 2; 8200Sstevel@tonic-gate goto again; 8210Sstevel@tonic-gate } 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate (void) close(mapfd); 8240Sstevel@tonic-gate nmap = n / sizeof (prxmap_t); 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) { 8270Sstevel@tonic-gate if ((rc = func(cd, pmp, NULL, i == nmap - 1, doswap)) != 0) { 8280Sstevel@tonic-gate free(prmapp); 8290Sstevel@tonic-gate return (rc); 8300Sstevel@tonic-gate } 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate 8332685Sakolb /* 8342685Sakolb * Mark the last element. 8352685Sakolb */ 8362685Sakolb if (map_count > 0) 8372685Sakolb maps[map_count - 1].md_last = B_TRUE; 8382685Sakolb 8390Sstevel@tonic-gate free(prmapp); 8400Sstevel@tonic-gate return (0); 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate /*ARGSUSED*/ 8440Sstevel@tonic-gate static int 8450Sstevel@tonic-gate look_map(void *data, const prmap_t *pmp, const char *object_name) 8460Sstevel@tonic-gate { 8470Sstevel@tonic-gate struct totals *t = data; 8480Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Pr); 8492685Sakolb size_t size; 8500Sstevel@tonic-gate char mname[PATH_MAX]; 8510Sstevel@tonic-gate char *lname = NULL; 8522685Sakolb size_t psz = pmp->pr_pagesize; 8532685Sakolb uintptr_t vaddr = pmp->pr_vaddr; 8542685Sakolb uintptr_t segment_end = vaddr + pmp->pr_size; 8552685Sakolb lgrp_id_t lgrp; 8562685Sakolb memory_chunk_t mchunk; 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate /* 8590Sstevel@tonic-gate * If the mapping is not anon or not part of the heap, make a name 8600Sstevel@tonic-gate * for it. We don't want to report the heap as a.out's data. 8610Sstevel@tonic-gate */ 8620Sstevel@tonic-gate if (!(pmp->pr_mflags & MA_ANON) || 8632685Sakolb segment_end <= Psp->pr_brkbase || 8640Sstevel@tonic-gate pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) { 8650Sstevel@tonic-gate lname = make_name(Pr, pmp->pr_vaddr, pmp->pr_mapname, 8660Sstevel@tonic-gate mname, sizeof (mname)); 8670Sstevel@tonic-gate } 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate if (lname == NULL && 8700Sstevel@tonic-gate ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD)) { 8710Sstevel@tonic-gate lname = anon_name(mname, Psp, pmp->pr_vaddr, 8720Sstevel@tonic-gate pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid); 8730Sstevel@tonic-gate } 8740Sstevel@tonic-gate 8752685Sakolb /* 8762685Sakolb * Adjust the address range if -A is specified. 8772685Sakolb */ 8782685Sakolb size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz, 8792685Sakolb &vaddr, &segment_end); 8802685Sakolb 8812685Sakolb if (size == 0) 8822685Sakolb return (0); 8832685Sakolb 8842685Sakolb if (!Lflag) { 8852685Sakolb /* 8862685Sakolb * Display the whole mapping 8872685Sakolb */ 8882685Sakolb size = ROUNDUP_KB(size); 8892685Sakolb 8902685Sakolb (void) printf(lname ? 8912685Sakolb "%.*lX %*luK %-6s %s\n" : 8922685Sakolb "%.*lX %*luK %s\n", 8932685Sakolb addr_width, vaddr, 8942685Sakolb size_width - 1, size, mflags(pmp->pr_mflags), lname); 8952685Sakolb 8962685Sakolb t->total_size += size; 8972685Sakolb return (0); 8982685Sakolb } 8992685Sakolb 9002685Sakolb /* 9012685Sakolb * We need to display lgroups backing physical memory, so we break the 9022685Sakolb * segment into individual pages and coalesce pages with the same lgroup 9032685Sakolb * into one "segment". 9042685Sakolb */ 9050Sstevel@tonic-gate 9062685Sakolb /* 9072685Sakolb * Initialize address descriptions for the mapping. 9082685Sakolb */ 9092685Sakolb mem_chunk_init(&mchunk, segment_end, psz); 9102685Sakolb size = 0; 9112685Sakolb 9122685Sakolb /* 9132685Sakolb * Walk mapping (page by page) and display contiguous ranges of memory 9142685Sakolb * allocated to same lgroup. 9152685Sakolb */ 9162685Sakolb do { 9172685Sakolb size_t size_contig; 9182685Sakolb 9192685Sakolb /* 9202685Sakolb * Get contiguous region of memory starting from vaddr allocated 9212685Sakolb * from the same lgroup. 9222685Sakolb */ 9232685Sakolb size_contig = get_contiguous_region(&mchunk, vaddr, 9242685Sakolb segment_end, pmp->pr_pagesize, &lgrp); 9252685Sakolb 9262685Sakolb (void) printf(lname ? "%.*lX %*luK %-6s%s %s\n" : 9272685Sakolb "%.*lX %*luK %s %s\n", 9282685Sakolb addr_width, vaddr, 9292685Sakolb size_width - 1, size_contig / KILOBYTE, 9302685Sakolb mflags(pmp->pr_mflags), 9312685Sakolb lgrp2str(lgrp), lname); 9322685Sakolb 9332685Sakolb vaddr += size_contig; 9342685Sakolb size += size_contig; 9352685Sakolb } while (vaddr < segment_end && !interrupt); 9362685Sakolb 9372685Sakolb /* Update the total size */ 9382685Sakolb t->total_size += ROUNDUP_KB(size); 9390Sstevel@tonic-gate return (0); 9400Sstevel@tonic-gate } 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate static void 9430Sstevel@tonic-gate printK(long value, int width) 9440Sstevel@tonic-gate { 9450Sstevel@tonic-gate if (value == 0) 9460Sstevel@tonic-gate (void) printf(width == 8 ? " -" : " -"); 9470Sstevel@tonic-gate else 9480Sstevel@tonic-gate (void) printf(" %*lu", width - 1, value); 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate static const char * 9520Sstevel@tonic-gate pagesize(const prxmap_t *pmp) 9530Sstevel@tonic-gate { 9540Sstevel@tonic-gate int pagesize = pmp->pr_hatpagesize; 9550Sstevel@tonic-gate static char buf[32]; 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate if (pagesize == 0) { 9580Sstevel@tonic-gate return ("-"); /* no underlying HAT mapping */ 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate 9612685Sakolb if (pagesize >= KILOBYTE && (pagesize % KILOBYTE) == 0) { 9622685Sakolb if ((pagesize % GIGABYTE) == 0) 9630Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%dG", 9642685Sakolb pagesize / GIGABYTE); 9652685Sakolb else if ((pagesize % MEGABYTE) == 0) 9660Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%dM", 9672685Sakolb pagesize / MEGABYTE); 9680Sstevel@tonic-gate else 9690Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%dK", 9702685Sakolb pagesize / KILOBYTE); 9710Sstevel@tonic-gate } else 9720Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%db", pagesize); 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate return (buf); 9750Sstevel@tonic-gate } 9760Sstevel@tonic-gate 9770Sstevel@tonic-gate /*ARGSUSED*/ 9780Sstevel@tonic-gate static int 9790Sstevel@tonic-gate look_smap(void *data, 9800Sstevel@tonic-gate const prxmap_t *pmp, 9810Sstevel@tonic-gate const char *object_name, 9820Sstevel@tonic-gate int last, int doswap) 9830Sstevel@tonic-gate { 9840Sstevel@tonic-gate struct totals *t = data; 9850Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Pr); 9862685Sakolb size_t size; 9870Sstevel@tonic-gate char mname[PATH_MAX]; 9880Sstevel@tonic-gate char *lname = NULL; 9890Sstevel@tonic-gate const char *format; 9902685Sakolb size_t psz = pmp->pr_pagesize; 9912685Sakolb uintptr_t vaddr = pmp->pr_vaddr; 9922685Sakolb uintptr_t segment_end = vaddr + pmp->pr_size; 9932685Sakolb lgrp_id_t lgrp; 9942685Sakolb memory_chunk_t mchunk; 9950Sstevel@tonic-gate 9960Sstevel@tonic-gate /* 9970Sstevel@tonic-gate * If the mapping is not anon or not part of the heap, make a name 9980Sstevel@tonic-gate * for it. We don't want to report the heap as a.out's data. 9990Sstevel@tonic-gate */ 10000Sstevel@tonic-gate if (!(pmp->pr_mflags & MA_ANON) || 10010Sstevel@tonic-gate pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase || 10020Sstevel@tonic-gate pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) { 10030Sstevel@tonic-gate lname = make_name(Pr, pmp->pr_vaddr, pmp->pr_mapname, 10040Sstevel@tonic-gate mname, sizeof (mname)); 10050Sstevel@tonic-gate } 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate if (lname == NULL && 10080Sstevel@tonic-gate ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD)) { 10090Sstevel@tonic-gate lname = anon_name(mname, Psp, pmp->pr_vaddr, 10100Sstevel@tonic-gate pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid); 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate 10132685Sakolb /* 10142685Sakolb * Adjust the address range if -A is specified. 10152685Sakolb */ 10162685Sakolb size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz, 10172685Sakolb &vaddr, &segment_end); 10182685Sakolb 10192685Sakolb if (size == 0) 10202685Sakolb return (0); 10212685Sakolb 10222685Sakolb if (!Lflag) { 10232685Sakolb /* 10242685Sakolb * Display the whole mapping 10252685Sakolb */ 10262685Sakolb if (lname != NULL) 10272685Sakolb format = "%.*lX %*luK %4s %-6s %s\n"; 10282685Sakolb else 10292685Sakolb format = "%.*lX %*luK %4s %s\n"; 10302685Sakolb 10312685Sakolb size = ROUNDUP_KB(size); 10322685Sakolb 10332685Sakolb (void) printf(format, addr_width, vaddr, size_width - 1, size, 10342685Sakolb pagesize(pmp), mflags(pmp->pr_mflags), lname); 10352685Sakolb 10362685Sakolb t->total_size += size; 10372685Sakolb return (0); 10382685Sakolb } 10392685Sakolb 10400Sstevel@tonic-gate if (lname != NULL) 10412685Sakolb format = "%.*lX %*luK %4s %-6s%s %s\n"; 10420Sstevel@tonic-gate else 10432685Sakolb format = "%.*lX %*luK %4s%s %s\n"; 10440Sstevel@tonic-gate 10452685Sakolb /* 10462685Sakolb * We need to display lgroups backing physical memory, so we break the 10472685Sakolb * segment into individual pages and coalesce pages with the same lgroup 10482685Sakolb * into one "segment". 10492685Sakolb */ 10502685Sakolb 10512685Sakolb /* 10522685Sakolb * Initialize address descriptions for the mapping. 10532685Sakolb */ 10542685Sakolb mem_chunk_init(&mchunk, segment_end, psz); 10552685Sakolb size = 0; 10560Sstevel@tonic-gate 10572685Sakolb /* 10582685Sakolb * Walk mapping (page by page) and display contiguous ranges of memory 10592685Sakolb * allocated to same lgroup. 10602685Sakolb */ 10612685Sakolb do { 10622685Sakolb size_t size_contig; 10632685Sakolb 10642685Sakolb /* 10652685Sakolb * Get contiguous region of memory starting from vaddr allocated 10662685Sakolb * from the same lgroup. 10672685Sakolb */ 10682685Sakolb size_contig = get_contiguous_region(&mchunk, vaddr, 10692685Sakolb segment_end, pmp->pr_pagesize, &lgrp); 10702685Sakolb 10712685Sakolb (void) printf(format, addr_width, vaddr, 10722685Sakolb size_width - 1, size_contig / KILOBYTE, 10732685Sakolb pagesize(pmp), mflags(pmp->pr_mflags), 10742685Sakolb lgrp2str(lgrp), lname); 10752685Sakolb 10762685Sakolb vaddr += size_contig; 10772685Sakolb size += size_contig; 10782685Sakolb } while (vaddr < segment_end && !interrupt); 10792685Sakolb 10802685Sakolb t->total_size += ROUNDUP_KB(size); 10810Sstevel@tonic-gate return (0); 10820Sstevel@tonic-gate } 10830Sstevel@tonic-gate 10840Sstevel@tonic-gate #define ANON(x) ((aflag || (((x)->pr_mflags & MA_SHARED) == 0)) ? \ 10850Sstevel@tonic-gate ((x)->pr_anon) : 0) 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate /*ARGSUSED*/ 10880Sstevel@tonic-gate static int 10890Sstevel@tonic-gate look_xmap(void *data, 10900Sstevel@tonic-gate const prxmap_t *pmp, 10910Sstevel@tonic-gate const char *object_name, 10920Sstevel@tonic-gate int last, int doswap) 10930Sstevel@tonic-gate { 10940Sstevel@tonic-gate struct totals *t = data; 10950Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Pr); 10960Sstevel@tonic-gate char mname[PATH_MAX]; 10970Sstevel@tonic-gate char *lname = NULL; 10980Sstevel@tonic-gate char *ln; 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate /* 11010Sstevel@tonic-gate * If the mapping is not anon or not part of the heap, make a name 11020Sstevel@tonic-gate * for it. We don't want to report the heap as a.out's data. 11030Sstevel@tonic-gate */ 11040Sstevel@tonic-gate if (!(pmp->pr_mflags & MA_ANON) || 11050Sstevel@tonic-gate pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase || 11060Sstevel@tonic-gate pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) { 11070Sstevel@tonic-gate lname = make_name(Pr, pmp->pr_vaddr, pmp->pr_mapname, 11080Sstevel@tonic-gate mname, sizeof (mname)); 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate if (lname != NULL) { 11120Sstevel@tonic-gate if ((ln = strrchr(lname, '/')) != NULL) 11130Sstevel@tonic-gate lname = ln + 1; 11140Sstevel@tonic-gate } else if ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD) { 11150Sstevel@tonic-gate lname = anon_name(mname, Psp, pmp->pr_vaddr, 11160Sstevel@tonic-gate pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid); 11170Sstevel@tonic-gate } 11180Sstevel@tonic-gate 11190Sstevel@tonic-gate (void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr); 11200Sstevel@tonic-gate 11212685Sakolb printK(ROUNDUP_KB(pmp->pr_size), size_width); 11222685Sakolb printK(pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE), size_width); 11232685Sakolb printK(ANON(pmp) * (pmp->pr_pagesize / KILOBYTE), size_width); 11242685Sakolb printK(pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE), size_width); 11250Sstevel@tonic-gate (void) printf(lname ? " %4s %-6s %s\n" : " %4s %s\n", 11260Sstevel@tonic-gate pagesize(pmp), mflags(pmp->pr_mflags), lname); 11270Sstevel@tonic-gate 11282685Sakolb t->total_size += ROUNDUP_KB(pmp->pr_size); 11292685Sakolb t->total_rss += pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE); 11302685Sakolb t->total_anon += ANON(pmp) * (pmp->pr_pagesize / KILOBYTE); 11312685Sakolb t->total_locked += (pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE)); 11320Sstevel@tonic-gate 11330Sstevel@tonic-gate return (0); 11340Sstevel@tonic-gate } 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate /*ARGSUSED*/ 11370Sstevel@tonic-gate static int 11380Sstevel@tonic-gate look_xmap_nopgsz(void *data, 11390Sstevel@tonic-gate const prxmap_t *pmp, 11400Sstevel@tonic-gate const char *object_name, 11410Sstevel@tonic-gate int last, int doswap) 11420Sstevel@tonic-gate { 11430Sstevel@tonic-gate struct totals *t = data; 11440Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Pr); 11450Sstevel@tonic-gate char mname[PATH_MAX]; 11460Sstevel@tonic-gate char *lname = NULL; 11470Sstevel@tonic-gate char *ln; 11480Sstevel@tonic-gate static uintptr_t prev_vaddr; 11490Sstevel@tonic-gate static size_t prev_size; 11500Sstevel@tonic-gate static offset_t prev_offset; 11510Sstevel@tonic-gate static int prev_mflags; 11520Sstevel@tonic-gate static char *prev_lname; 11530Sstevel@tonic-gate static char prev_mname[PATH_MAX]; 11540Sstevel@tonic-gate static ulong_t prev_rss; 11550Sstevel@tonic-gate static ulong_t prev_anon; 11560Sstevel@tonic-gate static ulong_t prev_locked; 11570Sstevel@tonic-gate static ulong_t prev_swap; 11580Sstevel@tonic-gate int merged = 0; 11590Sstevel@tonic-gate static int first = 1; 11600Sstevel@tonic-gate ulong_t swap = 0; 11610Sstevel@tonic-gate int kperpage; 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate /* 11640Sstevel@tonic-gate * Calculate swap reservations 11650Sstevel@tonic-gate */ 11660Sstevel@tonic-gate if (pmp->pr_mflags & MA_SHARED) { 11670Sstevel@tonic-gate if (aflag && (pmp->pr_mflags & MA_NORESERVE) == 0) { 11680Sstevel@tonic-gate /* Swap reserved for entire non-ism SHM */ 11690Sstevel@tonic-gate swap = pmp->pr_size / pmp->pr_pagesize; 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate } else if (pmp->pr_mflags & MA_NORESERVE) { 11720Sstevel@tonic-gate /* Swap reserved on fault for each anon page */ 11730Sstevel@tonic-gate swap = pmp->pr_anon; 11740Sstevel@tonic-gate } else if (pmp->pr_mflags & MA_WRITE) { 11750Sstevel@tonic-gate /* Swap reserve for entire writable segment */ 11760Sstevel@tonic-gate swap = pmp->pr_size / pmp->pr_pagesize; 11770Sstevel@tonic-gate } 11780Sstevel@tonic-gate 11790Sstevel@tonic-gate /* 11800Sstevel@tonic-gate * If the mapping is not anon or not part of the heap, make a name 11810Sstevel@tonic-gate * for it. We don't want to report the heap as a.out's data. 11820Sstevel@tonic-gate */ 11830Sstevel@tonic-gate if (!(pmp->pr_mflags & MA_ANON) || 11840Sstevel@tonic-gate pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase || 11850Sstevel@tonic-gate pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) { 11860Sstevel@tonic-gate lname = make_name(Pr, pmp->pr_vaddr, pmp->pr_mapname, 11870Sstevel@tonic-gate mname, sizeof (mname)); 11880Sstevel@tonic-gate } 11890Sstevel@tonic-gate 11900Sstevel@tonic-gate if (lname != NULL) { 11910Sstevel@tonic-gate if ((ln = strrchr(lname, '/')) != NULL) 11920Sstevel@tonic-gate lname = ln + 1; 11930Sstevel@tonic-gate } else if ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD) { 11940Sstevel@tonic-gate lname = anon_name(mname, Psp, pmp->pr_vaddr, 11950Sstevel@tonic-gate pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid); 11960Sstevel@tonic-gate } 11970Sstevel@tonic-gate 11982685Sakolb kperpage = pmp->pr_pagesize / KILOBYTE; 11990Sstevel@tonic-gate 12002685Sakolb t->total_size += ROUNDUP_KB(pmp->pr_size); 12010Sstevel@tonic-gate t->total_rss += pmp->pr_rss * kperpage; 12020Sstevel@tonic-gate t->total_anon += ANON(pmp) * kperpage; 12030Sstevel@tonic-gate t->total_locked += pmp->pr_locked * kperpage; 12040Sstevel@tonic-gate t->total_swap += swap * kperpage; 12050Sstevel@tonic-gate 12060Sstevel@tonic-gate if (first == 1) { 12070Sstevel@tonic-gate first = 0; 12080Sstevel@tonic-gate prev_vaddr = pmp->pr_vaddr; 12090Sstevel@tonic-gate prev_size = pmp->pr_size; 12100Sstevel@tonic-gate prev_offset = pmp->pr_offset; 12110Sstevel@tonic-gate prev_mflags = pmp->pr_mflags; 12120Sstevel@tonic-gate if (lname == NULL) { 12130Sstevel@tonic-gate prev_lname = NULL; 12140Sstevel@tonic-gate } else { 12150Sstevel@tonic-gate (void) strcpy(prev_mname, lname); 12160Sstevel@tonic-gate prev_lname = prev_mname; 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate prev_rss = pmp->pr_rss * kperpage; 12190Sstevel@tonic-gate prev_anon = ANON(pmp) * kperpage; 12200Sstevel@tonic-gate prev_locked = pmp->pr_locked * kperpage; 12210Sstevel@tonic-gate prev_swap = swap * kperpage; 12220Sstevel@tonic-gate if (last == 0) { 12230Sstevel@tonic-gate return (0); 12240Sstevel@tonic-gate } 12250Sstevel@tonic-gate merged = 1; 12260Sstevel@tonic-gate } else if (prev_vaddr + prev_size == pmp->pr_vaddr && 12270Sstevel@tonic-gate prev_mflags == pmp->pr_mflags && 12280Sstevel@tonic-gate ((prev_mflags & MA_ISM) || 12294450Seh208807 prev_offset + prev_size == pmp->pr_offset) && 12300Sstevel@tonic-gate ((lname == NULL && prev_lname == NULL) || 12314450Seh208807 (lname != NULL && prev_lname != NULL && 12324450Seh208807 strcmp(lname, prev_lname) == 0))) { 12330Sstevel@tonic-gate prev_size += pmp->pr_size; 12340Sstevel@tonic-gate prev_rss += pmp->pr_rss * kperpage; 12350Sstevel@tonic-gate prev_anon += ANON(pmp) * kperpage; 12360Sstevel@tonic-gate prev_locked += pmp->pr_locked * kperpage; 12370Sstevel@tonic-gate prev_swap += swap * kperpage; 12380Sstevel@tonic-gate if (last == 0) { 12390Sstevel@tonic-gate return (0); 12400Sstevel@tonic-gate } 12410Sstevel@tonic-gate merged = 1; 12420Sstevel@tonic-gate } 12430Sstevel@tonic-gate 12440Sstevel@tonic-gate (void) printf("%.*lX", addr_width, (ulong_t)prev_vaddr); 12452685Sakolb printK(ROUNDUP_KB(prev_size), size_width); 12460Sstevel@tonic-gate 12470Sstevel@tonic-gate if (doswap) 12480Sstevel@tonic-gate printK(prev_swap, size_width); 12490Sstevel@tonic-gate else { 12500Sstevel@tonic-gate printK(prev_rss, size_width); 12510Sstevel@tonic-gate printK(prev_anon, size_width); 12520Sstevel@tonic-gate printK(prev_locked, size_width); 12530Sstevel@tonic-gate } 12542685Sakolb (void) printf(prev_lname ? " %-6s %s\n" : "%s\n", 12550Sstevel@tonic-gate mflags(prev_mflags), prev_lname); 12560Sstevel@tonic-gate 12570Sstevel@tonic-gate if (last == 0) { 12580Sstevel@tonic-gate prev_vaddr = pmp->pr_vaddr; 12590Sstevel@tonic-gate prev_size = pmp->pr_size; 12600Sstevel@tonic-gate prev_offset = pmp->pr_offset; 12610Sstevel@tonic-gate prev_mflags = pmp->pr_mflags; 12620Sstevel@tonic-gate if (lname == NULL) { 12630Sstevel@tonic-gate prev_lname = NULL; 12640Sstevel@tonic-gate } else { 12650Sstevel@tonic-gate (void) strcpy(prev_mname, lname); 12660Sstevel@tonic-gate prev_lname = prev_mname; 12670Sstevel@tonic-gate } 12680Sstevel@tonic-gate prev_rss = pmp->pr_rss * kperpage; 12690Sstevel@tonic-gate prev_anon = ANON(pmp) * kperpage; 12700Sstevel@tonic-gate prev_locked = pmp->pr_locked * kperpage; 12710Sstevel@tonic-gate prev_swap = swap * kperpage; 12720Sstevel@tonic-gate } else if (merged == 0) { 12730Sstevel@tonic-gate (void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr); 12742685Sakolb printK(ROUNDUP_KB(pmp->pr_size), size_width); 12750Sstevel@tonic-gate if (doswap) 12760Sstevel@tonic-gate printK(swap * kperpage, size_width); 12770Sstevel@tonic-gate else { 12780Sstevel@tonic-gate printK(pmp->pr_rss * kperpage, size_width); 12790Sstevel@tonic-gate printK(ANON(pmp) * kperpage, size_width); 12800Sstevel@tonic-gate printK(pmp->pr_locked * kperpage, size_width); 12810Sstevel@tonic-gate } 12820Sstevel@tonic-gate (void) printf(lname ? " %-6s %s\n" : " %s\n", 12830Sstevel@tonic-gate mflags(pmp->pr_mflags), lname); 12840Sstevel@tonic-gate } 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate if (last != 0) 12870Sstevel@tonic-gate first = 1; 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate return (0); 12900Sstevel@tonic-gate } 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate static int 12930Sstevel@tonic-gate perr(char *s) 12940Sstevel@tonic-gate { 12950Sstevel@tonic-gate if (s) 12960Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", procname); 12970Sstevel@tonic-gate else 12980Sstevel@tonic-gate s = procname; 12990Sstevel@tonic-gate perror(s); 13000Sstevel@tonic-gate return (1); 13010Sstevel@tonic-gate } 13020Sstevel@tonic-gate 13030Sstevel@tonic-gate static char * 13040Sstevel@tonic-gate mflags(uint_t arg) 13050Sstevel@tonic-gate { 13060Sstevel@tonic-gate static char code_buf[80]; 13070Sstevel@tonic-gate char *str = code_buf; 13080Sstevel@tonic-gate 13090Sstevel@tonic-gate /* 13100Sstevel@tonic-gate * rwxsR 13110Sstevel@tonic-gate * 13120Sstevel@tonic-gate * r - segment is readable 13130Sstevel@tonic-gate * w - segment is writable 13140Sstevel@tonic-gate * x - segment is executable 13150Sstevel@tonic-gate * s - segment is shared 13160Sstevel@tonic-gate * R - segment is mapped MAP_NORESERVE 13170Sstevel@tonic-gate * 13180Sstevel@tonic-gate */ 13190Sstevel@tonic-gate (void) sprintf(str, "%c%c%c%c%c%c", 13200Sstevel@tonic-gate arg & MA_READ ? 'r' : '-', 13210Sstevel@tonic-gate arg & MA_WRITE ? 'w' : '-', 13220Sstevel@tonic-gate arg & MA_EXEC ? 'x' : '-', 13230Sstevel@tonic-gate arg & MA_SHARED ? 's' : '-', 13240Sstevel@tonic-gate arg & MA_NORESERVE ? 'R' : '-', 13250Sstevel@tonic-gate arg & MA_RESERVED1 ? '*' : ' '); 13260Sstevel@tonic-gate 13270Sstevel@tonic-gate return (str); 13280Sstevel@tonic-gate } 13290Sstevel@tonic-gate 13300Sstevel@tonic-gate static mapdata_t * 13310Sstevel@tonic-gate nextmap(void) 13320Sstevel@tonic-gate { 13330Sstevel@tonic-gate mapdata_t *newmaps; 13340Sstevel@tonic-gate int next; 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate if (map_count == map_alloc) { 13370Sstevel@tonic-gate if (map_alloc == 0) 13380Sstevel@tonic-gate next = 16; 13390Sstevel@tonic-gate else 13400Sstevel@tonic-gate next = map_alloc * 2; 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate newmaps = realloc(maps, next * sizeof (mapdata_t)); 13430Sstevel@tonic-gate if (newmaps == NULL) { 13440Sstevel@tonic-gate (void) perr("failed to allocate maps"); 13450Sstevel@tonic-gate exit(1); 13460Sstevel@tonic-gate } 13470Sstevel@tonic-gate (void) memset(newmaps + map_alloc, '\0', 13480Sstevel@tonic-gate (next - map_alloc) * sizeof (mapdata_t)); 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate map_alloc = next; 13510Sstevel@tonic-gate maps = newmaps; 13520Sstevel@tonic-gate } 13530Sstevel@tonic-gate 13540Sstevel@tonic-gate return (&maps[map_count++]); 13550Sstevel@tonic-gate } 13560Sstevel@tonic-gate 13570Sstevel@tonic-gate /*ARGSUSED*/ 13580Sstevel@tonic-gate static int 13590Sstevel@tonic-gate gather_map(void *ignored, const prmap_t *map, const char *objname) 13600Sstevel@tonic-gate { 13612685Sakolb mapdata_t *data; 13620Sstevel@tonic-gate 13632685Sakolb /* Skip mappings which are outside the range specified by -A */ 13642685Sakolb if (!address_in_range(map->pr_vaddr, 13654450Seh208807 map->pr_vaddr + map->pr_size, map->pr_pagesize)) 13662685Sakolb return (0); 13672685Sakolb 13682685Sakolb data = nextmap(); 13690Sstevel@tonic-gate data->md_map = *map; 13700Sstevel@tonic-gate if (data->md_objname != NULL) 13710Sstevel@tonic-gate free(data->md_objname); 13720Sstevel@tonic-gate data->md_objname = objname ? strdup(objname) : NULL; 13730Sstevel@tonic-gate 13740Sstevel@tonic-gate return (0); 13750Sstevel@tonic-gate } 13760Sstevel@tonic-gate 13770Sstevel@tonic-gate /*ARGSUSED*/ 13780Sstevel@tonic-gate static int 13790Sstevel@tonic-gate gather_xmap(void *ignored, const prxmap_t *xmap, const char *objname, 13800Sstevel@tonic-gate int last, int doswap) 13810Sstevel@tonic-gate { 13822685Sakolb mapdata_t *data; 13830Sstevel@tonic-gate 13842685Sakolb /* Skip mappings which are outside the range specified by -A */ 13852685Sakolb if (!address_in_range(xmap->pr_vaddr, 13864450Seh208807 xmap->pr_vaddr + xmap->pr_size, xmap->pr_pagesize)) 13872685Sakolb return (0); 13882685Sakolb 13892685Sakolb data = nextmap(); 13900Sstevel@tonic-gate data->md_xmap = *xmap; 13910Sstevel@tonic-gate if (data->md_objname != NULL) 13920Sstevel@tonic-gate free(data->md_objname); 13930Sstevel@tonic-gate data->md_objname = objname ? strdup(objname) : NULL; 13940Sstevel@tonic-gate data->md_last = last; 13950Sstevel@tonic-gate data->md_doswap = doswap; 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate return (0); 13980Sstevel@tonic-gate } 13990Sstevel@tonic-gate 14000Sstevel@tonic-gate static int 14010Sstevel@tonic-gate iter_map(proc_map_f *func, void *data) 14020Sstevel@tonic-gate { 14030Sstevel@tonic-gate int i; 14040Sstevel@tonic-gate int ret; 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate for (i = 0; i < map_count; i++) { 14072685Sakolb if (interrupt) 14082685Sakolb break; 14090Sstevel@tonic-gate if ((ret = func(data, &maps[i].md_map, 14100Sstevel@tonic-gate maps[i].md_objname)) != 0) 14110Sstevel@tonic-gate return (ret); 14120Sstevel@tonic-gate } 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate return (0); 14150Sstevel@tonic-gate } 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate static int 14180Sstevel@tonic-gate iter_xmap(proc_xmap_f *func, void *data) 14190Sstevel@tonic-gate { 14200Sstevel@tonic-gate int i; 14210Sstevel@tonic-gate int ret; 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate for (i = 0; i < map_count; i++) { 14242685Sakolb if (interrupt) 14252685Sakolb break; 14260Sstevel@tonic-gate if ((ret = func(data, &maps[i].md_xmap, maps[i].md_objname, 14270Sstevel@tonic-gate maps[i].md_last, maps[i].md_doswap)) != 0) 14280Sstevel@tonic-gate return (ret); 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate return (0); 14320Sstevel@tonic-gate } 14332685Sakolb 14342685Sakolb /* 14352685Sakolb * Convert lgroup ID to string. 14362685Sakolb * returns dash when lgroup ID is invalid. 14372685Sakolb */ 14382685Sakolb static char * 14392685Sakolb lgrp2str(lgrp_id_t lgrp) 14402685Sakolb { 14412685Sakolb static char lgrp_buf[20]; 14422685Sakolb char *str = lgrp_buf; 14432685Sakolb 14442685Sakolb (void) sprintf(str, lgrp == LGRP_NONE ? " -" : "%4d", lgrp); 14452685Sakolb return (str); 14462685Sakolb } 14472685Sakolb 14482685Sakolb /* 14492685Sakolb * Parse address range specification for -A option. 14502685Sakolb * The address range may have the following forms: 14512685Sakolb * 14522685Sakolb * address 14532685Sakolb * start and end is set to address 14542685Sakolb * address, 14552685Sakolb * start is set to address, end is set to INVALID_ADDRESS 14562685Sakolb * ,address 14572685Sakolb * start is set to 0, end is set to address 14582685Sakolb * address1,address2 14592685Sakolb * start is set to address1, end is set to address2 14602685Sakolb * 14612685Sakolb */ 14622685Sakolb static int 14632685Sakolb parse_addr_range(char *input_str, uintptr_t *start, uintptr_t *end) 14642685Sakolb { 14652685Sakolb char *startp = input_str; 14662685Sakolb char *endp = strchr(input_str, ','); 14672685Sakolb ulong_t s = (ulong_t)INVALID_ADDRESS; 14682685Sakolb ulong_t e = (ulong_t)INVALID_ADDRESS; 14692685Sakolb 14702685Sakolb if (endp != NULL) { 14712685Sakolb /* 14722685Sakolb * Comma is present. If there is nothing after comma, the end 14732685Sakolb * remains set at INVALID_ADDRESS. Otherwise it is set to the 14742685Sakolb * value after comma. 14752685Sakolb */ 14762685Sakolb *endp = '\0'; 14772685Sakolb endp++; 14782685Sakolb 14792685Sakolb if ((*endp != '\0') && sscanf(endp, "%lx", &e) != 1) 14802685Sakolb return (1); 14812685Sakolb } 14822685Sakolb 14832685Sakolb if (startp != NULL) { 14842685Sakolb /* 14852685Sakolb * Read the start address, if it is specified. If the address is 14862685Sakolb * missing, start will be set to INVALID_ADDRESS. 14872685Sakolb */ 14882685Sakolb if ((*startp != '\0') && sscanf(startp, "%lx", &s) != 1) 14892685Sakolb return (1); 14902685Sakolb } 14912685Sakolb 14922685Sakolb /* If there is no comma, end becomes equal to start */ 14932685Sakolb if (endp == NULL) 14942685Sakolb e = s; 14952685Sakolb 14962685Sakolb /* 14972685Sakolb * ,end implies 0..end range 14982685Sakolb */ 14992685Sakolb if (e != INVALID_ADDRESS && s == INVALID_ADDRESS) 15002685Sakolb s = 0; 15012685Sakolb 15022685Sakolb *start = (uintptr_t)s; 15032685Sakolb *end = (uintptr_t)e; 15042685Sakolb 15052685Sakolb /* Return error if neither start nor end address were specified */ 15062685Sakolb return (! (s != INVALID_ADDRESS || e != INVALID_ADDRESS)); 15072685Sakolb } 15082685Sakolb 15092685Sakolb /* 15102685Sakolb * Check whether any portion of [start, end] segment is within the 15112685Sakolb * [start_addr, end_addr] range. 15122685Sakolb * 15132685Sakolb * Return values: 15142685Sakolb * 0 - address is outside the range 15152685Sakolb * 1 - address is within the range 15162685Sakolb */ 15172685Sakolb static int 15182685Sakolb address_in_range(uintptr_t start, uintptr_t end, size_t psz) 15192685Sakolb { 15202685Sakolb int rc = 1; 15212685Sakolb 15222685Sakolb /* 15232685Sakolb * Nothing to do if there is no address range specified with -A 15242685Sakolb */ 15252685Sakolb if (start_addr != INVALID_ADDRESS || end_addr != INVALID_ADDRESS) { 15262685Sakolb /* The segment end is below the range start */ 15272685Sakolb if ((start_addr != INVALID_ADDRESS) && 15282685Sakolb (end < P2ALIGN(start_addr, psz))) 15292685Sakolb rc = 0; 15302685Sakolb 15312685Sakolb /* The segment start is above the range end */ 15322685Sakolb if ((end_addr != INVALID_ADDRESS) && 15332685Sakolb (start > P2ALIGN(end_addr + psz, psz))) 15342685Sakolb rc = 0; 15352685Sakolb } 15362685Sakolb return (rc); 15372685Sakolb } 15382685Sakolb 15392685Sakolb /* 15402685Sakolb * Returns an intersection of the [start, end] interval and the range specified 15412685Sakolb * by -A flag [start_addr, end_addr]. Unspecified parts of the address range 15422685Sakolb * have value INVALID_ADDRESS. 15432685Sakolb * 15442685Sakolb * The start_addr address is rounded down to the beginning of page and end_addr 15452685Sakolb * is rounded up to the end of page. 15462685Sakolb * 15472685Sakolb * Returns the size of the resulting interval or zero if the interval is empty 15482685Sakolb * or invalid. 15492685Sakolb */ 15502685Sakolb static size_t 15512685Sakolb adjust_addr_range(uintptr_t start, uintptr_t end, size_t psz, 15522685Sakolb uintptr_t *new_start, uintptr_t *new_end) 15532685Sakolb { 15542685Sakolb uintptr_t from; /* start_addr rounded down */ 15552685Sakolb uintptr_t to; /* end_addr rounded up */ 15562685Sakolb 15572685Sakolb /* 15582685Sakolb * Round down the lower address of the range to the beginning of page. 15592685Sakolb */ 15602685Sakolb if (start_addr == INVALID_ADDRESS) { 15612685Sakolb /* 15622685Sakolb * No start_addr specified by -A, the lower part of the interval 15632685Sakolb * does not change. 15642685Sakolb */ 15652685Sakolb *new_start = start; 15662685Sakolb } else { 15672685Sakolb from = P2ALIGN(start_addr, psz); 15682685Sakolb /* 15692685Sakolb * If end address is outside the range, return an empty 15702685Sakolb * interval 15712685Sakolb */ 15722685Sakolb if (end < from) { 15732685Sakolb *new_start = *new_end = 0; 15742685Sakolb return (0); 15752685Sakolb } 15762685Sakolb /* 15772685Sakolb * The adjusted start address is the maximum of requested start 15782685Sakolb * and the aligned start_addr of the -A range. 15792685Sakolb */ 15802685Sakolb *new_start = start < from ? from : start; 15812685Sakolb } 15822685Sakolb 15832685Sakolb /* 15842685Sakolb * Round up the higher address of the range to the end of page. 15852685Sakolb */ 15862685Sakolb if (end_addr == INVALID_ADDRESS) { 15872685Sakolb /* 15882685Sakolb * No end_addr specified by -A, the upper part of the interval 15892685Sakolb * does not change. 15902685Sakolb */ 15912685Sakolb *new_end = end; 15922685Sakolb } else { 15932685Sakolb /* 15942685Sakolb * If only one address is specified and it is the beginning of a 15952685Sakolb * segment, get information about the whole segment. This 15962685Sakolb * function is called once per segment and the 'end' argument is 15972685Sakolb * always the end of a segment, so just use the 'end' value. 15982685Sakolb */ 15992685Sakolb to = (end_addr == start_addr && start == start_addr) ? 16002685Sakolb end : 16012685Sakolb P2ALIGN(end_addr + psz, psz); 16022685Sakolb /* 16032685Sakolb * If start address is outside the range, return an empty 16042685Sakolb * interval 16052685Sakolb */ 16062685Sakolb if (start > to) { 16072685Sakolb *new_start = *new_end = 0; 16082685Sakolb return (0); 16092685Sakolb } 16102685Sakolb /* 16112685Sakolb * The adjusted end address is the minimum of requested end 16122685Sakolb * and the aligned end_addr of the -A range. 16132685Sakolb */ 16142685Sakolb *new_end = end > to ? to : end; 16152685Sakolb } 16162685Sakolb 16172685Sakolb /* 16182685Sakolb * Make sure that the resulting interval is legal. 16192685Sakolb */ 16202685Sakolb if (*new_end < *new_start) 16212685Sakolb *new_start = *new_end = 0; 16222685Sakolb 16232685Sakolb /* Return the size of the interval */ 16242685Sakolb return (*new_end - *new_start); 16252685Sakolb } 16262685Sakolb 16272685Sakolb /* 16282685Sakolb * Initialize memory_info data structure with information about a new segment. 16292685Sakolb */ 16302685Sakolb static void 16312685Sakolb mem_chunk_init(memory_chunk_t *chunk, uintptr_t end, size_t psz) 16322685Sakolb { 16332685Sakolb chunk->end_addr = end; 16342685Sakolb chunk->page_size = psz; 16352685Sakolb chunk->page_index = 0; 16362685Sakolb chunk->chunk_start = chunk->chunk_end = 0; 16372685Sakolb } 16382685Sakolb 16392685Sakolb /* 16402685Sakolb * Create a new chunk of addresses starting from vaddr. 16412685Sakolb * Pass the whole chunk to pr_meminfo to collect lgroup and page size 16422685Sakolb * information for each page in the chunk. 16432685Sakolb */ 16442685Sakolb static void 16452685Sakolb mem_chunk_get(memory_chunk_t *chunk, uintptr_t vaddr) 16462685Sakolb { 16472685Sakolb page_descr_t *pdp = chunk->page_info; 16482685Sakolb size_t psz = chunk->page_size; 16492685Sakolb uintptr_t addr = vaddr; 16502685Sakolb uint64_t inaddr[MAX_MEMINFO_CNT]; 16512685Sakolb uint64_t outdata[2 * MAX_MEMINFO_CNT]; 16522685Sakolb uint_t info[2] = { MEMINFO_VLGRP, MEMINFO_VPAGESIZE }; 16532685Sakolb uint_t validity[MAX_MEMINFO_CNT]; 16542685Sakolb uint64_t *dataptr = inaddr; 16552685Sakolb uint64_t *outptr = outdata; 16562685Sakolb uint_t *valptr = validity; 16572685Sakolb int i, j, rc; 16582685Sakolb 16592685Sakolb chunk->chunk_start = vaddr; 16602685Sakolb chunk->page_index = 0; /* reset index for the new chunk */ 16612685Sakolb 16622685Sakolb /* 16632685Sakolb * Fill in MAX_MEMINFO_CNT wotrh of pages starting from vaddr. Also, 16642685Sakolb * copy starting address of each page to inaddr array for pr_meminfo. 16652685Sakolb */ 16662685Sakolb for (i = 0, pdp = chunk->page_info; 16672685Sakolb (i < MAX_MEMINFO_CNT) && (addr <= chunk->end_addr); 16682685Sakolb i++, pdp++, dataptr++, addr += psz) { 16692685Sakolb *dataptr = (uint64_t)addr; 16702685Sakolb pdp->pd_start = addr; 16712685Sakolb pdp->pd_lgrp = LGRP_NONE; 16722685Sakolb pdp->pd_valid = 0; 16732685Sakolb pdp->pd_pagesize = 0; 16742685Sakolb } 16752685Sakolb 16762685Sakolb /* Mark the number of entries in the chunk and the last address */ 16772685Sakolb chunk->page_count = i; 16782685Sakolb chunk->chunk_end = addr - psz; 16792685Sakolb 16802685Sakolb if (interrupt) 16812685Sakolb return; 16822685Sakolb 16832685Sakolb /* Call meminfo for all collected addresses */ 16842685Sakolb rc = pr_meminfo(Pr, inaddr, i, info, 2, outdata, validity); 16852685Sakolb if (rc < 0) { 16862685Sakolb (void) perr("can not get memory information"); 16872685Sakolb return; 16882685Sakolb } 16892685Sakolb 16902685Sakolb /* Verify validity of each result and fill in the addrs array */ 16912685Sakolb pdp = chunk->page_info; 16922685Sakolb for (j = 0; j < i; j++, pdp++, valptr++, outptr += 2) { 16932685Sakolb /* Skip invalid address pointers */ 16942685Sakolb if ((*valptr & 1) == 0) { 16952685Sakolb continue; 16962685Sakolb } 16972685Sakolb 16982685Sakolb /* Is lgroup information available? */ 16992685Sakolb if ((*valptr & 2) != 0) { 17002685Sakolb pdp->pd_lgrp = (lgrp_id_t)*outptr; 17012685Sakolb pdp->pd_valid = 1; 17022685Sakolb } 17032685Sakolb 17042685Sakolb /* Is page size informaion available? */ 17052685Sakolb if ((*valptr & 4) != 0) { 17062685Sakolb pdp->pd_pagesize = *(outptr + 1); 17072685Sakolb } 17082685Sakolb } 17092685Sakolb } 17102685Sakolb 17112685Sakolb /* 17122685Sakolb * Starting from address 'vaddr' find the region with pages allocated from the 17132685Sakolb * same lgroup. 17142685Sakolb * 17152685Sakolb * Arguments: 17162685Sakolb * mchunk Initialized memory chunk structure 17172685Sakolb * vaddr Starting address of the region 17182685Sakolb * maxaddr Upper bound of the region 17192685Sakolb * pagesize Default page size to use 17202685Sakolb * ret_lgrp On exit contains the lgroup ID of all pages in the 17212685Sakolb * region. 17222685Sakolb * 17232685Sakolb * Returns: 17242685Sakolb * Size of the contiguous region in bytes 17252685Sakolb * The lgroup ID of all pages in the region in ret_lgrp argument. 17262685Sakolb */ 17272685Sakolb static size_t 17282685Sakolb get_contiguous_region(memory_chunk_t *mchunk, uintptr_t vaddr, 17292685Sakolb uintptr_t maxaddr, size_t pagesize, lgrp_id_t *ret_lgrp) 17302685Sakolb { 17312685Sakolb size_t size_contig = 0; 17322685Sakolb lgrp_id_t lgrp; /* Lgroup of the region start */ 17332685Sakolb lgrp_id_t curr_lgrp; /* Lgroup of the current page */ 17342685Sakolb size_t psz = pagesize; /* Pagesize to use */ 17352685Sakolb 17362685Sakolb /* Set both lgroup IDs to the lgroup of the first page */ 17372685Sakolb curr_lgrp = lgrp = addr_to_lgrp(mchunk, vaddr, &psz); 17382685Sakolb 17392685Sakolb /* 17402685Sakolb * Starting from vaddr, walk page by page until either the end 17412685Sakolb * of the segment is reached or a page is allocated from a different 17422685Sakolb * lgroup. Also stop if interrupted from keyboard. 17432685Sakolb */ 17442685Sakolb while ((vaddr < maxaddr) && (curr_lgrp == lgrp) && !interrupt) { 17452685Sakolb /* 17462685Sakolb * Get lgroup ID and the page size of the current page. 17472685Sakolb */ 17482685Sakolb curr_lgrp = addr_to_lgrp(mchunk, vaddr, &psz); 17492685Sakolb /* If there is no page size information, use the default */ 17502685Sakolb if (psz == 0) 17512685Sakolb psz = pagesize; 17522685Sakolb 17532685Sakolb if (curr_lgrp == lgrp) { 17542685Sakolb /* 17552685Sakolb * This page belongs to the contiguous region. 17562685Sakolb * Increase the region size and advance to the new page. 17572685Sakolb */ 17582685Sakolb size_contig += psz; 17592685Sakolb vaddr += psz; 17602685Sakolb } 17612685Sakolb } 17622685Sakolb 17632685Sakolb /* Return the region lgroup ID and the size */ 17642685Sakolb *ret_lgrp = lgrp; 17652685Sakolb return (size_contig); 17662685Sakolb } 17672685Sakolb 17682685Sakolb /* 17692685Sakolb * Given a virtual address, return its lgroup and page size. If there is meminfo 17702685Sakolb * information for an address, use it, otherwise shift the chunk window to the 17712685Sakolb * vaddr and create a new chunk with known meminfo information. 17722685Sakolb */ 17732685Sakolb static lgrp_id_t 17742685Sakolb addr_to_lgrp(memory_chunk_t *chunk, uintptr_t vaddr, size_t *psz) 17752685Sakolb { 17762685Sakolb page_descr_t *pdp; 17772685Sakolb lgrp_id_t lgrp = LGRP_NONE; 17782685Sakolb int i; 17792685Sakolb 17802685Sakolb *psz = chunk->page_size; 17812685Sakolb 17822685Sakolb if (interrupt) 17832685Sakolb return (0); 17842685Sakolb 17852685Sakolb /* 17862685Sakolb * Is there information about this address? If not, create a new chunk 17872685Sakolb * starting from vaddr and apply pr_meminfo() to the whole chunk. 17882685Sakolb */ 17892685Sakolb if (vaddr < chunk->chunk_start || vaddr > chunk->chunk_end) { 17902685Sakolb /* 17912685Sakolb * This address is outside the chunk, get the new chunk and 17922685Sakolb * collect meminfo information for it. 17932685Sakolb */ 17942685Sakolb mem_chunk_get(chunk, vaddr); 17952685Sakolb } 17962685Sakolb 17972685Sakolb /* 17982685Sakolb * Find information about the address. 17992685Sakolb */ 18002685Sakolb pdp = &chunk->page_info[chunk->page_index]; 18012685Sakolb for (i = chunk->page_index; i < chunk->page_count; i++, pdp++) { 18022685Sakolb if (pdp->pd_start == vaddr) { 18032685Sakolb if (pdp->pd_valid) { 18042685Sakolb lgrp = pdp->pd_lgrp; 18052685Sakolb /* 18062685Sakolb * Override page size information if it is 18072685Sakolb * present. 18082685Sakolb */ 18092685Sakolb if (pdp->pd_pagesize > 0) 18102685Sakolb *psz = pdp->pd_pagesize; 18112685Sakolb } 18122685Sakolb break; 18132685Sakolb } 18142685Sakolb } 18152685Sakolb /* 18162685Sakolb * Remember where we ended - the next search will start here. 18172685Sakolb * We can query for the lgrp for the same address again, so do not 18182685Sakolb * advance index past the current value. 18192685Sakolb */ 18202685Sakolb chunk->page_index = i; 18212685Sakolb 18222685Sakolb return (lgrp); 18232685Sakolb } 18242685Sakolb 18252685Sakolb /* ARGSUSED */ 18262685Sakolb static void 18272685Sakolb intr(int sig) 18282685Sakolb { 18292685Sakolb interrupt = 1; 18302685Sakolb } 1831