xref: /onnv-gate/usr/src/cmd/mdb/demo/README (revision 853:7c6224b571b5)
10Sstevel@tonic-gate#
20Sstevel@tonic-gate# CDDL HEADER START
30Sstevel@tonic-gate#
40Sstevel@tonic-gate# The contents of this file are subject to the terms of the
50Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate# with the License.
80Sstevel@tonic-gate#
90Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate# See the License for the specific language governing permissions
120Sstevel@tonic-gate# and limitations under the License.
130Sstevel@tonic-gate#
140Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate#
200Sstevel@tonic-gate# CDDL HEADER END
210Sstevel@tonic-gate#
220Sstevel@tonic-gate
23*853Svb160487Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*853Svb160487Use is subject to license terms.
250Sstevel@tonic-gate
260Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate1. Introduction
290Sstevel@tonic-gate
300Sstevel@tonic-gateThis directory contains source code for sample debugger modules for the Modular
310Sstevel@tonic-gateDebugger (MDB).  These modules demonstrate how developers can use the MDB
320Sstevel@tonic-gateprogramming API to extend the capabilities of MDB itself.  MDB is an extensible
330Sstevel@tonic-gateutility for low-level debugging and editing of the live operating system,
34*853Svb160487operating system crash dumps, user processes, user process core dumps, and
350Sstevel@tonic-gateobject files.  For a more detailed description of MDB features and documentation
360Sstevel@tonic-gatefor the MDB programming API, refer to the manual, "Solaris Modular Debugger
37*853Svb160487Guide".  This document is available on-line at http://docs.sun.com.
380Sstevel@tonic-gate
390Sstevel@tonic-gate2. Configuration
400Sstevel@tonic-gate
410Sstevel@tonic-gateAs the files in this directory are owned by the administrator, you should make
420Sstevel@tonic-gatea copy of this directory to your home directory or other location before you
430Sstevel@tonic-gatebegin experimenting with MDB.  If you wish to change the configuration, edit
44*853Svb160487the CC and LINT macro definitions in Makefile.sparc, Makefile.sparcv9,
45*853Svb160487Makefile.i386 and Makefile.amd64 to point to the appropriate pathnames.
46*853Svb160487The Makefiles contained in this directory are set up to use the C compiler (cc)
47*853Svb160487and lint utility found in your $PATH.  These four Makefiles can also be used
48*853Svb160487to define base compiler settings for the corresponding instruction set
49*853Svb160487architecture (ISA):
500Sstevel@tonic-gate
510Sstevel@tonic-gate	Makefile.sparc		- rules for building 32-bit SPARC objects
520Sstevel@tonic-gate	Makefile.sparcv9	- rules for building 64-bit SPARC objects
53*853Svb160487	Makefile.i386		- rules for building 32-bit x86 objects
54*853Svb160487	Makefile.amd64		- rules for building 64-bit x86 objects
550Sstevel@tonic-gate
560Sstevel@tonic-gateThe Makefile.common file adds common compiler and linker flags to these base
570Sstevel@tonic-gatedefinitions, and defines the rules for building the example modules.  You will
580Sstevel@tonic-gatenot need to change any of the definitions here in order to build the examples.
590Sstevel@tonic-gateIf you wish to construct additional modules of your own, edit the MODULES macro
600Sstevel@tonic-gateat the top of Makefile.common.  For example, if you create a new module source
610Sstevel@tonic-gatefile common/mymodule.c, you should change:
620Sstevel@tonic-gate
630Sstevel@tonic-gate<	MODULES = example1.so example2.so
640Sstevel@tonic-gate
650Sstevel@tonic-gateto:
660Sstevel@tonic-gate
670Sstevel@tonic-gate>	MODULES = example1.so example2.so mymodule.so
680Sstevel@tonic-gate
690Sstevel@tonic-gateand then execute "make".
700Sstevel@tonic-gate
710Sstevel@tonic-gate3. Targets
720Sstevel@tonic-gate
730Sstevel@tonic-gateThe Makefile in this directory supports the following targets:
740Sstevel@tonic-gate
750Sstevel@tonic-gate	make all (default)	- build all modules for the current machine
760Sstevel@tonic-gate	make clean		- remove object files from build directories
770Sstevel@tonic-gate	make clean.lint		- remove lint files from build directories
780Sstevel@tonic-gate	make clobber		- remove objects, modules, and lint files
790Sstevel@tonic-gate	make lint		- run lint against each example module
800Sstevel@tonic-gate
810Sstevel@tonic-gateTo build the example modules, execute "make" in this directory.  This will
820Sstevel@tonic-gateexecute the default "make all" target.
830Sstevel@tonic-gate
840Sstevel@tonic-gate4. Loading Modules
850Sstevel@tonic-gate
860Sstevel@tonic-gateAfter you successfully compile the example modules, the module object files
87*853Svb160487reside in one or more of the i386/, amd64/, sparc/, and sparcv9/ subdirectories
880Sstevel@tonic-gatedepending on the ISAs supported on your machine.  In order to load the example
890Sstevel@tonic-gatemodules, you can either use the ::load built-in dcmd with the absolute pathname
900Sstevel@tonic-gateof a given module, or you can adjust the module library path to include the
910Sstevel@tonic-gatedirectory where your modules are located.  This can be done using the ::set -L
920Sstevel@tonic-gatebuilt-in dcmd.  For example:
930Sstevel@tonic-gate
940Sstevel@tonic-gate	> ::set -L %o:/usr/demo/mdb/%i
950Sstevel@tonic-gate	> ::load example1
960Sstevel@tonic-gate
970Sstevel@tonic-gateThe %o token expands to the old value of the path.  The %i token expands to
980Sstevel@tonic-gatethe appropriate ISA name.  You can restore this setting each time you use
990Sstevel@tonic-gateMDB by adding the ::set directive to your $HOME/.mdbrc file.  This file, if
1000Sstevel@tonic-gatepresent, is processed automatically each time you start the debugger.
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate5. Example 1: Echo and Vmstat
1030Sstevel@tonic-gate
1040Sstevel@tonic-gateThe first example module provides the source code for two example loadable
1050Sstevel@tonic-gatedcmds.  ::simple_echo is a command to echo back its arguments, similar to
1060Sstevel@tonic-gate/usr/bin/echo or MDB's built-in ::echo dcmd.  ::vminfo is a command to read
1070Sstevel@tonic-gateand print the kernel's global virtual memory statistics structure.  This
1080Sstevel@tonic-gateexample introduces the basic structure of an MDB module and demonstrates some
1090Sstevel@tonic-gatesimple argument processing.  In order to use ::vminfo, you will need to apply
1100Sstevel@tonic-gateMDB to a crash dump of your system, or to the live kernel.  To apply MDB to a
1110Sstevel@tonic-gatecrash dump, you might execute:
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate	$ mdb unix.0 vmcore.0
1140Sstevel@tonic-gate
1150Sstevel@tonic-gateTo apply MDB to the live kernel, become super-user and then execute:
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate	# mdb -k
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate6. Example 2: Proc Walker and PS
1200Sstevel@tonic-gate
1210Sstevel@tonic-gateThe second example module provides a more realistic example of something you
1220Sstevel@tonic-gatemight want to do with MDB: print a formatted table of active processes,
1230Sstevel@tonic-gatesimilar to the /usr/bin/ps command or MDB's ::ps dcmd.  This example
1240Sstevel@tonic-gateintroduces the concept of a walker, a set of functions which describe how to
1250Sstevel@tonic-gateiterate over a data structure, and them demonstrates how the ::simple_ps
1260Sstevel@tonic-gatedcmd can be built using this walker.  Using the simple_proc walker, you can
1270Sstevel@tonic-gateobtain a listing of kernel proc_t addresses:
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate	> ::load example2
1300Sstevel@tonic-gate	> ::walk simple_proc
1310Sstevel@tonic-gate	71690a80
1320Sstevel@tonic-gate	7168ee40
1330Sstevel@tonic-gate	71611898
1340Sstevel@tonic-gate	[ ... ]
1350Sstevel@tonic-gate	7103b178
1360Sstevel@tonic-gate	7103b888
1370Sstevel@tonic-gate	1041ce20
1380Sstevel@tonic-gate
1390Sstevel@tonic-gateUsing the ::simple_ps dcmd you can obtain a formatted listing of processes:
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate	> ::simple_ps
1420Sstevel@tonic-gate	PID COMM
1430Sstevel@tonic-gate	285 sh
1440Sstevel@tonic-gate	271 mibiisa
1450Sstevel@tonic-gate	269 ttymon
1460Sstevel@tonic-gate	[ ... ]
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate7. Packaging and Installation
1490Sstevel@tonic-gate
1500Sstevel@tonic-gateIf you are a software developer, you may wish to develop and deliver MDB
1510Sstevel@tonic-gatemodules along with your software products in order to facilitate analysis
1520Sstevel@tonic-gateof software problems at customer sites.  Your completed MDB modules should
1530Sstevel@tonic-gatebe packaged along with your software and delivered into the appropriate
1540Sstevel@tonic-gateMDB module directory.  For kernel debugging modules, your module should
1550Sstevel@tonic-gatebe delivered in one of the following directories:
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate	/usr/lib/mdb/kvm
1580Sstevel@tonic-gate	/usr/platform/`uname -i`/lib/mdb/kvm
1590Sstevel@tonic-gate
1600Sstevel@tonic-gateand should be named after your kernel module.  For example, the "ip" kernel
1610Sstevel@tonic-gatemodule has a debugging module named "ip.so".
162