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
5*1580Sjwadams * Common Development and Distribution License (the "License").
6*1580Sjwadams * 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 */
210Sstevel@tonic-gate /*
22*1580Sjwadams * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Generic memory walker, used by both the genunix and libumem dmods.
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include "kgrep.h"
360Sstevel@tonic-gate
370Sstevel@tonic-gate #define KGREP_FULL_MASK (~(uintmax_t)0)
380Sstevel@tonic-gate
390Sstevel@tonic-gate typedef struct kgrep_data {
400Sstevel@tonic-gate uintmax_t kg_pattern;
410Sstevel@tonic-gate uintmax_t kg_mask; /* fancy only */
420Sstevel@tonic-gate uintmax_t kg_dist; /* fancy only */
430Sstevel@tonic-gate uintptr_t kg_minaddr; /* fancy only */
440Sstevel@tonic-gate uintptr_t kg_maxaddr; /* fancy only */
450Sstevel@tonic-gate void *kg_page;
460Sstevel@tonic-gate size_t kg_pagesize;
470Sstevel@tonic-gate char kg_cbtype;
480Sstevel@tonic-gate char kg_seen;
490Sstevel@tonic-gate } kgrep_data_t;
500Sstevel@tonic-gate
510Sstevel@tonic-gate #define KG_BASE 0
520Sstevel@tonic-gate #define KG_VERBOSE 1
530Sstevel@tonic-gate #define KG_PIPE 2
540Sstevel@tonic-gate
550Sstevel@tonic-gate static void
kgrep_cb(uintptr_t addr,uintmax_t * val,int type)560Sstevel@tonic-gate kgrep_cb(uintptr_t addr, uintmax_t *val, int type)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate switch (type) {
590Sstevel@tonic-gate case KG_BASE:
600Sstevel@tonic-gate default:
610Sstevel@tonic-gate mdb_printf("%p\n", addr);
620Sstevel@tonic-gate break;
630Sstevel@tonic-gate case KG_VERBOSE:
640Sstevel@tonic-gate mdb_printf("%p:\t%llx\n", addr, *val);
650Sstevel@tonic-gate break;
660Sstevel@tonic-gate case KG_PIPE:
670Sstevel@tonic-gate mdb_printf("%#lr\n", addr);
680Sstevel@tonic-gate break;
690Sstevel@tonic-gate }
700Sstevel@tonic-gate }
710Sstevel@tonic-gate
720Sstevel@tonic-gate static int
kgrep_range_basic(uintptr_t base,uintptr_t lim,void * kg_arg)730Sstevel@tonic-gate kgrep_range_basic(uintptr_t base, uintptr_t lim, void *kg_arg)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate kgrep_data_t *kg = kg_arg;
760Sstevel@tonic-gate size_t pagesize = kg->kg_pagesize;
770Sstevel@tonic-gate uintptr_t pattern = kg->kg_pattern;
780Sstevel@tonic-gate uintptr_t *page = kg->kg_page;
790Sstevel@tonic-gate uintptr_t *page_end = &page[pagesize / sizeof (uintptr_t)];
800Sstevel@tonic-gate uintptr_t *pos;
810Sstevel@tonic-gate
820Sstevel@tonic-gate uintptr_t addr, offset;
830Sstevel@tonic-gate int seen = 0;
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * page-align everything, to simplify the loop
870Sstevel@tonic-gate */
880Sstevel@tonic-gate base = P2ALIGN(base, pagesize);
890Sstevel@tonic-gate lim = P2ROUNDUP(lim, pagesize);
900Sstevel@tonic-gate
910Sstevel@tonic-gate for (addr = base; addr < lim; addr += pagesize) {
920Sstevel@tonic-gate if (mdb_vread(page, pagesize, addr) == -1)
930Sstevel@tonic-gate continue;
940Sstevel@tonic-gate seen = 1;
950Sstevel@tonic-gate
960Sstevel@tonic-gate for (pos = page; pos < page_end; pos++) {
970Sstevel@tonic-gate if (*pos != pattern)
980Sstevel@tonic-gate continue;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate offset = (caddr_t)pos - (caddr_t)page;
1010Sstevel@tonic-gate kgrep_cb(addr + offset, NULL, kg->kg_cbtype);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate if (seen)
1050Sstevel@tonic-gate kg->kg_seen = 1;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate return (WALK_NEXT);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate * Full-service template -- instantiated for each supported size. We support
1120Sstevel@tonic-gate * the following options:
1130Sstevel@tonic-gate *
1140Sstevel@tonic-gate * addr in [minaddr, maxaddr), and
1150Sstevel@tonic-gate * value in [pattern, pattern + dist) OR
1160Sstevel@tonic-gate * mask matching: (value & mask) == (pattern & mask)
1170Sstevel@tonic-gate */
1180Sstevel@tonic-gate #define KGREP_FANCY_TEMPLATE(kgrep_range_fancybits, uintbits_t) \
1190Sstevel@tonic-gate static int \
1200Sstevel@tonic-gate kgrep_range_fancybits(uintptr_t base, uintptr_t lim, void *kg_arg) \
1210Sstevel@tonic-gate { \
1220Sstevel@tonic-gate kgrep_data_t *kg = kg_arg; \
1230Sstevel@tonic-gate \
1240Sstevel@tonic-gate uintbits_t pattern = kg->kg_pattern; \
1250Sstevel@tonic-gate uintbits_t dist = kg->kg_dist; \
1260Sstevel@tonic-gate uintbits_t mask = kg->kg_mask; \
1270Sstevel@tonic-gate uintptr_t minaddr = kg->kg_minaddr; \
1280Sstevel@tonic-gate uintptr_t maxaddr = kg->kg_maxaddr; \
1290Sstevel@tonic-gate size_t pagesize = kg->kg_pagesize; \
1300Sstevel@tonic-gate uintbits_t *page = (uintbits_t *)kg->kg_page; \
1310Sstevel@tonic-gate uintbits_t *page_end; \
1320Sstevel@tonic-gate uintbits_t *pos; \
1330Sstevel@tonic-gate uintbits_t cur; \
1340Sstevel@tonic-gate uintmax_t out; \
1350Sstevel@tonic-gate \
1360Sstevel@tonic-gate uintptr_t addr, size, offset; \
1370Sstevel@tonic-gate int seen = 0; \
1380Sstevel@tonic-gate \
1390Sstevel@tonic-gate base = P2ROUNDUP(MAX(base, minaddr), sizeof (uintbits_t)); \
1400Sstevel@tonic-gate \
1410Sstevel@tonic-gate if (maxaddr != 0 && lim > maxaddr) \
1420Sstevel@tonic-gate lim = maxaddr; \
1430Sstevel@tonic-gate \
1440Sstevel@tonic-gate for (addr = base; addr < lim; addr += size) { \
1450Sstevel@tonic-gate /* P2END(...) computes the next page boundry */ \
1460Sstevel@tonic-gate size = MIN(lim, P2END(addr, pagesize)) - addr; \
1470Sstevel@tonic-gate \
1480Sstevel@tonic-gate if (mdb_vread(page, size, addr) == -1) \
1490Sstevel@tonic-gate continue; \
1500Sstevel@tonic-gate \
1510Sstevel@tonic-gate seen = 1; \
1520Sstevel@tonic-gate \
1530Sstevel@tonic-gate page_end = &page[size / sizeof (uintbits_t)]; \
1540Sstevel@tonic-gate for (pos = page; pos < page_end; pos++) { \
1550Sstevel@tonic-gate cur = *pos; \
1560Sstevel@tonic-gate \
1570Sstevel@tonic-gate if (((cur ^ pattern) & mask) != 0 && \
1580Sstevel@tonic-gate (cur - pattern) >= dist) \
1590Sstevel@tonic-gate continue; \
1600Sstevel@tonic-gate \
1610Sstevel@tonic-gate out = cur; \
1620Sstevel@tonic-gate offset = (caddr_t)pos - (caddr_t)page; \
1630Sstevel@tonic-gate kgrep_cb(addr + offset, &out, kg->kg_cbtype); \
1640Sstevel@tonic-gate } \
1650Sstevel@tonic-gate } \
1660Sstevel@tonic-gate if (seen) \
1670Sstevel@tonic-gate kg->kg_seen = 1; \
1680Sstevel@tonic-gate \
1690Sstevel@tonic-gate return (WALK_NEXT); \
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
KGREP_FANCY_TEMPLATE(kgrep_range_fancy8,uint8_t)1720Sstevel@tonic-gate KGREP_FANCY_TEMPLATE(kgrep_range_fancy8, uint8_t)
1730Sstevel@tonic-gate KGREP_FANCY_TEMPLATE(kgrep_range_fancy16, uint16_t)
1740Sstevel@tonic-gate KGREP_FANCY_TEMPLATE(kgrep_range_fancy32, uint32_t)
1750Sstevel@tonic-gate KGREP_FANCY_TEMPLATE(kgrep_range_fancy64, uint64_t)
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate #undef KGREP_FANCY_TEMPLATE
1780Sstevel@tonic-gate
179*1580Sjwadams void
180*1580Sjwadams kgrep_help(void)
181*1580Sjwadams {
182*1580Sjwadams mdb_printf(
183*1580Sjwadams "\n"
184*1580Sjwadams "Search the entire virtual address space for a particular pattern,\n"
185*1580Sjwadams "%<u>addr%</u>. By default, a pointer-sized search for an exact match is\n"
186*1580Sjwadams "done.\n\n");
187*1580Sjwadams mdb_dec_indent(2);
188*1580Sjwadams mdb_printf("%<b>OPTIONS%</b>\n");
189*1580Sjwadams mdb_inc_indent(2);
190*1580Sjwadams mdb_printf(
191*1580Sjwadams " -v Report the value matched at each address\n"
192*1580Sjwadams " -a minaddr\n"
193*1580Sjwadams " Restrict the search to addresses >= minaddr\n"
194*1580Sjwadams " -A maxaddr\n"
195*1580Sjwadams " Restrict the search to addresses < maxaddr\n"
196*1580Sjwadams " -d dist\n"
197*1580Sjwadams " Search for values in [addr, addr + dist)\n"
198*1580Sjwadams " -m mask\n"
199*1580Sjwadams " Search for values where (value & mask) == addr\n"
200*1580Sjwadams " -M invmask\n"
201*1580Sjwadams " Search for values where (value & ~invmask) == addr\n"
202*1580Sjwadams " -s size\n"
203*1580Sjwadams " Instead of pointer-sized values, search for size-byte values.\n"
204*1580Sjwadams " size must be 1, 2, 4, or 8.\n");
205*1580Sjwadams }
206*1580Sjwadams
2070Sstevel@tonic-gate /*ARGSUSED*/
2080Sstevel@tonic-gate int
kgrep(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2090Sstevel@tonic-gate kgrep(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate uintmax_t pattern = mdb_get_dot();
2120Sstevel@tonic-gate uintmax_t mask = KGREP_FULL_MASK;
2130Sstevel@tonic-gate uintmax_t invmask = 0;
2140Sstevel@tonic-gate uintmax_t dist = 0;
2150Sstevel@tonic-gate uintptr_t size = sizeof (uintptr_t);
2160Sstevel@tonic-gate uintptr_t minaddr = 0;
2170Sstevel@tonic-gate uintptr_t maxaddr = 0;
2180Sstevel@tonic-gate size_t pagesize = kgrep_subr_pagesize();
2190Sstevel@tonic-gate int verbose = 0;
2200Sstevel@tonic-gate int ret;
2210Sstevel@tonic-gate int args = 0;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate kgrep_cb_func *func;
2240Sstevel@tonic-gate kgrep_data_t kg;
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate uintmax_t size_mask;
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate if (mdb_getopts(argc, argv,
2290Sstevel@tonic-gate 'a', MDB_OPT_UINTPTR, &minaddr,
2300Sstevel@tonic-gate 'A', MDB_OPT_UINTPTR, &maxaddr,
2310Sstevel@tonic-gate 'd', MDB_OPT_UINT64, &dist,
2320Sstevel@tonic-gate 'm', MDB_OPT_UINT64, &mask,
2330Sstevel@tonic-gate 'M', MDB_OPT_UINT64, &invmask,
2340Sstevel@tonic-gate 's', MDB_OPT_UINTPTR, &size,
2350Sstevel@tonic-gate 'v', MDB_OPT_SETBITS, B_TRUE, &verbose, NULL) != argc)
2360Sstevel@tonic-gate return (DCMD_USAGE);
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate if (invmask != 0)
2390Sstevel@tonic-gate args++;
2400Sstevel@tonic-gate if (mask != KGREP_FULL_MASK)
2410Sstevel@tonic-gate args++;
2420Sstevel@tonic-gate if (dist != 0)
2430Sstevel@tonic-gate args++;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if (args > 1) {
2460Sstevel@tonic-gate mdb_warn("only one of -d, -m and -M may be specified\n");
2470Sstevel@tonic-gate return (DCMD_USAGE);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC))
2510Sstevel@tonic-gate return (DCMD_USAGE);
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate if (invmask != 0)
2540Sstevel@tonic-gate mask = ~invmask;
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate if (pattern & ~mask)
2570Sstevel@tonic-gate mdb_warn("warning: pattern does not match mask\n");
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate if (size > sizeof (uintmax_t)) {
2600Sstevel@tonic-gate mdb_warn("sizes greater than %d not supported\n",
2610Sstevel@tonic-gate sizeof (uintmax_t));
2620Sstevel@tonic-gate return (DCMD_ERR);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate if (size == 0 || (size & (size - 1)) != 0) {
2660Sstevel@tonic-gate mdb_warn("size must be a power of 2\n");
2670Sstevel@tonic-gate return (DCMD_ERR);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate if (size == sizeof (uintmax_t))
2710Sstevel@tonic-gate size_mask = KGREP_FULL_MASK;
2720Sstevel@tonic-gate else
2730Sstevel@tonic-gate size_mask = (1ULL << (size * NBBY)) - 1ULL;
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate if (pattern & ~size_mask)
2760Sstevel@tonic-gate mdb_warn("warning: pattern %llx overflows requested size "
2770Sstevel@tonic-gate "%d (max: %llx)\n",
2780Sstevel@tonic-gate pattern, size, size_mask);
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate if (dist > 0 &&
2810Sstevel@tonic-gate ((dist & ~size_mask) || size_mask + 1 - dist < pattern)) {
2820Sstevel@tonic-gate mdb_warn("pattern %llx + distance %llx overflows size\n"
2830Sstevel@tonic-gate "%d (max: %llx)\n", pattern, dist, size, size_mask);
2840Sstevel@tonic-gate return (DCMD_ERR);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate * All arguments have now been validated.
2890Sstevel@tonic-gate */
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate (void) memset(&kg, '\0', sizeof (kg));
2920Sstevel@tonic-gate kg.kg_page = mdb_alloc(pagesize, UM_SLEEP | UM_GC);
2930Sstevel@tonic-gate kg.kg_pagesize = pagesize;
2940Sstevel@tonic-gate kg.kg_pattern = pattern;
2950Sstevel@tonic-gate kg.kg_mask = mask;
2960Sstevel@tonic-gate kg.kg_dist = dist;
2970Sstevel@tonic-gate kg.kg_minaddr = minaddr;
2980Sstevel@tonic-gate kg.kg_maxaddr = maxaddr;
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate if (flags & DCMD_PIPE_OUT) {
3010Sstevel@tonic-gate verbose = 0;
3020Sstevel@tonic-gate kg.kg_cbtype = KG_PIPE;
3030Sstevel@tonic-gate } else if (verbose) {
3040Sstevel@tonic-gate kg.kg_cbtype = KG_VERBOSE;
3050Sstevel@tonic-gate } else {
3060Sstevel@tonic-gate kg.kg_cbtype = KG_BASE;
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * kgrep_range_basic handles the common case (no arguments)
3110Sstevel@tonic-gate * with dispatch.
3120Sstevel@tonic-gate */
3130Sstevel@tonic-gate if (size == sizeof (uintptr_t) && !verbose && mask == KGREP_FULL_MASK &&
3140Sstevel@tonic-gate dist == 0 && minaddr == 0 && maxaddr == 0)
3150Sstevel@tonic-gate func = kgrep_range_basic;
3160Sstevel@tonic-gate else {
3170Sstevel@tonic-gate switch (size) {
3180Sstevel@tonic-gate case 1:
3190Sstevel@tonic-gate func = kgrep_range_fancy8;
3200Sstevel@tonic-gate break;
3210Sstevel@tonic-gate case 2:
3220Sstevel@tonic-gate func = kgrep_range_fancy16;
3230Sstevel@tonic-gate break;
3240Sstevel@tonic-gate case 4:
3250Sstevel@tonic-gate func = kgrep_range_fancy32;
3260Sstevel@tonic-gate break;
3270Sstevel@tonic-gate case 8:
3280Sstevel@tonic-gate func = kgrep_range_fancy64;
3290Sstevel@tonic-gate break;
3300Sstevel@tonic-gate default:
3310Sstevel@tonic-gate mdb_warn("can't happen: non-recognized kgrep size\n");
3320Sstevel@tonic-gate return (DCMD_ERR);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate * Invoke the target, which should invoke func(start, end, &kg) for
3380Sstevel@tonic-gate * every range [start, end) of vaddrs which might have backing.
3390Sstevel@tonic-gate * Both start and end must be multiples of kgrep_subr_pagesize().
3400Sstevel@tonic-gate */
3410Sstevel@tonic-gate ret = kgrep_subr(func, &kg);
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate if (ret == DCMD_OK && !kg.kg_seen)
3440Sstevel@tonic-gate mdb_warn("warning: nothing searched\n");
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate return (ret);
3470Sstevel@tonic-gate }
348