This page is about the DTrace facility in "Darwin-land". What is DTrace?The dtrace command is a generic front-end to the DTrace facility. Thecommand implements a simple interface to invoke the D language com- piler, the ability to retrieve buffered trace data from the DTrace ker- nel facility, and a set of basic routines to format and print traced data. said `man dtrace'.Simply, DTrace is
In Mac OS X 10.5, Instruments.app found in developer tools is another front-end to the DTrace facility. Warning: If the operating system becomes too busy or if you attempt to trace too many events (ie: all function entry related to the kernel), DTrace can drop events and even abort tracing and execution. Consequently, security auditing with DTrace is impacted and more, system() action is not synchronous (so unreliable for security purpose). Pros
Cons
A real world example: show missing filesOn Linux systems, "strace -eopen -f ... 2>&1 | grep ENOENT" can be used to see which files a process tries to open. Using dtrace, the equivalent is:
In the first terminal, you will see the files that are tried to be opened but fail to open (e.g., ENOENT). A cliché example: "Hello world!"dtrace -n "BEGIN { trace(\"Hello World\!\"); exit(0);}" dtrace: description 'BEGIN ' matched 1 probe CPU ID FUNCTION:NAME 0 1 :BEGIN Hello World! In a fileEdit and save Hello.d:BEGIN { trace("Hello World!"); exit(0); } dtrace -s Hello.d dtrace: script 'Hello.d' matched 1 probe CPU ID FUNCTION:NAME 0 1 :BEGIN Hello World! Detect new process (successfully started)proc:::exec-success{ printf("%s(pid=%d) started by uid - %d\n",execname, pid, uid); } Detect sigkillproc:::signal-send /args[2] == SIGKILL/ { printf("to %s",args[1]->pr_fname); printf(" at %d", timestamp); printf(" by (%d)", uid); printf(" from %s\n", execname); } Detect promiscuous mode changesfbt:mach_kernel:ifnet_set_promiscuous:* { printf("%s", execname); } Watch setuid/setgid calls/* '*' == {entry|return} */ fbt:mach_kernel:setuid:* { printf("%s", execname); } fbt:mach_kernel:setgid:* { printf("%s", execname); } Classic examplesDTrace one-linersThe DTraceToolkit (~100 scripts) D Programming languageIt seems to have been based on C language for some parts.(please add text)
copyin() Copies len bytes of data from the user-space address uaddr to the kernel-space address kaddr. copyinstr() Copies a NUL-terminated string, at most len bytes long, from user-space address uaddr to kernel-space address kaddr. The number of bytes actually copied, including the terminating NUL, is returned in *done. A generic D script exampleprobe descriptionA provider is a collection of probe. Probe syntaxFour fields describe a probe: provider:module:function:name Examples: fbt:mach_kernel:setuid:entry syscall::open*:entry Notes: Some special characters *, ?, [, and ] are allowed. * is the well known wildcard extension. ? character is for any single character in the name. [ and ] characters are used to specify a set of specific characters in the name. Listing probesdtrace -l lists all available probes. ID PROVIDER MODULE FUNCTION NAME 1 dtrace BEGIN 2 dtrace END 3 dtrace ERROR 4 lockstat mach_kernel lck_mtx_lock adaptive-acquire [...] 22527 plockstat1 libSystem.B.dylib pthread_rwlock_unlock$UNIX2003 rw-release Available providersSome providers (non exhaustive list):
Default variablesFIXME
Predicate (optional)When the predicate evaluates to true, "action" is executed.
Action (optional)C-style functions and semicolon (;) separation for the "body".Examples: printf(), ustack(), trace, ... Kernel destructive actionsUnderstand "destructive for the running OS"."-w" flag required for... -w permit destructive actions man dtruss man procsystime Jon Haslam. http://wikis.sun.com/display/DTrace/Security, Sun Microsystems, 2008. Glenn Brunette & Jon Haslam. Enhancing Security Awareness with DTrace, Sun Microsystems, 2006. How To Use DTrace. http://www.sun.com/software/solaris/howtoguides/dtracehowto.jsp, Sun Microsystems, 2005. Solaris Dynamic Tracing Guide. http://docs.sun.com/app/docs/doc/817-6223, Sun Microsystems, 2005. |
The goal of this project is to make Darwin more usable by providing an installation ISO, documentation, and add-on software. You are welcome to join #puredarwin on irc.freenode.net if you would like to join PureDarwin development and to add to this site.