ftrace uprobe使用填坑历程


8种机械键盘轴体对比
本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

准备

打算用一下ftrace对用户态程序的trace支持。

测试用程序test.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
static void
print_curr_state_one(void)
{
printf("This is the print current state one functionn");
}


static void
print_curr_state_two(void)
{
printf("This is the print current state two functionn");
}


int () {
while(1) {
print_curr_state_one();
sleep(1);
print_curr_state_two();
}
}

编译:

gcc -o test test.c

Obtain Offset:

objdump -d test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
000000000040055d <print_curr_state_one>:
40055d: 55 push %rbp
40055e: 48 89 e5 mov %rsp,%rbp
400561: bf 30 06 40 00 mov $0x400630,%edi
400566: e8 c5 fe ff ff callq 400430 <[email protected]>
40056b: 5d pop %rbp
40056c: c3 retq

000000000040056d <print_curr_state_two>:
40056d: 55 push %rbp
40056e: 48 89 e5 mov %rsp,%rbp
400571: bf 60 06 40 00 mov $0x400660,%edi
400576: e8 b5 fe ff ff callq 400430 <[email protected]>
40057b: 5d pop %rbp
40057c: c3 retq

添加uprobe trace event:

1
2
echo 'p:print_current_state_one /root/test/uprobe/uprobe:0x55d' >> /sys/kernel/debug/tracing/uprobe_events
echo 'p:print_current_state_two /root/test/uprobe/uprobe:0x56d' >> /sys/kernel/debug/tracing/uprobe_events

有时会出现Invalid argument的错误。用sudo su获取root权限。

这里注意,偏移的大小只写0x55d,不能写0x40055d

开启trace

先启动test程序:./test

echo 1 > /sys/kernel/debug/tracing/event/enable

如果此时cat /sys/kernel/debug/tracing/event/enable显示为X

echo 1 > /sys/kernel/debug/tracing/event/uprobes/enable

最后cat /sys/kernel/debug/tracing/trace应该就能看到了