Dtracing gethostbyname_r

Today's dtrace “one liner” is to dig into why a process that is failing when a call to gethostbyname_r(). Now if the application reported all that was going on this would not be needed, meanwhile in the real world we are lucky enough to have dtrace. To test it out I'm tracing the getent(1) command. In real life it would use the -p flag to dtrace and the process ID of the daemon.

/usr/sbin/dtrace -n 'pid$target::gethostbyname_r:entry {
        self->name = arg0;
}
pid$target::gethostbyname_r:return / arg1 == 0 / {
        ustack(5);
}
pid$target::gethostbyname_r:return / self->name / {
        printf("%s", copyinstr(self->name));
        self->name = 0
}' -c "getent hosts xxxxxdredd"
dtrace: description 'pid$target::gethostbyname_r:entry ' matched 3 probes
dtrace: pid 4748 has exited
CPU     ID                    FUNCTION:NAME
  2  48759           gethostbyname_r:return 
              libnsl.so.1`gethostbyname_r+0xc4
              getent`dogethost+0x54
              getent`main+0x7c
              getent`_start+0x108

  2  48759           gethostbyname_r:return xxxxxdredd

It would be nice to be able to get the h_errno value as well but so far I've not managed that.

Update:

Jon Haslem kindly explained to me the subtleties of copyin() so that I can get the h_errno value.

/usr/sbin/dtrace -Zn 'pid$target::gethostbyname_r:entry {
        self->name = arg0; 
        self->errno = arg4; 
}
pid$target::gethostbyname_r:return / arg1 == 0 / {
        ustack(5);
        printf("%d %s h_errno %x", pid,
                copyinstr(self->name), 
        \*(int \*)copyin(self->errno,sizeof(int)));
}' -c "getent hosts xxxxxdredd"
dtrace: description 'pid$target::gethostbyname_r:entry ' matched 2 probes
dtrace: pid 5087 has exited
CPU     ID                    FUNCTION:NAME
  2  48764            gethostbyname_r:entry errno: d4220008
  2  48765           gethostbyname_r:return 
              libnsl.so.1`gethostbyname_r+0xc4
              getent`dogethost+0x54
              getent`main+0x7c
              getent`_start+0x108
5087 xxxxxdredd h_errno 1

# 
Comments:

The \*debug\* option mentioned in the options sections of the resolve.conf man page appears to be little known and very useful for getting applications to spit out what they are doing with resolver.

Does have the disadvantage of being system wide. Not looked into a way of setting RES_DEBUG on a per process level, but should be possible.

Posted by guest on July 03, 2008 at 03:54 AM BST #

[Trackback] The gethostbyname.d “one liner” is now no longer anywhere near a one liner as to not be funny. It does however do all the things you would want it to do. Printing the entire hostent 1 structure on success and all the h_error valu...

Posted by The dot in ... --- ... on July 07, 2008 at 11:11 AM BST #

Post a Comment:
Comments are closed for this entry.
About

This is the old blog of Chris Gerhard. It has mostly moved to http://chrisgerhard.wordpress.com

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today