File:  [NetBSD Developer Wiki] / wikisrc / Attic / how_to_enable_and_run_dtrace.mdwn
Revision 1.3: download - view: text, annotated - select for diffs
Wed Jun 20 23:17:20 2012 UTC (22 months ago) by wiz
Branches: MAIN
CVS tags: HEAD
dtrace supports amd64 now, mention it.

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).

DTrace is a work-in-progress effort and it is for amd64 and i386 systems only. Two providers are available; the Statically Defined Tracing (SDT) provider and the Function Boundary Tracer (FBT) provider.

###Building DTrace

You need the following options in your kernel:

    options         INSECURE
    options         KDTRACE_HOOKS   # DTrace support
    options         MODULAR

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.

You also need to build distribution with the options MKMODULAR=yes and MKDTRACE=yes.

###Running hello world

Load the solaris and dtrace modules, and the SDT (Statically Defined Tracing) and FBT (Function Boundary Tracing) modules:

    modload solaris
    modload dtrace
    modload sdt
    modload fbt

Make the dtrace device node:

    mkdir /dev/dtrace
    mknod /dev/dtrace/dtrace c dtrace 0

List the dtrace probes

    dtrace -l

       ID   PROVIDER            MODULE                          FUNCTION NAME
        1     dtrace                                                     BEGIN
        2     dtrace                                                     END
        3     dtrace                                                     ERROR
        4        fbt            netbsd             AcpiAcquireGlobalLock entry
        5        fbt            netbsd             AcpiAcquireGlobalLock return
        6        fbt            netbsd             AcpiAllocateRootTable entry
        7        fbt            netbsd                    AcpiAttachData entry
        . 
        .
    29129        fbt           solaris                   zfs_vop_getattr entry 
    29130        fbt           solaris                   zfs_vop_getattr return
    29131       proc                                                     create
    29132       proc                                                     exec 
        .
        .
    29140       proc                                                     lwp_start
    29141       proc                                                     lwp_exit

Put the following into the file hello.d

    BEGIN
    {
        trace("Hello world");
        exit(0);
    }

Run the hello world script:

    dtrace -s hello.d
    
    dtrace: script './hello.d' matched 1 probe
    CPU     ID                    FUNCTION:NAME
      0      1                           :BEGIN   Hello world

A more complex example that traces the execution of a sleep operation in the kernel:

    #pragma D option flowindent
    
    fbt::syscall:entry
    /execname == "sleep" && guard++ == 0/
    {
            self->traceme = 1;
            printf("fd: %d", arg0);
    }
    
    fbt:::
    /self->traceme/
    {}

    fbt::syscall:return
    /self->traceme/
    {
            self->traceme = 0;
            exit(0);
    }

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