根据man 5 proc,可以使用/proc文件系统在Linux上访问以下信息:
/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是否有明显的结构,但没有看到任何东西。
因此,有两个部分的问题:
/proc读取文本呢?如果是这样的话,是否有一个公认的使用C ( /proc )从/proc读取的“最佳实践”?(fscanf()?正则表达式?)/proc/[pid]/maps数据的话,你就可以得到额外的分数。=)发布于 2012-03-19 03:47:01
我认为对于perl或shell脚本来说,读取和解析/proc/ data是非常好的。在C程序中,如果需要健壮性,我将使用内核(可能是sysctl)接口。
结果是,procps捆绑的pmap实现逐行解析/proc/PID/maps文件如下(参见one_proc()函数):
sscanf(mapbuf,"%"KLF"x-%"KLF"x %31s %Lx %x:%x %Lu", &start, &end, flags, &file_offset, &dev_major, &dev_minor, &inode);编辑:我的答案的原始版本展示了一种仅在共享内存段上解析数据的方法,而不是按照OP的要求对所有映射的内存段进行解析。
我相信ipcs -m会为多个进程提供相同的数据。因此,第二个问题的答案是,您应该阅读ipcs代码:(例如,BSD版本,Linux版本):
发布于 2012-03-19 05:49:38
在Linux上,读取像/proc/self/maps这样的顺序文本伪文件是获取这些信息的标准方法(否则就没有一种容易的获取方法;这是内核给它的方式)。
您可以使用一些库(比如libproc)来实现这一点。但最终,应用程序将解析(可能通过一些库) /proc下的文件。我不确定你避免那样做是对的。
只需尝试运行像ps或top这样的命令,并卸载/proc。可能不会再起作用了。
发布于 2012-03-19 07:52:55
libproc提供了一种方便的方式来访问/proc下的内容。
https://stackoverflow.com/questions/9764639
复制相似问题