File:  [NetBSD Developer Wiki] / wikisrc / tutorials / how_to_enable_and_run_dtrace.mdwn
Revision 1.15: download - view: text, annotated - select for diffs
Wed Mar 22 15:43:09 2017 UTC (3 years, 8 months ago) by sevan
Branches: MAIN
CVS tags: HEAD
Link to Brendan Gregg's DTrace one lines & FreeBSD articles.

    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://dtrace.org) for more information.
    2: Also see [DTrace Introduction](http://dtrace.org/guide/preface.html), Brendan Gregg's [DTrace one liners](http://www.brendangregg.com/DTrace/dtrace_oneliners.txt) and his notes for [DTrace on FreeBSD](https://wiki.freebsd.org/DTrace/).
    3: 
    4: # Current status
    5: 
    6: ## Supported platforms
    7: 
    8: DTrace is a work-in-progress effort and it is for x86 systems and some arm boards.
    9: 
   10: * i386 and amd64
   11: * earm* (evbarm and armv4 based ports (armv4 side requires further testing but system is built with CTF)
   12: 
   13: ## Supported providers
   14: 
   15: * SDT: Statically Defined Tracing
   16: * FBT: Function Boundary Tracing
   17: * Lockstat: Kernel Lock Statistics
   18: * Profile: Time based interrupt event source for Profiling
   19: * Syscall: System Calls
   20: * Syscall Linux (32bit & 64 bit): System calls via the Linux binary emulation layer
   21: 
   22: ## TODO for netbsd-7
   23: 
   24: * Measure effect of `options KDTRACE_HOOKS` on system performance.
   25: * Determine whether the profile module works and list it here.
   26: * Integrate [[riz|users/riz]]'s syscall provider patch.
   27: 
   28: ## TODO for netbsd-6
   29: 
   30: Need to identify changes to pull up to netbsd-6 and pull them up.
   31: Candidates:
   32: 
   33: * Profile provider.
   34: 
   35: # How to use
   36: 
   37: ##  Building DTrace 
   38: 
   39: You need the following options in your kernel: 
   40:     
   41:     options         KDTRACE_HOOKS   # kernel DTrace hooks
   42:     options         MODULAR
   43: 
   44: Optionally:
   45:     options         INSECURE   # permit modules to loaded from user space once system has gone multiuser and securelevel has been raised.
   46: 
   47: A Distribution needs to be built with the options `MKDTRACE=yes` and `MKCTF=yes`, this is taken care of automatically and doesn't need to be specified manually. The list of platforms it is applied to automatically is set in `src/share/mk/bsd.own.mk`
   48: 
   49: Set the system to load the solaris and dtrace related modules in `/etc/modules.conf`, for a list of available modules, see `/stand/$MACHINE/$VERSION/modules/`
   50:     
   51:     solaris
   52:     dtrace
   53:     dtrace_sdt
   54:     dtrace_fbt
   55:     dtrace_lockstat
   56:     dtrace_profile
   57:     dtrace_syscall
   58:     
   59: A `dtrace` device node is created automatically in `/dev/dtrace` when the modules are loaded into place.
   60:     
   61: List the dtrace probes 
   62:     
   63:     dtrace -l
   64:     
   65:        ID   PROVIDER            MODULE                          FUNCTION NAME
   66:         1     dtrace                                                     BEGIN
   67:         2     dtrace                                                     END
   68:         3     dtrace                                                     ERROR
   69:         4        fbt            netbsd             AcpiAcquireGlobalLock entry
   70:         5        fbt            netbsd             AcpiAcquireGlobalLock return
   71:         6        fbt            netbsd             AcpiAllocateRootTable entry
   72:         7        fbt            netbsd                    AcpiAttachData entry
   73:         . 
   74:         .
   75:     29129        fbt           solaris                   zfs_vop_getattr entry 
   76:     29130        fbt           solaris                   zfs_vop_getattr return
   77:     29131       proc                                                     create
   78:     29132       proc                                                     exec 
   79:         .
   80:         .
   81:     29140       proc                                                     lwp_start
   82:     29141       proc                                                     lwp_exit
   83: 
   84: 
   85: ##  Running hello world 
   86: 
   87: Put the following into the file hello.d:
   88:     
   89:     BEGIN
   90:     {
   91:         trace("Hello world");
   92:         exit(0);
   93:     }
   94:     
   95: 
   96: Run the hello world script: 
   97:     
   98:     dtrace -s hello.d
   99:     
  100:     dtrace: script './hello.d' matched 1 probe
  101:     CPU     ID                    FUNCTION:NAME
  102:       0      1                           :BEGIN   Hello world
  103:     
  104: 
  105: A more complex example that traces the execution of a sleep operation
  106: in the kernel. Put it in sleep.d:
  107:     
  108:     #pragma D option flowindent
  109:     
  110:     fbt::syscall:entry
  111:     /execname == "sleep" && guard++ == 0/
  112:     {
  113:             self->traceme = 1;
  114:             printf("fd: %d", arg0);
  115:     }
  116:     
  117:     fbt::syscall:entry /self->traceme/ {}
  118:     
  119:     fbt::syscall:return
  120:     /self->traceme/
  121:     {
  122:             self->traceme = 0;
  123:             exit(0);
  124:     }
  125:     
  126: 
  127: Start the script running (dtrace -s sleep.d) and then execute a "sleep 2" in another shell. 

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