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