免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 938 | 回复: 0
打印 上一主题 下一主题

Figure out the work flow of an linux utility [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-24 16:38 |只看该作者 |倒序浏览
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?



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8754/showart_1909750.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP