xref: /titanic_50/usr/src/cmd/mdb/demo/README (revision 77b938d5c5dbaf1d833182acdb9de0fc1782fcba)
17c478bd9Sstevel@tonic-gate#
27c478bd9Sstevel@tonic-gate# CDDL HEADER START
37c478bd9Sstevel@tonic-gate#
47c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate# with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate
23*77b938d5Svb160487Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*77b938d5Svb160487Use is subject to license terms.
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate1. Introduction
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gateThis directory contains source code for sample debugger modules for the Modular
317c478bd9Sstevel@tonic-gateDebugger (MDB).  These modules demonstrate how developers can use the MDB
327c478bd9Sstevel@tonic-gateprogramming API to extend the capabilities of MDB itself.  MDB is an extensible
337c478bd9Sstevel@tonic-gateutility for low-level debugging and editing of the live operating system,
347c478bd9Sstevel@tonic-gateoperating system crash dumps, user processes, user process core dumps, and
357c478bd9Sstevel@tonic-gateobject files.  For a more detailed description of MDB features and documentation
367c478bd9Sstevel@tonic-gatefor the MDB programming API, refer to the manual, "Solaris Modular Debugger
37*77b938d5Svb160487Guide".  This document is available on-line at http://docs.sun.com.
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate2. Configuration
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gateAs the files in this directory are owned by the administrator, you should make
427c478bd9Sstevel@tonic-gatea copy of this directory to your home directory or other location before you
437c478bd9Sstevel@tonic-gatebegin experimenting with MDB.  If you wish to change the configuration, edit
44*77b938d5Svb160487the CC and LINT macro definitions in Makefile.sparc, Makefile.sparcv9,
45*77b938d5Svb160487Makefile.i386 and Makefile.amd64 to point to the appropriate pathnames.
46*77b938d5Svb160487The Makefiles contained in this directory are set up to use the C compiler (cc)
47*77b938d5Svb160487and lint utility found in your $PATH.  These four Makefiles can also be used
48*77b938d5Svb160487to define base compiler settings for the corresponding instruction set
49*77b938d5Svb160487architecture (ISA):
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate	Makefile.sparc		- rules for building 32-bit SPARC objects
527c478bd9Sstevel@tonic-gate	Makefile.sparcv9	- rules for building 64-bit SPARC objects
53*77b938d5Svb160487	Makefile.i386		- rules for building 32-bit x86 objects
54*77b938d5Svb160487	Makefile.amd64		- rules for building 64-bit x86 objects
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gateThe Makefile.common file adds common compiler and linker flags to these base
577c478bd9Sstevel@tonic-gatedefinitions, and defines the rules for building the example modules.  You will
587c478bd9Sstevel@tonic-gatenot need to change any of the definitions here in order to build the examples.
597c478bd9Sstevel@tonic-gateIf you wish to construct additional modules of your own, edit the MODULES macro
607c478bd9Sstevel@tonic-gateat the top of Makefile.common.  For example, if you create a new module source
617c478bd9Sstevel@tonic-gatefile common/mymodule.c, you should change:
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate<	MODULES = example1.so example2.so
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gateto:
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate>	MODULES = example1.so example2.so mymodule.so
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gateand then execute "make".
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate3. Targets
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gateThe Makefile in this directory supports the following targets:
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate	make all (default)	- build all modules for the current machine
767c478bd9Sstevel@tonic-gate	make clean		- remove object files from build directories
777c478bd9Sstevel@tonic-gate	make clean.lint		- remove lint files from build directories
787c478bd9Sstevel@tonic-gate	make clobber		- remove objects, modules, and lint files
797c478bd9Sstevel@tonic-gate	make lint		- run lint against each example module
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gateTo build the example modules, execute "make" in this directory.  This will
827c478bd9Sstevel@tonic-gateexecute the default "make all" target.
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate4. Loading Modules
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gateAfter you successfully compile the example modules, the module object files
87*77b938d5Svb160487reside in one or more of the i386/, amd64/, sparc/, and sparcv9/ subdirectories
887c478bd9Sstevel@tonic-gatedepending on the ISAs supported on your machine.  In order to load the example
897c478bd9Sstevel@tonic-gatemodules, you can either use the ::load built-in dcmd with the absolute pathname
907c478bd9Sstevel@tonic-gateof a given module, or you can adjust the module library path to include the
917c478bd9Sstevel@tonic-gatedirectory where your modules are located.  This can be done using the ::set -L
927c478bd9Sstevel@tonic-gatebuilt-in dcmd.  For example:
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate	> ::set -L %o:/usr/demo/mdb/%i
957c478bd9Sstevel@tonic-gate	> ::load example1
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gateThe %o token expands to the old value of the path.  The %i token expands to
987c478bd9Sstevel@tonic-gatethe appropriate ISA name.  You can restore this setting each time you use
997c478bd9Sstevel@tonic-gateMDB by adding the ::set directive to your $HOME/.mdbrc file.  This file, if
1007c478bd9Sstevel@tonic-gatepresent, is processed automatically each time you start the debugger.
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate5. Example 1: Echo and Vmstat
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gateThe first example module provides the source code for two example loadable
1057c478bd9Sstevel@tonic-gatedcmds.  ::simple_echo is a command to echo back its arguments, similar to
1067c478bd9Sstevel@tonic-gate/usr/bin/echo or MDB's built-in ::echo dcmd.  ::vminfo is a command to read
1077c478bd9Sstevel@tonic-gateand print the kernel's global virtual memory statistics structure.  This
1087c478bd9Sstevel@tonic-gateexample introduces the basic structure of an MDB module and demonstrates some
1097c478bd9Sstevel@tonic-gatesimple argument processing.  In order to use ::vminfo, you will need to apply
1107c478bd9Sstevel@tonic-gateMDB to a crash dump of your system, or to the live kernel.  To apply MDB to a
1117c478bd9Sstevel@tonic-gatecrash dump, you might execute:
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate	$ mdb unix.0 vmcore.0
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gateTo apply MDB to the live kernel, become super-user and then execute:
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate	# mdb -k
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate6. Example 2: Proc Walker and PS
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gateThe second example module provides a more realistic example of something you
1227c478bd9Sstevel@tonic-gatemight want to do with MDB: print a formatted table of active processes,
1237c478bd9Sstevel@tonic-gatesimilar to the /usr/bin/ps command or MDB's ::ps dcmd.  This example
1247c478bd9Sstevel@tonic-gateintroduces the concept of a walker, a set of functions which describe how to
1257c478bd9Sstevel@tonic-gateiterate over a data structure, and them demonstrates how the ::simple_ps
1267c478bd9Sstevel@tonic-gatedcmd can be built using this walker.  Using the simple_proc walker, you can
1277c478bd9Sstevel@tonic-gateobtain a listing of kernel proc_t addresses:
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate	> ::load example2
1307c478bd9Sstevel@tonic-gate	> ::walk simple_proc
1317c478bd9Sstevel@tonic-gate	71690a80
1327c478bd9Sstevel@tonic-gate	7168ee40
1337c478bd9Sstevel@tonic-gate	71611898
1347c478bd9Sstevel@tonic-gate	[ ... ]
1357c478bd9Sstevel@tonic-gate	7103b178
1367c478bd9Sstevel@tonic-gate	7103b888
1377c478bd9Sstevel@tonic-gate	1041ce20
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gateUsing the ::simple_ps dcmd you can obtain a formatted listing of processes:
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate	> ::simple_ps
1427c478bd9Sstevel@tonic-gate	PID COMM
1437c478bd9Sstevel@tonic-gate	285 sh
1447c478bd9Sstevel@tonic-gate	271 mibiisa
1457c478bd9Sstevel@tonic-gate	269 ttymon
1467c478bd9Sstevel@tonic-gate	[ ... ]
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate7. Packaging and Installation
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gateIf you are a software developer, you may wish to develop and deliver MDB
1517c478bd9Sstevel@tonic-gatemodules along with your software products in order to facilitate analysis
1527c478bd9Sstevel@tonic-gateof software problems at customer sites.  Your completed MDB modules should
1537c478bd9Sstevel@tonic-gatebe packaged along with your software and delivered into the appropriate
1547c478bd9Sstevel@tonic-gateMDB module directory.  For kernel debugging modules, your module should
1557c478bd9Sstevel@tonic-gatebe delivered in one of the following directories:
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate	/usr/lib/mdb/kvm
1587c478bd9Sstevel@tonic-gate	/usr/platform/`uname -i`/lib/mdb/kvm
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gateand should be named after your kernel module.  For example, the "ip" kernel
1617c478bd9Sstevel@tonic-gatemodule has a debugging module named "ip.so".
162