Chinaunix首页 | 论坛 | 博客
  • 博客访问: 898401
  • 博文数量: 194
  • 博客积分: 7991
  • 博客等级: 少将
  • 技术积分: 2067
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-09 22:03
文章分类

全部博文(194)

文章存档

2010年(8)

2009年(71)

2008年(109)

2007年(6)

我的朋友

分类: LINUX

2009-04-24 16:38:21

Using strace to figure out the work flow of an utility
 
Offen you want to know how an linux utility is implemented, if you have it's source codes around, just go throuth them to get your point, but if it's not available, which is offen the case, don't worry, we have got "strace".
 
"strace is a useful diagnostic, instructional, and debugging tool. It intercepts and records the system calls which are called by a process and the signals which are received by a process.  The name of each system call, its arguments and its return value are printed on standard error" -- from man pages.
 
Let's just see how it works(we use ifconfig as the example):
 
# strace ifconfig eth0
execve("/sbin/ifconfig", ["ifconfig", "eth0"], [/* 21 vars */]) = 0
uname({sys="Linux", node="esoogle.itc.inventec", ...}) = 0
brk(0)                                  = 0x880c000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
......
socket(PF_FILE, SOCK_DGRAM, 0)          = 3
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 4
access("/proc/net/if_inet6", R_OK)      = 0
socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 5
......
ioctl(5, SIOCGIFFLAGS, {ifr_name="eth0", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
ioctl(5, SIOCGIFHWADDR, {ifr_name="eth0", ifr_hwaddr=00:a0:d1:e1:e8:ac}) = 0
ioctl(5, SIOCGIFMETRIC, {ifr_name="eth0", ifr_metric=0}) = 0
ioctl(5, SIOCGIFMTU, {ifr_name="eth0", ifr_mtu=1500}) = 0
ioctl(5, SIOCGIFMAP, 0xbfe86970)        = 0
ioctl(5, SIOCGIFMAP, 0xbfe86970)        = 0
ioctl(5, SIOCGIFTXQLEN, 0xbfe86970)     = 0
......
ioctl(4, SIOCGIFADDR, {ifr_name="eth0", ifr_addr={AF_INET, inet_addr("10.190.60.39")}}) = 0
ioctl(4, SIOCGIFDSTADDR, {ifr_name="eth0", ifr_dstaddr={AF_INET, inet_addr("10.190.60.39")}}) = 0
ioctl(4, SIOCGIFBRDADDR, {ifr_name="eth0", ifr_broadaddr={AF_INET, inet_addr("10.255.255.255")}}) = 0
ioctl(4, SIOCGIFNETMASK, {ifr_name="eth0", ifr_netmask={AF_INET, inet_addr("255.0.0.0")}}) = 0
......
munmap(0xb7dea000, 4096)                = 0
exit_group(0)                           = ?

 
From the above output information, we now can have a very clear figure about "ifconfig"'s work flow:
 
It first creates sockets:
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 4
socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 5
 
And then use IOCTL interfaces to get information:
ioctl(4, SIOCGIFADDR, {ifr_name="eth0", ifr_addr={AF_INET, inet_addr("10.190.60.39")}}) = 0
.....
 
Very clear, isn't it?
 
阅读(711) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~