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