Annotation of wikisrc/tutorials/how_to_enable_and_run_dtrace.mdwn, revision 1.26

1.15      sevan       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/).
1.2       schmonz     3: 
1.5       wiki        4: # Current status
1.2       schmonz     5: 
1.5       wiki        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
1.18      sevan      11: * earm* (evbarm and armv4 based ports)
1.5       wiki       12: 
                     13: ## Supported providers
                     14: 
1.25      sevan      15: * DTrace: What to do when a script BEGINs, ENDs, ERRORs
1.5       wiki       16: * FBT: Function Boundary Tracing
1.25      sevan      17: * IO: Disk I/O
1.14      sevan      18: * Lockstat: Kernel Lock Statistics
1.25      sevan      19: * Proc: Process and thread related events
1.14      sevan      20: * Profile: Time based interrupt event source for Profiling
1.25      sevan      21: * SDT: Statically Defined Tracing
1.14      sevan      22: * Syscall: System Calls
                     23: * Syscall Linux (32bit & 64 bit): System calls via the Linux binary emulation layer
1.25      sevan      24: * VFS: Filesystem operations (confined to namecache events at time of writing - 8.99.22)
1.2       schmonz    25: 
1.6       riastrad   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.
1.7       riastrad   30: * Integrate [[riz|users/riz]]'s syscall provider patch.
1.6       riastrad   31: 
1.5       wiki       32: # How to use
                     33: 
1.2       schmonz    34: ##  Building DTrace 
                     35: 
                     36: You need the following options in your kernel: 
                     37:     
1.10      wiz        38:     options         KDTRACE_HOOKS   # kernel DTrace hooks
1.2       schmonz    39:     options         MODULAR
                     40: 
1.14      sevan      41: Optionally:
1.16      sevan      42: 
1.14      sevan      43:     options         INSECURE   # permit modules to loaded from user space once system has gone multiuser and securelevel has been raised.
1.2       schmonz    44: 
1.14      sevan      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`
1.2       schmonz    46: 
1.14      sevan      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/`
1.17      sevan      48: 
                     49: For example, add the following to `/etc/modules.conf` (the file may not exist already on a system):
1.2       schmonz    50:     
1.22      leot       51: - `solaris`
                     52: - `dtrace`
                     53: - `dtrace_fbt`
                     54: - `dtrace_lockstat`
                     55: - `dtrace_profile`
1.26    ! sevan      56: - `dtrace_sdt`
1.22      leot       57: - `dtrace_syscall`
1.26    ! sevan      58: - `dtrace_syscall_linux`
1.2       schmonz    59:     
1.14      sevan      60: A `dtrace` device node is created automatically in `/dev/dtrace` when the modules are loaded into place.
1.2       schmonz    61:     
                     62: List the dtrace probes 
                     63:     
                     64:     dtrace -l
                     65:     
                     66:        ID   PROVIDER            MODULE                          FUNCTION NAME
                     67:         1     dtrace                                                     BEGIN
                     68:         2     dtrace                                                     END
                     69:         3     dtrace                                                     ERROR
                     70:         4        fbt            netbsd             AcpiAcquireGlobalLock entry
                     71:         5        fbt            netbsd             AcpiAcquireGlobalLock return
                     72:         6        fbt            netbsd             AcpiAllocateRootTable entry
                     73:         7        fbt            netbsd                    AcpiAttachData entry
                     74:         . 
                     75:         .
                     76:     29129        fbt           solaris                   zfs_vop_getattr entry 
                     77:     29130        fbt           solaris                   zfs_vop_getattr return
                     78:     29131       proc                                                     create
                     79:     29132       proc                                                     exec 
                     80:         .
                     81:         .
                     82:     29140       proc                                                     lwp_start
                     83:     29141       proc                                                     lwp_exit
                     84: 
                     85: 
1.24      gson       86: ## Running hello world 
1.2       schmonz    87: 
1.9       wiz        88: Put the following into the file hello.d:
1.2       schmonz    89:     
                     90:     BEGIN
                     91:     {
                     92:         trace("Hello world");
                     93:         exit(0);
                     94:     }
                     95:     
                     96: 
                     97: Run the hello world script: 
                     98:     
                     99:     dtrace -s hello.d
                    100:     
                    101:     dtrace: script './hello.d' matched 1 probe
                    102:     CPU     ID                    FUNCTION:NAME
                    103:       0      1                           :BEGIN   Hello world
                    104:     
                    105: 
1.23      gson      106: ## A more complex example
                    107: 
                    108: The following script traces the execution of a sleep operation
1.9       wiz       109: in the kernel. Put it in sleep.d:
1.2       schmonz   110:     
                    111:     #pragma D option flowindent
1.23      gson      112: 
                    113:     syscall::nanosleep:entry
1.2       schmonz   114:     /execname == "sleep" && guard++ == 0/
                    115:     {
                    116:             self->traceme = 1;
                    117:     }
1.23      gson      118: 
                    119:     fbt:::
                    120:     /self->traceme/
                    121:     {}
                    122: 
                    123:     syscall::nanosleep:return
1.2       schmonz   124:     /self->traceme/
                    125:     {
                    126:             self->traceme = 0;
                    127:             exit(0);
                    128:     }
                    129: 
1.23      gson      130: Start the script running:
                    131: 
                    132:     dtrace -s sleep.d
                    133: 
                    134: This will take a while as the script instruments every function in the
                    135: kernel. When it's ready, it will print a message like "dtrace: script
                    136: 'sleep.d' matched 59268 probes".  Then execute a "sleep 2" in another
                    137: shell.
1.19      sevan     138: 
1.24      gson      139: ## Tools included in base
1.19      sevan     140: 
                    141: Starting with NetBSD-8, on builds where `MKDTRACE=yes` is set, scripts from
                    142: [Brendan Gregg's DTrace toolkit](https://github.com/opendtrace/toolkit/) are installed in base as standard.
                    143: 
1.21      sevan     144: At present, the following scripts are installed in `/usr/sbin`: 
1.19      sevan     145: 
1.22      leot      146: - `dtruss` - An implementation of the truss utility in DTrace which traces the system calls
                    147: made by a process
                    148: - `execsnoop` - snoop on execution of processes as they occur
                    149: - `opensnoop` - snoop on openning of files as they occur
                    150: - `procsystime` -  print process system call time details.

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