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