这篇来学习一下,strace -p 调试进程,关于网络编程的。
服务端:
lj@lj-virtual-machine:~/network-code/server$ cat server.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 4096
int main()
{
int listenfd,acceptfd;
socklen_t clilen;
struct sockaddr_in cliaddr,servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8888);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(struct sockaddr_in));
listen(listenfd,5);
acceptfd=accept(listenfd,(struct sockaddr *)NULL,NULL);
char recvbuf[200000];
while(1)
{
getchar();
read(acceptfd,recvbuf,sizeof(recvbuf));
}
getchar();
close(listenfd);
return 0;
}
lj@lj-virtual-machine:~/network-code/server$
客户端:
lj@lj-virtual-machine:~/network-code/client$ cat client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 4096
int main()
{
int sockfd;
struct sockaddr_in servaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8888);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
int ret=connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
char sendbuf[200000] = "123";
while(1)
{
getchar();
printf("send data\n");
write(sockfd,sendbuf,sizeof(sendbuf));
}
close(sockfd);
return 0;
}
lj@lj-virtual-machine:~/network-code/client$
编译客户端和服务端,并启动进程,在客户端和服务端分别按回车键,用strace -p追踪。
服务端:
root@lj-virtual-machine:/home/lj/network-code# strace -p 3739
Process 3739 attached
read(0, "\n", 1024) = 1
read(4, "123\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 200000) = 200000
read(0,
客户端:
root@lj-virtual-machine:/home/lj# strace -p 3740
Process 3740 attached
read(0, "\n", 1024) = 1
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 11), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa13a701000
write(1, "send data\n", 10) = 10
write(3, "123\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 200000) = 200000
read(0,
可以看到数据收发相关的系统调用。
strace -i ./client 追踪客户端的可执行文件
root@lj-virtual-machine:/home/lj/network-code/client# strace -i ./client
[00007ff1312f9137] execve("./client", ["./client"], [/* 63 vars */]) = 0
[00007effca25421c] brk(0) = 0x1f39000
[00007effca255617] access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
[00007effca255617] access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
[00007effca2555b7] open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
[00007effca255544] fstat(3, {st_mode=S_IFREG|0644, st_size=150584, ...}) = 0
[00007effca25573a] mmap(NULL, 150584, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7effca439000
[00007effca255717] close(3) = 0
[00007effca255617] access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
[00007effca2555b7] open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
[00007effca2555d7] read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P \2\0\0\0\0\0"..., 832) = 832
[00007effca255544] fstat(3, {st_mode=S_IFREG|0755, st_size=1857312, ...}) = 0
[00007effca25573a] mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7effca438000
[00007effca25573a] mmap(NULL, 3965632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7effc9e73000
[00007effca255787] mprotect(0x7effca031000, 2097152, PROT_NONE) = 0
[00007effca25573a] mmap(0x7effca231000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1be000) = 0x7effca231000
[00007effca25573a] mmap(0x7effca237000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7effca237000
[00007effca255717] close(3) = 0
[00007effca25573a] mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7effca436000
[00007effca23cf74] arch_prctl(ARCH_SET_FS, 0x7effca436740) = 0
[00007effca255787] mprotect(0x7effca231000, 16384, PROT_READ) = 0
[00007effca255787] mprotect(0x600000, 4096, PROT_READ) = 0
[00007effca255787] mprotect(0x7effca45e000, 4096, PROT_READ) = 0
[00007effca255767] munmap(0x7effca439000, 150584) = 0
[00007effc9f72277] socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
[00007effc9f71e20] connect(3, {sa_family=AF_INET, sin_port=htons(8888), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
[00007effc9f61d44] fstat(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 14), ...}) = 0
[00007effc9f6b53a] mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7effca45d000
[00007effc9f62360] read(0,
可以看到客户端朝哪个IP和PORT发数据。