xref: /onnv-gate/usr/src/cmd/mdb/common/modules/ipc/ipc.c (revision 2565:4ca8919fcdc0)
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*2565Sudpa  * Common Development and Distribution License (the "License").
6*2565Sudpa  * 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*2565Sudpa  * 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 #include <mdb/mdb_modapi.h>
290Sstevel@tonic-gate #include <mdb/mdb_ks.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/mman.h>
330Sstevel@tonic-gate #include <sys/project.h>
340Sstevel@tonic-gate #include <sys/ipc_impl.h>
350Sstevel@tonic-gate #include <sys/shm_impl.h>
360Sstevel@tonic-gate #include <sys/sem_impl.h>
370Sstevel@tonic-gate #include <sys/msg_impl.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <vm/anon.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define	CMN_HDR_START	"%<u>"
420Sstevel@tonic-gate #define	CMN_HDR_END	"%</u>\n"
430Sstevel@tonic-gate #define	CMN_INDENT	(4)
440Sstevel@tonic-gate #define	CMN_INACTIVE	"%s facility inactive.\n"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * Bitmap data for page protection flags suitable for use with %b.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate const mdb_bitmask_t prot_flag_bits[] = {
500Sstevel@tonic-gate 	{ "PROT_READ", PROT_READ, PROT_READ },
510Sstevel@tonic-gate 	{ "PROT_WRITE", PROT_WRITE, PROT_WRITE },
520Sstevel@tonic-gate 	{ "PROT_EXEC", PROT_EXEC, PROT_EXEC },
530Sstevel@tonic-gate 	{ "PROT_USER", PROT_USER, PROT_USER },
540Sstevel@tonic-gate 	{ NULL, 0, 0 }
550Sstevel@tonic-gate };
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static void
580Sstevel@tonic-gate printtime_nice(const char *str, time_t time)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	if (time)
610Sstevel@tonic-gate 		mdb_printf("%s%Y\n", str, time);
620Sstevel@tonic-gate 	else
630Sstevel@tonic-gate 		mdb_printf("%sn/a\n", str);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Print header common to all IPC types.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate static void
700Sstevel@tonic-gate ipcperm_header()
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	mdb_printf(CMN_HDR_START "%?s %5s %5s %8s %5s %5s %6s %5s %5s %5s %5s"
730Sstevel@tonic-gate 	    CMN_HDR_END, "ADDR", "REF", "ID", "KEY", "MODE", "PRJID", "ZONEID",
740Sstevel@tonic-gate 	    "OWNER", "GROUP", "CREAT", "CGRP");
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Print data common to all IPC types.
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate static void
810Sstevel@tonic-gate ipcperm_print(uintptr_t addr, kipc_perm_t *perm)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	kproject_t proj;
840Sstevel@tonic-gate 	int res;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	res = mdb_vread(&proj, sizeof (kproject_t), (uintptr_t)perm->ipc_proj);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (res == -1)
890Sstevel@tonic-gate 		mdb_warn("failed to read kproject_t at %#p", perm->ipc_proj);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	mdb_printf("%0?p %5d %5d", addr, perm->ipc_ref, perm->ipc_id);
920Sstevel@tonic-gate 	if (perm->ipc_key)
930Sstevel@tonic-gate 		mdb_printf(" %8x", perm->ipc_key);
940Sstevel@tonic-gate 	else
950Sstevel@tonic-gate 		mdb_printf(" %8s", "private");
960Sstevel@tonic-gate 	mdb_printf(" %5#o", perm->ipc_mode & 07777);
970Sstevel@tonic-gate 	if (res == -1)
980Sstevel@tonic-gate 		mdb_printf(" %5s %5s", "<flt>", "<flt>");
990Sstevel@tonic-gate 	else
1000Sstevel@tonic-gate 		mdb_printf(" %5d %6d", proj.kpj_id, proj.kpj_zoneid);
1010Sstevel@tonic-gate 	mdb_printf(" %5d %5d %5d %5d\n", perm->ipc_uid, perm->ipc_gid,
1020Sstevel@tonic-gate 	    perm->ipc_cuid, perm->ipc_cgid);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*ARGSUSED*/
1070Sstevel@tonic-gate static int
1080Sstevel@tonic-gate ipcperm(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	kipc_perm_t perm;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
1130Sstevel@tonic-gate 		return (DCMD_USAGE);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
1160Sstevel@tonic-gate 		ipcperm_header();
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (mdb_vread(&perm, sizeof (kipc_perm_t), addr) == -1) {
1190Sstevel@tonic-gate 		mdb_warn("failed to read kipc_perm_t at %#lx", addr);
1200Sstevel@tonic-gate 		return (DCMD_ERR);
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	ipcperm_print(addr, &perm);
1240Sstevel@tonic-gate 	return (DCMD_OK);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static void
1280Sstevel@tonic-gate msq_print(kmsqid_t *msqid, uintptr_t addr)
1290Sstevel@tonic-gate {
130*2565Sudpa 	int	ii;
131*2565Sudpa 
1320Sstevel@tonic-gate 	mdb_printf("&list: %-?p\n", addr + OFFSETOF(kmsqid_t, msg_list));
1330Sstevel@tonic-gate 	mdb_printf("cbytes: 0t%lu    qnum: 0t%lu    qbytes: 0t%lu"
1340Sstevel@tonic-gate 	    "    qmax: 0t%lu\n", msqid->msg_cbytes, msqid->msg_qnum,
1350Sstevel@tonic-gate 	    msqid->msg_qbytes, msqid->msg_qmax);
1360Sstevel@tonic-gate 	mdb_printf("lspid: 0t%d    lrpid: 0t%d\n",
1370Sstevel@tonic-gate 	    (int)msqid->msg_lspid, (int)msqid->msg_lrpid);
1380Sstevel@tonic-gate 	printtime_nice("stime: ", msqid->msg_stime);
1390Sstevel@tonic-gate 	printtime_nice("rtime: ", msqid->msg_rtime);
1400Sstevel@tonic-gate 	printtime_nice("ctime: ", msqid->msg_ctime);
141*2565Sudpa 	mdb_printf("snd_cnt: 0t%lld    snd_cv: %hd (%p)\n",
142*2565Sudpa 	    msqid->msg_snd_cnt, msqid->msg_snd_cv._opaque,
143*2565Sudpa 	    addr + (uintptr_t)OFFSETOF(kmsqid_t, msg_snd_cv));
144*2565Sudpa 
145*2565Sudpa 	mdb_printf("#    rcv_cnt:     rcv_cv:\n");
146*2565Sudpa 	for (ii = 0; ii < MAX_QNUM_CV; ii++) {
147*2565Sudpa 		if (msqid->msg_rcv_cnt[ii] || msqid->msg_rcv_cv[ii]._opaque) {
148*2565Sudpa 			mdb_printf("%2d    0t%lld          %hd  (%p)\n", ii,
149*2565Sudpa 			    msqid->msg_rcv_cnt[ii],
150*2565Sudpa 			    msqid->msg_rcv_cv[ii]._opaque, addr +
151*2565Sudpa 			    (uintptr_t)OFFSETOF(kmsqid_t, msg_rcv_cv[ii]));
152*2565Sudpa 		}
153*2565Sudpa 	}
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*ARGSUSED1*/
1580Sstevel@tonic-gate static void
1590Sstevel@tonic-gate shm_print(kshmid_t *shmid, uintptr_t addr)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	shmatt_t nattch;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	nattch = shmid->shm_perm.ipc_ref - (IPC_FREE(&shmid->shm_perm) ? 0 : 1);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	mdb_printf(CMN_HDR_START "%10s %?s %5s %7s %7s %7s %7s" CMN_HDR_END,
1660Sstevel@tonic-gate 	    "SEGSZ", "AMP", "LKCNT", "LPID", "CPID", "NATTCH", "CNATTCH");
1670Sstevel@tonic-gate 	mdb_printf("%10#lx %?p %5u %7d %7d %7lu %7lu\n",
1680Sstevel@tonic-gate 	    shmid->shm_segsz, shmid->shm_amp, shmid->shm_lkcnt,
1690Sstevel@tonic-gate 	    (int)shmid->shm_lpid, (int)shmid->shm_cpid, nattch,
1700Sstevel@tonic-gate 	    shmid->shm_ismattch);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	printtime_nice("atime: ", shmid->shm_atime);
1730Sstevel@tonic-gate 	printtime_nice("dtime: ", shmid->shm_dtime);
1740Sstevel@tonic-gate 	printtime_nice("ctime: ", shmid->shm_ctime);
1750Sstevel@tonic-gate 	mdb_printf("sptinfo: %-?p    sptseg: %-?p\n",
1760Sstevel@tonic-gate 	    shmid->shm_sptinfo, shmid->shm_sptseg);
1770Sstevel@tonic-gate 	mdb_printf("sptprot: <%lb>\n", shmid->shm_sptprot, prot_flag_bits);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /*ARGSUSED1*/
1820Sstevel@tonic-gate static void
1830Sstevel@tonic-gate sem_print(ksemid_t *semid, uintptr_t addr)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	mdb_printf("base: %-?p    nsems: 0t%u\n",
1860Sstevel@tonic-gate 	    semid->sem_base, semid->sem_nsems);
1870Sstevel@tonic-gate 	printtime_nice("otime: ", semid->sem_otime);
1880Sstevel@tonic-gate 	printtime_nice("ctime: ", semid->sem_ctime);
1890Sstevel@tonic-gate 	mdb_printf("binary: %s\n", semid->sem_binary ? "yes" : "no");
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate typedef struct ipc_ops_vec {
1930Sstevel@tonic-gate 	char	*iv_wcmd;	/* walker name		*/
1940Sstevel@tonic-gate 	char	*iv_ocmd;	/* output dcmd		*/
1950Sstevel@tonic-gate 	char	*iv_service;	/* service pointer	*/
1960Sstevel@tonic-gate 	void	(*iv_print)(void *, uintptr_t); /* output callback */
1970Sstevel@tonic-gate 	size_t	iv_idsize;
1980Sstevel@tonic-gate } ipc_ops_vec_t;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate ipc_ops_vec_t msq_ops_vec = {
2010Sstevel@tonic-gate 	"msq",
2020Sstevel@tonic-gate 	"kmsqid",
2030Sstevel@tonic-gate 	"msq_svc",
2040Sstevel@tonic-gate 	(void(*)(void *, uintptr_t))msq_print,
2050Sstevel@tonic-gate 	sizeof (kmsqid_t)
2060Sstevel@tonic-gate };
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate ipc_ops_vec_t shm_ops_vec = {
2090Sstevel@tonic-gate 	"shm",
2100Sstevel@tonic-gate 	"kshmid",
2110Sstevel@tonic-gate 	"shm_svc",
2120Sstevel@tonic-gate 	(void(*)(void *, uintptr_t))shm_print,
2130Sstevel@tonic-gate 	sizeof (kshmid_t)
2140Sstevel@tonic-gate };
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate ipc_ops_vec_t sem_ops_vec = {
2170Sstevel@tonic-gate 	"sem",
2180Sstevel@tonic-gate 	"ksemid",
2190Sstevel@tonic-gate 	"sem_svc",
2200Sstevel@tonic-gate 	(void(*)(void *, uintptr_t))sem_print,
2210Sstevel@tonic-gate 	sizeof (ksemid_t)
2220Sstevel@tonic-gate };
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * Generic IPC data structure display code
2270Sstevel@tonic-gate  */
2280Sstevel@tonic-gate static int
2290Sstevel@tonic-gate ds_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
2300Sstevel@tonic-gate     ipc_ops_vec_t *iv)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	void *iddata;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
2350Sstevel@tonic-gate 		uint_t oflags = 0;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		if (mdb_getopts(argc, argv, 'l', MDB_OPT_SETBITS, 1, &oflags,
2380Sstevel@tonic-gate 		    NULL) != argc)
2390Sstevel@tonic-gate 			return (DCMD_USAGE);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 		if (mdb_walk_dcmd(iv->iv_wcmd, oflags ? iv->iv_ocmd : "ipcperm",
2420Sstevel@tonic-gate 		    argc, argv) == -1) {
2430Sstevel@tonic-gate 			mdb_warn("can't walk '%s'", iv->iv_wcmd);
2440Sstevel@tonic-gate 			return (DCMD_ERR);
2450Sstevel@tonic-gate 		}
2460Sstevel@tonic-gate 		return (DCMD_OK);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	iddata = mdb_alloc(iv->iv_idsize, UM_SLEEP | UM_GC);
2500Sstevel@tonic-gate 	if (mdb_vread(iddata, iv->iv_idsize, addr) == -1) {
2510Sstevel@tonic-gate 		mdb_warn("failed to read %s at %#lx", iv->iv_ocmd, addr);
2520Sstevel@tonic-gate 		return (DCMD_ERR);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if (!DCMD_HDRSPEC(flags) && iv->iv_print)
2560Sstevel@tonic-gate 		mdb_printf("\n");
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags) || iv->iv_print)
2590Sstevel@tonic-gate 		ipcperm_header();
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	ipcperm_print(addr, (struct kipc_perm *)iddata);
2620Sstevel@tonic-gate 	if (iv->iv_print) {
2630Sstevel@tonic-gate 		mdb_inc_indent(CMN_INDENT);
2640Sstevel@tonic-gate 		iv->iv_print(iddata, addr);
2650Sstevel@tonic-gate 		mdb_dec_indent(CMN_INDENT);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	return (DCMD_OK);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate  * Stubs to call ds_print with the appropriate ops vector
2740Sstevel@tonic-gate  */
2750Sstevel@tonic-gate static int
2760Sstevel@tonic-gate cmd_kshmid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate 	return (ds_print(addr, flags, argc, argv, &shm_ops_vec));
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate static int
2830Sstevel@tonic-gate cmd_kmsqid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	return (ds_print(addr, flags, argc, argv, &msq_ops_vec));
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate static int
2890Sstevel@tonic-gate cmd_ksemid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	return (ds_print(addr, flags, argc, argv, &sem_ops_vec));
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * Generic IPC walker
2960Sstevel@tonic-gate  */
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate static int
2990Sstevel@tonic-gate ds_walk_init(mdb_walk_state_t *wsp)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	ipc_ops_vec_t	*iv = wsp->walk_arg;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (wsp->walk_arg != NULL && wsp->walk_addr != NULL)
3040Sstevel@tonic-gate 		mdb_printf("ignoring provided address\n");
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if (wsp->walk_arg)
3070Sstevel@tonic-gate 		if (mdb_readvar(&wsp->walk_addr, iv->iv_service) == -1) {
3080Sstevel@tonic-gate 			mdb_printf("failed to read '%s'; module not present\n",
3090Sstevel@tonic-gate 			    iv->iv_service);
3100Sstevel@tonic-gate 			return (WALK_DONE);
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 	else
3130Sstevel@tonic-gate 		wsp->walk_addr = wsp->walk_addr +
3140Sstevel@tonic-gate 		    OFFSETOF(ipc_service_t, ipcs_usedids);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if (mdb_layered_walk("list", wsp) == -1)
3170Sstevel@tonic-gate 		return (WALK_ERR);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	return (WALK_NEXT);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate static int
3240Sstevel@tonic-gate ds_walk_step(mdb_walk_state_t *wsp)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
3270Sstevel@tonic-gate 	    wsp->walk_cbdata));
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate /*
3310Sstevel@tonic-gate  * Generic IPC ID/key to pointer code
3320Sstevel@tonic-gate  */
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate static int
3350Sstevel@tonic-gate ipcid_impl(uintptr_t svcptr, uintptr_t id, uintptr_t *addr)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate 	ipc_service_t service;
3380Sstevel@tonic-gate 	kipc_perm_t perm;
3390Sstevel@tonic-gate 	ipc_slot_t slot;
3400Sstevel@tonic-gate 	uintptr_t slotptr;
3410Sstevel@tonic-gate 	uint_t index;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (id > INT_MAX) {
3440Sstevel@tonic-gate 		mdb_warn("id out of range\n");
3450Sstevel@tonic-gate 		return (DCMD_ERR);
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (mdb_vread(&service, sizeof (ipc_service_t), svcptr) == -1) {
3490Sstevel@tonic-gate 		mdb_warn("failed to read ipc_service_t at %#lx", svcptr);
3500Sstevel@tonic-gate 		return (DCMD_ERR);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	index = (uint_t)id & (service.ipcs_tabsz - 1);
3540Sstevel@tonic-gate 	slotptr = (uintptr_t)(service.ipcs_table + index);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	if (mdb_vread(&slot, sizeof (ipc_slot_t), slotptr) == -1) {
3570Sstevel@tonic-gate 		mdb_warn("failed to read ipc_slot_t at %#lx", slotptr);
3580Sstevel@tonic-gate 		return (DCMD_ERR);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if (slot.ipct_data == NULL)
3620Sstevel@tonic-gate 		return (DCMD_ERR);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	if (mdb_vread(&perm, sizeof (kipc_perm_t),
3650Sstevel@tonic-gate 	    (uintptr_t)slot.ipct_data) == -1) {
3660Sstevel@tonic-gate 		mdb_warn("failed to read kipc_perm_t at %#p",
3670Sstevel@tonic-gate 		    slot.ipct_data);
3680Sstevel@tonic-gate 		return (DCMD_ERR);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	if (perm.ipc_id != (uint_t)id)
3720Sstevel@tonic-gate 		return (DCMD_ERR);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	*addr = (uintptr_t)slot.ipct_data;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	return (DCMD_OK);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate typedef struct findkey_data {
3810Sstevel@tonic-gate 	key_t fk_key;
3820Sstevel@tonic-gate 	uintptr_t fk_addr;
3830Sstevel@tonic-gate 	boolean_t fk_found;
3840Sstevel@tonic-gate } findkey_data_t;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate static int
3870Sstevel@tonic-gate findkey(uintptr_t addr, kipc_perm_t *perm, findkey_data_t *arg)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate 	if (perm->ipc_key == arg->fk_key) {
3900Sstevel@tonic-gate 		arg->fk_found = B_TRUE;
3910Sstevel@tonic-gate 		arg->fk_addr = addr;
3920Sstevel@tonic-gate 		return (WALK_DONE);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 	return (WALK_NEXT);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate static int
3980Sstevel@tonic-gate ipckey_impl(uintptr_t svcptr, uintptr_t key, uintptr_t *addr)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	ipc_service_t	service;
4010Sstevel@tonic-gate 	findkey_data_t	fkdata;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	if ((key == IPC_PRIVATE) || (key > INT_MAX)) {
4040Sstevel@tonic-gate 		mdb_warn("key out of range\n");
4050Sstevel@tonic-gate 		return (DCMD_ERR);
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if (mdb_vread(&service, sizeof (ipc_service_t), svcptr) == -1) {
4090Sstevel@tonic-gate 		mdb_warn("failed to read ipc_service_t at %#lx", svcptr);
4100Sstevel@tonic-gate 		return (DCMD_ERR);
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	fkdata.fk_key = (key_t)key;
4140Sstevel@tonic-gate 	fkdata.fk_found = B_FALSE;
4150Sstevel@tonic-gate 	if ((mdb_pwalk("avl", (mdb_walk_cb_t)findkey, &fkdata,
4160Sstevel@tonic-gate 	    svcptr + OFFSETOF(ipc_service_t, ipcs_keys)) == -1) ||
4170Sstevel@tonic-gate 	    !fkdata.fk_found)
4180Sstevel@tonic-gate 		return (DCMD_ERR);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	*addr = fkdata.fk_addr;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	return (DCMD_OK);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate static int
4260Sstevel@tonic-gate ipckeyid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
4270Sstevel@tonic-gate     int(*fp)(uintptr_t, uintptr_t, uintptr_t *))
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	uintmax_t val;
4300Sstevel@tonic-gate 	uintptr_t raddr;
4310Sstevel@tonic-gate 	int result;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || (argc != 1))
4340Sstevel@tonic-gate 		return (DCMD_USAGE);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (argv[0].a_type == MDB_TYPE_IMMEDIATE)
4370Sstevel@tonic-gate 		val = argv[0].a_un.a_val;
4380Sstevel@tonic-gate 	else if (argv[0].a_type == MDB_TYPE_STRING)
4390Sstevel@tonic-gate 		val = mdb_strtoull(argv[0].a_un.a_str);
4400Sstevel@tonic-gate 	else
4410Sstevel@tonic-gate 		return (DCMD_USAGE);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	result = fp(addr, val, &raddr);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	if (result == DCMD_OK)
4460Sstevel@tonic-gate 		mdb_printf("%lx", raddr);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	return (result);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate static int
4520Sstevel@tonic-gate ipckey(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 	return (ipckeyid(addr, flags, argc, argv, ipckey_impl));
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate static int
4580Sstevel@tonic-gate ipcid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	return (ipckeyid(addr, flags, argc, argv, ipcid_impl));
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate static int
4640Sstevel@tonic-gate ds_ptr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
4650Sstevel@tonic-gate     ipc_ops_vec_t *iv)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	uint_t		kflag = FALSE;
4680Sstevel@tonic-gate 	uintptr_t	svcptr, raddr;
4690Sstevel@tonic-gate 	int		result;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
4720Sstevel@tonic-gate 		return (DCMD_USAGE);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
4750Sstevel@tonic-gate 	    'k', MDB_OPT_SETBITS, TRUE, &kflag, NULL) != argc)
4760Sstevel@tonic-gate 		return (DCMD_USAGE);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	if (mdb_readvar(&svcptr, iv->iv_service) == -1) {
4790Sstevel@tonic-gate 		mdb_warn("failed to read '%s'; module not present\n",
4800Sstevel@tonic-gate 		    iv->iv_service);
4810Sstevel@tonic-gate 		return (DCMD_ERR);
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	result = kflag ? ipckey_impl(svcptr, addr, &raddr) :
4850Sstevel@tonic-gate 	    ipcid_impl(svcptr, addr, &raddr);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	if (result == DCMD_OK)
4880Sstevel@tonic-gate 		mdb_printf("%lx", raddr);
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	return (result);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate /*
4940Sstevel@tonic-gate  * Stubs to call ds_ptr with the appropriate ops vector
4950Sstevel@tonic-gate  */
4960Sstevel@tonic-gate static int
4970Sstevel@tonic-gate id2shm(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4980Sstevel@tonic-gate {
4990Sstevel@tonic-gate 	return (ds_ptr(addr, flags, argc, argv, &shm_ops_vec));
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate static int
5030Sstevel@tonic-gate id2msq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	return (ds_ptr(addr, flags, argc, argv, &msq_ops_vec));
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate static int
5090Sstevel@tonic-gate id2sem(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	return (ds_ptr(addr, flags, argc, argv, &sem_ops_vec));
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate  * The message queue contents walker
5170Sstevel@tonic-gate  */
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate static int
5200Sstevel@tonic-gate msg_walk_init(mdb_walk_state_t *wsp)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	wsp->walk_addr += OFFSETOF(kmsqid_t, msg_list);
5230Sstevel@tonic-gate 	if (mdb_layered_walk("list", wsp) == -1)
5240Sstevel@tonic-gate 		return (WALK_ERR);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	return (WALK_NEXT);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate static int
5300Sstevel@tonic-gate msg_walk_step(mdb_walk_state_t *wsp)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
5330Sstevel@tonic-gate 	    wsp->walk_cbdata));
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate  * The "::ipcs" command itself.  Just walks each IPC type in turn.
5390Sstevel@tonic-gate  */
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate /*ARGSUSED*/
5420Sstevel@tonic-gate static int
5430Sstevel@tonic-gate ipcs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	uint_t	oflags = 0;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || mdb_getopts(argc, argv, 'l',
5480Sstevel@tonic-gate 	    MDB_OPT_SETBITS, 1, &oflags, NULL) != argc)
5490Sstevel@tonic-gate 		return (DCMD_USAGE);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	mdb_printf("Message queues:\n");
5520Sstevel@tonic-gate 	if (mdb_walk_dcmd("msq", oflags ? "kmsqid" : "ipcperm", argc, argv) ==
5530Sstevel@tonic-gate 	    -1) {
5540Sstevel@tonic-gate 		mdb_warn("can't walk 'msq'");
5550Sstevel@tonic-gate 		return (DCMD_ERR);
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	mdb_printf("\nShared memory:\n");
5590Sstevel@tonic-gate 	if (mdb_walk_dcmd("shm", oflags ? "kshmid" : "ipcperm", argc, argv) ==
5600Sstevel@tonic-gate 	    -1) {
5610Sstevel@tonic-gate 		mdb_warn("can't walk 'shm'");
5620Sstevel@tonic-gate 		return (DCMD_ERR);
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	mdb_printf("\nSemaphores:\n");
5660Sstevel@tonic-gate 	if (mdb_walk_dcmd("sem", oflags ? "ksemid" : "ipcperm", argc, argv) ==
5670Sstevel@tonic-gate 	    -1) {
5680Sstevel@tonic-gate 		mdb_warn("can't walk 'sem'");
5690Sstevel@tonic-gate 		return (DCMD_ERR);
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	return (DCMD_OK);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate static int
5760Sstevel@tonic-gate msgprint(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	struct msg message;
5790Sstevel@tonic-gate 	uint_t	lflag = FALSE;
5800Sstevel@tonic-gate 	long	type = 0;
5810Sstevel@tonic-gate 	char	*tflag = NULL;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || (mdb_getopts(argc, argv,
5840Sstevel@tonic-gate 	    'l', MDB_OPT_SETBITS, TRUE, &lflag,
5850Sstevel@tonic-gate 	    't', MDB_OPT_STR, &tflag, NULL) != argc))
5860Sstevel@tonic-gate 		return (DCMD_USAGE);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/*
5890Sstevel@tonic-gate 	 * Handle negative values.
5900Sstevel@tonic-gate 	 */
5910Sstevel@tonic-gate 	if (tflag != NULL) {
5920Sstevel@tonic-gate 		if (*tflag == '-') {
5930Sstevel@tonic-gate 			tflag++;
5940Sstevel@tonic-gate 			type = -1;
5950Sstevel@tonic-gate 		} else {
5960Sstevel@tonic-gate 			type = 1;
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 		type *= mdb_strtoull(tflag);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
6020Sstevel@tonic-gate 		mdb_printf("%<u>%?s %?s %8s %8s %8s%</u>\n",
6030Sstevel@tonic-gate 		    "ADDR", "TEXT", "SIZE", "TYPE", "REF");
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	if (mdb_vread(&message, sizeof (struct msg), addr) == -1) {
6060Sstevel@tonic-gate 		mdb_warn("failed to read msg at %#lx", addr);
6070Sstevel@tonic-gate 		return (DCMD_ERR);
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	/*
6110Sstevel@tonic-gate 	 * If we are meeting our type contraints, display the message.
6120Sstevel@tonic-gate 	 * If -l was specified, we will also display the message
6130Sstevel@tonic-gate 	 * contents.
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate 	if ((type == 0) ||
6160Sstevel@tonic-gate 	    (type > 0 && message.msg_type == type) ||
6170Sstevel@tonic-gate 	    (type < 0 && message.msg_type <= -type)) {
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 		if (lflag && !DCMD_HDRSPEC(flags))
6200Sstevel@tonic-gate 			mdb_printf("\n");
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		mdb_printf("%0?lx %?p %8ld %8ld %8ld\n", addr, message.msg_addr,
6230Sstevel@tonic-gate 		    message.msg_size, message.msg_type, message.msg_copycnt);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		if (lflag) {
6260Sstevel@tonic-gate 			mdb_printf("\n");
6270Sstevel@tonic-gate 			mdb_inc_indent(CMN_INDENT);
6280Sstevel@tonic-gate 			if (mdb_dumpptr(
6290Sstevel@tonic-gate 			    (uintptr_t)message.msg_addr, message.msg_size,
6300Sstevel@tonic-gate 			    MDB_DUMP_RELATIVE | MDB_DUMP_TRIM |
6310Sstevel@tonic-gate 			    MDB_DUMP_ASCII | MDB_DUMP_HEADER |
6320Sstevel@tonic-gate 			    MDB_DUMP_GROUP(4),
6330Sstevel@tonic-gate 			    (mdb_dumpptr_cb_t)mdb_vread, NULL)) {
6340Sstevel@tonic-gate 				mdb_dec_indent(CMN_INDENT);
6350Sstevel@tonic-gate 				return (DCMD_ERR);
6360Sstevel@tonic-gate 			}
6370Sstevel@tonic-gate 			mdb_dec_indent(CMN_INDENT);
6380Sstevel@tonic-gate 		}
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	return (DCMD_OK);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate /*
6450Sstevel@tonic-gate  * MDB module linkage
6460Sstevel@tonic-gate  */
6470Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
6480Sstevel@tonic-gate 	/* Generic routines */
6490Sstevel@tonic-gate 	{ "ipcperm", ":", "display an IPC perm structure", ipcperm },
6500Sstevel@tonic-gate 	{ "ipcid", ":id", "perform an IPC id lookup", ipcid },
6510Sstevel@tonic-gate 	{ "ipckey", ":key", "perform an IPC key lookup", ipckey },
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/* Specific routines */
6540Sstevel@tonic-gate 	{ "kshmid", "?[-l]", "display a struct kshmid", cmd_kshmid },
6550Sstevel@tonic-gate 	{ "kmsqid", "?[-l]", "display a struct kmsqid", cmd_kmsqid },
6560Sstevel@tonic-gate 	{ "ksemid", "?[-l]", "display a struct ksemid", cmd_ksemid },
6570Sstevel@tonic-gate 	{ "msg", ":[-l] [-t type]", "display contents of a message", msgprint },
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/* Convenience routines */
6600Sstevel@tonic-gate 	{ "id2shm", ":[-k]", "convert shared memory ID to pointer", id2shm },
6610Sstevel@tonic-gate 	{ "id2msq", ":[-k]", "convert message queue ID to pointer", id2msq },
6620Sstevel@tonic-gate 	{ "id2sem", ":[-k]", "convert semaphore ID to pointer", id2sem },
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	{ "ipcs", "[-l]", "display System V IPC information", ipcs },
6650Sstevel@tonic-gate 	{ NULL }
6660Sstevel@tonic-gate };
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
6690Sstevel@tonic-gate 	{ "ipcsvc", "walk a System V IPC service",
6700Sstevel@tonic-gate 		ds_walk_init, ds_walk_step },
6710Sstevel@tonic-gate 	{ "shm", "walk the active shmid_ds structures",
6720Sstevel@tonic-gate 		ds_walk_init, ds_walk_step, NULL, &shm_ops_vec },
6730Sstevel@tonic-gate 	{ "msq", "walk the active msqid_ds structures",
6740Sstevel@tonic-gate 		ds_walk_init, ds_walk_step, NULL, &msq_ops_vec },
6750Sstevel@tonic-gate 	{ "sem", "walk the active semid_ds structures",
6760Sstevel@tonic-gate 		ds_walk_init, ds_walk_step, NULL, &sem_ops_vec },
6770Sstevel@tonic-gate 	{ "msgqueue", "walk messages on a message queue",
6780Sstevel@tonic-gate 		msg_walk_init, msg_walk_step },
6790Sstevel@tonic-gate 	{ NULL }
6800Sstevel@tonic-gate };
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate const mdb_modinfo_t *
6850Sstevel@tonic-gate _mdb_init(void)
6860Sstevel@tonic-gate {
6870Sstevel@tonic-gate 	return (&modinfo);
6880Sstevel@tonic-gate }
689