File:  [NetBSD Developer Wiki] / wikisrc / tutorials / how_to_enable_and_run_dtrace.mdwn
Revision 1.25: download - view: text, annotated - select for diffs
Sun Jul 22 15:42:50 2018 UTC (2 years, 2 months ago) by sevan
Branches: MAIN
CVS tags: HEAD
Document new providers introduced with DTrace update.
Drop netbsd-6 TODO, it is unlikely to happen before the EoL of the branch.

    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)
   12: 
   13: ## Supported providers
   14: 
   15: * DTrace: What to do when a script BEGINs, ENDs, ERRORs
   16: * FBT: Function Boundary Tracing
   17: * IO: Disk I/O
   18: * Lockstat: Kernel Lock Statistics
   19: * Proc: Process and thread related events
   20: * Profile: Time based interrupt event source for Profiling
   21: * SDT: Statically Defined Tracing
   22: * Syscall: System Calls
   23: * Syscall Linux (32bit & 64 bit): System calls via the Linux binary emulation layer
   24: * VFS: Filesystem operations (confined to namecache events at time of writing - 8.99.22)
   25: 
   26: ## TODO for netbsd-7
   27: 
   28: * Measure effect of `options KDTRACE_HOOKS` on system performance.
   29: * Determine whether the profile module works and list it here.
   30: * Integrate [[riz|users/riz]]'s syscall provider patch.
   31: 
   32: # How to use
   33: 
   34: ##  Building DTrace 
   35: 
   36: You need the following options in your kernel: 
   37:     
   38:     options         KDTRACE_HOOKS   # kernel DTrace hooks
   39:     options         MODULAR
   40: 
   41: Optionally:
   42: 
   43:     options         INSECURE   # permit modules to loaded from user space once system has gone multiuser and securelevel has been raised.
   44: 
   45: 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`
   46: 
   47: 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/`
   48: 
   49: For example, add the following to `/etc/modules.conf` (the file may not exist already on a system):
   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
  106: 
  107: The following script traces the execution of a sleep operation
  108: in the kernel. Put it in sleep.d:
  109:     
  110:     #pragma D option flowindent
  111: 
  112:     syscall::nanosleep:entry
  113:     /execname == "sleep" && guard++ == 0/
  114:     {
  115:             self->traceme = 1;
  116:     }
  117: 
  118:     fbt:::
  119:     /self->traceme/
  120:     {}
  121: 
  122:     syscall::nanosleep:return
  123:     /self->traceme/
  124:     {
  125:             self->traceme = 0;
  126:             exit(0);
  127:     }
  128: 
  129: Start the script running:
  130: 
  131:     dtrace -s sleep.d
  132: 
  133: This will take a while as the script instruments every function in the
  134: kernel. When it's ready, it will print a message like "dtrace: script
  135: 'sleep.d' matched 59268 probes".  Then execute a "sleep 2" in another
  136: shell.
  137: 
  138: ## Tools included in base
  139: 
  140: Starting with NetBSD-8, on builds where `MKDTRACE=yes` is set, scripts from
  141: [Brendan Gregg's DTrace toolkit](https://github.com/opendtrace/toolkit/) are installed in base as standard.
  142: 
  143: At present, the following scripts are installed in `/usr/sbin`: 
  144: 
  145: - `dtruss` - An implementation of the truss utility in DTrace which traces the system calls
  146: made by a process
  147: - `execsnoop` - snoop on execution of processes as they occur
  148: - `opensnoop` - snoop on openning of files as they occur
  149: - `procsystime` -  print process system call time details.

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