によると、1は、Linux上で、以下の情報にアクセスするために/proc
ファイルシステムを使用することができます。Cのユーザスペースコードを使用してLinux/procインタフェースから読み込む最良の方法は何ですか?
/proc/[pid]/maps
A file containing the currently mapped memory regions and their access
permissions.
The format is:
address perms offset dev inode pathname
08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm
08056000-08058000 rw-p 0000d000 03:0c 64593 /usr/sbin/gpm
08058000-0805b000 rwxp 00000000 00:00 0
40000000-40013000 r-xp 00000000 03:0c 4165 /lib/ld-2.2.4.so
40013000-40015000 rw-p 00012000 03:0c 4165 /lib/ld-2.2.4.so
4001f000-40135000 r-xp 00000000 03:0c 45494 /lib/libc-2.2.4.so
40135000-4013e000 rw-p 00115000 03:0c 45494 /lib/libc-2.2.4.so
4013e000-40142000 rw-p 00000000 00:00 0
bffff000-c0000000 rwxp 00000000 00:00 0
where "address" is the address space in the process that it occupies,
"perms" is a set of permissions:
r = read
w = write
x = execute
s = shared
p = private (copy on write)
"offset" is the offset into the file/whatever, "dev" is the device
(major:minor), and "inode" is the inode on that device. 0 indicates
that no inode is associated with the memory region, as the case would
be with BSS (uninitialized data).
Under Linux 2.0 there is no field giving pathname.
を私は本当にC言語でのテキスト解析コードを書くことにしたくありません。むしろ、OSを呼び出すだけで、情報を構造体に直接読み込むことができます。私は/usr/include/linux
を見て、APIで明らかな構造があるかどうかを見ましたが、何も見えませんでした。
ので、2つのパートの質問:これは "悪い考え"
- を考慮することはできますか?つまり、カーネルインターフェースが変更された場合、ユーザプログラムがただちに文字列を噛んで、
/proc
のテキストを読み込むべきですか?その場合は、Cを使用して/proc
から読むための「ベストプラクティス」が受け入れられていますか? (fscanf()
?正規表現?) - 私はこのデータを直接読み取ることができるカーネルインタフェース(存在すると仮定)のドキュメントを見つける方法はありますか? (カーネルソース自体は、起動するのに最適な場所ですか?あれば、カーネルソースのどこを調べるべきですか?)どのインタフェースが
/proc/[pid]/maps
のデータを具体的に提供できるか分かっている場合は、 =)
Busybox "top"は少なくとも/ procファイルを読み込んでスクリーンスクレイプします。ユーザーランドからは利用できないさまざまな情報があると思います。しかし、それは醜いです。 – blueshift
は 'strtok'と' atoi'だけでまっすぐに見えます。'' \ n ''はレコード区切り文字で、' ''はフィールド区切り文字で、 'perms'は多分特殊ロジックを必要とします。 – bkconrad
は、sscanfを使用する方が 'strtok'と' atoi'より高速です。 – Paschalis