xref: /illumos-gate/usr/src/cmd/mdb/demo/common/example2.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 (c) 1998-1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <sys/mdb_modapi.h>
28 #include <sys/proc.h>
29 
30 /*
31  * Initialize the proc_t walker by either using the given starting address,
32  * or reading the value of the kernel's practive pointer.  We also allocate
33  * a proc_t for storage, and save this using the walk_data pointer.
34  */
35 static int
36 sp_walk_init(mdb_walk_state_t *wsp)
37 {
38 	if (wsp->walk_addr == NULL &&
39 	    mdb_readvar(&wsp->walk_addr, "practive") == -1) {
40 		mdb_warn("failed to read 'practive'");
41 		return (WALK_ERR);
42 	}
43 
44 	wsp->walk_data = mdb_alloc(sizeof (proc_t), UM_SLEEP);
45 	return (WALK_NEXT);
46 }
47 
48 /*
49  * At each step, read a proc_t into our private storage, and then invoke
50  * the callback function.  We terminate when we reach a NULL p_next pointer.
51  */
52 static int
53 sp_walk_step(mdb_walk_state_t *wsp)
54 {
55 	int status;
56 
57 	if (wsp->walk_addr == NULL)
58 		return (WALK_DONE);
59 
60 	if (mdb_vread(wsp->walk_data, sizeof (proc_t), wsp->walk_addr) == -1) {
61 		mdb_warn("failed to read proc at %p", wsp->walk_addr);
62 		return (WALK_DONE);
63 	}
64 
65 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
66 	    wsp->walk_cbdata);
67 
68 	wsp->walk_addr = (uintptr_t)(((proc_t *)wsp->walk_data)->p_next);
69 	return (status);
70 }
71 
72 /*
73  * The walker's fini function is invoked at the end of each walk.  Since we
74  * dynamically allocated a proc_t in sp_walk_init, we must free it now.
75  */
76 static void
77 sp_walk_fini(mdb_walk_state_t *wsp)
78 {
79 	mdb_free(wsp->walk_data, sizeof (proc_t));
80 }
81 
82 static int
83 simple_ps(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
84 {
85 	struct pid pid;
86 	proc_t p;
87 
88 	if (argc != 0)
89 		return (DCMD_USAGE);
90 
91 	/*
92 	 * If no proc_t address was specified on the command line, we can
93 	 * print out all processes by invoking the walker, using this
94 	 * dcmd itself as the callback.
95 	 */
96 	if (!(flags & DCMD_ADDRSPEC)) {
97 		if (mdb_walk_dcmd("simple_proc", "simple_ps",
98 		    argc, argv) == -1) {
99 			mdb_warn("failed to walk 'simple_proc'");
100 			return (DCMD_ERR);
101 		}
102 		return (DCMD_OK);
103 	}
104 
105 	/*
106 	 * If this is the first invocation of the command, print a nice
107 	 * header line for the output that will follow.
108 	 */
109 	if (DCMD_HDRSPEC(flags))
110 		mdb_printf("%5s %s\n", "PID", "COMM");
111 
112 	/*
113 	 * For each process, we just need to read the proc_t struct, read
114 	 * the pid struct addressed by p_pidp, and then print out the pid
115 	 * and the command name.
116 	 */
117 	if (mdb_vread(&p, sizeof (p), addr) == sizeof (p)) {
118 
119 		if (mdb_vread(&pid, sizeof (pid),
120 		    (uintptr_t)p.p_pidp) == sizeof (pid))
121 			mdb_printf("%5d %s\n", pid.pid_id, p.p_user.u_comm);
122 		else
123 			mdb_warn("failed to read struct pid at %p", p.p_pidp);
124 	} else
125 		mdb_warn("failed to read process at %p", addr);
126 
127 	return (DCMD_OK);
128 }
129 
130 /*
131  * MDB module linkage information:
132  *
133  * We declare a list of structures describing our dcmds, a list of structures
134  * describing our walkers, and a function named _mdb_init to return a pointer
135  * to our module information.
136  */
137 
138 static const mdb_dcmd_t dcmds[] = {
139 	{ "simple_ps", NULL, "simple process list", simple_ps },
140 	{ NULL }
141 };
142 
143 static const mdb_walker_t walkers[] = {
144 	{ "simple_proc", "walk list of proc_t structures",
145 		sp_walk_init, sp_walk_step, sp_walk_fini },
146 	{ NULL }
147 };
148 
149 static const mdb_modinfo_t modinfo = {
150 	MDB_API_VERSION, dcmds, walkers
151 };
152 
153 const mdb_modinfo_t *
154 _mdb_init(void)
155 {
156 	return (&modinfo);
157 }
158