xref: /illumos-gate/usr/src/cmd/mdb/demo/common/example1.c (revision d227ea68051b5404410ee1880d0bba1ebb33a4cc)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1998-1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <sys/mdb_modapi.h>
28 #include <sys/sysinfo.h>
29 
30 /*
31  * simple_echo dcmd - Demonstrate some simple argument processing by iterating
32  * through the argument array and printing back each argument.
33  */
34 static int
35 simple_echo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
36 {
37 	if (flags & DCMD_ADDRSPEC)
38 		mdb_printf("You specified address %p\n", addr);
39 	else
40 		mdb_printf("Current address is %p\n", addr);
41 
42 	while (argc-- != 0) {
43 		if (argv->a_type == MDB_TYPE_STRING)
44 			mdb_printf("%s ", argv->a_un.a_str);
45 		else
46 			mdb_printf("%llr ", argv->a_un.a_val);
47 		argv++;
48 	}
49 
50 	mdb_printf("\n");
51 	return (DCMD_OK);
52 }
53 
54 /*
55  * vminfo dcmd - Print out the global vminfo structure, nicely formatted.
56  * This function illustrates one of the functions for reading data from
57  * the target program (or core file): mdb_readvar().  The vminfo_t
58  * structure contains cumulative counters for various system virtual
59  * memory statistics.  Each second, these are incremented by the current
60  * values of freemem and the other corresponding statistical counters.
61  */
62 /*ARGSUSED*/
63 static int
64 vminfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
65 {
66 	vminfo_t vm;
67 
68 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
69 		return (DCMD_USAGE);
70 
71 	if (mdb_readvar(&vm, "vminfo") == -1) {
72 		/*
73 		 * If the mdb_warn string does not end in a \n, mdb will
74 		 * automatically append the reason for the failure.
75 		 */
76 		mdb_warn("failed to read vminfo structure");
77 		return (DCMD_ERR);
78 	}
79 
80 	mdb_printf("Cumulative memory statistics:\n");
81 	mdb_printf("%8llu pages of free memory\n", vm.freemem);
82 	mdb_printf("%8llu pages of reserved swap\n", vm.swap_resv);
83 	mdb_printf("%8llu pages of allocated swap\n", vm.swap_alloc);
84 	mdb_printf("%8llu pages of unreserved swap\n", vm.swap_avail);
85 	mdb_printf("%8llu pages of unallocated swap\n", vm.swap_free);
86 
87 	return (DCMD_OK);
88 }
89 
90 /*
91  * MDB module linkage information:
92  *
93  * We declare a list of structures describing our dcmds, and a function
94  * named _mdb_init to return a pointer to our module information.
95  */
96 
97 static const mdb_dcmd_t dcmds[] = {
98 	{ "simple_echo", "[ args ... ]", "echo back arguments", simple_echo },
99 	{ "vminfo", NULL, "print vm information", vminfo },
100 	{ NULL }
101 };
102 
103 static const mdb_modinfo_t modinfo = {
104 	MDB_API_VERSION, dcmds, NULL
105 };
106 
107 const mdb_modinfo_t *
108 _mdb_init(void)
109 {
110 	return (&modinfo);
111 }
112