|
root / base / usr / src / cmd / mdb / demo / common
common Plain Text 269 lines 7.3 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1998-1999 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#include <sys/mdb_modapi.h>
#include <sys/sysinfo.h>

/*
 * simple_echo dcmd - Demonstrate some simple argument processing by iterating
 * through the argument array and printing back each argument.
 */
static int
simple_echo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	if (flags & DCMD_ADDRSPEC)
		mdb_printf("You specified address %p\n", addr);
	else
		mdb_printf("Current address is %p\n", addr);

	while (argc-- != 0) {
		if (argv->a_type == MDB_TYPE_STRING)
			mdb_printf("%s ", argv->a_un.a_str);
		else
			mdb_printf("%llr ", argv->a_un.a_val);
		argv++;
	}

	mdb_printf("\n");
	return (DCMD_OK);
}

/*
 * vminfo dcmd - Print out the global vminfo structure, nicely formatted.
 * This function illustrates one of the functions for reading data from
 * the target program (or core file): mdb_readvar().  The vminfo_t
 * structure contains cumulative counters for various system virtual
 * memory statistics.  Each second, these are incremented by the current
 * values of freemem and the other corresponding statistical counters.
 */
/*ARGSUSED*/
static int
vminfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	vminfo_t vm;

	if ((flags & DCMD_ADDRSPEC) || argc != 0)
		return (DCMD_USAGE);

	if (mdb_readvar(&vm, "vminfo") == -1) {
		/*
		 * If the mdb_warn string does not end in a \n, mdb will
		 * automatically append the reason for the failure.
		 */
		mdb_warn("failed to read vminfo structure");
		return (DCMD_ERR);
	}

	mdb_printf("Cumulative memory statistics:\n");
	mdb_printf("%8llu pages of free memory\n", vm.freemem);
	mdb_printf("%8llu pages of reserved swap\n", vm.swap_resv);
	mdb_printf("%8llu pages of allocated swap\n", vm.swap_alloc);
	mdb_printf("%8llu pages of unreserved swap\n", vm.swap_avail);
	mdb_printf("%8llu pages of unallocated swap\n", vm.swap_free);

	return (DCMD_OK);
}

/*
 * MDB module linkage information:
 *
 * We declare a list of structures describing our dcmds, and a function
 * named _mdb_init to return a pointer to our module information.
 */

static const mdb_dcmd_t dcmds[] = {
	{ "simple_echo", "[ args ... ]", "echo back arguments", simple_echo },
	{ "vminfo", NULL, "print vm information", vminfo },
	{ NULL }
};

static const mdb_modinfo_t modinfo = {
	MDB_API_VERSION, dcmds, NULL
};

const mdb_modinfo_t *
_mdb_init(void)
{
	return (&modinfo);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1998-1999 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#include <sys/mdb_modapi.h>
#include <sys/proc.h>

/*
 * Initialize the proc_t walker by either using the given starting address,
 * or reading the value of the kernel's practive pointer.  We also allocate
 * a proc_t for storage, and save this using the walk_data pointer.
 */
static int
sp_walk_init(mdb_walk_state_t *wsp)
{
	if (wsp->walk_addr == NULL &&
	    mdb_readvar(&wsp->walk_addr, "practive") == -1) {
		mdb_warn("failed to read 'practive'");
		return (WALK_ERR);
	}

	wsp->walk_data = mdb_alloc(sizeof (proc_t), UM_SLEEP);
	return (WALK_NEXT);
}

/*
 * At each step, read a proc_t into our private storage, and then invoke
 * the callback function.  We terminate when we reach a NULL p_next pointer.
 */
static int
sp_walk_step(mdb_walk_state_t *wsp)
{
	int status;

	if (wsp->walk_addr == NULL)
		return (WALK_DONE);

	if (mdb_vread(wsp->walk_data, sizeof (proc_t), wsp->walk_addr) == -1) {
		mdb_warn("failed to read proc at %p", wsp->walk_addr);
		return (WALK_DONE);
	}

	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
	    wsp->walk_cbdata);

	wsp->walk_addr = (uintptr_t)(((proc_t *)wsp->walk_data)->p_next);
	return (status);
}

/*
 * The walker's fini function is invoked at the end of each walk.  Since we
 * dynamically allocated a proc_t in sp_walk_init, we must free it now.
 */
static void
sp_walk_fini(mdb_walk_state_t *wsp)
{
	mdb_free(wsp->walk_data, sizeof (proc_t));
}

static int
simple_ps(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	struct pid pid;
	proc_t p;

	if (argc != 0)
		return (DCMD_USAGE);

	/*
	 * If no proc_t address was specified on the command line, we can
	 * print out all processes by invoking the walker, using this
	 * dcmd itself as the callback.
	 */
	if (!(flags & DCMD_ADDRSPEC)) {
		if (mdb_walk_dcmd("simple_proc", "simple_ps",
		    argc, argv) == -1) {
			mdb_warn("failed to walk 'simple_proc'");
			return (DCMD_ERR);
		}
		return (DCMD_OK);
	}

	/*
	 * If this is the first invocation of the command, print a nice
	 * header line for the output that will follow.
	 */
	if (DCMD_HDRSPEC(flags))
		mdb_printf("%5s %s\n", "PID", "COMM");

	/*
	 * For each process, we just need to read the proc_t struct, read
	 * the pid struct addressed by p_pidp, and then print out the pid
	 * and the command name.
	 */
	if (mdb_vread(&p, sizeof (p), addr) == sizeof (p)) {

		if (mdb_vread(&pid, sizeof (pid),
		    (uintptr_t)p.p_pidp) == sizeof (pid))
			mdb_printf("%5d %s\n", pid.pid_id, p.p_user.u_comm);
		else
			mdb_warn("failed to read struct pid at %p", p.p_pidp);
	} else
		mdb_warn("failed to read process at %p", addr);

	return (DCMD_OK);
}

/*
 * MDB module linkage information:
 *
 * We declare a list of structures describing our dcmds, a list of structures
 * describing our walkers, and a function named _mdb_init to return a pointer
 * to our module information.
 */

static const mdb_dcmd_t dcmds[] = {
	{ "simple_ps", NULL, "simple process list", simple_ps },
	{ NULL }
};

static const mdb_walker_t walkers[] = {
	{ "simple_proc", "walk list of proc_t structures",
		sp_walk_init, sp_walk_step, sp_walk_fini },
	{ NULL }
};

static const mdb_modinfo_t modinfo = {
	MDB_API_VERSION, dcmds, walkers
};

const mdb_modinfo_t *
_mdb_init(void)
{
	return (&modinfo);
}