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 2003 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/strsubr.h>
34*0Sstevel@tonic-gate #include <sys/ptms.h>
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate typedef struct pt_flags {
37*0Sstevel@tonic-gate const char *pt_name;
38*0Sstevel@tonic-gate const char *pt_descr;
39*0Sstevel@tonic-gate } ptflags_t;
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate static const struct pt_flags pf[] = {
42*0Sstevel@tonic-gate { "PTLOCK", "Master/slave pair is locked" },
43*0Sstevel@tonic-gate { "PTMOPEN", "Master side is open" },
44*0Sstevel@tonic-gate { "PTSOPEN", "Slave side is open" },
45*0Sstevel@tonic-gate { "PTSTTY", "Slave side is tty" },
46*0Sstevel@tonic-gate { NULL },
47*0Sstevel@tonic-gate };
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate static int
pt_parse_flag(const ptflags_t ftable[],const char * arg,uint32_t * flag)50*0Sstevel@tonic-gate pt_parse_flag(const ptflags_t ftable[], const char *arg, uint32_t *flag)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate int i;
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate for (i = 0; ftable[i].pt_name != NULL; i++) {
55*0Sstevel@tonic-gate if (strcasecmp(arg, ftable[i].pt_name) == 0) {
56*0Sstevel@tonic-gate *flag |= (1 << i);
57*0Sstevel@tonic-gate return (0);
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate }
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate return (-1);
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate static void
pt_flag_usage(const ptflags_t ftable[])65*0Sstevel@tonic-gate pt_flag_usage(const ptflags_t ftable[])
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate int i;
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate for (i = 0; ftable[i].pt_name != NULL; i++)
70*0Sstevel@tonic-gate mdb_printf("%12s %s\n",
71*0Sstevel@tonic-gate ftable[i].pt_name, ftable[i].pt_descr);
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate static void
ptms_pr_qinfo(char * buf,size_t nbytes,struct pt_ttys * pt,char * peername,queue_t * peerq,char * procname)77*0Sstevel@tonic-gate ptms_pr_qinfo(char *buf, size_t nbytes, struct pt_ttys *pt, char *peername,
78*0Sstevel@tonic-gate queue_t *peerq, char *procname)
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate (void) mdb_snprintf(buf, nbytes,
81*0Sstevel@tonic-gate "pts/%d:%s: %p\nprocess: %d(%s)",
82*0Sstevel@tonic-gate pt->pt_minor, peername, peerq, pt->pt_pid, procname);
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate static int
ptms(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)86*0Sstevel@tonic-gate ptms(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate const int PT_FLGDELT = (int)(sizeof (uintptr_t) * 2 + 5);
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate struct pt_ttys pt;
91*0Sstevel@tonic-gate char c[MAXCOMLEN + 1];
92*0Sstevel@tonic-gate const char *flag = NULL, *not_flag = NULL;
93*0Sstevel@tonic-gate proc_t p;
94*0Sstevel@tonic-gate uint_t verbose = FALSE;
95*0Sstevel@tonic-gate uint32_t mask = 0, not_mask = 0;
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC))
98*0Sstevel@tonic-gate return (mdb_walk_dcmd("ptms", "ptms", argc, argv));
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate if (mdb_getopts(argc, argv,
101*0Sstevel@tonic-gate 'v', MDB_OPT_SETBITS, TRUE, &verbose,
102*0Sstevel@tonic-gate 'f', MDB_OPT_STR, &flag,
103*0Sstevel@tonic-gate 'F', MDB_OPT_STR, ¬_flag, NULL) != argc)
104*0Sstevel@tonic-gate return (DCMD_USAGE);
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate if (DCMD_HDRSPEC(flags) && flag == NULL && not_flag == NULL) {
107*0Sstevel@tonic-gate (void) mdb_printf("%?-s %s %s %?-s %?-s %3s %-6s %s\n",
108*0Sstevel@tonic-gate "ADDR", "PTY", "FL", "MASTERQ", "SLAVEQ",
109*0Sstevel@tonic-gate "ZID", "PID", "PROC");
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate if (flag != NULL && pt_parse_flag(pf, flag, &mask) == -1) {
113*0Sstevel@tonic-gate mdb_warn("unrecognized pty flag '%s'\n", flag);
114*0Sstevel@tonic-gate pt_flag_usage(pf);
115*0Sstevel@tonic-gate return (DCMD_USAGE);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate if (not_flag != NULL && pt_parse_flag(pf, not_flag, ¬_mask) == -1) {
119*0Sstevel@tonic-gate mdb_warn("unrecognized queue flag '%s'\n", flag);
120*0Sstevel@tonic-gate pt_flag_usage(pf);
121*0Sstevel@tonic-gate return (DCMD_USAGE);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate if (mdb_vread(&pt, sizeof (pt), addr) == -1) {
125*0Sstevel@tonic-gate mdb_warn("failed to read pty structure");
126*0Sstevel@tonic-gate return (DCMD_ERR);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate if (mask != 0 && !(pt.pt_state & mask))
130*0Sstevel@tonic-gate return (DCMD_OK);
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate if (not_mask != 0 && (pt.pt_state & not_mask))
133*0Sstevel@tonic-gate return (DCMD_OK);
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate /*
136*0Sstevel@tonic-gate * Options are specified for filtering, so If any option is specified on
137*0Sstevel@tonic-gate * the command line, just print address and exit.
138*0Sstevel@tonic-gate */
139*0Sstevel@tonic-gate if (flag != NULL || not_flag != NULL) {
140*0Sstevel@tonic-gate mdb_printf("%0?p\n", addr);
141*0Sstevel@tonic-gate return (DCMD_OK);
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate if (pt.pt_pid != 0) {
145*0Sstevel@tonic-gate if (mdb_pid2proc(pt.pt_pid, &p) == NULL)
146*0Sstevel@tonic-gate (void) strcpy(c, "<defunct>");
147*0Sstevel@tonic-gate else
148*0Sstevel@tonic-gate (void) strcpy(c, p.p_user.u_comm);
149*0Sstevel@tonic-gate } else
150*0Sstevel@tonic-gate (void) strcpy(c, "<unknown>");
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate (void) mdb_printf("%0?p %3d %2x %0?p %0?p %3d %6d %s\n",
153*0Sstevel@tonic-gate addr, pt.pt_minor, pt.pt_state, pt.ptm_rdq, pt.pts_rdq,
154*0Sstevel@tonic-gate pt.pt_zoneid, pt.pt_pid, c);
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate if (verbose) {
157*0Sstevel@tonic-gate int i, arm = 0;
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate for (i = 0; pf[i].pt_name != NULL; i++) {
160*0Sstevel@tonic-gate if (!(pt.pt_state & (1 << i)))
161*0Sstevel@tonic-gate continue;
162*0Sstevel@tonic-gate if (!arm) {
163*0Sstevel@tonic-gate mdb_printf("%*s|\n%*s+--> ",
164*0Sstevel@tonic-gate PT_FLGDELT, "", PT_FLGDELT, "");
165*0Sstevel@tonic-gate arm = 1;
166*0Sstevel@tonic-gate } else
167*0Sstevel@tonic-gate mdb_printf("%*s ", PT_FLGDELT, "");
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate mdb_printf("%-12s %s\n",
170*0Sstevel@tonic-gate pf[i].pt_name, pf[i].pt_descr);
171*0Sstevel@tonic-gate }
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate return (DCMD_OK);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate static void
ptms_qinfo(const queue_t * q,char * buf,size_t nbytes,int ismaster)178*0Sstevel@tonic-gate ptms_qinfo(const queue_t *q, char *buf, size_t nbytes, int ismaster)
179*0Sstevel@tonic-gate {
180*0Sstevel@tonic-gate char c[MAXCOMLEN + 1];
181*0Sstevel@tonic-gate struct pt_ttys pt;
182*0Sstevel@tonic-gate proc_t p;
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate (void) mdb_vread(&pt, sizeof (pt), (uintptr_t)q->q_ptr);
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate if (pt.pt_pid != 0) {
187*0Sstevel@tonic-gate if (mdb_pid2proc(pt.pt_pid, &p) == NULL)
188*0Sstevel@tonic-gate (void) strcpy(c, "<defunct>");
189*0Sstevel@tonic-gate else
190*0Sstevel@tonic-gate (void) strcpy(c, p.p_user.u_comm);
191*0Sstevel@tonic-gate } else
192*0Sstevel@tonic-gate (void) strcpy(c, "<unknown>");
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate if (ismaster)
195*0Sstevel@tonic-gate ptms_pr_qinfo(buf, nbytes, &pt, "slave", pt.pts_rdq, c);
196*0Sstevel@tonic-gate else
197*0Sstevel@tonic-gate ptms_pr_qinfo(buf, nbytes, &pt, "master", pt.ptm_rdq, c);
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate void
ptm_qinfo(const queue_t * q,char * buf,size_t nbytes)201*0Sstevel@tonic-gate ptm_qinfo(const queue_t *q, char *buf, size_t nbytes)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate ptms_qinfo(q, buf, nbytes, 1);
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate void
pts_qinfo(const queue_t * q,char * buf,size_t nbytes)207*0Sstevel@tonic-gate pts_qinfo(const queue_t *q, char *buf, size_t nbytes)
208*0Sstevel@tonic-gate {
209*0Sstevel@tonic-gate ptms_qinfo(q, buf, nbytes, 0);
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate
212*0Sstevel@tonic-gate static int
ptms_walk_init(mdb_walk_state_t * wsp)213*0Sstevel@tonic-gate ptms_walk_init(mdb_walk_state_t *wsp)
214*0Sstevel@tonic-gate {
215*0Sstevel@tonic-gate size_t nslots;
216*0Sstevel@tonic-gate
217*0Sstevel@tonic-gate if (wsp->walk_addr != NULL) {
218*0Sstevel@tonic-gate mdb_warn("ptms supports only global walks");
219*0Sstevel@tonic-gate return (WALK_ERR);
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate if (mdb_readvar(&wsp->walk_addr, "ptms_slots") == -1) {
223*0Sstevel@tonic-gate mdb_warn("failed to read 'ptms_slots'");
224*0Sstevel@tonic-gate return (WALK_ERR);
225*0Sstevel@tonic-gate }
226*0Sstevel@tonic-gate
227*0Sstevel@tonic-gate if (mdb_readvar(&nslots, "ptms_nslots") == -1) {
228*0Sstevel@tonic-gate mdb_warn("failed to read 'ptms_nslots'");
229*0Sstevel@tonic-gate return (WALK_ERR);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate /*
233*0Sstevel@tonic-gate * We remember the pointer value at the end of the array. When
234*0Sstevel@tonic-gate * the walk gets there, we're done.
235*0Sstevel@tonic-gate */
236*0Sstevel@tonic-gate wsp->walk_arg = (((struct pt_ttys **)wsp->walk_addr) + (nslots - 1));
237*0Sstevel@tonic-gate wsp->walk_data = mdb_alloc(sizeof (struct pt_ttys), UM_SLEEP);
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate return (WALK_NEXT);
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate static int
ptms_walk_step(mdb_walk_state_t * wsp)243*0Sstevel@tonic-gate ptms_walk_step(mdb_walk_state_t *wsp)
244*0Sstevel@tonic-gate {
245*0Sstevel@tonic-gate int status;
246*0Sstevel@tonic-gate uintptr_t ptr;
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate if (wsp->walk_addr > (uintptr_t)wsp->walk_arg)
249*0Sstevel@tonic-gate return (WALK_DONE);
250*0Sstevel@tonic-gate
251*0Sstevel@tonic-gate if (mdb_vread(&ptr, sizeof (struct pt_ttys *), wsp->walk_addr) !=
252*0Sstevel@tonic-gate (sizeof (struct pt_ttys *))) {
253*0Sstevel@tonic-gate mdb_warn("failed to read pt_ttys* at %p", wsp->walk_addr);
254*0Sstevel@tonic-gate return (WALK_DONE);
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gate if (ptr == NULL) {
258*0Sstevel@tonic-gate wsp->walk_addr += sizeof (uintptr_t);
259*0Sstevel@tonic-gate return (WALK_NEXT);
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate if (mdb_vread(wsp->walk_data, sizeof (struct pt_ttys), ptr) !=
263*0Sstevel@tonic-gate sizeof (struct pt_ttys)) {
264*0Sstevel@tonic-gate mdb_warn("failed to read pt_ttys at %p", ptr);
265*0Sstevel@tonic-gate return (WALK_DONE);
266*0Sstevel@tonic-gate }
267*0Sstevel@tonic-gate
268*0Sstevel@tonic-gate status = wsp->walk_callback(ptr, wsp->walk_data, wsp->walk_cbdata);
269*0Sstevel@tonic-gate wsp->walk_addr += sizeof (uintptr_t);
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gate return (status);
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate
274*0Sstevel@tonic-gate static void
ptms_walk_fini(mdb_walk_state_t * wsp)275*0Sstevel@tonic-gate ptms_walk_fini(mdb_walk_state_t *wsp)
276*0Sstevel@tonic-gate {
277*0Sstevel@tonic-gate mdb_free(wsp->walk_data, sizeof (struct pt_ttys));
278*0Sstevel@tonic-gate }
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
281*0Sstevel@tonic-gate { "ptms", "?[-v] [-f flag] [-F flag]",
282*0Sstevel@tonic-gate "print pseudo-terminal information", ptms },
283*0Sstevel@tonic-gate { "pty", "?[-v] [-f flag] [-F flag]",
284*0Sstevel@tonic-gate "print pseudo-terminal information (alias of ::ptms", ptms },
285*0Sstevel@tonic-gate { NULL }
286*0Sstevel@tonic-gate };
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
289*0Sstevel@tonic-gate { "ptms", "walk list of pseudo-tty's",
290*0Sstevel@tonic-gate ptms_walk_init, ptms_walk_step, ptms_walk_fini },
291*0Sstevel@tonic-gate { "pty", "walk list of pseudo-tty's (alias of ::walk ptms)",
292*0Sstevel@tonic-gate ptms_walk_init, ptms_walk_step, ptms_walk_fini },
293*0Sstevel@tonic-gate { NULL }
294*0Sstevel@tonic-gate };
295*0Sstevel@tonic-gate
296*0Sstevel@tonic-gate static const mdb_qops_t ptm_qops = {
297*0Sstevel@tonic-gate ptm_qinfo, mdb_qrnext_default, mdb_qwnext_default
298*0Sstevel@tonic-gate };
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate static const mdb_qops_t pts_qops = {
301*0Sstevel@tonic-gate pts_qinfo, mdb_qrnext_default, mdb_qwnext_default
302*0Sstevel@tonic-gate };
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gate static const mdb_modinfo_t modinfo = {
305*0Sstevel@tonic-gate MDB_API_VERSION, dcmds, walkers
306*0Sstevel@tonic-gate };
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gate const mdb_modinfo_t *
_mdb_init(void)309*0Sstevel@tonic-gate _mdb_init(void)
310*0Sstevel@tonic-gate {
311*0Sstevel@tonic-gate GElf_Sym sym;
312*0Sstevel@tonic-gate
313*0Sstevel@tonic-gate if (mdb_lookup_by_obj("ptm", "ptmwint", &sym) == 0)
314*0Sstevel@tonic-gate mdb_qops_install(&ptm_qops, (uintptr_t)sym.st_value);
315*0Sstevel@tonic-gate if (mdb_lookup_by_obj("pts", "ptswint", &sym) == 0)
316*0Sstevel@tonic-gate mdb_qops_install(&pts_qops, (uintptr_t)sym.st_value);
317*0Sstevel@tonic-gate
318*0Sstevel@tonic-gate return (&modinfo);
319*0Sstevel@tonic-gate }
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate void
_mdb_fini(void)322*0Sstevel@tonic-gate _mdb_fini(void)
323*0Sstevel@tonic-gate {
324*0Sstevel@tonic-gate GElf_Sym sym;
325*0Sstevel@tonic-gate
326*0Sstevel@tonic-gate if (mdb_lookup_by_obj("ptm", "ptmwint", &sym) == 0)
327*0Sstevel@tonic-gate mdb_qops_remove(&ptm_qops, (uintptr_t)sym.st_value);
328*0Sstevel@tonic-gate if (mdb_lookup_by_obj("pts", "ptswint", &sym) == 0)
329*0Sstevel@tonic-gate mdb_qops_remove(&pts_qops, (uintptr_t)sym.st_value);
330*0Sstevel@tonic-gate }
331