lsof使用&交叉编译

本文最后更新于:2025年11月19日 下午

lsof是一个查看进程打开哪些文件的命令,平常使用ls -l /proc/pid/fd也能看。但不如lsof详细,它帮你解析了socket的连接信息等。不用自己去/proc/net/tcp等里面去找了

编译

我当前的版本为github上最新版本,约为4.99.4。
因为编译不过(rpc库链接找不到),我需要先做修改Configure,删除这几行,强制定义HASNORPC_H。修改如下
image.png

然后开始配置环境变量,编译。

1
2
3
4
export LSOF_CC=/home/leon/code/hisilicon-wifi7/tmp/luofu_cu3000_release/tools/cross_toolchain/arm-mix510-linux/arm-mix510-linux/bin/arm-mix510-linux-gcc
export LSOF_INCLUDE=/home/leon/code/hisilicon-wifi7/tmp/luofu_cu3000_release/tools/cross_toolchain/arm-mix510-linux/arm-mix510-linux/target/usr/include
./Configure linux
make

使用

我们看下lighttpd的信息

1
2
# -n 不解析主机名 -P 打印端口号数字,-p 指定pid 
./lsof -nPp 3107

输出如下:

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
31
32
33
34
35
36
COMMAND   PID USER  FD      TYPE DEVICE SIZE/OFF  NODE NAME
lighttpd 3107 root cwd DIR 31,11 0 1 /
lighttpd 3107 root rtd DIR 31,11 0 1 /
lighttpd 3107 root txt REG 31,11 515360 79 /bin/lighttpd
lighttpd 3107 root mem REG 31,11 2062520 1534 /usr/lib/libcrypto.so.1.1
lighttpd 3107 root mem REG 31,11 465192 1636 /usr/lib/libssl.so.1.1
lighttpd 3107 root mem REG 31,11 366280 1025 /lib/libpcre2-8.so.0.11.0
lighttpd 3107 root mem REG 31,11 694008 996 /lib/libc.so
lighttpd 3107 root DEL REG 0,1 18 /SYSV661400f7
lighttpd 3107 root mem REG 31,11 136 1868 /usr/share/zoneinfo/GMT-8
lighttpd 3107 root 0u CHR 1,3 0t0 72 /dev/null
lighttpd 3107 root 1u CHR 1,3 0t0 72 /dev/null
lighttpd 3107 root 2w CHR 5,1 0t0 21 /dev/console
lighttpd 3107 root 3r CHR 1,9 0t0 75 /dev/urandom
lighttpd 3107 root 4w REG 0,21 5 471 /var/run/lighttpd.pid
lighttpd 3107 root 5u IPv6 12312 0t0 TCP *:80 (LISTEN)
lighttpd 3107 root 6u IPv4 12313 0t0 TCP *:443 (LISTEN)
lighttpd 3107 root 7u IPv6 12314 0t0 TCP *:443 (LISTEN)
lighttpd 3107 root 8u IPv4 12315 0t0 TCP *:80 (LISTEN)
lighttpd 3107 root 9w REG 0,21 6444 472 /var/log/lighttpd_error.log
lighttpd 3107 root 10u a_inode 0,10 0 5679 [eventpoll:5,6,7,8,13,15,16,17,19,21]
lighttpd 3107 root 11r FIFO 0,9 0t0 12317 pipe
lighttpd 3107 root 12w FIFO 0,9 0t0 12317 pipe
lighttpd 3107 root 13u IPv4 749320 0t0 TCP 192.168.2.1:80->192.168.2.238:50296 (ESTABLISHED)
lighttpd 3107 root 15u IPv4 749326 0t0 TCP 192.168.2.1:80->192.168.2.238:50297 (ESTABLISHED)
lighttpd 3107 root 16u IPv4 749333 0t0 TCP 192.168.2.1:80->192.168.2.238:50298 (ESTABLISHED)
lighttpd 3107 root 17u IPv4 749378 0t0 TCP 192.168.2.1:80->192.168.2.123:52272 (ESTABLISHED)
lighttpd 3107 root 18r REG 31,11 283176 982 /home/www/static/css/vendor.01634eab.min.css
lighttpd 3107 root 19u IPv4 749383 0t0 TCP 192.168.2.1:80->192.168.2.123:52288 (ESTABLISHED)
lighttpd 3107 root 20r REG 31,11 863 979 /home/www/static/css/login.7f3726e1.min.css
lighttpd 3107 root 21u IPv4 749384 0t0 TCP 192.168.2.1:80->192.168.2.123:52292 (ESTABLISHED)
lighttpd 3107 root 22r REG 0,21 2834 19 /var/macro.js
lighttpd 3107 root 23r REG 31,11 5923 944 /home/www/js/runtime-08e20d77.bundle.js
lighttpd 3107 root 24r REG 31,11 1071469 952 /home/www/js/vendor-c1e9f0ac.bundle.js
lighttpd 3107 root 25r REG 31,11 2636 934 /home/www/js/login-16d40bd8.bundle.js
lighttpd 3107 root 26r REG 31,11 3626 912 /home/www/js/7104-1763dd2e.chunk.js

FD这列表示描述符.cwd-工作目录,rtd-进程根目录,mem-映射的内存文件,DEL-已被删除但占用的文件,u-读写,r-读,w-写。
SIZE/OFF这列表示大小,如果是普通文件,那么表示大小,如果是socket 0t0表示无意义。

内容非常翔实。排查socket卡住时,可以很方便的知道是哪个socket的问题。


人生苦短,远离bug Leon, 2025-03-20

lsof使用&交叉编译
https://leon0625.github.io/2025/03/20/15e1016500d3/
作者
leon.liu
发布于
2025年3月20日
许可协议