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