xref: /illumos-gate/usr/src/cmd/mdb/common/modules/sockfs/sockfs.c (revision 3b4422300b98a1be9831b52861a41c8db9440a2d)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2019 Joyent, Inc.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/stropts.h>
30 #include <sys/socket.h>
31 #include <sys/socketvar.h>
32 
33 #include <mdb/mdb_modapi.h>
34 #include <mdb/mdb_ks.h>
35 
36 /*
37  * Look up the symbol name for the given sockparams list and walk
38  * all the entries.
39  */
40 static boolean_t
41 sockparams_walk_list(const char *symname, int argc, const mdb_arg_t *argv)
42 {
43 	GElf_Sym sym;
44 
45 	if (mdb_lookup_by_name(symname, &sym)) {
46 		mdb_warn("can't find symbol %s", symname);
47 		return (B_FALSE);
48 	}
49 
50 	if (mdb_pwalk_dcmd("list", "sockfs`sockparams", argc, argv,
51 	    sym.st_value) != 0) {
52 		mdb_warn("can't walk %s", symname);
53 		return (B_FALSE);
54 	}
55 
56 	return (B_TRUE);
57 }
58 
59 /*
60  * dcmd to print sockparams info.
61  *
62  * If no address is given then the default is to print all sockparams on the
63  * global list (i.e., installed with soconfig(1)). To also print the ephemeral
64  * entries the '-e' flag should be used. Only ephemeral entries can be printed
65  * by specifying the '-E' flag.
66  */
67 static int
68 sockparams_prt(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
69 {
70 	struct sockparams sp;
71 	char strdev[MAXPATHLEN];
72 	char sockmod[MODMAXNAMELEN];
73 
74 	if ((flags & DCMD_ADDRSPEC) == 0) {
75 		uint_t opt_e = 0;
76 		uint_t opt_E = 0;
77 
78 		/*
79 		 * Determine what lists should be printed
80 		 */
81 		if (mdb_getopts(argc, argv,
82 		    'e', MDB_OPT_SETBITS, 1, &opt_e,
83 		    'E', MDB_OPT_SETBITS, 1, &opt_E, NULL) != argc)
84 			return (DCMD_USAGE);
85 
86 		if (!opt_E) {
87 			if (!sockparams_walk_list("sphead", argc, argv))
88 				return (DCMD_ERR);
89 		}
90 
91 		if (opt_e || opt_E) {
92 			if (!sockparams_walk_list("sp_ephem_list", argc, argv))
93 				return (DCMD_ERR);
94 		}
95 
96 		return (DCMD_OK);
97 	}
98 
99 	/*
100 	 * If we are piping the output, then just print out the address,
101 	 * otherwise summarize the sockparams info.
102 	 */
103 	if ((flags & DCMD_PIPE_OUT) != 0) {
104 		mdb_printf("%#lr\n", addr);
105 		return (DCMD_OK);
106 	}
107 
108 	if (DCMD_HDRSPEC(flags)) {
109 		mdb_printf("%-?s %3s %3s %3s %15s %15s %6s %6s\n",
110 		    "ADDR", "FAM", "TYP", "PRO", "STRDEV", "SOCKMOD", "REFS",
111 		    "FLGS");
112 	}
113 
114 	if (mdb_vread(&sp, sizeof (sp), addr) == -1) {
115 		mdb_warn("failed to read sockparams at %0?p", addr);
116 		return (DCMD_ERR);
117 	}
118 
119 	if ((sp.sp_sdev_info.sd_devpath == NULL) ||
120 	    (mdb_readstr(strdev, sizeof (strdev),
121 	    (uintptr_t)sp.sp_sdev_info.sd_devpath) <= 0))
122 		strcpy(strdev, "-");
123 	if (mdb_readstr(sockmod, sizeof (sockmod),
124 	    (uintptr_t)sp.sp_smod_name) <= 0)
125 		strcpy(sockmod, "");
126 
127 	mdb_printf("%0?p %3u %3u %3u %15s %15s %6u %#6x\n",
128 	    addr,
129 	    sp.sp_family, sp.sp_type, sp.sp_protocol,
130 	    strdev, sockmod, sp.sp_refcnt,
131 	    sp.sp_flags);
132 
133 
134 	return (DCMD_OK);
135 }
136 
137 /*
138  * Help function
139  */
140 void
141 sockparams_help(void)
142 {
143 	mdb_printf("Print sockparams information for a give sockparams ptr.\n"
144 	    "Without the address, list available sockparams. Default "
145 	    "behavior is to list only entries that were installed by the "
146 	    "admin (via soconfig(1M)).\n\n"
147 	    "Options:\n"
148 	    "	-e:\t\tlist ephemeral sockparams\n"
149 	    "	-E:\t\tonly list ephemeral sockparams\n");
150 }
151 
152 static const mdb_dcmd_t dcmds[] = {
153 	{ "sockparams", "[-eE]", "print sockparams", sockparams_prt,
154 	    sockparams_help },
155 	{ NULL }
156 };
157 
158 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, NULL };
159 
160 const mdb_modinfo_t *
161 _mdb_init(void)
162 {
163 	return (&modinfo);
164 }
165