8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / mdb / demo / common / example2.c
blob6c7ae07a411f7e2ece01968159e5295f2488f12b
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 1998-1999 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/mdb_modapi.h>
30 #include <sys/proc.h>
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.
37 static int
38 sp_walk_init(mdb_walk_state_t *wsp)
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);
46 wsp->walk_data = mdb_alloc(sizeof (proc_t), UM_SLEEP);
47 return (WALK_NEXT);
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.
54 static int
55 sp_walk_step(mdb_walk_state_t *wsp)
57 int status;
59 if (wsp->walk_addr == NULL)
60 return (WALK_DONE);
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);
67 status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
68 wsp->walk_cbdata);
70 wsp->walk_addr = (uintptr_t)(((proc_t *)wsp->walk_data)->p_next);
71 return (status);
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.
78 static void
79 sp_walk_fini(mdb_walk_state_t *wsp)
81 mdb_free(wsp->walk_data, sizeof (proc_t));
84 static int
85 simple_ps(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
87 struct pid pid;
88 proc_t p;
90 if (argc != 0)
91 return (DCMD_USAGE);
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.
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);
104 return (DCMD_OK);
108 * If this is the first invocation of the command, print a nice
109 * header line for the output that will follow.
111 if (DCMD_HDRSPEC(flags))
112 mdb_printf("%5s %s\n", "PID", "COMM");
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.
119 if (mdb_vread(&p, sizeof (p), addr) == sizeof (p)) {
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);
129 return (DCMD_OK);
133 * MDB module linkage information:
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.
140 static const mdb_dcmd_t dcmds[] = {
141 { "simple_ps", NULL, "simple process list", simple_ps },
142 { NULL }
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 }
151 static const mdb_modinfo_t modinfo = {
152 MDB_API_VERSION, dcmds, walkers
155 const mdb_modinfo_t *
156 _mdb_init(void)
158 return (&modinfo);