I agree with Philip that the heart of your problem is that you''re never
actually grabbing the return value from open(2).
It''s worth noting that in Solaris Express (and OpenSolaris and soon in
a
Solaris 10 update), fds[] is a built-in array which can get you the file
name by doing fds[N]->fi_pathname. It can save you some of the trouble
of remembering the file name.
Adam
On Thu, Sep 22, 2005 at 01:15:47PM +0100, Philip Beevers
wrote:> Hi Siva,
>
> There''s a few reasons I can think of that you might be having
problems with
> this script - I''ll try and take you through them...
>
> * Your script needs to be given a pid as argument - as such,
you''re
> attaching to the target process after it has started. Thus it''s
almost
> certain you''ve missed some calls to open(). You could fix this by
using the
> pid$target construct (see the DTrace answerbook for how to use this).
> * Calls to close() won''t necessarily match calls to open, for a
number of
> reasons:
> * File descriptors could be returned from all sorts of other system calls
> - e.g. socket(), accept(), dup(), dup2(), pipe(). For example, my
> application will open a file, dup() the returned file descriptor until
it''s
> >256, then close those under 256, in order to leave low file descriptors
> available for anything which uses a FILE* (e.g. gethostbyname()).
> * close() might be called more frequently than open; it might be called
on
> an invalid file descriptor and cause an error. Now this is interesting in
> itself, as it''s probably a bug - and of course it''s by
the power of DTrace
> that you can see these things more easily than before :-)
> * Your use of the arguments is perhaps a bit unusual. arg0 in
> pid$1:libc.so.1:open:entry is the pointer to the filename string, so
> probably isn''t that interesting in itself; you''d be
better off using the
> return probe from open() to do something sensible when a file descriptor is
> returned to the application.
>
> I''ve pulled some of these points together into the following
script, which I
> suspect will do something more like what you''re expecting.
I''ve also tried
> to comment it to give you an insight into some of the idioms
you''ll see in a
> number of other D scripts.
>
>
> #!/usr/sbin/dtrace -qs
>
> pid$1:libc.so.1:open:entry
> {
> /*
> * Store the filename in a
> * thread-local var
> */
> self->fileName = copyinstr(arg0);
> }
>
> /*
> * Only fire this probe if we''ve just seen the entry probe -
> * avoids race conditions - and only do this when open
> * succeeded.
> */
> pid$1:libc.so.1:open:return
> / (self->fileName != "") && (arg1 >= 0) /
> {
> printf("Opening: fd: %d fileName: %s\n", arg1,
self->fileName);
>
> /*
> * Store the filename by fd for
> * use in the close probe
> */
> fds[arg1] = self->fileName;
>
> /*
> * Unset thread-local filename, for next
> * use
> */
> self->fileName = "";
> }
>
> pid$1:libc.so.1:close:entry
> / fds[arg0] != "" /
> {
> printf("Closing: fd: %d fileName %s\n", arg0, fds[arg0]);
>
> /*
> * fd now closed - clear its name
> */
> fds[arg0] = "";
> }
>
>
> Of course, this isn''t really anything more than you can achieve
with
> truss(1), but it illustrates the point.
>
> HTH,
>
>
> --
>
> Philip Beevers
> Fidessa Infrastructure Development
>
> mailto:philip.beevers at fidessa.com
> phone: +44 1483 206571
>
>
> > -----Original Message-----
> > From: dtrace-discuss-bounces at opensolaris.org
> > [mailto:dtrace-discuss-bounces at opensolaris.org]On Behalf Of
Sivakumar
> > Shanmugasundaram
> > Sent: 22 September 2005 12:17
> > To: dtrace-discuss at opensolaris.org
> > Subject: [dtrace-discuss] File Descriptor Leakage
> >
> >
> > I am a total newbie. Trying to figure out a way to locate any
> > file descriptor leakage. So, tried the following.
> >
> >
> > #!/usr/sbin/dtrace -s
> >
> > pid$1:libc.so.1:open:entry
> > {
> > self->fd = copyinstr(arg0);
> > printf("Opening %s (%d) ", self->fd, arg0);
> > }
> > pid$1:libc.so.1:close:entry
> > {
> > printf("Closing %d,%d", arg0,arg1);
> > }
> >
> > But some how the two print statements do not match. Where am
> > I going wrong?
> > I am sure there are already good scripts to do it. Any help
> > is much appreciated.
> >
> > Siva
> > This message posted from opensolaris.org
> > _______________________________________________
> > dtrace-discuss mailing list
> > dtrace-discuss at opensolaris.org
> >
>
>
> ******************************************************************
> This message is intended only for the stated addressee(s) and
> may be confidential. Access to this email by anyone else is
> unauthorised. Any opinions expressed in this email do not
> necessarily reflect the opinions of royalblue. Any unauthorised
> disclosure, use or dissemination, either whole or in part is
> prohibited. If you are not the intended recipient of this message,
> please notify the sender immediately.
> ******************************************************************
>
> _______________________________________________
> dtrace-discuss mailing list
> dtrace-discuss at opensolaris.org
--
Adam Leventhal, Solaris Kernel Development http://blogs.sun.com/ahl