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