xref: /illumos-gate/usr/src/cmd/mdb/demo/common/example2.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <sys/mdb_modapi.h>
28*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Initialize the proc_t walker by either using the given starting address,
32*7c478bd9Sstevel@tonic-gate  * or reading the value of the kernel's practive pointer.  We also allocate
33*7c478bd9Sstevel@tonic-gate  * a proc_t for storage, and save this using the walk_data pointer.
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate static int
sp_walk_init(mdb_walk_state_t * wsp)36*7c478bd9Sstevel@tonic-gate sp_walk_init(mdb_walk_state_t *wsp)
37*7c478bd9Sstevel@tonic-gate {
38*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL &&
39*7c478bd9Sstevel@tonic-gate 	    mdb_readvar(&wsp->walk_addr, "practive") == -1) {
40*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'practive'");
41*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
42*7c478bd9Sstevel@tonic-gate 	}
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = mdb_alloc(sizeof (proc_t), UM_SLEEP);
45*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
46*7c478bd9Sstevel@tonic-gate }
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate  * At each step, read a proc_t into our private storage, and then invoke
50*7c478bd9Sstevel@tonic-gate  * the callback function.  We terminate when we reach a NULL p_next pointer.
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate static int
sp_walk_step(mdb_walk_state_t * wsp)53*7c478bd9Sstevel@tonic-gate sp_walk_step(mdb_walk_state_t *wsp)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	int status;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
58*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(wsp->walk_data, sizeof (proc_t), wsp->walk_addr) == -1) {
61*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read proc at %p", wsp->walk_addr);
62*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
63*7c478bd9Sstevel@tonic-gate 	}
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
66*7c478bd9Sstevel@tonic-gate 	    wsp->walk_cbdata);
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)(((proc_t *)wsp->walk_data)->p_next);
69*7c478bd9Sstevel@tonic-gate 	return (status);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate  * The walker's fini function is invoked at the end of each walk.  Since we
74*7c478bd9Sstevel@tonic-gate  * dynamically allocated a proc_t in sp_walk_init, we must free it now.
75*7c478bd9Sstevel@tonic-gate  */
76*7c478bd9Sstevel@tonic-gate static void
sp_walk_fini(mdb_walk_state_t * wsp)77*7c478bd9Sstevel@tonic-gate sp_walk_fini(mdb_walk_state_t *wsp)
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (proc_t));
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate static int
simple_ps(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)83*7c478bd9Sstevel@tonic-gate simple_ps(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	struct pid pid;
86*7c478bd9Sstevel@tonic-gate 	proc_t p;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	if (argc != 0)
89*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	/*
92*7c478bd9Sstevel@tonic-gate 	 * If no proc_t address was specified on the command line, we can
93*7c478bd9Sstevel@tonic-gate 	 * print out all processes by invoking the walker, using this
94*7c478bd9Sstevel@tonic-gate 	 * dcmd itself as the callback.
95*7c478bd9Sstevel@tonic-gate 	 */
96*7c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
97*7c478bd9Sstevel@tonic-gate 		if (mdb_walk_dcmd("simple_proc", "simple_ps",
98*7c478bd9Sstevel@tonic-gate 		    argc, argv) == -1) {
99*7c478bd9Sstevel@tonic-gate 			mdb_warn("failed to walk 'simple_proc'");
100*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
101*7c478bd9Sstevel@tonic-gate 		}
102*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	/*
106*7c478bd9Sstevel@tonic-gate 	 * If this is the first invocation of the command, print a nice
107*7c478bd9Sstevel@tonic-gate 	 * header line for the output that will follow.
108*7c478bd9Sstevel@tonic-gate 	 */
109*7c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
110*7c478bd9Sstevel@tonic-gate 		mdb_printf("%5s %s\n", "PID", "COMM");
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	/*
113*7c478bd9Sstevel@tonic-gate 	 * For each process, we just need to read the proc_t struct, read
114*7c478bd9Sstevel@tonic-gate 	 * the pid struct addressed by p_pidp, and then print out the pid
115*7c478bd9Sstevel@tonic-gate 	 * and the command name.
116*7c478bd9Sstevel@tonic-gate 	 */
117*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&p, sizeof (p), addr) == sizeof (p)) {
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(&pid, sizeof (pid),
120*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)p.p_pidp) == sizeof (pid))
121*7c478bd9Sstevel@tonic-gate 			mdb_printf("%5d %s\n", pid.pid_id, p.p_user.u_comm);
122*7c478bd9Sstevel@tonic-gate 		else
123*7c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read struct pid at %p", p.p_pidp);
124*7c478bd9Sstevel@tonic-gate 	} else
125*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read process at %p", addr);
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate /*
131*7c478bd9Sstevel@tonic-gate  * MDB module linkage information:
132*7c478bd9Sstevel@tonic-gate  *
133*7c478bd9Sstevel@tonic-gate  * We declare a list of structures describing our dcmds, a list of structures
134*7c478bd9Sstevel@tonic-gate  * describing our walkers, and a function named _mdb_init to return a pointer
135*7c478bd9Sstevel@tonic-gate  * to our module information.
136*7c478bd9Sstevel@tonic-gate  */
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
139*7c478bd9Sstevel@tonic-gate 	{ "simple_ps", NULL, "simple process list", simple_ps },
140*7c478bd9Sstevel@tonic-gate 	{ NULL }
141*7c478bd9Sstevel@tonic-gate };
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
144*7c478bd9Sstevel@tonic-gate 	{ "simple_proc", "walk list of proc_t structures",
145*7c478bd9Sstevel@tonic-gate 		sp_walk_init, sp_walk_step, sp_walk_fini },
146*7c478bd9Sstevel@tonic-gate 	{ NULL }
147*7c478bd9Sstevel@tonic-gate };
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate static const mdb_modinfo_t modinfo = {
150*7c478bd9Sstevel@tonic-gate 	MDB_API_VERSION, dcmds, walkers
151*7c478bd9Sstevel@tonic-gate };
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate const mdb_modinfo_t *
_mdb_init(void)154*7c478bd9Sstevel@tonic-gate _mdb_init(void)
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	return (&modinfo);
157*7c478bd9Sstevel@tonic-gate }
158