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