ftrace(二)实现-----scripts/recordmcount.pl 解析(一)

看了下recordmcount.pl,合着google看懂了其意图,因此我自己尝试着用一般的脚本重复wheel:

test.c:

static int ldat_one_data_default = 0;
int gdat_two_data_default = 1;

static int lfun_one_text_default(void)
{
    return 0;
}

static int __attribute__ ((__section__ (".sched.data"))) ldat_three_set_sched = 2;

static int lfun_two_text_default(void)
{
    return 1;
}

static int __attribute__ ((__section__ (".sched.text")))
lfun_three_set_sched(void)
{
    return 2;
}

int gfun_four_text_default(void)
{
    return 3;
}

int __attribute__ ((__section__ (".sched.text"))) gfun_five_set_sched(void)

 

{
    return 4;
}

 

int __attribute__ ((__section__ (".sched.data"))) gdat_four_set_sched = 3;

 

static int __attribute__ ((__section__ (".own.timer"))) lfun_six_set_own(void)
{
    return 5;
}

 

static int __attribute__ ((__section__ (".own.timer")))
lfun_seven_set_own(void)
{
    return 6;
}

 

static int __attribute__ ((__section__ (".own.timer")))
lfun_eight_set_own(void)
{
    return 7;
}

 

gcc -c -pg -O0 test.c -o test.o

脚本test.sh

#! /bin/bash                                                                                                                     

objdump -dhr $1 > jerry.s

sed -n \
        -e '/^[0-9a-fA-F]\{1,\}[[:blank:]]\{1,\}<\(.*\)>[[:blank:]]*:.*/=' \
        -e '/Disassembly of section[[:blank:]]\{1,\}\(.*\)[[:blank:]]*:.*/=' \
        -e '/^[[:blank:]]*\([0-9a-fA-F]\{1,\}\)[[:blank:]]*:.*\(mcount\).*/=' \
        -e 's/^[[:blank:]]*\([0-9a-fA-F]\{1,\}\)[[:blank:]]\{1,\}<\(.*\)>[[:blank:]]*:.*/function: 0x\1 \2/p' \
        -e 's/Disassembly of section[[:blank:]]\{1,\}\(.*\)[[:blank:]]*:.*/section: \1/p' \
        -e 's/^[[:blank:]]*\([0-9a-fA-F]\{1,\}\)[[:blank:]]*:.*\(mcount\).*/mcount: \2    0x\1/p' \
        jerry.s > jerry.txt

sed -i -n -e 'N;s/\n/ /gp' jerry.txt

current_line=""
nm $1 | sed -n -e 's/.*[[:blank:]]\{1,\}\(t\|T\)[[:blank:]]\{1,\}\(.*\)/\2 \1/p' | while read current_line
do
array_line=(${current_line})
first_item=${array_line[0]}
sed -i -e 's/\(.*\)'"${first_item}"'/\1'"${current_line}"'/' jerry.txt
done

测试:./test.sh test.o

得到jerry.txt文件

25 section: .text                                                                                                                
27 function: 0x0000000000000000 lfun_one_text_default t
31 mcount: mcount    0x5
36 function: 0x0000000000000010 lfun_two_text_default t
40 mcount: mcount    0x15   
45 function: 0x0000000000000020 gfun_four_text_default T
49 mcount: mcount    0x25   
54 section: .sched.text
56 function: 0x0000000000000000 lfun_three_set_sched t
60 mcount: mcount    0x5
65 function: 0x0000000000000010 gfun_five_set_sched T
69 mcount: mcount    0x15   
74 section: .own.timer
76 function: 0x0000000000000000 lfun_six_set_own t
80 mcount: mcount    0x5
85 function: 0x0000000000000010 lfun_seven_set_own t
89 mcount: mcount    0x15   
94 function: 0x0000000000000020 lfun_eight_set_own t
98 mcount: mcount    0x25

基本要素已经解析了出来,后面继续分析……
我就是很好奇,为了实现ftrace引入了如此多的东西。

2014-12-20 凌晨 2:52

posted on 2014-12-20 02:52  阿加  阅读(703)  评论(0)    收藏  举报

导航