File:  [NetBSD Developer Wiki] / wikisrc / Attic / how_to_enable_and_run_dtrace.mdwn
Revision 1.1: download - view: text, annotated - select for diffs
Thu Jun 9 09:55:17 2011 UTC (10 years, 5 months ago) by wiki
Branches: MAIN
CVS tags: HEAD
web commit by darran

    1: DTrace is a Dynamic Tracing framework developed by Sun and ported to NetBSD. It enables extensive instrumentation of the kernel and user space. See the [DTrace Community Page](http://hub.opensolaris.org/bin/view/Community+Group+dtrace/WebHome) for more information. Also see [DTrace Introduction](http://wikis.sun.com/display/DTrace/Introduction).
    2: 
    3: DTrace is a work-in-progress effort and it is for i386 systems only. Two providers are available; the Statically Defined Tracing (SDT) provider and the Function Boundary Tracer (FBT) provider.
    4: 
    5: ##Building DTrace
    6: 
    7: You need the following options in your kernel:
    8: 
    9:     options         INSECURE
   10:     options         KDTRACE_HOOKS   # DTrace support
   11:     options         MODULAR
   12: 
   13: Note: Make sure "options DEBUG" is not defined. There is currently a problem where the debug symbols overwrite the CTF symbols.  Its not fatal, but your DTrace scripts will not recognize any symbols in the kernel rendering them unusable.
   14: 
   15: You also need to build distribution with the options MKMODULAR=yes and MKDTRACE=yes.
   16: 
   17: ##Running hello world
   18: 
   19: Load the solaris and dtrace modules, and the SDT (Statically Defined Tracing) and FBT (Function Boundary Tracing) modules:
   20: 
   21:     modload solaris
   22:     modload dtrace
   23:     modload sdt
   24:     modload fbt
   25: 
   26: Make the dtrace device node:
   27: 
   28:     mkdir /dev/dtrace
   29:     mknod /dev/dtrace/dtrace c dtrace 0
   30: 
   31: List the dtrace probes
   32: 
   33:     dtrace -l
   34: 
   35:        ID   PROVIDER            MODULE                          FUNCTION NAME
   36:         1     dtrace                                                     BEGIN
   37:         2     dtrace                                                     END
   38:         3     dtrace                                                     ERROR
   39:         4        fbt            netbsd             AcpiAcquireGlobalLock entry
   40:         5        fbt            netbsd             AcpiAcquireGlobalLock return
   41:         6        fbt            netbsd             AcpiAllocateRootTable entry
   42:         7        fbt            netbsd                    AcpiAttachData entry
   43:         . 
   44:         .
   45:     29129        fbt           solaris                   zfs_vop_getattr entry 
   46:     29130        fbt           solaris                   zfs_vop_getattr return
   47:     29131       proc                                                     create
   48:     29132       proc                                                     exec 
   49:         .
   50:         .
   51:     29140       proc                                                     lwp_start
   52:     29141       proc                                                     lwp_exit
   53: 
   54: Put the following into the file hello.d
   55: 
   56:     BEGIN
   57:     {
   58:         trace("Hello world");
   59:         exit(0);
   60:     }
   61: 
   62: Run the hello world script:
   63: 
   64:     dtrace -s hello.d
   65:     
   66:     dtrace: script './hello.d' matched 1 probe
   67:     CPU     ID                    FUNCTION:NAME
   68:       0      1                           :BEGIN   Hello world
   69: 
   70: A more complex example that traces the execution of a sleep operation in the kernel:
   71: 
   72:     #pragma D option flowindent
   73:     
   74:     fbt::syscall:entry
   75:     /execname == "sleep" && guard++ == 0/
   76:     {
   77:             self->traceme = 1;
   78:             printf("fd: %d", arg0);
   79:     }
   80:     
   81:     fbt:::
   82:     /self->traceme/
   83:     {}
   84: 
   85:     fbt::syscall:return
   86:     /self->traceme/
   87:     {
   88:             self->traceme = 0;
   89:             exit(0);
   90:     }
   91: 
   92: Start the script running (dtrace -s scriptname.d) and then execute a sleep 2 in another shell.

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb