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