12.4 ustack Action

Note

If you want to perform symbol lookup in a stripped executable, you must specify the --export-dynamic option when linking the program. This option causes the linker to add all symbols to the dynamic symbol table (that is, the set of symbols which are visible from dynamic objects at run time). If you use gcc to link the objects, specify the option as -Wl,--export-dynamic to pass the correct option to the linker.

If you want to look up symbols in shared libraries or unstripped executables, the --export-dynamic option is not required.

Tracing a process thread's stack at the time a particular probe is activated is often useful for examining a problem in more detail. The ustack action traces the user thread's stack. If, for example, a process that opens many files occasionally fails in the open() system call, you can use the ustack action to discover the code path that executes the failed open:

syscall::open:entry
/pid == $1/
{
  self->path = copyinstr(arg0);
}

syscall::open:return
/self->path != NULL && errno != 0/
{
  printf("open for '%s' failed", self->path);
  ustack();
}

This script also illustrates the use of the $1 macro variable which takes the value of the first operand specified on the dtrace command-line:

# dtrace -s ./badopen.d 3430
dtrace: script './badopen.d' matched 2 probes
CPU     ID                    FUNCTION:NAME
  1    489                    openat:return open for '/usr/lib/foo' failed
              libc.so.6`sleep+0xe0
              ld-linux-x86-64.so.2`do_lookup_x+0x847
              libc.so.6`0x3cb8003630
              libc.so.6`0x3cb8003c48
              libc.so.6`0x3cb800e2c8
              libc.so.6`0x3cb8003c48
              looper`0x400612
              libc.so.6`getenv+0x2a
              looper`0x4003c8
              looper`0x4009b0
              libc.so.6`0x3cb800e2c8
              looper`0x4009b0
              looper`doOpenLoop+0x33
              looper`0x400e9c
              looper`main+0x5f
              looper`0x400ea9
              libc.so.6`__libc_start_main+0xfd
              looper`main
              looper`0x4009b0
              looper`__libc_csu_init

The ustack action records program counter (PC) values for the stack and dtrace resolves the PC values to symbol names by looking though the process's symbol tables. If dtrace cannot resolve the PC value to a symbol, it prints out the value as a hexadecimal integer.

If a process exits or is killed before the ustack data is formatted for output, dtrace might be unable to convert the PC values in the stack trace to symbol names, and it displays them as hexadecimal integers.