Rumah  >  Artikel  >  Tutorial sistem  >  Analisis ringkas fail dalam direktori /proc/[pid] di Linux

Analisis ringkas fail dalam direktori /proc/[pid] di Linux

WBOY
WBOYke hadapan
2024-03-16 19:31:121165semak imbas

Inti Linux menyediakan mekanisme untuk mengakses struktur data dalaman kernel dan menukar tetapan kernel semasa masa jalan melalui sistem fail proc. Sistem fail proc ialah sistem fail pseudo yang hanya wujud dalam ingatan dan tidak menempati ruang memori luaran. Ia menyediakan antara muka untuk mengakses data kernel sistem dalam bentuk sistem fail.
简析Linux中 /proc/[pid] 目录的各文件

Pengguna dan aplikasi boleh mendapatkan maklumat sistem melalui proc dan menukar parameter tertentu kernel. Memandangkan maklumat sistem, seperti proses, berubah secara dinamik, apabila pengguna atau aplikasi membaca fail proc, sistem fail proc membaca secara dinamik maklumat yang diperlukan daripada kernel sistem dan menyerahkannya.

Tidak semua fail atau subfolder yang disenaraikan di bawah wujud dalam sistem anda, bergantung pada konfigurasi kernel anda dan modul yang dimuatkan. Di samping itu, terdapat tiga direktori yang sangat penting di bawah proc: net, scsi dan sys. Direktori sys boleh ditulis dan boleh digunakan untuk mengakses atau mengubah suai parameter kernel, manakala net dan scsi bergantung pada konfigurasi kernel. Sebagai contoh, jika sistem tidak menyokong scsi, direktori scsi tidak wujud.

Selain yang diperkenalkan di atas, terdapat juga beberapa direktori yang dinamakan dengan nombor, iaitu direktori proses. Setiap proses yang sedang berjalan dalam sistem mempunyai direktori yang sepadan di bawah proc, dengan nombor PID proses sebagai nama direktori Ia adalah antara muka untuk membaca maklumat proses. Direktori diri ialah antara muka maklumat untuk membaca proses itu sendiri, dan merupakan pautan.

/proc/[pid]/auxv

/proc/[pid]/auxv mengandungi maklumat penterjemah ELF yang dihantar kepada proses ini ialah setiap item ialah ID panjang yang tidak ditandatangani ditambah dengan nilai panjang yang tidak ditandatangani. Item terakhir bermula dengan dua 0x00s berturut-turut. Contohnya seperti berikut:

$ hexdump -x /proc/2406/auxv

0000000    0021    0000    0000    0000    f000    f7fa    7fff    0000
0000010    0010    0000    0000    0000    fbff    1f8b    0000    0000
0000020    0006    0000    0000    0000    1000    0000    0000    0000
0000030    0011    0000    0000    0000    0064    0000    0000    0000
0000040    0003    0000    0000    0000    0040    0040    0000    0000
0000050    0004    0000    0000    0000    0038    0000    0000    0000
0000060    0005    0000    0000    0000    0007    0000    0000    0000
0000070    0007    0000    0000    0000    0000    0000    0000    0000
0000080    0008    0000    0000    0000    0000    0000    0000    0000
0000090    0009    0000    0000    0000    55e0    0045    0000    0000
00000a0    000b    0000    0000    0000    0000    0000    0000    0000
00000b0    000c    0000    0000    0000    0000    0000    0000    0000
00000c0    000d    0000    0000    0000    0000    0000    0000    0000
00000d0    000e    0000    0000    0000    0000    0000    0000    0000
00000e0    0017    0000    0000    0000    0000    0000    0000    0000
00000f0    0019    0000    0000    0000    f079    f7f6    7fff    0000
0000100    001f    0000    0000    0000    ffea    f7f6    7fff    0000
0000110    000f    0000    0000    0000    f089    f7f6    7fff    0000
0000120    0000    0000    0000    0000    0000    0000    0000    0000
0000130
/proc/[pid]/cmdline

/proc/[pid]/cmdline ialah fail baca sahaja yang mengandungi maklumat baris arahan lengkap proses tersebut. Jika proses telah ditukar daripada memori atau proses itu adalah proses zombi, fail ini tidak akan mempunyai kandungan. Fail berakhir dengan aksara nol dan bukannya aksara baris baharu. Contohnya seperti berikut:

$ ps aux|grep frps
root      2406  0.1  0.1  54880 10524 ?        Sl   Dec11  21:30 frps -c ./frps.ini

$ cat /proc/2406/cmdline
frps-c./frps.ini
/proc/[pid]/comm

/proc/[pid]/comm mengandungi nama arahan proses. Contohnya seperti berikut:

$ cat /proc/2406/comm
frps
/proc/[pid]/cwd

/proc/[pid]/cwd ialah pautan simbolik kepada direktori kerja semasa proses tersebut. Contohnya seperti berikut:

$ ls -lt /proc/2406/cwd
lrwxrwxrwx 1 root root 0 Dec 12 20:39 /proc/2406/cwd -> /home/mike/frp_0.13.0_linux_amd64
/proc/[pid]/environ

/proc/[pid]/environ memaparkan pembolehubah persekitaran proses. Contohnya seperti berikut:

$ strings /proc/2406/environ

SUPERVISOR_GROUP_NAME=ssh
TERM=linux
SUPERVISOR_SERVER_URL=unix:///var/run/supervisor.sock
SUPERVISOR_PROCESS_NAME=ssh
RUNLEVEL=2
UPSTART_EVENTS=runlevel
PREVLEVEL=N
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
UPSTART_INSTANCE=
UPSTART_JOB=rc
SUPERVISOR_ENABLED=1
runlevel=2
PWD=/
previous=N
/proc/[pid]/exe

/proc/[pid]/exe ialah pautan simbolik kepada program yang sedang berjalan. Contohnya seperti berikut:

$ ls -lt /proc/2406/exe
lrwxrwxrwx 1 root root 0 Dec 11 19:00 /proc/2406/exe -> /usr/bin/frps
/proc/[pid]/fd

/proc/[pid]/fd ialah direktori yang mengandungi fail yang dibuka melalui proses. Contohnya seperti berikut:

$ ls -lt /proc/2406/fd

lrwx------ 1 root root 64 Dec 24 09:39 77 -> socket:[44377722]
lrwx------ 1 root root 64 Dec 17 15:07 47 -> socket:[29482617]
lr-x------ 1 root root 64 Dec 12 20:18 0 -> pipe:[13282]
l-wx------ 1 root root 64 Dec 12 20:18 1 -> pipe:[13283]
lrwx------ 1 root root 64 Dec 12 20:18 10 -> socket:[12238218]
lrwx------ 1 root root 64 Dec 12 20:18 4 -> anon_inode:[eventpoll]
lrwx------ 1 root root 64 Dec 12 20:18 40 -> socket:[19378614]

Setiap item dalam direktori ialah pautan simbolik yang menghala ke fail terbuka, dan nombor itu mewakili deskriptor fail.

/proc/[pid]/latency

/proc/[pid]/latency menunjukkan kod yang menyebabkan kelewatan yang lebih besar. Jika anda ingin menggunakan ciri ini, anda perlu melaksanakan:

$ echo 1 > /proc/sys/kernel/latencytop

Contohnya adalah seperti berikut

$ cat /proc/2406/latency

Latency Top version : v0.1
30667 10650491 4891 poll_schedule_timeout do_sys_poll SyS_poll system_call_fastpath 0x7f636573dc1d
8 105 44 futex_wait_queue_me futex_wait do_futex SyS_futex system_call_fastpath 0x7f6365a167bc

Tiga nombor pertama dalam setiap baris ialah bilangan kali kod berikut dilaksanakan, jumlah masa tunda pelaksanaan (unit ialah mikrosaat) dan masa tunda pelaksanaan maksimum (unit ialah mikrosaat). Yang berikut ialah timbunan panggilan lengkap kod.

/proc/[pid]/maps

/proc/[pid]/maps 显示进程的内存区域映射信息。举例如下:

$ cat /proc/2406/maps
00400000-006ea000 r-xp 00000000 fd:01 1727569                            /usr/bin/frps
006ea000-00a6c000 r--p 002ea000 fd:01 1727569                            /usr/bin/frps
00a6c000-00ab1000 rw-p 0066c000 fd:01 1727569                            /usr/bin/frps
00ab1000-00ad4000 rw-p 00000000 00:00 0
c000000000-c00000b000 rw-p 00000000 00:00 0
c41feac000-c420000000 rw-p 00000000 00:00 0
c420000000-c420400000 rw-p 00000000 00:00 0
c420400000-c420700000 rw-p 00000000 00:00 0
c420700000-c420800000 rw-p 00000000 00:00 0
c420800000-c420900000 rw-p 00000000 00:00 0
c420900000-c420a00000 rw-p 00000000 00:00 0
c420a00000-c421ea0000 rw-p 00000000 00:00 0
c421ea0000-c422a00000 rw-p 00000000 00:00 0
c422a00000-c422a60000 rw-p 00000000 00:00 0
7f0418c01000-7f0418ee1000 rw-p 00000000 00:00 0
7ffff7f4f000-7ffff7f70000 rw-p 00000000 00:00 0                          [stack:5121]
7ffff7fad000-7ffff7faf000 r--p 00000000 00:00 0                          [vvar]
7ffff7faf000-7ffff7fb1000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

其中注意的一点是 [stack:] 是线程的堆栈信息,对应于 /proc/[pid]/task/[tid]/ 路径。

/proc/[pid]/root

/proc/[pid]/root 是进程根目录的符号链接。举例如下:

$  ls -lt /proc/2406/root
lrwxrwxrwx 1 root root 0 Dec 12 20:39 /proc/2406/root -> /
/proc/[pid]/stack

/proc/[pid]/stack 示当前进程的内核调用栈信息,只有内核编译时打开了 CONFIG_STACKTRACE 编译选项,才会生成这个文件。举例如下:

$ cat /proc/2406/stack
[<ffffffff810fa996>] futex_wait_queue_me+0xc6/0x130
[<ffffffff810fb05d>] futex_wait+0x17d/0x270
[<ffffffff810fd2d5>] do_futex+0xd5/0x520
[<ffffffff810fd791>] SyS_futex+0x71/0x150
[<ffffffff8180cc76>] entry_SYSCALL_64_fastpath+0x16/0x75
[<ffffffffffffffff>] 0xffffffffffffffff
</ffffffffffffffff></ffffffff8180cc76></ffffffff810fd791></ffffffff810fd2d5></ffffffff810fb05d></ffffffff810fa996>
/proc/[pid]/statm

/proc/[pid]/statm 显示进程所占用内存大小的统计信息。包含七个值,度量单位是 page(page大小可通过 getconf PAGESIZE 得到)。举例如下:

$ cat /proc/2406/statm
13720 2617 493 746 0 12007 0

各个值含义:

a)进程占用的总的内存
b)进程当前时刻占用的物理内存
c)同其它进程共享的内存
d)进程的代码段
e)共享库(从2.6版本起,这个值为0)
f)进程的堆栈
g)dirty pages(从2.6版本起,这个值为0)
/proc/[pid]/status

/proc/[pid]/status 包含进程的状态信息。其很多内容与 /proc/[pid]/stat 和 /proc/[pid]/statm 相同,但是却是以一种更清晰地方式展现出来。举例如下:

$ cat /proc/2406/status
Name:   frps
State:  S (sleeping)
Tgid:   2406
Ngid:   0
Pid:    2406
PPid:   2130
TracerPid:  0
Uid:    0   0   0   0
Gid:    0   0   0   0
FDSize: 128
Groups: 0
NStgid: 2406
NSpid:  2406
NSpgid: 2406
NSsid:  2130
VmPeak:    54880 kB
VmSize:    54880 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:     34872 kB
VmRSS:     10468 kB
VmData:    47896 kB
VmStk:       132 kB
VmExe:      2984 kB
VmLib:         0 kB
VmPTE:        68 kB
VmPMD:        20 kB
VmSwap:        0 kB
HugetlbPages:          0 kB
Threads:    11
SigQ:   0/31834
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000000
SigCgt: fffffffe7fc1feff
CapInh: 0000000000000000
CapPrm: 0000003fffffffff
CapEff: 0000003fffffffff
CapBnd: 0000003fffffffff
CapAmb: 0000000000000000
Seccomp:    0
Cpus_allowed:   f
Cpus_allowed_list:  0-3
Mems_allowed:   00000000,00000001
Mems_allowed_list:  0
voluntary_ctxt_switches:    2251028
nonvoluntary_ctxt_switches: 18031

关于信号(signal)的信息:SigQ 分为两部分(例如 0/31834),前面表示当前处在队列中的信号(0),后面则表示队列一共可以存储多少信号(31834);SigPnd 表示当前线程 pending 的信号,而ShdPnd 则表示整个进程 pending 的信号;SigBlk、SigIgn 和 SigCgt 分别表示对信号的处理是阻塞,忽略,还是捕获。(关于Unix信号的相关知识,可以参考 Unix: Dealing with signals)。

/proc/[pid]/syscall

/proc/[pid]/syscall 显示当前进程正在执行的系统调用。举例如下:

$ cat /proc/2406/syscall
202 0xab3730 0x0 0x0 0x0 0x0 0x0 0x7ffff7f6ec68 0x455bb3

第一个值是系统调用号(202代表poll),后面跟着 6 个系统调用的参数值(位于寄存器中),最后两个值依次是堆栈指针和指令计数器的值。如果当前进程虽然阻塞,但阻塞函数并不是系统调用,则系统调用号的值为 -1,后面只有堆栈指针和指令计数器的值。如果进程没有阻塞,则这个文件只有一个 running 的字符串。

内核编译时打开了 CONFIG_HAVE_ARCH_TRACEHOOK 编译选项,才会生成这个文件。

/proc/[pid]/wchan

/proc/[pid]/wchan 显示当进程 sleep 时,kernel 当前运行的函数。举例如下:

$ cat /proc/2406/wchan
futex_wait_queue_meadmin

Atas ialah kandungan terperinci Analisis ringkas fail dalam direktori /proc/[pid] di Linux. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:linuxprobe.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam