打印日志代码及运行日志(多余日志被删除了些):
登录 - Gitee.comhttps://gitee.com/r77683962/linux-6.9.0/commit/55a53caa06c1472398fac30113c9731cb9e3b482
测试步骤和手段:
1、清空 kern.log;
2、使用wget 下载linux-6.9.tar.gz,这里需要注意基于url 解析出来的202.120.58.157:443,因为需要基于打印的日志来验证
root@a-VMware-Virtual-Platform:/var/log# echo "" > kern.log
root@a-VMware-Virtual-Platform:/var/log# wget https://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/kernel/v6.x/linux-6.9.tar.gz
--2024-06-18 10:56:47-- https://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/kernel/v6.x/linux-6.9.tar.gz
Resolving ftp.sjtu.edu.cn (ftp.sjtu.edu.cn)... 202.120.58.157, 2001:da8:8000:6023::230
Connecting to ftp.sjtu.edu.cn (ftp.sjtu.edu.cn)|202.120.58.157|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 232821653 (222M) [application/octet-stream]
Saving to: ‘linux-6.9.tar.gz’
linux-6.9.tar.gz 100%[===========================================>] 222.04M 1009KB/s in 3m 48s
2024-06-18 11:00:36 (995 KB/s) - ‘linux-6.9.tar.gz’ saved [232821653/232821653]
3、在下载的过程中(或者搜索有代码中打印的日志搜索到时备份kern.log)
代码:
pr_info("sk->sk_family: %u, sk->sk_portpair: %u, sk->sk_dport: %u, sk->sk_daddr: %u, sk->sk_rcv_saddr: %u",
sk->sk_family,
sk->sk_portpair,
sk->sk_dport,
sk->sk_daddr,
sk->sk_rcv_saddr);
在搜索日志
root@a-VMware-Virtual-Platform:/var/log# grep -Hrn "sk_family" kern.log
10:56:47.643581kernel: net/ipv4/tcp.c tcp_sendmsg TCP: sk->sk_family: 2, sk->sk_portpair: 3157441281, sk->sk_dport: 47873, sk->sk_daddr: 2637854922, sk->sk_rcv_saddr: 1979754688
10:56:47.721334kernel: net/ipv4/tcp.c tcp_sendmsg TCP: sk->sk_family: 2, sk->sk_portpair: 3157441281, sk->sk_dport: 47873, sk->sk_daddr: 2637854922, sk->sk_rcv_saddr: 1979754688
10:56:47.742188kernel: net/ipv4/tcp.c tcp_sendmsg TCP: sk->sk_family: 2, sk->sk_portpair: 3157441281, sk->sk_dport: 47873, sk->sk_daddr: 2637854922, sk->sk_rcv_saddr: 1979754688
10:59:21.204456kernel: net/ipv4/tcp.c tcp_sendmsg TCP: sk->sk_family: 2, sk->sk_portpair: 3049148416, sk->sk_dport: 20480, sk->sk_daddr: 297696697, sk->sk_rcv_saddr: 1979754688
在搜索日志的时候,首先看到 sk->sk_daddr: 2637854922和sk->sk_dport: 47873
2637854922转换成十六进制:0x9D3A78CA,然后再把每个字节转为十进制
0x9D=157
0x3A=58
0x78=120
0xCA=202
同理47873也是一样转换成十六进制0xBB01,把这两个字节换个位置0x01BB,再转为十进制就是443
其实就是网络字节序和主机字节序的转换,在文件系统解析也经常会碰到这样的类似转换。
这上面的结果就是Connecting to ftp.sjtu.edu.cn (ftp.sjtu.edu.cn)|202.120.58.157|:443... connected.这里的信息
这样在操作系统应用层使用wget从网络下载文件的流程,在操作系统内核运行的一部分日志解析大概就是这样的。
日志增长量是非常快的,学习的时候,需要注意。