File:  [NetBSD Developer Wiki] / wikisrc / tutorials / how_to_enable_and_run_dtrace.mdwn
Revision 1.11: download - view: text, annotated - select for diffs
Thu Sep 18 15:05:39 2014 UTC (6 years, 7 months ago) by wiz
Branches: MAIN
CVS tags: HEAD
Use existing MAKEDEV dtrace target to create nodes.

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]( for more information. Also see [DTrace Introduction]( 

# Current status

## Supported platforms

DTrace is a work-in-progress effort and it is for x86 systems and some arm boards.

* i386 and amd64
* evbarm

## Supported providers

* SDT: Statically Defined Tracing
* FBT: Function Boundary Tracing

You can currently run a hello world DScript. 

## TODO for netbsd-7

* Rename provider modules to `dtrace_*.kmod`: `dtrace_fbt.kmod`, &c.
* Measure effect of `options KDTRACE_HOOKS` on system performance.
* Determine whether the profile module works and list it here.
* Integrate [[riz|users/riz]]'s syscall provider patch.

## TODO for netbsd-6

Need to identify changes to pull up to netbsd-6 and pull them up.

* Profile provider.

# How to use

##  Building DTrace 

You need the following options in your kernel: 
    options         INSECURE
    options         KDTRACE_HOOKS   # kernel DTrace hooks
    options         MODULAR

You also need to build distribution with the options 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: 
    cd /dev
    sh MAKEDEV dtrace

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:
        trace("Hello world");

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. Put it in sleep.d:
    #pragma D option flowindent
    /execname == "sleep" && guard++ == 0/
            self->traceme = 1;
            printf("fd: %d", arg0);
    fbt::syscall:entry /self->traceme/ {}
            self->traceme = 0;

Start the script running (dtrace -s sleep.d) and then execute a "sleep 2" in another shell. 

CVSweb for NetBSD wikisrc <> software: FreeBSD-CVSweb