FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Building strace-plus

strace-plusstrace+ is an improved version of strace that collects stack traces associated with each system call. Since system calls require an expensive user-kernel context switch, they are often sources of performance bottlenecks. strace+ allows programmers to do more detailed system call profiling and determine, say, which call sites led to costly syscalls and thus have potential for optimization.

strace vs strace+

strace vs strace+

Build Pre-requisites

  • binutils
  • autoconf
  • gdb
  • make
  • gcc-c++
  • gcc
  • gcc-x86_64-linux-gnu
  • glibc-static
  • python

Compile and Build strace+

  1. Check out the source code from Git (requires git >= 1.6.6)
    $ git clone https://code.google.com/p/strace-plus/
  2. Compile strace+
    $ cd strace-plus/
    $ autoreconf -f -i
    $ ./configure
    $ make
    $ cp strace strace+

Compile a "hello world" test program

  1. Create a file named hello.c. hello.c is a simple program that makes four write system calls via printf statements:
    #include 
    
    void bar() {
      printf("bar\n");
      printf("bar again\n");
    }
    
    void foo() {
      printf("foo\n");
      bar();
    }
    
    int main() {
      printf("Hello world\n");
      foo();
      return 0;
    }
  2. Compile it:
    $ gcc hello.c -o hello
  3. Test:
    $ ./hello
  4. Run strace+ on the hello executable to generate a trace file named hello.out.
    $ ./strace+ -o hello.out ./hello
  5. Post-process hello.out to print out a list of system calls each augmented with stack traces
    python scripts/pretty_print_strace_out.py hello.out --trace

Build Statically

I like to always try and compile tools statically if possible. Especially in a case like this where you don't want strace+ to replace strace.

$ cd strace-plus/
$ export CFLAGS="-Os -fomit-frame-pointer -static -static-libgcc -ffunction-sections -fdata-sections -falign-functions=1 -falign-jumps=1 -falign-labels=1 -falign-loops=1 -fno-unwind-tables -fno-asynchronous-unwind-tables -Wl,--gc-sections -Wl,-Map=strace.mapfile"
$ autoreconf -i -f
$ ./configure
$ make CFLAGS="$CFLAGS"
$ cp strace strace+
# Normal strace
$ file /usr/bin/strace
/usr/bin/strace: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

# Static strace-plus
$ file strace+
strace+: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, not stripped

Bash Aliases Functions

Useful to stick in your .bash_profile

Alias: enhanced strace

alias strace='command strace -fq -s1000  -e trace=all 2>&1'

Alias: trace file calls

alias stracef='command strace -fq -s1000  -e trace=file 2>&1'

Function: tputtrace

function tputtrace ()
{
  ( 2>&2 strace -s5000 -e write tput $1 2>&1 )  | tee -a /dev/stderr | grep --color=always -o '"[^"]*"';
}

See Also

vistrace: a visualization of strace

Linux gcc linux strace

 

 

Comments