xref: /onnv-gate/usr/src/cmd/mdb/common/modules/ipc/ipc.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
30*0Sstevel@tonic-gate #include <mdb/mdb_ks.h>
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <sys/mman.h>
34*0Sstevel@tonic-gate #include <sys/project.h>
35*0Sstevel@tonic-gate #include <sys/ipc_impl.h>
36*0Sstevel@tonic-gate #include <sys/shm_impl.h>
37*0Sstevel@tonic-gate #include <sys/sem_impl.h>
38*0Sstevel@tonic-gate #include <sys/msg_impl.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <vm/anon.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #define	CMN_HDR_START	"%<u>"
43*0Sstevel@tonic-gate #define	CMN_HDR_END	"%</u>\n"
44*0Sstevel@tonic-gate #define	CMN_INDENT	(4)
45*0Sstevel@tonic-gate #define	CMN_INACTIVE	"%s facility inactive.\n"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate  * Bitmap data for page protection flags suitable for use with %b.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate const mdb_bitmask_t prot_flag_bits[] = {
51*0Sstevel@tonic-gate 	{ "PROT_READ", PROT_READ, PROT_READ },
52*0Sstevel@tonic-gate 	{ "PROT_WRITE", PROT_WRITE, PROT_WRITE },
53*0Sstevel@tonic-gate 	{ "PROT_EXEC", PROT_EXEC, PROT_EXEC },
54*0Sstevel@tonic-gate 	{ "PROT_USER", PROT_USER, PROT_USER },
55*0Sstevel@tonic-gate 	{ NULL, 0, 0 }
56*0Sstevel@tonic-gate };
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate static void
59*0Sstevel@tonic-gate printtime_nice(const char *str, time_t time)
60*0Sstevel@tonic-gate {
61*0Sstevel@tonic-gate 	if (time)
62*0Sstevel@tonic-gate 		mdb_printf("%s%Y\n", str, time);
63*0Sstevel@tonic-gate 	else
64*0Sstevel@tonic-gate 		mdb_printf("%sn/a\n", str);
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  * Print header common to all IPC types.
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate static void
71*0Sstevel@tonic-gate ipcperm_header()
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate 	mdb_printf(CMN_HDR_START "%?s %5s %5s %8s %5s %5s %6s %5s %5s %5s %5s"
74*0Sstevel@tonic-gate 	    CMN_HDR_END, "ADDR", "REF", "ID", "KEY", "MODE", "PRJID", "ZONEID",
75*0Sstevel@tonic-gate 	    "OWNER", "GROUP", "CREAT", "CGRP");
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate  * Print data common to all IPC types.
80*0Sstevel@tonic-gate  */
81*0Sstevel@tonic-gate static void
82*0Sstevel@tonic-gate ipcperm_print(uintptr_t addr, kipc_perm_t *perm)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	kproject_t proj;
85*0Sstevel@tonic-gate 	int res;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	res = mdb_vread(&proj, sizeof (kproject_t), (uintptr_t)perm->ipc_proj);
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	if (res == -1)
90*0Sstevel@tonic-gate 		mdb_warn("failed to read kproject_t at %#p", perm->ipc_proj);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	mdb_printf("%0?p %5d %5d", addr, perm->ipc_ref, perm->ipc_id);
93*0Sstevel@tonic-gate 	if (perm->ipc_key)
94*0Sstevel@tonic-gate 		mdb_printf(" %8x", perm->ipc_key);
95*0Sstevel@tonic-gate 	else
96*0Sstevel@tonic-gate 		mdb_printf(" %8s", "private");
97*0Sstevel@tonic-gate 	mdb_printf(" %5#o", perm->ipc_mode & 07777);
98*0Sstevel@tonic-gate 	if (res == -1)
99*0Sstevel@tonic-gate 		mdb_printf(" %5s %5s", "<flt>", "<flt>");
100*0Sstevel@tonic-gate 	else
101*0Sstevel@tonic-gate 		mdb_printf(" %5d %6d", proj.kpj_id, proj.kpj_zoneid);
102*0Sstevel@tonic-gate 	mdb_printf(" %5d %5d %5d %5d\n", perm->ipc_uid, perm->ipc_gid,
103*0Sstevel@tonic-gate 	    perm->ipc_cuid, perm->ipc_cgid);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*ARGSUSED*/
108*0Sstevel@tonic-gate static int
109*0Sstevel@tonic-gate ipcperm(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate 	kipc_perm_t perm;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
114*0Sstevel@tonic-gate 		return (DCMD_USAGE);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
117*0Sstevel@tonic-gate 		ipcperm_header();
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	if (mdb_vread(&perm, sizeof (kipc_perm_t), addr) == -1) {
120*0Sstevel@tonic-gate 		mdb_warn("failed to read kipc_perm_t at %#lx", addr);
121*0Sstevel@tonic-gate 		return (DCMD_ERR);
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	ipcperm_print(addr, &perm);
125*0Sstevel@tonic-gate 	return (DCMD_OK);
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate static void
129*0Sstevel@tonic-gate msq_print(kmsqid_t *msqid, uintptr_t addr)
130*0Sstevel@tonic-gate {
131*0Sstevel@tonic-gate 	mdb_printf("&list: %-?p\n", addr + OFFSETOF(kmsqid_t, msg_list));
132*0Sstevel@tonic-gate 	mdb_printf("cbytes: 0t%lu    qnum: 0t%lu    qbytes: 0t%lu"
133*0Sstevel@tonic-gate 	    "    qmax: 0t%lu\n", msqid->msg_cbytes, msqid->msg_qnum,
134*0Sstevel@tonic-gate 	    msqid->msg_qbytes, msqid->msg_qmax);
135*0Sstevel@tonic-gate 	mdb_printf("lspid: 0t%d    lrpid: 0t%d\n",
136*0Sstevel@tonic-gate 	    (int)msqid->msg_lspid, (int)msqid->msg_lrpid);
137*0Sstevel@tonic-gate 	printtime_nice("stime: ", msqid->msg_stime);
138*0Sstevel@tonic-gate 	printtime_nice("rtime: ", msqid->msg_rtime);
139*0Sstevel@tonic-gate 	printtime_nice("ctime: ", msqid->msg_ctime);
140*0Sstevel@tonic-gate 	mdb_printf("snd_cnt: 0t%lld    rcv_cnt: 0t%lld\n",
141*0Sstevel@tonic-gate 	    msqid->msg_snd_cnt, msqid->msg_rcv_cnt);
142*0Sstevel@tonic-gate 	mdb_printf("snd_cv: %hd (%p)    rcv_cv: %hd (%p)\n",
143*0Sstevel@tonic-gate 	    msqid->msg_snd_cv._opaque,
144*0Sstevel@tonic-gate 	    addr + (uintptr_t)OFFSETOF(kmsqid_t, msg_snd_cv),
145*0Sstevel@tonic-gate 	    msqid->msg_rcv_cv._opaque,
146*0Sstevel@tonic-gate 	    addr + (uintptr_t)OFFSETOF(kmsqid_t, msg_rcv_cv));
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate /*ARGSUSED1*/
151*0Sstevel@tonic-gate static void
152*0Sstevel@tonic-gate shm_print(kshmid_t *shmid, uintptr_t addr)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	shmatt_t nattch;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	nattch = shmid->shm_perm.ipc_ref - (IPC_FREE(&shmid->shm_perm) ? 0 : 1);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	mdb_printf(CMN_HDR_START "%10s %?s %5s %7s %7s %7s %7s" CMN_HDR_END,
159*0Sstevel@tonic-gate 	    "SEGSZ", "AMP", "LKCNT", "LPID", "CPID", "NATTCH", "CNATTCH");
160*0Sstevel@tonic-gate 	mdb_printf("%10#lx %?p %5u %7d %7d %7lu %7lu\n",
161*0Sstevel@tonic-gate 	    shmid->shm_segsz, shmid->shm_amp, shmid->shm_lkcnt,
162*0Sstevel@tonic-gate 	    (int)shmid->shm_lpid, (int)shmid->shm_cpid, nattch,
163*0Sstevel@tonic-gate 	    shmid->shm_ismattch);
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	printtime_nice("atime: ", shmid->shm_atime);
166*0Sstevel@tonic-gate 	printtime_nice("dtime: ", shmid->shm_dtime);
167*0Sstevel@tonic-gate 	printtime_nice("ctime: ", shmid->shm_ctime);
168*0Sstevel@tonic-gate 	mdb_printf("sptinfo: %-?p    sptseg: %-?p\n",
169*0Sstevel@tonic-gate 	    shmid->shm_sptinfo, shmid->shm_sptseg);
170*0Sstevel@tonic-gate 	mdb_printf("sptprot: <%lb>\n", shmid->shm_sptprot, prot_flag_bits);
171*0Sstevel@tonic-gate }
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate /*ARGSUSED1*/
175*0Sstevel@tonic-gate static void
176*0Sstevel@tonic-gate sem_print(ksemid_t *semid, uintptr_t addr)
177*0Sstevel@tonic-gate {
178*0Sstevel@tonic-gate 	mdb_printf("base: %-?p    nsems: 0t%u\n",
179*0Sstevel@tonic-gate 	    semid->sem_base, semid->sem_nsems);
180*0Sstevel@tonic-gate 	printtime_nice("otime: ", semid->sem_otime);
181*0Sstevel@tonic-gate 	printtime_nice("ctime: ", semid->sem_ctime);
182*0Sstevel@tonic-gate 	mdb_printf("binary: %s\n", semid->sem_binary ? "yes" : "no");
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate typedef struct ipc_ops_vec {
186*0Sstevel@tonic-gate 	char	*iv_wcmd;	/* walker name		*/
187*0Sstevel@tonic-gate 	char	*iv_ocmd;	/* output dcmd		*/
188*0Sstevel@tonic-gate 	char	*iv_service;	/* service pointer	*/
189*0Sstevel@tonic-gate 	void	(*iv_print)(void *, uintptr_t); /* output callback */
190*0Sstevel@tonic-gate 	size_t	iv_idsize;
191*0Sstevel@tonic-gate } ipc_ops_vec_t;
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate ipc_ops_vec_t msq_ops_vec = {
194*0Sstevel@tonic-gate 	"msq",
195*0Sstevel@tonic-gate 	"kmsqid",
196*0Sstevel@tonic-gate 	"msq_svc",
197*0Sstevel@tonic-gate 	(void(*)(void *, uintptr_t))msq_print,
198*0Sstevel@tonic-gate 	sizeof (kmsqid_t)
199*0Sstevel@tonic-gate };
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate ipc_ops_vec_t shm_ops_vec = {
202*0Sstevel@tonic-gate 	"shm",
203*0Sstevel@tonic-gate 	"kshmid",
204*0Sstevel@tonic-gate 	"shm_svc",
205*0Sstevel@tonic-gate 	(void(*)(void *, uintptr_t))shm_print,
206*0Sstevel@tonic-gate 	sizeof (kshmid_t)
207*0Sstevel@tonic-gate };
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate ipc_ops_vec_t sem_ops_vec = {
210*0Sstevel@tonic-gate 	"sem",
211*0Sstevel@tonic-gate 	"ksemid",
212*0Sstevel@tonic-gate 	"sem_svc",
213*0Sstevel@tonic-gate 	(void(*)(void *, uintptr_t))sem_print,
214*0Sstevel@tonic-gate 	sizeof (ksemid_t)
215*0Sstevel@tonic-gate };
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate /*
219*0Sstevel@tonic-gate  * Generic IPC data structure display code
220*0Sstevel@tonic-gate  */
221*0Sstevel@tonic-gate static int
222*0Sstevel@tonic-gate ds_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
223*0Sstevel@tonic-gate     ipc_ops_vec_t *iv)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate 	void *iddata;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
228*0Sstevel@tonic-gate 		uint_t oflags = 0;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 		if (mdb_getopts(argc, argv, 'l', MDB_OPT_SETBITS, 1, &oflags,
231*0Sstevel@tonic-gate 		    NULL) != argc)
232*0Sstevel@tonic-gate 			return (DCMD_USAGE);
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 		if (mdb_walk_dcmd(iv->iv_wcmd, oflags ? iv->iv_ocmd : "ipcperm",
235*0Sstevel@tonic-gate 		    argc, argv) == -1) {
236*0Sstevel@tonic-gate 			mdb_warn("can't walk '%s'", iv->iv_wcmd);
237*0Sstevel@tonic-gate 			return (DCMD_ERR);
238*0Sstevel@tonic-gate 		}
239*0Sstevel@tonic-gate 		return (DCMD_OK);
240*0Sstevel@tonic-gate 	}
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	iddata = mdb_alloc(iv->iv_idsize, UM_SLEEP | UM_GC);
243*0Sstevel@tonic-gate 	if (mdb_vread(iddata, iv->iv_idsize, addr) == -1) {
244*0Sstevel@tonic-gate 		mdb_warn("failed to read %s at %#lx", iv->iv_ocmd, addr);
245*0Sstevel@tonic-gate 		return (DCMD_ERR);
246*0Sstevel@tonic-gate 	}
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	if (!DCMD_HDRSPEC(flags) && iv->iv_print)
249*0Sstevel@tonic-gate 		mdb_printf("\n");
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags) || iv->iv_print)
252*0Sstevel@tonic-gate 		ipcperm_header();
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	ipcperm_print(addr, (struct kipc_perm *)iddata);
255*0Sstevel@tonic-gate 	if (iv->iv_print) {
256*0Sstevel@tonic-gate 		mdb_inc_indent(CMN_INDENT);
257*0Sstevel@tonic-gate 		iv->iv_print(iddata, addr);
258*0Sstevel@tonic-gate 		mdb_dec_indent(CMN_INDENT);
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	return (DCMD_OK);
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate /*
266*0Sstevel@tonic-gate  * Stubs to call ds_print with the appropriate ops vector
267*0Sstevel@tonic-gate  */
268*0Sstevel@tonic-gate static int
269*0Sstevel@tonic-gate cmd_kshmid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
270*0Sstevel@tonic-gate {
271*0Sstevel@tonic-gate 	return (ds_print(addr, flags, argc, argv, &shm_ops_vec));
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate static int
276*0Sstevel@tonic-gate cmd_kmsqid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
277*0Sstevel@tonic-gate {
278*0Sstevel@tonic-gate 	return (ds_print(addr, flags, argc, argv, &msq_ops_vec));
279*0Sstevel@tonic-gate }
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate static int
282*0Sstevel@tonic-gate cmd_ksemid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
283*0Sstevel@tonic-gate {
284*0Sstevel@tonic-gate 	return (ds_print(addr, flags, argc, argv, &sem_ops_vec));
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate /*
288*0Sstevel@tonic-gate  * Generic IPC walker
289*0Sstevel@tonic-gate  */
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate static int
292*0Sstevel@tonic-gate ds_walk_init(mdb_walk_state_t *wsp)
293*0Sstevel@tonic-gate {
294*0Sstevel@tonic-gate 	ipc_ops_vec_t	*iv = wsp->walk_arg;
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	if (wsp->walk_arg != NULL && wsp->walk_addr != NULL)
297*0Sstevel@tonic-gate 		mdb_printf("ignoring provided address\n");
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	if (wsp->walk_arg)
300*0Sstevel@tonic-gate 		if (mdb_readvar(&wsp->walk_addr, iv->iv_service) == -1) {
301*0Sstevel@tonic-gate 			mdb_printf("failed to read '%s'; module not present\n",
302*0Sstevel@tonic-gate 			    iv->iv_service);
303*0Sstevel@tonic-gate 			return (WALK_DONE);
304*0Sstevel@tonic-gate 		}
305*0Sstevel@tonic-gate 	else
306*0Sstevel@tonic-gate 		wsp->walk_addr = wsp->walk_addr +
307*0Sstevel@tonic-gate 		    OFFSETOF(ipc_service_t, ipcs_usedids);
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	if (mdb_layered_walk("list", wsp) == -1)
310*0Sstevel@tonic-gate 		return (WALK_ERR);
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	return (WALK_NEXT);
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate static int
317*0Sstevel@tonic-gate ds_walk_step(mdb_walk_state_t *wsp)
318*0Sstevel@tonic-gate {
319*0Sstevel@tonic-gate 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
320*0Sstevel@tonic-gate 	    wsp->walk_cbdata));
321*0Sstevel@tonic-gate }
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate /*
324*0Sstevel@tonic-gate  * Generic IPC ID/key to pointer code
325*0Sstevel@tonic-gate  */
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate static int
328*0Sstevel@tonic-gate ipcid_impl(uintptr_t svcptr, uintptr_t id, uintptr_t *addr)
329*0Sstevel@tonic-gate {
330*0Sstevel@tonic-gate 	ipc_service_t service;
331*0Sstevel@tonic-gate 	kipc_perm_t perm;
332*0Sstevel@tonic-gate 	ipc_slot_t slot;
333*0Sstevel@tonic-gate 	uintptr_t slotptr;
334*0Sstevel@tonic-gate 	uint_t index;
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	if (id > INT_MAX) {
337*0Sstevel@tonic-gate 		mdb_warn("id out of range\n");
338*0Sstevel@tonic-gate 		return (DCMD_ERR);
339*0Sstevel@tonic-gate 	}
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	if (mdb_vread(&service, sizeof (ipc_service_t), svcptr) == -1) {
342*0Sstevel@tonic-gate 		mdb_warn("failed to read ipc_service_t at %#lx", svcptr);
343*0Sstevel@tonic-gate 		return (DCMD_ERR);
344*0Sstevel@tonic-gate 	}
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	index = (uint_t)id & (service.ipcs_tabsz - 1);
347*0Sstevel@tonic-gate 	slotptr = (uintptr_t)(service.ipcs_table + index);
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	if (mdb_vread(&slot, sizeof (ipc_slot_t), slotptr) == -1) {
350*0Sstevel@tonic-gate 		mdb_warn("failed to read ipc_slot_t at %#lx", slotptr);
351*0Sstevel@tonic-gate 		return (DCMD_ERR);
352*0Sstevel@tonic-gate 	}
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	if (slot.ipct_data == NULL)
355*0Sstevel@tonic-gate 		return (DCMD_ERR);
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	if (mdb_vread(&perm, sizeof (kipc_perm_t),
358*0Sstevel@tonic-gate 	    (uintptr_t)slot.ipct_data) == -1) {
359*0Sstevel@tonic-gate 		mdb_warn("failed to read kipc_perm_t at %#p",
360*0Sstevel@tonic-gate 		    slot.ipct_data);
361*0Sstevel@tonic-gate 		return (DCMD_ERR);
362*0Sstevel@tonic-gate 	}
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	if (perm.ipc_id != (uint_t)id)
365*0Sstevel@tonic-gate 		return (DCMD_ERR);
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	*addr = (uintptr_t)slot.ipct_data;
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	return (DCMD_OK);
370*0Sstevel@tonic-gate }
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate typedef struct findkey_data {
374*0Sstevel@tonic-gate 	key_t fk_key;
375*0Sstevel@tonic-gate 	uintptr_t fk_addr;
376*0Sstevel@tonic-gate 	boolean_t fk_found;
377*0Sstevel@tonic-gate } findkey_data_t;
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate static int
380*0Sstevel@tonic-gate findkey(uintptr_t addr, kipc_perm_t *perm, findkey_data_t *arg)
381*0Sstevel@tonic-gate {
382*0Sstevel@tonic-gate 	if (perm->ipc_key == arg->fk_key) {
383*0Sstevel@tonic-gate 		arg->fk_found = B_TRUE;
384*0Sstevel@tonic-gate 		arg->fk_addr = addr;
385*0Sstevel@tonic-gate 		return (WALK_DONE);
386*0Sstevel@tonic-gate 	}
387*0Sstevel@tonic-gate 	return (WALK_NEXT);
388*0Sstevel@tonic-gate }
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate static int
391*0Sstevel@tonic-gate ipckey_impl(uintptr_t svcptr, uintptr_t key, uintptr_t *addr)
392*0Sstevel@tonic-gate {
393*0Sstevel@tonic-gate 	ipc_service_t	service;
394*0Sstevel@tonic-gate 	findkey_data_t	fkdata;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	if ((key == IPC_PRIVATE) || (key > INT_MAX)) {
397*0Sstevel@tonic-gate 		mdb_warn("key out of range\n");
398*0Sstevel@tonic-gate 		return (DCMD_ERR);
399*0Sstevel@tonic-gate 	}
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	if (mdb_vread(&service, sizeof (ipc_service_t), svcptr) == -1) {
402*0Sstevel@tonic-gate 		mdb_warn("failed to read ipc_service_t at %#lx", svcptr);
403*0Sstevel@tonic-gate 		return (DCMD_ERR);
404*0Sstevel@tonic-gate 	}
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	fkdata.fk_key = (key_t)key;
407*0Sstevel@tonic-gate 	fkdata.fk_found = B_FALSE;
408*0Sstevel@tonic-gate 	if ((mdb_pwalk("avl", (mdb_walk_cb_t)findkey, &fkdata,
409*0Sstevel@tonic-gate 	    svcptr + OFFSETOF(ipc_service_t, ipcs_keys)) == -1) ||
410*0Sstevel@tonic-gate 	    !fkdata.fk_found)
411*0Sstevel@tonic-gate 		return (DCMD_ERR);
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	*addr = fkdata.fk_addr;
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	return (DCMD_OK);
416*0Sstevel@tonic-gate }
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate static int
419*0Sstevel@tonic-gate ipckeyid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
420*0Sstevel@tonic-gate     int(*fp)(uintptr_t, uintptr_t, uintptr_t *))
421*0Sstevel@tonic-gate {
422*0Sstevel@tonic-gate 	uintmax_t val;
423*0Sstevel@tonic-gate 	uintptr_t raddr;
424*0Sstevel@tonic-gate 	int result;
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || (argc != 1))
427*0Sstevel@tonic-gate 		return (DCMD_USAGE);
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 	if (argv[0].a_type == MDB_TYPE_IMMEDIATE)
430*0Sstevel@tonic-gate 		val = argv[0].a_un.a_val;
431*0Sstevel@tonic-gate 	else if (argv[0].a_type == MDB_TYPE_STRING)
432*0Sstevel@tonic-gate 		val = mdb_strtoull(argv[0].a_un.a_str);
433*0Sstevel@tonic-gate 	else
434*0Sstevel@tonic-gate 		return (DCMD_USAGE);
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	result = fp(addr, val, &raddr);
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	if (result == DCMD_OK)
439*0Sstevel@tonic-gate 		mdb_printf("%lx", raddr);
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	return (result);
442*0Sstevel@tonic-gate }
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate static int
445*0Sstevel@tonic-gate ipckey(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
446*0Sstevel@tonic-gate {
447*0Sstevel@tonic-gate 	return (ipckeyid(addr, flags, argc, argv, ipckey_impl));
448*0Sstevel@tonic-gate }
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate static int
451*0Sstevel@tonic-gate ipcid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
452*0Sstevel@tonic-gate {
453*0Sstevel@tonic-gate 	return (ipckeyid(addr, flags, argc, argv, ipcid_impl));
454*0Sstevel@tonic-gate }
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate static int
457*0Sstevel@tonic-gate ds_ptr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
458*0Sstevel@tonic-gate     ipc_ops_vec_t *iv)
459*0Sstevel@tonic-gate {
460*0Sstevel@tonic-gate 	uint_t		kflag = FALSE;
461*0Sstevel@tonic-gate 	uintptr_t	svcptr, raddr;
462*0Sstevel@tonic-gate 	int		result;
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
465*0Sstevel@tonic-gate 		return (DCMD_USAGE);
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
468*0Sstevel@tonic-gate 	    'k', MDB_OPT_SETBITS, TRUE, &kflag, NULL) != argc)
469*0Sstevel@tonic-gate 		return (DCMD_USAGE);
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	if (mdb_readvar(&svcptr, iv->iv_service) == -1) {
472*0Sstevel@tonic-gate 		mdb_warn("failed to read '%s'; module not present\n",
473*0Sstevel@tonic-gate 		    iv->iv_service);
474*0Sstevel@tonic-gate 		return (DCMD_ERR);
475*0Sstevel@tonic-gate 	}
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	result = kflag ? ipckey_impl(svcptr, addr, &raddr) :
478*0Sstevel@tonic-gate 	    ipcid_impl(svcptr, addr, &raddr);
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	if (result == DCMD_OK)
481*0Sstevel@tonic-gate 		mdb_printf("%lx", raddr);
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	return (result);
484*0Sstevel@tonic-gate }
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate /*
487*0Sstevel@tonic-gate  * Stubs to call ds_ptr with the appropriate ops vector
488*0Sstevel@tonic-gate  */
489*0Sstevel@tonic-gate static int
490*0Sstevel@tonic-gate id2shm(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
491*0Sstevel@tonic-gate {
492*0Sstevel@tonic-gate 	return (ds_ptr(addr, flags, argc, argv, &shm_ops_vec));
493*0Sstevel@tonic-gate }
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate static int
496*0Sstevel@tonic-gate id2msq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
497*0Sstevel@tonic-gate {
498*0Sstevel@tonic-gate 	return (ds_ptr(addr, flags, argc, argv, &msq_ops_vec));
499*0Sstevel@tonic-gate }
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate static int
502*0Sstevel@tonic-gate id2sem(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
503*0Sstevel@tonic-gate {
504*0Sstevel@tonic-gate 	return (ds_ptr(addr, flags, argc, argv, &sem_ops_vec));
505*0Sstevel@tonic-gate }
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate /*
509*0Sstevel@tonic-gate  * The message queue contents walker
510*0Sstevel@tonic-gate  */
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate static int
513*0Sstevel@tonic-gate msg_walk_init(mdb_walk_state_t *wsp)
514*0Sstevel@tonic-gate {
515*0Sstevel@tonic-gate 	wsp->walk_addr += OFFSETOF(kmsqid_t, msg_list);
516*0Sstevel@tonic-gate 	if (mdb_layered_walk("list", wsp) == -1)
517*0Sstevel@tonic-gate 		return (WALK_ERR);
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 	return (WALK_NEXT);
520*0Sstevel@tonic-gate }
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate static int
523*0Sstevel@tonic-gate msg_walk_step(mdb_walk_state_t *wsp)
524*0Sstevel@tonic-gate {
525*0Sstevel@tonic-gate 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
526*0Sstevel@tonic-gate 	    wsp->walk_cbdata));
527*0Sstevel@tonic-gate }
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate /*
531*0Sstevel@tonic-gate  * The "::ipcs" command itself.  Just walks each IPC type in turn.
532*0Sstevel@tonic-gate  */
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate /*ARGSUSED*/
535*0Sstevel@tonic-gate static int
536*0Sstevel@tonic-gate ipcs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
537*0Sstevel@tonic-gate {
538*0Sstevel@tonic-gate 	uint_t	oflags = 0;
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || mdb_getopts(argc, argv, 'l',
541*0Sstevel@tonic-gate 	    MDB_OPT_SETBITS, 1, &oflags, NULL) != argc)
542*0Sstevel@tonic-gate 		return (DCMD_USAGE);
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	mdb_printf("Message queues:\n");
545*0Sstevel@tonic-gate 	if (mdb_walk_dcmd("msq", oflags ? "kmsqid" : "ipcperm", argc, argv) ==
546*0Sstevel@tonic-gate 	    -1) {
547*0Sstevel@tonic-gate 		mdb_warn("can't walk 'msq'");
548*0Sstevel@tonic-gate 		return (DCMD_ERR);
549*0Sstevel@tonic-gate 	}
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 	mdb_printf("\nShared memory:\n");
552*0Sstevel@tonic-gate 	if (mdb_walk_dcmd("shm", oflags ? "kshmid" : "ipcperm", argc, argv) ==
553*0Sstevel@tonic-gate 	    -1) {
554*0Sstevel@tonic-gate 		mdb_warn("can't walk 'shm'");
555*0Sstevel@tonic-gate 		return (DCMD_ERR);
556*0Sstevel@tonic-gate 	}
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	mdb_printf("\nSemaphores:\n");
559*0Sstevel@tonic-gate 	if (mdb_walk_dcmd("sem", oflags ? "ksemid" : "ipcperm", argc, argv) ==
560*0Sstevel@tonic-gate 	    -1) {
561*0Sstevel@tonic-gate 		mdb_warn("can't walk 'sem'");
562*0Sstevel@tonic-gate 		return (DCMD_ERR);
563*0Sstevel@tonic-gate 	}
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 	return (DCMD_OK);
566*0Sstevel@tonic-gate }
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate static int
569*0Sstevel@tonic-gate msgprint(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
570*0Sstevel@tonic-gate {
571*0Sstevel@tonic-gate 	struct msg message;
572*0Sstevel@tonic-gate 	uint_t	lflag = FALSE;
573*0Sstevel@tonic-gate 	long	type = 0;
574*0Sstevel@tonic-gate 	char	*tflag = NULL;
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || (mdb_getopts(argc, argv,
577*0Sstevel@tonic-gate 	    'l', MDB_OPT_SETBITS, TRUE, &lflag,
578*0Sstevel@tonic-gate 	    't', MDB_OPT_STR, &tflag, NULL) != argc))
579*0Sstevel@tonic-gate 		return (DCMD_USAGE);
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 	/*
582*0Sstevel@tonic-gate 	 * Handle negative values.
583*0Sstevel@tonic-gate 	 */
584*0Sstevel@tonic-gate 	if (tflag != NULL) {
585*0Sstevel@tonic-gate 		if (*tflag == '-') {
586*0Sstevel@tonic-gate 			tflag++;
587*0Sstevel@tonic-gate 			type = -1;
588*0Sstevel@tonic-gate 		} else {
589*0Sstevel@tonic-gate 			type = 1;
590*0Sstevel@tonic-gate 		}
591*0Sstevel@tonic-gate 		type *= mdb_strtoull(tflag);
592*0Sstevel@tonic-gate 	}
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
595*0Sstevel@tonic-gate 		mdb_printf("%<u>%?s %?s %8s %8s %8s%</u>\n",
596*0Sstevel@tonic-gate 		    "ADDR", "TEXT", "SIZE", "TYPE", "REF");
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 	if (mdb_vread(&message, sizeof (struct msg), addr) == -1) {
599*0Sstevel@tonic-gate 		mdb_warn("failed to read msg at %#lx", addr);
600*0Sstevel@tonic-gate 		return (DCMD_ERR);
601*0Sstevel@tonic-gate 	}
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate 	/*
604*0Sstevel@tonic-gate 	 * If we are meeting our type contraints, display the message.
605*0Sstevel@tonic-gate 	 * If -l was specified, we will also display the message
606*0Sstevel@tonic-gate 	 * contents.
607*0Sstevel@tonic-gate 	 */
608*0Sstevel@tonic-gate 	if ((type == 0) ||
609*0Sstevel@tonic-gate 	    (type > 0 && message.msg_type == type) ||
610*0Sstevel@tonic-gate 	    (type < 0 && message.msg_type <= -type)) {
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 		if (lflag && !DCMD_HDRSPEC(flags))
613*0Sstevel@tonic-gate 			mdb_printf("\n");
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 		mdb_printf("%0?lx %?p %8ld %8ld %8ld\n", addr, message.msg_addr,
616*0Sstevel@tonic-gate 		    message.msg_size, message.msg_type, message.msg_copycnt);
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 		if (lflag) {
619*0Sstevel@tonic-gate 			mdb_printf("\n");
620*0Sstevel@tonic-gate 			mdb_inc_indent(CMN_INDENT);
621*0Sstevel@tonic-gate 			if (mdb_dumpptr(
622*0Sstevel@tonic-gate 			    (uintptr_t)message.msg_addr, message.msg_size,
623*0Sstevel@tonic-gate 			    MDB_DUMP_RELATIVE | MDB_DUMP_TRIM |
624*0Sstevel@tonic-gate 			    MDB_DUMP_ASCII | MDB_DUMP_HEADER |
625*0Sstevel@tonic-gate 			    MDB_DUMP_GROUP(4),
626*0Sstevel@tonic-gate 			    (mdb_dumpptr_cb_t)mdb_vread, NULL)) {
627*0Sstevel@tonic-gate 				mdb_dec_indent(CMN_INDENT);
628*0Sstevel@tonic-gate 				return (DCMD_ERR);
629*0Sstevel@tonic-gate 			}
630*0Sstevel@tonic-gate 			mdb_dec_indent(CMN_INDENT);
631*0Sstevel@tonic-gate 		}
632*0Sstevel@tonic-gate 	}
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate 	return (DCMD_OK);
635*0Sstevel@tonic-gate }
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate /*
638*0Sstevel@tonic-gate  * MDB module linkage
639*0Sstevel@tonic-gate  */
640*0Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
641*0Sstevel@tonic-gate 	/* Generic routines */
642*0Sstevel@tonic-gate 	{ "ipcperm", ":", "display an IPC perm structure", ipcperm },
643*0Sstevel@tonic-gate 	{ "ipcid", ":id", "perform an IPC id lookup", ipcid },
644*0Sstevel@tonic-gate 	{ "ipckey", ":key", "perform an IPC key lookup", ipckey },
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 	/* Specific routines */
647*0Sstevel@tonic-gate 	{ "kshmid", "?[-l]", "display a struct kshmid", cmd_kshmid },
648*0Sstevel@tonic-gate 	{ "kmsqid", "?[-l]", "display a struct kmsqid", cmd_kmsqid },
649*0Sstevel@tonic-gate 	{ "ksemid", "?[-l]", "display a struct ksemid", cmd_ksemid },
650*0Sstevel@tonic-gate 	{ "msg", ":[-l] [-t type]", "display contents of a message", msgprint },
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 	/* Convenience routines */
653*0Sstevel@tonic-gate 	{ "id2shm", ":[-k]", "convert shared memory ID to pointer", id2shm },
654*0Sstevel@tonic-gate 	{ "id2msq", ":[-k]", "convert message queue ID to pointer", id2msq },
655*0Sstevel@tonic-gate 	{ "id2sem", ":[-k]", "convert semaphore ID to pointer", id2sem },
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 	{ "ipcs", "[-l]", "display System V IPC information", ipcs },
658*0Sstevel@tonic-gate 	{ NULL }
659*0Sstevel@tonic-gate };
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
662*0Sstevel@tonic-gate 	{ "ipcsvc", "walk a System V IPC service",
663*0Sstevel@tonic-gate 		ds_walk_init, ds_walk_step },
664*0Sstevel@tonic-gate 	{ "shm", "walk the active shmid_ds structures",
665*0Sstevel@tonic-gate 		ds_walk_init, ds_walk_step, NULL, &shm_ops_vec },
666*0Sstevel@tonic-gate 	{ "msq", "walk the active msqid_ds structures",
667*0Sstevel@tonic-gate 		ds_walk_init, ds_walk_step, NULL, &msq_ops_vec },
668*0Sstevel@tonic-gate 	{ "sem", "walk the active semid_ds structures",
669*0Sstevel@tonic-gate 		ds_walk_init, ds_walk_step, NULL, &sem_ops_vec },
670*0Sstevel@tonic-gate 	{ "msgqueue", "walk messages on a message queue",
671*0Sstevel@tonic-gate 		msg_walk_init, msg_walk_step },
672*0Sstevel@tonic-gate 	{ NULL }
673*0Sstevel@tonic-gate };
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate const mdb_modinfo_t *
678*0Sstevel@tonic-gate _mdb_init(void)
679*0Sstevel@tonic-gate {
680*0Sstevel@tonic-gate 	return (&modinfo);
681*0Sstevel@tonic-gate }
682