1 Contributor: Andrew Tridgell <samba-bugs@samba.anu.edu.au>
5 Subject: How to trace samba system calls for debugging purposes
6 =============================================================================
8 This file describes how to do a system call trace on Samba to work out
9 what its doing wrong. This is not for the faint of heart, but if you
10 are reading this then you are probably desperate.
12 Actually its not as bad as the the above makes it sound, just don't
13 expect the output to be very pretty :-)
15 Ok, down to business. One of the big advantages of unix systems is
16 that they nearly all come with a system trace utility that allows you
17 to monitor all system calls that a program is making. This is
18 extremely using for debugging and also helps when trying to work out
19 why something is slower than you expect. You can use system tracing
20 without any special compilation options.
22 The system trace utility is called different things on different
23 systems. On Linux systems its called strace. Under SunOS 4 its called
24 trace. Under SVR4 style systems (including solaris) its called
25 truss. Under many BSD systems its called ktrace.
27 The first thing you should do is read the man page for your native
28 system call tracer. In the discussion below I'll assume its called
29 strace as strace is the only portable system tracer (its available for
30 free for many unix types) and its also got some of the nicest
33 Next, try using strace on some simple commands. For example, "strace
34 ls" or "strace echo hello".
36 You'll notice that it produces a LOT of output. It is showing you the
37 arguments to every system call that the program makes and the
38 result. Very little happens in a program without a system call so you
39 get lots of output. You'll also find that it produces a lot of
40 "preamble" stuff showing the loading of shared libraries etc. Ignore
41 this (unless its going wrong!)
43 For example, the only line that really matters in the "strace echo
46 write(1, "hello\n", 6) = 6
48 all the rest is just setting up to run the program.
50 Ok, now you're famialiar with strace. To use it on Samba you need to
51 strace the running smbd daemon. The way I tend ot use it is to first
52 login from my Windows PC to the Samba server, then use smbstatus to
53 find which process ID that client is attached to, then as root I do
54 "strace -p PID" to attach to that process. I normally redirect the
55 stderr output from this command to a file for later perusal. For
56 example, if I'm using a csh style shell:
58 strace -f -p 3872 >& strace.out
60 or with a sh style shell:
62 strace -f -p 3872 > strace.out 2>&1
64 Note the "-f" option. This is only available on some systems, and
65 allows you to trace not just the current process, but any children it
66 forks. This is great for finding printing problems caused by the
67 "print command" being wrong.
69 Once you are attached you then can do whatever it is on the client
70 that is causing problems and you will capture all the system calls
73 So how do you interpret the results? Generally I search thorugh the
74 output for strings that I know will appear when the problem
75 happens. For example, if I am having touble with permissions on a file
76 I would search for that files name in the strace output and look at
77 the surrounding lines. Another trick is to match up file descriptor
78 numbers and "follow" what happens to an open file until it is closed.
80 Beyond this you will have to use your initiative. To give you an idea
81 of wehat you are looking for here is a piece of strace output that
82 shows that /dev/null is not world writeable, which causes printing to
85 [pid 28268] open("/dev/null", O_RDWR) = -1 EACCES (Permission denied)
86 [pid 28268] open("/dev/null", O_WRONLY) = -1 EACCES (Permission denied)
88 the process is trying to first open /dev/null read-write then
89 read-only. Both fail. This means /dev/null has incorrect permissions.
93 (please send updates/fixes to this file to samba-bugs@samba.anu.edu.au)