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