Recent from talks
Nothing was collected or created yet.
DTrace
View on Wikipedia| DTrace | |
|---|---|
The DTrace command | |
| Original authors | Bryan Cantrill, Adam Leventhal, Mike Shapiro (Sun Microsystems) |
| Developers | Sun Microsystems, Oracle, Microsoft |
| Initial release | January 2005 |
| Repository | github |
| Written in | C |
| Operating system | Solaris, illumos, macOS, FreeBSD, NetBSD, Linux,[1] Windows[2] |
| Type | Tracing |
| License | CDDL, GPLv2, UPL |
| Website | dtrace |
DTrace is a comprehensive dynamic tracing framework originally created by Sun Microsystems for troubleshooting kernel and application problems on production systems in real time. Originally developed for Solaris, it has since been released under the free Common Development and Distribution License (CDDL) in OpenSolaris and its descendant illumos, and has been ported to several other Unix-like systems. Windows Server systems from Windows Server 2025 will have DTrace as part of the system.
DTrace can be used to get a global overview of a running system, such as the amount of memory, CPU time, filesystem and network resources used by the active processes. It can also provide much more fine-grained information, such as a log of the arguments with which a specific function is being called, or a list of the processes accessing a specific file.
In 2010, Oracle Corporation acquired Sun Microsystems and announced the discontinuation of OpenSolaris. As a community effort of some core Solaris engineers to create a truly open source Solaris, illumos operating system was announced via webinar on Thursday, 3 August 2010,[3] as a fork on OpenSolaris OS/Net consolidation, including DTrace technology.
In October 2011, Oracle announced the porting of DTrace to Linux,[4] and in 2019 official DTrace for Fedora is available on GitHub. For several years an unofficial DTrace port to Linux was available, with no changes in licensing terms.[5]
In August 2017, Oracle released DTrace kernel code under the GPLv2+ license, and user space code under GPLv2 and UPL licensing.[6] In September 2018 Microsoft announced that they had ported DTrace from FreeBSD to Windows.[2]
In September 2016 the OpenDTrace effort began on GitHub with both code and comprehensive documentation of the system's internals. The OpenDTrace effort maintains the original CDDL licensing for the code from OpenSolaris with additional code contributions coming under a BSD 2 Clause license. The goal of OpenDTrace is to provide an OS agnostic, portable implementation of DTrace that is acceptable to all consumers, including macOS, FreeBSD, OpenBSD, NetBSD, and Linux as well as embedded systems.
Description
[edit]Sun Microsystems designed DTrace to give operational insights that allow users to tune and troubleshoot applications and the OS itself.
Testers write tracing programs (also referred to as scripts) using the D programming language (not to be confused with other programming languages named "D"). The language, inspired by C, includes added functions and variables specific to tracing. D programs resemble AWK programs in structure; they consist of a list of one or more probes (instrumentation points), and each probe is associated with an action. These probes are comparable to a pointcut in aspect-oriented programming. Whenever the condition for the probe is met, the associated action is executed (the probe "fires"). A typical probe might fire when a certain file is opened, or a process is started, or a certain line of code is executed. A probe that fires may analyze the run-time situation by accessing the call stack and context variables and evaluating expressions; it can then print out or log some information, record it in a database, or modify context variables. The reading and writing of context variables allows probes to pass information to each other, allowing them to cooperatively analyze the correlation of different events.
Special consideration has been taken to make DTrace safe to use in a production environment. For example, there is minimal probe effect when tracing is underway, and no performance impact associated with any disabled probe; this is important since there are tens of thousands of DTrace probes that can be enabled. New probes can also be created dynamically.
Command line examples
[edit]DTrace scripts can be invoked directly from the command line, providing one or more probes and actions as arguments. Some examples:
# New processes with arguments
dtrace -n 'proc:::exec-success { trace(curpsinfo->pr_psargs); }'
# Files opened by process
dtrace -n 'syscall::open*:entry { printf("%s %s",execname,copyinstr(arg0)); }'
# Syscall count by program
dtrace -n 'syscall:::entry { @num[execname] = count(); }'
# Syscall count by syscall
dtrace -n 'syscall:::entry { @num[probefunc] = count(); }'
# Syscall count by process
dtrace -n 'syscall:::entry { @num[pid,execname] = count(); }'
# Disk size by process
dtrace -n 'io:::start { printf("%d %s %d",pid,execname,args[0]->b_bcount); }'
# Pages paged in by process
dtrace -n 'vminfo:::pgpgin { @pg[execname] = sum(arg0); }'
Scripts can also be written which can reach hundreds of lines in length, although typically only tens of lines are needed for advanced troubleshooting and analysis. Over 200 examples of open source DTrace scripts can be found in the DTraceToolkit,[7] created by Brendan Gregg (author of the DTrace book[8]), which also provides documentation and demonstrations of each.
Supported platforms
[edit]DTrace first became available for use in November 2003, and was formally released as part of Sun's Solaris 10 in January 2005. DTrace was the first component of the OpenSolaris project to have its source code released under the Common Development and Distribution License (CDDL).
DTrace is an integral part of illumos and related distributions.
DTrace is a standard part of FreeBSD[9] and NetBSD.[10]
Apple added DTrace support in Mac OS X 10.5 "Leopard", including a GUI called Instruments.[11] Over 40 DTrace scripts from the DTraceToolkit are included in /usr/bin,[12] including tools to examine disk I/O (iosnoop) and process execution (execsnoop). Unlike other platforms that DTrace is supported on, Mac OS X has a flag (P_LNOATTACH) that a program may set that disallows tracing of that process by debugging utilities such as DTrace and gdb. In the original Mac OS X DTrace implementation, this could affect tracing of other system information, as unrelated probes that should fire while a program with this flag set was running would fail to do so.[13] The OS X 10.5.3 update addressed this issue a few months later.[14] However, since El Capitan, System Integrity Protection prevents user from DTracing protected binary by default.
The Linux port of DTrace has been available since 2008;[15] work continues actively to enhance and fix issues. There is also an active implementation on github. Standard core providers are available (fbt, syscall, profile), plus a special "instr" provider (some of the Solaris providers are not yet available as of 2013[update]). The Linux DTrace implementation is a loadable kernel module, which means that the kernel itself requires no modification, and thus allows DTrace to avoid CDDL vs. GPL licensing conflicts (in its source form, at least). However, once DTrace is loaded the kernel instance will be marked as tainted.
In 2007, a developer at QNX Software Systems announced on his blog that he and a colleague were working on incorporating DTrace into the QNX operating system.[16]
Oracle Corporation added beta DTrace support for Oracle Linux in 2011,[1] as a technology preview in the Unbreakable Enterprise Kernel release 2, which is under GPLv2 (the DTrace Linux kernel module was originally released under CDDL).[17] General availability was announced in December 2012.[18][19]
On March 11, 2019, Microsoft released a version of DTrace for Windows 10 insider builds.[20] Microsoft included DTrace as a built-in tool in Windows Server 2025.[21][22]
Language and application providers
[edit]With a supported language provider, DTrace can retrieve context of the code, including function, source file, and line number location. Further, dynamic memory allocation and garbage collection can be made available if supported by the language.[23] Supported language providers include assembly language[clarification needed], C, C++, Java, Erlang, JavaScript, Perl, PHP, Python, Ruby, shell script, and Tcl.
Application providers allow DTrace to follow the operation of applications through system calls and into the kernel. Applications that offer DTrace application providers include MySQL, PostgreSQL, Oracle Database, Oracle Grid Engine, and Firefox.[23][24][25]
Authors and awards
[edit]DTrace was designed and implemented by Bryan Cantrill, Mike Shapiro, and Adam Leventhal.
The authors received recognition in 2005 for the innovations in DTrace from InfoWorld and Technology Review.[26][27] DTrace won the top prize in The Wall Street Journal's 2006 Technology Innovation Awards competition.[28] The authors were recognized by USENIX with the Software Tools User Group (STUG) award in 2008.[29]
See also
[edit]- eBPF – Linux kernel tracing backend providing a set of features similar to DTrace[30] since kernel version 4.9
- ftrace – a tracing framework for the Linux kernel, capable of tracing scheduling events, interrupts, memory-mapped I/O, CPU power state transitions, etc.
- ktrace – a BSD Unix and macOS utility that traces kernel–program interactions
- ltrace – a Linux debugging utility, displays the calls a userland application makes to shared libraries
- strace – a debugging utility for Linux, monitors system calls used by a program and all received signals
- SystemTap – a scripting language and utility used for instrumenting Linux installations
- LTTng
- IBM ProbeVue
References
[edit]- Cantrill, Bryan (February 2006). "Hidden in Plain Sight". ACM Queue. 4 (1): 26–36. doi:10.1145/1117389.1117401. Retrieved 2017-12-19.
- Bryan M. Cantrill, Michael W. Shapiro and Adam H. Leventhal (June 2004). Dynamic Instrumentation of Production Systems. Proceedings of the 2004 USENIX Annual Technical Conference. Retrieved 2006-09-08.
Notes
[edit]- ^ a b Wim Coekaerts (2011-10-09). "Trying out dtrace". blogs.oracle.com. Retrieved 2018-02-15.
- ^ a b "OS internals: Technical deep-dive into operating system innovations - BRK3365". Microsoft Ignite Channel. 2018-10-08.
- ^ D'Amore, Garrett (3 August 2010). "Illumos - Hope and Light Springs Anew - Presented by Garrett D'Amore" (PDF). illumos.org. Retrieved 3 August 2010.
- ^ "Oracle To Bring Dtrace To Linux". Slashdot. 2011-10-04. Retrieved 2020-11-11.
- ^ [1] "The original DTrace is licensed under Sun's (now Oracle) CDDL license. Original copyrights are left intact. No GPL code is incorporated into the release, to avoid legal conflicts."
- ^ Wielaard, Mark J. (2018-02-14). "dtrace for linux; Oracle does the right thing". Mark J. Wielaard blog. Retrieved 2018-02-14.
- ^ "DTraceToolkit". Brendan Gregg. Retrieved 2014-06-08.
- ^ DTrace: Dynamic Tracing in Oracle Solaris, Mac OS X and FreeBSD. Safari Books. 2011. ISBN 978-0132091510. Archived from the original on 2011-04-06. Retrieved 2011-01-03.
- ^ "FreeBSD 7.1-RELEASE Announcement". 2009-01-06. Retrieved 2009-01-06.
- ^ "NetBSD source changes, 21 February 2010".
- ^ "Mac OS X Leopard - Developer Tools - Instruments". Apple Inc. Archived from the original on 2007-10-24. Retrieved 2007-10-19.
- ^ "Mac OS X DTrace". Apple Inc. Archived from the original on 2022-06-05. Retrieved 2010-05-31.
- ^ "Mac OS X and the missing probes". Leventhal, Adam H. January 18, 2008. Retrieved 2008-01-20.
- ^ "Apple Updates DTrace". Leventhal, Adam H. June 7, 2008. Retrieved 2008-06-16.
- ^ "CRiSP tools download page". Archived from the original on 2020-11-16. Retrieved 2011-03-02.
- ^ "DTrace on QNX!". Oracle The Observation Deck Blog. November 8, 2007.
- ^ Zannoni, Elena; Van Hees, Kris (2012). DTrace on Linux (PDF). Linux Foundation Collaboration Summit. Archived from the original (PDF) on 2014-07-07. Retrieved 2012-04-05.
- ^ Koch, Zeynep (December 12, 2012). "Announcement: DTrace for Oracle Linux General Availability". Oracle Linux Blog.
- ^ DTrace module source code for Linux
- ^ Pulapaka, Hari (March 11, 2019). "DTrace on Windows". Microsoft Tech Community.
- ^ "DTrace". Microsoft Learn. 2024-04-19. Retrieved 2024-11-07.
- ^ Gatlan, Sergiu (2024-11-04). "Windows Server 2025 released—here are the new features". BleepingComputer. Archived from the original on 2024-11-05. Retrieved 2024-11-07.
- ^ a b DTrace: Dynamic Tracing in Oracle Solaris, Mac OS X and FreeBSD. Prentice Hall. 2011. p. 1152. ISBN 9780132091510.
- ^ "Open Grid Scheduler / Grid Engine Documentation". Open Grid Scheduler. Retrieved December 30, 2012.
- ^ "DTrace – MDN". Mozilla. Archived from the original on September 5, 2014. Retrieved December 30, 2012.
- ^ "Tracing software in real time". Technology Review. MIT. 2005. Retrieved 2007-03-31.
- ^ McAllister, Neil (August 2005). "Innovation is alive and well in 2005". InfoWorld. IDG. Archived from the original on 2005-11-23. Retrieved 2007-03-31.
- ^ Totty, Michael (September 2006). "The Winners Are..." The Wall Street Journal. Dow Jones & Company, Inc. Retrieved 2007-03-31.
- ^ "2008 USENIX Annual Technical Conference (USENIX '08)". 2008. Retrieved 2008-11-26.
- ^ "DTrace Tools". Retrieved 2017-11-27.
External links
[edit]- DTrace Tools – Brendan Gregg's DTrace examples (2004)
- DTraceToolkit – a collection of DTrace scripts
- DTrace book scripts – DTrace book scripts on GitHub
- FreeBSD DTrace page – FreeBSD DTrace homepage, includes a tutorial and one-liners
- DTrace book
- DTrace guide – Illumos DTrace book
- Dynamic Tracing with DTrace & SystemTap – free book with examples and exercises
- DTrace Hands On Lab – a step-by-step course to learn DTrace
- DLight Tutorial – an interactive GUI utility for C/C++ developers based on DTrace technology; part of Oracle Solaris Studio prior to version 12.4
- Exploring Leopard with DTrace – DTrace for debugging and exploration
- Tech Talk on DTrace given by Bryan Cantrill
- Hidden in Plain Sight, Sun Microsystems, by Bryan Cantrill
- Official Oracle DTrace port to Linux:
- DTrace on Fedora
DTrace
View on Grokipediafbt for function boundary tracing or io for I/O events), a central kernel facility for managing probe activation and data buffering, and user-space consumers like the dtrace command-line utility for scripting and output processing.[6] Its low-overhead design—achieved through just-in-time compilation of scripts and per-CPU buffering—makes it suitable for live systems, supporting use cases from real-time diagnostics to postmortem analysis of crash dumps.[1][6] DTrace's innovation earned it the top prize in the 2006 Wall Street Journal Technology Innovation Awards, recognizing its impact on system observability.[3]
Overview
Core Concept
DTrace is a comprehensive dynamic tracing framework originally developed by Sun Microsystems for the Solaris operating system, enabling troubleshooting of kernel and application issues on live production systems without modifying binaries or incurring downtime.[1] It provides a unified mechanism to instrument both user-level processes and kernel code, allowing administrators and developers to observe system behavior in real time for performance analysis and debugging.[6] Central to DTrace's design is its non-intrusive nature, achieved through dynamic instrumentation that imposes zero overhead when probes are disabled and minimal impact when active, as only enabled probes execute code.[1] Safety is ensured by built-in features such as predicates—conditional expressions that limit tracing to specific conditions—preventing system instability or corruption even during intensive use on production environments.[6] This architecture addresses key limitations of static tracing tools like strace and gdb, which require process attachment, generate significant overhead, and lack systemic scope suitable for broad production diagnostics.[1] The core workflow of DTrace involves identifying and enabling instrumentation points called probes, provided by kernel modules or applications, which fire in response to system events; upon firing, user-defined actions in the D programming language aggregate data—such as timestamps, arguments, or metrics—into per-CPU buffers for real-time examination and summarization.[1] This enables scalable, on-the-fly insights into complex interactions across the entire software stack, from individual threads to global resource usage.[6]Key Features
DTrace enables dynamic instrumentation, permitting the attachment of probes to any kernel or user-space function at runtime without access to source code or recompilation.[7] This capability allows for on-the-fly exploration of system behavior in production environments, where traditional static instrumentation would be impractical.[8] When not enabled, DTrace imposes zero overhead, as probes are compiled into binaries but remain inactive until explicitly activated by the framework.[9] To ensure safe operation on live systems, DTrace incorporates built-in protections against instability, including the deadman mechanism, which monitors for excessive CPU usage or unresponsiveness induced by tracing and automatically aborts tracing to prevent system hangs.[10] Additional safeguards include safe compilation into an intermediate form that prevents runtime errors like division by zero or invalid pointer dereferences from crashing the system, instead disabling affected probes and reporting errors.[10] These features collectively minimize the risk of performance degradation or system failure during tracing.[11] DTrace supports rich data aggregation through arbitrary actions tied to probes, including built-in statistical functions such as@count for counting occurrences, @sum for accumulating values, and higher-level aggregations for quantizing or averaging data across system events.[7] This allows users to perform complex, on-the-fly analysis without post-processing large trace logs. Integration with the D programming language further enables custom scripting for tailored queries, such as filtering events by process ID or aggregating metrics by thread, facilitating deep insights into system dynamics.[7]
The framework's portability stems from its provider abstraction layer, which standardizes probe interfaces across diverse operating systems—including Solaris, FreeBSD, macOS, Linux distributions, and Windows—and CPU architectures like x86, SPARC, and ARM, ensuring consistent functionality without vendor-specific modifications.[3] This design promotes widespread adoption by abstracting underlying kernel differences, allowing scripts written for one platform to run with minimal adaptation on others.[11]
History
Origins at Sun Microsystems
DTrace's development commenced in 2001 at Sun Microsystems, spearheaded by kernel engineer Bryan Cantrill to tackle persistent production debugging challenges in the Solaris operating system, where traditional tools often required service interruptions or risked system instability.[12] The effort was driven by the need for a dynamic tracing framework that could safely instrument live systems without overhead or risk, enabling real-time analysis of complex, componentized environments.[13] This initiative drew partial inspiration from Solaris's earlier Trace Normal Form (TNF) framework, a user-level tracing tool introduced in Solaris 2.5, but sought to overcome TNF's limitations, such as its restricted probe coverage, crude filtering mechanisms, and postmortem-only data handling, by providing a more comprehensive and scalable solution.[14] Adam Leventhal and Mike Shapiro soon joined Cantrill, forming the core team that shaped DTrace's architecture within Sun's Solaris Kernel Development group.[15] Central to the early design were principles of whole-system visibility, allowing tracing across kernel, device drivers, and user applications to uncover systemic behaviors and performance bottlenecks that process-centric tools could not address.[13] The framework prioritized safety—ensuring probes could not crash the system—and minimal intrusion, with zero probe effect when tracing was disabled, facilitating its use in high-stakes production environments without requiring code recompilation or restarts.[13] DTrace made its initial public appearance integrated into Solaris 10, which Sun Microsystems released in January 2005 as a standard OS component.[16] From the outset of its external availability, Sun positioned DTrace as an open-source project under the Common Development and Distribution License (CDDL), encouraging broader adoption and contributions from the developer community while aligning with Sun's emerging OpenSolaris initiative.[17] This licensing choice reflected Sun's intent to extend DTrace's impact beyond proprietary Solaris deployments, laying the groundwork for its evolution into a versatile diagnostic tool.[18]Open-Sourcing and Expansion
DTrace was open-sourced by Sun Microsystems in 2005 as part of the OpenSolaris project, licensed under the Common Development and Distribution License (CDDL), which facilitated its adoption and porting to other operating systems.[19][20] This release enabled community contributions and led to early ports, including integration into FreeBSD starting with version 7.1 in 2009, following development efforts that began in 2007.[21][22] Apple incorporated a port of DTrace into Mac OS X 10.5 Leopard in 2007, enhancing kernel and application diagnostics for its ecosystem.[23] For Linux, Oracle announced an official port in October 2011, initially as a kernel module for the Unbreakable Enterprise Kernel (UEK), alongside alternatives like SystemTap that drew inspiration from DTrace concepts.[24] Sun's acquisition by Oracle in 2010 marked a transition in stewardship, with Oracle continuing active development of DTrace for Solaris and extending it to Oracle Linux via UEK kernels, where it became a standard tool for real-time troubleshooting.[25][26] Community efforts further expanded DTrace's reach, including ports to NetBSD—fully integrated in NetBSD 10.0 released in 2024—and QNX, where an initial port was completed in 2007 to support its real-time Neutrino kernel.[27][28][29] Recent advancements include Microsoft's native support for DTrace in Windows Server 2025, announced in 2024 and shipped in November 2024, providing built-in diagnostics through a cross-platform port derived from the open-source OpenDTrace project.[30][31] Oracle's ongoing enhancements culminated in DTrace 2.0.3-1 for Linux, released on June 10, 2025, which added support for User-space Statically Defined Tracing (USDT) probes in executables and shared libraries compiled with Link-Time Optimization (LTO).[32] By 2025, DTrace's maturity is evident in its default inclusion across major operating system distributions, including Oracle Linux, FreeBSD, NetBSD, and Windows Server, diminishing the need for separate installations.[33][34][35]Technical Architecture
Probes and Providers
In DTrace, probes represent specific instrumentation points embedded within the operating system kernel, user applications, or libraries, designed to fire in response to particular events such as function entry or exit. These points are statically placed during compilation but remain inactive until dynamically enabled by a DTrace script, ensuring zero runtime overhead when not in use.[36] Each probe is uniquely identified by a description consisting of four components in the syntaxprovider:module:function:name, where the provider indicates the module responsible for the probe, the module specifies the kernel module or binary containing the probe, the function denotes the specific function or operation, and the name describes the event type (e.g., entry for function start or return for exit).[36] This syntax allows precise targeting of probes, with optional predicates—conditional expressions—to filter firings based on runtime criteria, and actions to define responses, such as recording data or aggregating statistics.[36]
Providers serve as the modular components that expose and manage sets of probes, functioning as kernel modules or user-level interfaces that implement particular types of instrumentation. For instance, the fbt (Function Boundary Tracing) provider instruments kernel function boundaries to trace calls and returns; the syscall provider monitors system call entry and return; the proc provider tracks process lifecycle events like creation and execution; and the sdt (Statically Defined Tracing, often referred to as USDT) provider enables user-space applications to embed custom probes for application-specific tracing.[37] Oracle Solaris includes numerous built-in providers—over two dozen core ones documented in official guides, with the total expanding based on installed software and kernel modules—allowing comprehensive system coverage from hardware interrupts to network protocols.[37] These providers are extensible, permitting developers to create custom ones for specialized instrumentation, such as for third-party drivers or applications, thereby supporting virtually unlimited probe availability.[36]
A key architectural benefit of providers is their role in abstracting underlying hardware and operating system differences, promoting portability of DTrace scripts across supported platforms. By standardizing probe interfaces and semantics, providers encapsulate platform-specific details—like instruction sets or kernel internals—enabling the same probe description to function consistently wherever the provider is implemented, though the exact set of available probes may vary by system configuration.[36] When enabled, probes introduce minimal computational overhead, typically equivalent to a few no-op instructions, on the order of nanoseconds per firing, which allows safe use even in production environments without significant performance impact.[38]
D Programming Language
The D programming language is a domain-specific language designed specifically for writing DTrace scripts, enabling the definition of tracing actions in response to probe firings. It features a C-like syntax augmented with tracing-specific primitives, allowing scripts to execute safely in either kernel or user space contexts without risking system instability. D programs are structured as a series of clauses that associate probe descriptions with optional predicates and actions, facilitating dynamic instrumentation without recompilation or system downtime.[39] Key elements of D include probe clauses, which integrate probe descriptions with predicates and actions. A typical clause follows the formprobe description /predicate/ { action statements; }, where the predicate is an optional conditional expression enclosed in slashes, such as /arg0 > 100/, that evaluates to true (non-zero) or false (zero) when the probe fires. Actions, enclosed in braces, consist of straight-line statements like trace(arg0); to output data or printf("Value: %d\n", arg1); for formatted printing. Aggregations are defined using the @ prefix for associative arrays that summarize data across probe firings, as in @foo[probefunc] = [count](/page/Count)(); to tally occurrences by function name.[40][41]
Built-in variables provide contextual information during execution. The variables arg0 through arg9 represent the first ten arguments passed to the probe as 64-bit integers, with their interpretation depending on the probe's ABI. Other variables include curthread, a pointer to the current kernel thread structure for thread-specific details, and timestamp, a nanosecond-resolution counter since an arbitrary epoch, useful for relative timing measurements.[42]
Aggregation types in D leverage the @ operator to create thread-local, lock-free associative arrays for efficient data summarization. Common aggregation functions include count() to track invocation frequency, avg(expression) to compute arithmetic means of scalar values, and lquantize(expression, lower_bound, upper_bound, step) to generate linear histograms distributing values into buckets of fixed width. These functions store intermediate results per CPU, enabling scalable analysis without global synchronization.[41][43]
D scripts are interpreted directly at runtime by the DTrace facility, requiring no separate compilation step; the dtrace command compiles and loads them on-the-fly into a safe intermediate form with built-in error handling for issues like division by zero. This supports both one-liner commands, such as dtrace -n 'syscall:::entry { trace(execname); }', and multi-clause scripts saved in .d files executed via dtrace -s script.d.[39]
Unlike full C, D omits control-flow constructs like loops and branches within action blocks to ensure deterministic, bounded execution and prevent infinite loops or resource exhaustion in kernel context. Instead, conditional logic relies on predicates, and post-processing is handled via special END clauses tied to the dtrace:::END probe, which fires after all other probes to format or output aggregated results, such as using printa(@foo); to display histogram data.[40][44]
Usage and Examples
Command-Line Basics
DTrace is invoked from the command line using thedtrace utility, which serves as the primary interface for compiling, enabling, and executing D programs. The basic syntax allows for inline probe descriptions with the -n option or loading from a script file with -s. For example, dtrace -n 'probe /predicate/ { action }' executes a simple one-liner, where probe specifies the instrumentation point, predicate is an optional condition, and action defines the tracing behavior.[45] Alternatively, dtrace -s script.d compiles and runs a D program stored in a file, enabling tracing unless overridden.[8]
Several common options control invocation and output. The -Z flag permits execution even if no probes match the description, allowing partial or experimental scripts to run without error.[45] For quieter operation, -q suppresses non-data output, displaying only explicit print actions from the script.[8] Output can be directed to a file with -o outputfile, useful for post-processing large traces.[45] To attach to a running process, -p pid specifies the process ID for user-space tracing.[8] Similarly, -c command executes and traces a specified command, such as dtrace -c 'ls -l', capturing its activity.[45] Probe discovery is facilitated by -l, which lists available probes matching criteria, e.g., dtrace -l -n syscall:::entry to enumerate system call entry points.[46]
Tracing sessions can be managed programmatically within D scripts using built-in functions like stop() to halt the kernel temporarily for inspection and exit() to terminate the tracing run.[45] By default, DTrace produces formatted ASCII output in a tabular style, showing timestamps, probe details, and variable values; the -x option enables extended variables, such as aggsize for aggregation buffer sizing, to customize this further.[45]
DTrace typically requires root privileges for kernel-level access, ensuring secure instrumentation of system-wide events, though user-space tracing is possible with appropriate permissions granted via tools like priv_set.[45] For analysis, output integrates seamlessly with shell pipelines, such as piping to awk for filtering, e.g., dtrace -n 'syscall:::entry { trace(execname); }' | awk '{print $1}' | sort | uniq -c to count unique process names.[8]
Practical Scripting Scenarios
DTrace scripts enable administrators and developers to diagnose performance issues in real-time without modifying applications or rebooting systems. Common troubleshooting scenarios include monitoring system calls to identify excessive kernel interactions, profiling functions to pinpoint CPU-intensive code paths, and tracing user-space events for application-specific insights. These scripts leverage DTrace's probes to collect data dynamically, often aggregating results for analysis.[47] One practical scenario involves tracing system calls to understand process behavior and detect anomalies, such as a process making an unusually high number of calls. For example, the following one-liner counts system calls by executable name:dtrace -n 'syscall:::entry { @[execname] = count(); }'. This aggregates counts across all system calls, revealing which processes are most active at the kernel level, such as a database server dominating with thousands of reads.[48]
Function-level profiling helps identify "hot" functions consuming disproportionate CPU time within a binary, aiding in optimization during high-load troubleshooting. Using the fbt (Function Boundary Tracing) provider, a script can measure time spent in kernel functions; for instance: fbt::delay:entry, fbt::drv_usecwait:entry { self->in = timestamp; } fbt::delay:return, fbt::drv_usecwait:return /self->in/ { @snoozers[stack()] = quantize(timestamp - self->in); self->in = 0; }. This quantizes delays in device driver waits, showing stack traces and latency distributions to diagnose blocking operations during system boot or runtime.[49]
For user-space tracing, the USDT (User Statically Defined Tracing) provider allows instrumentation of applications like databases without recompilation. In PostgreSQL, which embeds USDT probes, a script can monitor transaction activity: postgresql$1:::transaction-start { @start["Start"] = [count](/page/Count)(); self->ts = [timestamp](/page/Timestamp); } postgresql$1:::transaction-abort { @abort["Abort"] = [count](/page/Count)(); } postgresql$1:::transaction-commit /self->ts/ { @commit["Commit"] = [count](/page/Count)(); @time["Total time (ns)"] = sum([timestamp](/page/Timestamp) - self->ts); self->ts=0; }, executed as ./txn_count.d <PID>. This counts starts, aborts, and commits while summing durations, helping troubleshoot transaction bottlenecks in production environments.[50]
Error handling in DTrace scripts ensures robust execution, particularly for long-running traces where predicates might fail or resources exhaust. The BEGIN probe initializes setup, such as printing headers or validating inputs: BEGIN { [printf](/page/Printf)("Tracing started\n"); }, while the END probe summarizes aggregations upon completion: END { printa(@); }. For runtime errors like null dereferences, the ERROR probe can log details: dtrace:::ERROR { trace("Error at probe %s\n", probefunc); }, preventing silent failures and aiding debugging.[51][52]
Scripts can also profile I/O latency to isolate disk bottlenecks, a frequent cause of application slowdowns. A representative example tracks I/O latency by device: io:::start { self->ts = timestamp; self->dev = args[1]->dev_statname; } io:::done /self->ts/ { printf("%s: %d ms\n", self->dev, (timestamp - self->ts)/1000000); self->ts = 0; self->dev = 0; }. This prints completion times in milliseconds for each I/O operation, highlighting outliers like multi-second waits on overloaded volumes.[53]
An advanced technique for Java applications involves DTrace's built-in jstack() action to capture stack traces during profiling, focusing on core DTrace capabilities while complementing external tools. For instance: syscall::write:entry /execname == "java"/ { jstack(); } traces Java stacks on write system calls, revealing code paths like java.io.PrintStream.println leading to kernel I/O, thus identifying inefficient logging or file operations without relying solely on JVM-specific utilities.[54]
