需求: 使用 trace_marker 来跟踪程序
trace_marker 是一个文件节点,允许用户程序写入字符串,frace 会记录写入动作的时间戳
cd /sys/kernel/debug/tracing/
echo nop > current_tracer // 设置function 跟踪器是不能捕捉到示踪标志
echo 1 > tracing_on // 打开ftrace 才能捕捉到示踪标志
echo 0 > tracing_on // 关闭ftrace
一、程序编写
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <linux/unistd.h>
#include <stdarg.h>
#include <unistd.h>
#include <ctype.h>
static int mark_fd = -1;
static __thread char buff[BUFSIZ+1];
static void setup_ftrace_marker(void)
{
struct stat st;
char *files[] = {
"/sys/kernel/debug/tracing/trace_marker",
"/debug/tracing/trace_marker",
"/debugfs/tracing/trace_marker",
};
int ret;
int i;
for (i = 0; i < (sizeof(files) / sizeof(char *)); i++) {
ret = stat(files[i], &st);
if (ret >= 0)
goto found;
}
/* todo, check mounts system */
printf("canot found the sys tracing\n");
return;
found:
mark_fd = open(files[i], O_WRONLY);
}
static void ftrace_write(const char *fmt, ...)
{
va_list ap;
int n;
if (mark_fd < 0)
return;
va_start(ap, fmt);
n = vsnprintf(buff, BUFSIZ, fmt, ap);
va_end(ap);
write(mark_fd, buff, n);
}
int main()
{
int count = 0;
setup_ftrace_marker();
ftrace_write("rlk start program\n");
while (1) {
usleep(100*1000);
count++;
ftrace_write("rlk count=%d\n", count);
}
}
编译: arm-linux-gnueabi-gcc trace_marker_test.c -o trace_marker_test --static
运行:
/sys/kernel/debug/tracing # /mnt/trace_marker_test
... 等待几秒
Ctrl+c 终止程序
/sys/kernel/debug/tracing # cat trace
# tracer: nop
#
# entries-in-buffer/entries-written: 170/170 #P:4
#
# _-----=> irqs-off
# / _----=> need-resched
# | / _---=> hardirq/softirq
# || / _--=> preempt-depth
# ||| / delay
# TASK-PID CPU# |||| TIMESTAMP FUNCTION
# | | | |||| | |
<...>-778 [000] ...1 124.054672: tracing_mark_write: rlk start program
<...>-778 [000] ...1 124.180858: tracing_mark_write: rlk count=1
<...>-778 [000] ...1 124.297803: tracing_mark_write: rlk count=2
<...>-778 [000] ...1 124.408593: tracing_mark_write: rlk count=3
<...>-778 [000] ...1 124.518589: tracing_mark_write: rlk count=4
<...>-778 [000] ...1 124.628374: tracing_mark_write: rlk count=5
<...>-778 [000] ...1 124.738971: tracing_mark_write: rlk count=6
<...>-778 [000] ...1 124.849366: tracing_mark_write: rlk count=7
<...>-778 [000] ...1 124.959502: tracing_mark_write: rlk count=8
<...>-778 [000] ...1 125.071421: tracing_mark_write: rlk count=9
<...>-778 [000] ...1 125.190520: tracing_mark_write: rlk count=10
<...>-778 [000] ...1 125.300335: tracing_mark_write: rlk count=11
<...>-778 [000] ...1 125.401471: tracing_mark_write: rlk count=12
<...>-778 [000] ...1 125.511686: tracing_mark_write: rlk count=13
<...>-778 [000] ...1 125.621551: tracing_mark_write: rlk count=14
<...>-778 [000] ...1 125.732367: tracing_mark_write: rlk count=15
<...>-778 [000] ...1 125.842521: tracing_mark_write: rlk count=16
<...>-778 [000] ...1 125.952524: tracing_mark_write: rlk count=17
<...>-778 [000] ...1 126.063389: tracing_mark_write: rlk count=18
<...>-778 [000] ...1 126.173682: tracing_mark_write: rlk count=19