我是靠谱客的博主 孤独项链,最近开发中收集的这篇文章主要介绍awk 解析maps文件中的地址,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

maps文件一般是这个样子:

pi@raspberrypi:~ $ sudo cat /proc/1/maps
54b88000-54c8d000 r-xp 00000000 b3:07 655537     /lib/systemd/systemd
54c9c000-54cac000 r--p 00104000 b3:07 655537     /lib/systemd/systemd
54cac000-54cad000 rw-p 00114000 b3:07 655537     /lib/systemd/systemd
5628b000-56354000 rw-p 00000000 00:00 0          [heap]
75900000-75929000 rw-p 00000000 00:00 0 
75929000-75a00000 ---p 00000000 00:00 0 
75b00000-75b29000 rw-p 00000000 00:00 0 
75b29000-75c00000 ---p 00000000 00:00 0 
75c4c000-75c4d000 ---p 00000000 00:00 0 
75c4d000-7644c000 rwxp 00000000 00:00 0 
7644c000-7644d000 ---p 00000000 00:00 0 
7644d000-76c4c000 rwxp 00000000 00:00 0 
76c4c000-76c8e000 rw-p 00000000 00:00 0 
76c8e000-76c92000 r-xp 00000000 b3:07 651732     /lib/arm-linux-gnueabihf/libattr.so.1.1.0
76c92000-76ca1000 ---p 00004000 b3:07 651732     /lib/arm-linux-gnueabihf/libattr.so.1.1.0
76ca1000-76ca2000 r--p 00003000 b3:07 651732     /lib/arm-linux-gnueabihf/libattr.so.1.1.0
76ca2000-76ca3000 rw-p 00004000 b3:07 651732     /lib/arm-linux-gnueabihf/libattr.so.1.1.0
76ca3000-76ca5000 r-xp 00000000 b3:07 651591     /lib/arm-linux-gnueabihf/libdl-2.19.so
76ca5000-76cb4000 ---p 00002000 b3:07 651591     /lib/arm-linux-gnueabihf/libdl-2.19.so
76cb4000-76cb5000 r--p 00001000 b3:07 651591     /lib/arm-linux-gnueabihf/libdl-2.19.so
76cb5000-76cb6000 rw-p 00002000 b3:07 651591     /lib/arm-linux-gnueabihf/libdl-2.19.so
76cb6000-76d18000 r-xp 00000000 b3:07 651752     /lib/arm-linux-gnueabihf/libpcre.so.3.13.1
76d18000-76d27000 ---p 00062000 b3:07 651752     /lib/arm-linux-gnueabihf/libpcre.so.3.13.1
76d27000-76d28000 r--p 00061000 b3:07 651752     /lib/arm-linux-gnueabihf/libpcre.so.3.13.1
76d28000-76d29000 rw-p 00062000 b3:07 651752     /lib/arm-linux-gnueabihf/libpcre.so.3.13.1
76d29000-76d2f000 r-xp 00000000 b3:07 651761     /lib/arm-linux-gnueabihf/librt-2.19.so
76d2f000-76d3e000 ---p 00006000 b3:07 651761     /lib/arm-linux-gnueabihf/librt-2.19.so
76d3e000-76d3f000 r--p 00005000 b3:07 651761     /lib/arm-linux-gnueabihf/librt-2.19.so
76d3f000-76d40000 rw-p 00006000 b3:07 651761     /lib/arm-linux-gnueabihf/librt-2.19.so
76d40000-76d53000 r-xp 00000000 b3:07 651716     /lib/arm-linux-gnueabihf/libkmod.so.2.2.8
76d53000-76d62000 ---p 00013000 b3:07 651716     /lib/arm-linux-gnueabihf/libkmod.so.2.2.8
76d62000-76d63000 r--p 00012000 b3:07 651716     /lib/arm-linux-gnueabihf/libkmod.so.2.2.8
76d63000-76d64000 rw-p 00013000 b3:07 651716     /lib/arm-linux-gnueabihf/libkmod.so.2.2.8
76d64000-76d67000 r-xp 00000000 b3:07 651697     /lib/arm-linux-gnueabihf/libcap.so.2.24
76d67000-76d76000 ---p 00003000 b3:07 651697     /lib/arm-linux-gnueabihf/libcap.so.2.24
76d76000-76d77000 r--p 00002000 b3:07 651697     /lib/arm-linux-gnueabihf/libcap.so.2.24
76d77000-76d78000 rw-p 00003000 b3:07 651697     /lib/arm-linux-gnueabihf/libcap.so.2.24
76d78000-76d90000 r-xp 00000000 b3:07 651756     /lib/arm-linux-gnueabihf/libaudit.so.1.0.0
76d90000-76d9f000 ---p 00018000 b3:07 651756     /lib/arm-linux-gnueabihf/libaudit.so.1.0.0
76d9f000-76da0000 r--p 00017000 b3:07 651756     /lib/arm-linux-gnueabihf/libaudit.so.1.0.0
76da0000-76da1000 rw-p 00018000 b3:07 651756     /lib/arm-linux-gnueabihf/libaudit.so.1.0.0
76da1000-76dab000 rw-p 00000000 00:00 0 
76dab000-76db6000 r-xp 00000000 b3:07 651734     /lib/arm-linux-gnueabihf/libpam.so.0.83.1
76db6000-76dc5000 ---p 0000b000 b3:07 651734     /lib/arm-linux-gnueabihf/libpam.so.0.83.1
76dc5000-76dc6000 r--p 0000a000 b3:07 651734     /lib/arm-linux-gnueabihf/libpam.so.0.83.1
76dc6000-76dc7000 rw-p 0000b000 b3:07 651734     /lib/arm-linux-gnueabihf/libpam.so.0.83.1
76dc7000-76de2000 r-xp 00000000 b3:07 651657     /lib/arm-linux-gnueabihf/libselinux.so.1
76de2000-76df1000 ---p 0001b000 b3:07 651657     /lib/arm-linux-gnueabihf/libselinux.so.1
76df1000-76df2000 r--p 0001a000 b3:07 651657     /lib/arm-linux-gnueabihf/libselinux.so.1
76df2000-76df3000 rw-p 0001b000 b3:07 651657     /lib/arm-linux-gnueabihf/libselinux.so.1
76df3000-76df4000 rw-p 00000000 00:00 0 
76df4000-76f1f000 r-xp 00000000 b3:07 651612     /lib/arm-linux-gnueabihf/libc-2.19.so
76f1f000-76f2f000 ---p 0012b000 b3:07 651612     /lib/arm-linux-gnueabihf/libc-2.19.so
76f2f000-76f31000 r--p 0012b000 b3:07 651612     /lib/arm-linux-gnueabihf/libc-2.19.so
76f31000-76f32000 rw-p 0012d000 b3:07 651612     /lib/arm-linux-gnueabihf/libc-2.19.so
76f32000-76f35000 rw-p 00000000 00:00 0 
76f35000-76f49000 r-xp 00000000 b3:07 651758     /lib/arm-linux-gnueabihf/libpthread-2.19.so
76f49000-76f59000 ---p 00014000 b3:07 651758     /lib/arm-linux-gnueabihf/libpthread-2.19.so
76f59000-76f5a000 r--p 00014000 b3:07 651758     /lib/arm-linux-gnueabihf/libpthread-2.19.so
76f5a000-76f5b000 rw-p 00015000 b3:07 651758     /lib/arm-linux-gnueabihf/libpthread-2.19.so
76f5b000-76f5d000 rw-p 00000000 00:00 0 
76f6f000-76f74000 r-xp 00000000 b3:07 528562     /usr/lib/arm-linux-gnueabihf/libarmmem.so
76f74000-76f83000 ---p 00005000 b3:07 528562     /usr/lib/arm-linux-gnueabihf/libarmmem.so
76f83000-76f84000 rw-p 00004000 b3:07 528562     /usr/lib/arm-linux-gnueabihf/libarmmem.so
76f84000-76fa4000 r-xp 00000000 b3:07 651597     /lib/arm-linux-gnueabihf/ld-2.19.so
76fab000-76fb3000 rw-p 00000000 00:00 0 
76fb3000-76fb4000 r--p 0001f000 b3:07 651597     /lib/arm-linux-gnueabihf/ld-2.19.so
76fb4000-76fb5000 rw-p 00020000 b3:07 651597     /lib/arm-linux-gnueabihf/ld-2.19.so
7ef76000-7ef97000 rwxp 00000000 00:00 0          [stack]
7eff2000-7eff3000 r-xp 00000000 00:00 0          [sigpage]
7eff3000-7eff4000 r--p 00000000 00:00 0          [vvar]
7eff4000-7eff5000 r-xp 00000000 00:00 0          [vdso]
ffff0000-ffff1000 r-xp 00000000 00:00 0          [vectors]
pi@raspberrypi:~ $ 

只有模块起始和结束地址,但是没有模块的size。有时我们需要size,要怎么来获取?比较容易的方法是写个shell脚本,大致思路是:提取第一列,再从第一列中提取出开始地址和结束地址,再两个相减得到size。

研究了一下awk的man,发现上述的几个步骤awk完全可以包办了:

sudo cat /proc/1/maps | awk '{split($1,RANGE,"-"); if(RANGE[1]~/^0x/){STARTADDR=strtonum(RANGE[1])} else{STARTADDR=strtonum("0x"RANGE[1])}; if(RANGE[2]~/^0x/){ENDADDR=strtonum(RANGE[2])} else{ENDADDR=strtonum("0x"RANGE[2])};ADDRSIZE=strtonum(ENDADDR-STARTADDR);printf("0x%x 0x%x 0x%x %sn", STARTADDR, ENDADDR, ADDRSIZE, $0)}'

大致解释一下:

{split($1,RANGE,"-")

此句意思是将第一列拆分成数组,结果保存在变量RANGE中,分割符为“-”。即:RANGE[1]为起始地址,RANGE[2]为结束地址

if(RANGE[1]~/^0x/){STARTADDR=strtonum(RANGE[1])} else{STARTADDR=strtonum("0x"RANGE[1])}

对于Linux的有些发行版来说,第一列的地址可能以“0x”开头,而有些则不是(比如本人使用的树莓派Raspbian GNU/Linux),这里兼容了一下这两种情况,结果STARTADDR的值为0x????????

if(RANGE[2]~/^0x/){ENDADDR=strtonum(RANGE[2])} else{ENDADDR=strtonum("0x"RANGE[2])}

同上,这里得到的ENDADDR值也是0x????????的形式

ADDRSIZE=strtonum(ENDADDR-STARTADDR)

这里调用了awk的內建函数strtonum,该函数将一个字符串转换为十进制数字,前面的STARTADDR和ENDADDR之所以要加上“0x”前缀也是因为strtonum的要求:如果是16进制数,则必须有前缀,否则会按照十进制处理,发生错误。注意awk要求引用变量时不需要加$符号。某些较低版本的awk可能会不支持strtonum,此时需要upgrade更新,或者下载gawk的最近代码重新编译:http://mirrors.ustc.edu.cn/gnu/

printf("0x%x 0x%x 0x%x %sn", STARTADDR, ENDADDR, ADDRSIZE, $0)

最后一步是格式化输出,这个跟C语言的printf是很相似的。这里$0表示原文件中一行的所有列。

最后得到的结果是这个样子:

0x54b88000 0x54c8d000 0x105000 54b88000-54c8d000 r-xp 00000000 b3:07 655537     /lib/systemd/systemd
0x54c9c000 0x54cac000 0x10000 54c9c000-54cac000 r--p 00104000 b3:07 655537     /lib/systemd/systemd
0x54cac000 0x54cad000 0x1000 54cac000-54cad000 rw-p 00114000 b3:07 655537     /lib/systemd/systemd
0x5628b000 0x56354000 0xc9000 5628b000-56354000 rw-p 00000000 00:00 0          [heap]
0x75900000 0x75929000 0x29000 75900000-75929000 rw-p 00000000 00:00 0 
0x75929000 0x75a00000 0xd7000 75929000-75a00000 ---p 00000000 00:00 0 
0x75b00000 0x75b29000 0x29000 75b00000-75b29000 rw-p 00000000 00:00 0 
0x75b29000 0x75c00000 0xd7000 75b29000-75c00000 ---p 00000000 00:00 0 
0x75c4c000 0x75c4d000 0x1000 75c4c000-75c4d000 ---p 00000000 00:00 0 
0x75c4d000 0x7644c000 0x7ff000 75c4d000-7644c000 rwxp 00000000 00:00 0 
0x7644c000 0x7644d000 0x1000 7644c000-7644d000 ---p 00000000 00:00 0 
0x7644d000 0x76c4c000 0x7ff000 7644d000-76c4c000 rwxp 00000000 00:00 0 
0x76c4c000 0x76c8e000 0x42000 76c4c000-76c8e000 rw-p 00000000 00:00 0 
0x76c8e000 0x76c92000 0x4000 76c8e000-76c92000 r-xp 00000000 b3:07 651732     /lib/arm-linux-gnueabihf/libattr.so.1.1.0
0x76c92000 0x76ca1000 0xf000 76c92000-76ca1000 ---p 00004000 b3:07 651732     /lib/arm-linux-gnueabihf/libattr.so.1.1.0
0x76ca1000 0x76ca2000 0x1000 76ca1000-76ca2000 r--p 00003000 b3:07 651732     /lib/arm-linux-gnueabihf/libattr.so.1.1.0
0x76ca2000 0x76ca3000 0x1000 76ca2000-76ca3000 rw-p 00004000 b3:07 651732     /lib/arm-linux-gnueabihf/libattr.so.1.1.0
0x76ca3000 0x76ca5000 0x2000 76ca3000-76ca5000 r-xp 00000000 b3:07 651591     /lib/arm-linux-gnueabihf/libdl-2.19.so
0x76ca5000 0x76cb4000 0xf000 76ca5000-76cb4000 ---p 00002000 b3:07 651591     /lib/arm-linux-gnueabihf/libdl-2.19.so
0x76cb4000 0x76cb5000 0x1000 76cb4000-76cb5000 r--p 00001000 b3:07 651591     /lib/arm-linux-gnueabihf/libdl-2.19.so
0x76cb5000 0x76cb6000 0x1000 76cb5000-76cb6000 rw-p 00002000 b3:07 651591     /lib/arm-linux-gnueabihf/libdl-2.19.so
0x76cb6000 0x76d18000 0x62000 76cb6000-76d18000 r-xp 00000000 b3:07 651752     /lib/arm-linux-gnueabihf/libpcre.so.3.13.1
0x76d18000 0x76d27000 0xf000 76d18000-76d27000 ---p 00062000 b3:07 651752     /lib/arm-linux-gnueabihf/libpcre.so.3.13.1
0x76d27000 0x76d28000 0x1000 76d27000-76d28000 r--p 00061000 b3:07 651752     /lib/arm-linux-gnueabihf/libpcre.so.3.13.1
0x76d28000 0x76d29000 0x1000 76d28000-76d29000 rw-p 00062000 b3:07 651752     /lib/arm-linux-gnueabihf/libpcre.so.3.13.1
0x76d29000 0x76d2f000 0x6000 76d29000-76d2f000 r-xp 00000000 b3:07 651761     /lib/arm-linux-gnueabihf/librt-2.19.so
0x76d2f000 0x76d3e000 0xf000 76d2f000-76d3e000 ---p 00006000 b3:07 651761     /lib/arm-linux-gnueabihf/librt-2.19.so
0x76d3e000 0x76d3f000 0x1000 76d3e000-76d3f000 r--p 00005000 b3:07 651761     /lib/arm-linux-gnueabihf/librt-2.19.so
0x76d3f000 0x76d40000 0x1000 76d3f000-76d40000 rw-p 00006000 b3:07 651761     /lib/arm-linux-gnueabihf/librt-2.19.so
0x76d40000 0x76d53000 0x13000 76d40000-76d53000 r-xp 00000000 b3:07 651716     /lib/arm-linux-gnueabihf/libkmod.so.2.2.8
0x76d53000 0x76d62000 0xf000 76d53000-76d62000 ---p 00013000 b3:07 651716     /lib/arm-linux-gnueabihf/libkmod.so.2.2.8
0x76d62000 0x76d63000 0x1000 76d62000-76d63000 r--p 00012000 b3:07 651716     /lib/arm-linux-gnueabihf/libkmod.so.2.2.8
0x76d63000 0x76d64000 0x1000 76d63000-76d64000 rw-p 00013000 b3:07 651716     /lib/arm-linux-gnueabihf/libkmod.so.2.2.8
0x76d64000 0x76d67000 0x3000 76d64000-76d67000 r-xp 00000000 b3:07 651697     /lib/arm-linux-gnueabihf/libcap.so.2.24
0x76d67000 0x76d76000 0xf000 76d67000-76d76000 ---p 00003000 b3:07 651697     /lib/arm-linux-gnueabihf/libcap.so.2.24
0x76d76000 0x76d77000 0x1000 76d76000-76d77000 r--p 00002000 b3:07 651697     /lib/arm-linux-gnueabihf/libcap.so.2.24
0x76d77000 0x76d78000 0x1000 76d77000-76d78000 rw-p 00003000 b3:07 651697     /lib/arm-linux-gnueabihf/libcap.so.2.24
0x76d78000 0x76d90000 0x18000 76d78000-76d90000 r-xp 00000000 b3:07 651756     /lib/arm-linux-gnueabihf/libaudit.so.1.0.0
0x76d90000 0x76d9f000 0xf000 76d90000-76d9f000 ---p 00018000 b3:07 651756     /lib/arm-linux-gnueabihf/libaudit.so.1.0.0
0x76d9f000 0x76da0000 0x1000 76d9f000-76da0000 r--p 00017000 b3:07 651756     /lib/arm-linux-gnueabihf/libaudit.so.1.0.0
0x76da0000 0x76da1000 0x1000 76da0000-76da1000 rw-p 00018000 b3:07 651756     /lib/arm-linux-gnueabihf/libaudit.so.1.0.0
0x76da1000 0x76dab000 0xa000 76da1000-76dab000 rw-p 00000000 00:00 0 
0x76dab000 0x76db6000 0xb000 76dab000-76db6000 r-xp 00000000 b3:07 651734     /lib/arm-linux-gnueabihf/libpam.so.0.83.1
0x76db6000 0x76dc5000 0xf000 76db6000-76dc5000 ---p 0000b000 b3:07 651734     /lib/arm-linux-gnueabihf/libpam.so.0.83.1
0x76dc5000 0x76dc6000 0x1000 76dc5000-76dc6000 r--p 0000a000 b3:07 651734     /lib/arm-linux-gnueabihf/libpam.so.0.83.1
0x76dc6000 0x76dc7000 0x1000 76dc6000-76dc7000 rw-p 0000b000 b3:07 651734     /lib/arm-linux-gnueabihf/libpam.so.0.83.1
0x76dc7000 0x76de2000 0x1b000 76dc7000-76de2000 r-xp 00000000 b3:07 651657     /lib/arm-linux-gnueabihf/libselinux.so.1
0x76de2000 0x76df1000 0xf000 76de2000-76df1000 ---p 0001b000 b3:07 651657     /lib/arm-linux-gnueabihf/libselinux.so.1
0x76df1000 0x76df2000 0x1000 76df1000-76df2000 r--p 0001a000 b3:07 651657     /lib/arm-linux-gnueabihf/libselinux.so.1
0x76df2000 0x76df3000 0x1000 76df2000-76df3000 rw-p 0001b000 b3:07 651657     /lib/arm-linux-gnueabihf/libselinux.so.1
0x76df3000 0x76df4000 0x1000 76df3000-76df4000 rw-p 00000000 00:00 0 
0x76df4000 0x76f1f000 0x12b000 76df4000-76f1f000 r-xp 00000000 b3:07 651612     /lib/arm-linux-gnueabihf/libc-2.19.so
0x76f1f000 0x76f2f000 0x10000 76f1f000-76f2f000 ---p 0012b000 b3:07 651612     /lib/arm-linux-gnueabihf/libc-2.19.so
0x76f2f000 0x76f31000 0x2000 76f2f000-76f31000 r--p 0012b000 b3:07 651612     /lib/arm-linux-gnueabihf/libc-2.19.so
0x76f31000 0x76f32000 0x1000 76f31000-76f32000 rw-p 0012d000 b3:07 651612     /lib/arm-linux-gnueabihf/libc-2.19.so
0x76f32000 0x76f35000 0x3000 76f32000-76f35000 rw-p 00000000 00:00 0 
0x76f35000 0x76f49000 0x14000 76f35000-76f49000 r-xp 00000000 b3:07 651758     /lib/arm-linux-gnueabihf/libpthread-2.19.so
0x76f49000 0x76f59000 0x10000 76f49000-76f59000 ---p 00014000 b3:07 651758     /lib/arm-linux-gnueabihf/libpthread-2.19.so
0x76f59000 0x76f5a000 0x1000 76f59000-76f5a000 r--p 00014000 b3:07 651758     /lib/arm-linux-gnueabihf/libpthread-2.19.so
0x76f5a000 0x76f5b000 0x1000 76f5a000-76f5b000 rw-p 00015000 b3:07 651758     /lib/arm-linux-gnueabihf/libpthread-2.19.so
0x76f5b000 0x76f5d000 0x2000 76f5b000-76f5d000 rw-p 00000000 00:00 0 
0x76f6f000 0x76f74000 0x5000 76f6f000-76f74000 r-xp 00000000 b3:07 528562     /usr/lib/arm-linux-gnueabihf/libarmmem.so
0x76f74000 0x76f83000 0xf000 76f74000-76f83000 ---p 00005000 b3:07 528562     /usr/lib/arm-linux-gnueabihf/libarmmem.so
0x76f83000 0x76f84000 0x1000 76f83000-76f84000 rw-p 00004000 b3:07 528562     /usr/lib/arm-linux-gnueabihf/libarmmem.so
0x76f84000 0x76fa4000 0x20000 76f84000-76fa4000 r-xp 00000000 b3:07 651597     /lib/arm-linux-gnueabihf/ld-2.19.so
0x76fab000 0x76fb3000 0x8000 76fab000-76fb3000 rw-p 00000000 00:00 0 
0x76fb3000 0x76fb4000 0x1000 76fb3000-76fb4000 r--p 0001f000 b3:07 651597     /lib/arm-linux-gnueabihf/ld-2.19.so
0x76fb4000 0x76fb5000 0x1000 76fb4000-76fb5000 rw-p 00020000 b3:07 651597     /lib/arm-linux-gnueabihf/ld-2.19.so
0x7ef76000 0x7ef97000 0x21000 7ef76000-7ef97000 rwxp 00000000 00:00 0          [stack]
0x7eff2000 0x7eff3000 0x1000 7eff2000-7eff3000 r-xp 00000000 00:00 0          [sigpage]
0x7eff3000 0x7eff4000 0x1000 7eff3000-7eff4000 r--p 00000000 00:00 0          [vvar]
0x7eff4000 0x7eff5000 0x1000 7eff4000-7eff5000 r-xp 00000000 00:00 0          [vdso]
0xffff0000 0xffff1000 0x1000 ffff0000-ffff1000 r-xp 00000000 00:00 0          [vectors]

 

转载于:https://www.cnblogs.com/shijianyujingshen/p/5905719.html

最后

以上就是孤独项链为你收集整理的awk 解析maps文件中的地址的全部内容,希望文章能够帮你解决awk 解析maps文件中的地址所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部