脚本概述 由于使用objdump反汇编linux内核的输出太多(2.6.32-220.el6.x86_64统计结果为1457706行),而很多时候只是想查看特定部分的机器码与汇编指令,例如函数的入口、堆栈、调用了哪个函数等,为了高效和通用,因此编写了一个简单的awk脚本,其命令行参数说明如下:
● SLINE表示匹配的起始行号(不小于1),SPAT表示匹配的起始行模式,这两个只能有一个生效,当都有效时,以SLINE为准。
● NUM表示从起始行开始的连续输出行数(不小于1,含起始行),EPAT表示匹配的结束行模式,这两个只能有一个生效,当都有效时,以NUM为准。
脚本实现 检查传值 由于向脚本传入的值在BEGIN块内没生效,在动作块{}和END块内有效,但若在{}内进行检查则太低效,因为处理每条记录都要判断,所以为了避免在{}内进行多余的判断,就在BEGIN块内解析命令行参数来间接获得传值,当传值无效时,给出提示并退出。
1
for(k=1;k<ARGC;++k){
2
str=ARGV[k]
3
if(1==match(str,"SLINE=")){
4
SLINE = substr(str,7)
5
}else if(1==match(str,"SPAT=")){
6
SPAT = substr(str,6)
7
}else if(1==match(str,"NUM=")){
8
NUM = substr(str,5)
9
}else if(1==match(str,"EPAT=")){
10
EPAT = substr(str,6)
11
}
12
}
13
14 if(SLINE<=0 && SPAT==""){
15 print "Usage: rangeshow must specifies valid SLINE which must be greater than 0, or SPAT which can't be empty"
16 exit 1
17 }
18
19 if(NUM<=0 && EPAT==""){
20 print "Usage: rangeshow must specifies valid NUM which must be greater than 0, or EPAT which can't be empty"
21
exit 1
22
} 结束处理 当处理了NUM条记录或匹配了结束行模式时,应退出动作块{}。
1
if(0==start_nr){
2
3
}else{
4
if(NUM>0) {
5
if(NR<start_nr+NUM) {
6
++matched_nr
7
print $0
8
}else
9
exit 0
10
11
}else{
12
++matched_nr
13
print $0
14
if(0!=match($0,EPAT))
15
exit 0
16
}
17
} 完整脚本下载:
rangeshow。
脚本示例
查看linux内核第10000行开始的10条指令,如下图
查看linux内核函数do_fork入口开始的10条指令,如下图

查看linux内核第10000行开始到callq的一段指令,如下图

查看linux内核函数do_exit入口到调用profile_task_exit的一段指令,如下图
posted on 2015-10-27 15:36
春秋十二月 阅读(1768)
评论(1) 编辑 收藏 引用 所属分类:
System