What's it doing, then?

Non-invasive debugging with strace and ltrace

Richard Dawe <rich@phekda.gotadsl.co.uk>
http://homepages.nildram.co.uk/~phekda/richdawe/

How Linux programs do things

Library calls

C library (libc)

Other libraries (gd)

System calls

Libraries eventually do syscalls

Interface to kernel

Program

libthingy.so.1

libc.so.6

Kernel

Library call

System call (syscall)

(ltrace)

(ltrace)

(strace)

(strace)

Example: Hello!

perl -e 'print ”Hello Mum!\n”;'

Uses the write() syscall

Demo: strace perl -e 'print ”Hello Mum!\n”;'

perl -e 'print ”Hello Mum!\n”; print ”Hello Dad!\n”;'
vs.
perl -e 'print ”Hello Mum! ”; print ”Hello Dad!\n”;'

Why strace?

Amaze your friends and family

Can help debug long-running daemons

Can help debug time-sensitive problems

Reaches parts the Perl debugger can't

Another tool in the toolkit

strace options

Run command, or attach to process (-p)

Follow forks to trace children (-f, -ff)

Log to a file (-o), string length (-s)

Filter output (-e, e.g.: -e trace=file)

Example: strace -p 1234 -ff -o some.log -e trace=network -s 4096

Spot the syscalls (1 of 2)

#!/usr/bin/perl -w

use IO::Socket::INET;

my $sock = IO::Socket::INET->new(

Listen => 5,

Proto => 'tcp',

LocalAddr => '127.0.0.1',

LocalPort => 12345,

ReuseAddr => 1) || die $@;

while ((my $newsock = $sock->accept()))

{

my $buf;

$newsock->recv($buf, 1024);

print $buf;

undef $newsock;

}

Spot the syscalls (2 of 2)

#!/usr/bin/perl -w

use IO::Socket::INET;

my $sock = IO::Socket::INET->new(

Listen => 5,

Proto => 'tcp',

LocalAddr => '127.0.0.1',

LocalPort => 12345,

ReuseAddr => 1) || die $@;	<-- socket, bind, listen

while ((my $newsock = $sock->accept()))	<-- accept

{

my $buf;

$newsock->recv($buf, 1024);	<-- recvfrom

print $buf;	<-- write

undef $newsock;	<-- close

}

Problems with strace

Not entirely non-invasive

Signals can interrupt syscalls

Programs may not handle interrupted syscalls and may terminate, e.g.: qmail-send from qmail

Logs can be quite verbose (i.e.: big)

You may not know what you're looking for until you've looked

ltrace

Traces calls to dynamic libraries (.so)

Can't trace libraries loaded on-the-fly

Perl XS loads libraries on demand, e.g.: XML::LibXML

So ltrace won't help you debug your Perl XS (sorry)

Demo: Trace /bin/ls

The End

strace can help ”black box” debugging.

http://sourceforge.net/projects/strace/

ltrace can help ”black box” debugging, but is not particularly useful for Perl.

ftp://ftp.debian.org/debian/pool/main/l/ltrace/

Any questions?

2005-06-20T20:47:30