This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[kai/samba.git] / docs / docbook / devdoc / Tracing.sgml
1 <chapter id="tracing">
2 <chapterinfo>
3         <author>
4                 <firstname>Andrew</firstname><surname>Tridgell</surname>
5                 <affiliation>
6                         <orgname>Samba Team</orgname>
7                 </affiliation>
8         </author>
9 </chapterinfo>
10
11 <title>Tracing samba system calls</title>
12
13 <para>
14 This file describes how to do a system call trace on Samba to work out
15 what its doing wrong. This is not for the faint of heart, but if you
16 are reading this then you are probably desperate.
17 </para>
18
19 <para>
20 Actually its not as bad as the the above makes it sound, just don't
21 expect the output to be very pretty :-)
22 </para>
23
24 <para>
25 Ok, down to business. One of the big advantages of unix systems is
26 that they nearly all come with a system trace utility that allows you
27 to monitor all system calls that a program is making. This is
28 extremely using for debugging and also helps when trying to work out
29 why something is slower than you expect. You can use system tracing
30 without any special compilation options. 
31 </para>
32
33 <para>
34 The system trace utility is called different things on different
35 systems. On Linux systems its called strace. Under SunOS 4 its called
36 trace. Under SVR4 style systems (including solaris) its called
37 truss. Under many BSD systems its called ktrace. 
38 </para>
39
40 <para>
41 The first thing you should do is read the man page for your native
42 system call tracer. In the discussion below I'll assume its called
43 strace as strace is the only portable system tracer (its available for
44 free for many unix types) and its also got some of the nicest
45 features.
46 </para>
47
48 <para>
49 Next, try using strace on some simple commands. For example, <command>strace
50 ls</command> or <command>strace echo hello</command>.
51 </para>
52
53 <para> 
54 You'll notice that it produces a LOT of output. It is showing you the
55 arguments to every system call that the program makes and the
56 result. Very little happens in a program without a system call so you
57 get lots of output. You'll also find that it produces a lot of
58 "preamble" stuff showing the loading of shared libraries etc. Ignore
59 this (unless its going wrong!)
60 </para>
61
62 <para>
63 For example, the only line that really matters in the <command>strace echo
64 hello</command> output is:
65 </para>
66
67 <para><programlisting>
68 write(1, "hello\n", 6)                  = 6
69 </programlisting></para>
70
71 <para>all the rest is just setting up to run the program.</para>
72
73 <para>
74 Ok, now you're familiar with strace. To use it on Samba you need to
75 strace the running smbd daemon. The way I tend ot use it is to first
76 login from my Windows PC to the Samba server, then use smbstatus to
77 find which process ID that client is attached to, then as root I do
78 <command>strace -p PID</command> to attach to that process. I normally redirect the
79 stderr output from this command to a file for later perusal. For
80 example, if I'm using a csh style shell:
81 </para>
82
83 <para><command>strace -f -p 3872 >& strace.out</command></para>
84
85 <para>or with a sh style shell:</para>
86
87 <para><command>strace -f -p 3872 > strace.out 2>&1</command></para>
88
89 <para>
90 Note the "-f" option. This is only available on some systems, and
91 allows you to trace not just the current process, but any children it
92 forks. This is great for finding printing problems caused by the
93 "print command" being wrong.
94 </para>
95
96 <para>
97 Once you are attached you then can do whatever it is on the client
98 that is causing problems and you will capture all the system calls
99 that smbd makes. 
100 </para>
101
102 <para>
103 So how do you interpret the results? Generally I search through the
104 output for strings that I know will appear when the problem
105 happens. For example, if I am having touble with permissions on a file
106 I would search for that files name in the strace output and look at
107 the surrounding lines. Another trick is to match up file descriptor
108 numbers and "follow" what happens to an open file until it is closed.
109 </para>
110
111 <para>
112 Beyond this you will have to use your initiative. To give you an idea
113 of what you are looking for here is a piece of strace output that
114 shows that <filename>/dev/null</filename> is not world writeable, which
115 causes printing to fail with Samba:
116 </para>
117
118 <para><programlisting>
119 [pid 28268] open("/dev/null", O_RDWR)   = -1 EACCES (Permission denied)
120 [pid 28268] open("/dev/null", O_WRONLY) = -1 EACCES (Permission denied)
121 </programlisting></para>
122
123 <para>
124 The process is trying to first open <filename>/dev/null</filename> read-write 
125 then read-only. Both fail. This means <filename>/dev/null</filename> has 
126 incorrect permissions.
127 </para>
128
129 </chapter>