首页 > 其他分享 >【调试】ftrace(二)新增跟踪点

【调试】ftrace(二)新增跟踪点

时间:2023-03-03 22:44:27浏览次数:46  
标签:ftrace TRACE trace timer sample 跟踪 foo EVENT 调试

内核的各个子系统已经有大量的跟踪点,如果这些跟踪点无法满足工作中的需求,可以自己手动添加跟踪点。

添加跟踪点有两种方式,一种是仿照events/目录下的跟踪点,使用TRACE_EVENT() 宏添加。另一种是参考内核目录samples/trace_events添加。本文对这两种方式分别进行介绍。

使用 TRACE_EVENT 定义 tracepoint

我们仿照events/timer/timer_start,添加一个timer_stat的跟踪点,获取start_pidslack参数。

首先,需要在include/trace/events/timer.h头文件种添加名为timer_stat的跟踪点。

/**
 * timer_stat - ftrace interface timer_stat
 * @timer:	pointer to struct timer_list
 */
TRACE_EVENT(timer_stat,

	TP_PROTO(struct timer_list *timer),

	TP_ARGS(timer),

	TP_STRUCT__entry(
		__field( void *,	timer		)
		__field( int,		start_pid		)
		__field( int,		slack)
	),

	TP_fast_assign(
		__entry->timer			= timer;
		__entry->start_pid		= timer->start_pid;
		__entry->slack			= timer->slack;
	),

	TP_printk("ftrace interface timer_stat:timer=%p pid=%d slack=%d\n",
		  __entry->timer,__entry->start_pid,__entry->slack)
);

TRACE_EVENT()宏如下

#define TRACE_EVENT(name, proto, args, struct, assign, print)	\
	DEFINE_TRACE(name)
  • name:表示跟踪点的名字,如上面的timer_stat。
  • proto:表示跟踪点调用的入参的原型,比如timer类型为struct timer_list *
  • args:表示参数。
  • struct:定义跟踪器内部使用的__entry数据结构。
  • assign:把参数复制到__entry数据结构中。
  • print:定义输出的格式。

接着在kernel/kernel/time/timer.c debug_activate()添加trace_timer_stat()

static inline void
debug_activate(struct timer_list *timer, unsigned long expires)
{
        debug_timer_activate(timer);
        trace_timer_start(timer, expires, timer->flags);
        trace_timer_stat(timer);
}

重新编译内核后,烧写到设备中,即可看到sys节点已经有了新增的跟踪点。

使能跟踪点后,查看trace点的输出。

编译为独立的ko文件

内核还提供了一个跟踪点的例子,在samples/trace_events 目录下。

trace_event_init()创建内核线程一个名为event-sample内核线程。

static int __init trace_event_init(void)
{
	simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
	if (IS_ERR(simple_tsk))
		return -1;

	return 0;
}

kthread_should_stop()用于创建的线程检查结束标志,并决定是否退出。

static int simple_thread(void *arg)
{
	int cnt = 0;

	while (!kthread_should_stop())
		simple_thread_func(cnt++);

	return 0;
}

set_current_state() 来设置进程的状态,设置为TASK_INTERRUPTIBLE表示是可以被信号和wake_up()唤醒的,当信号到来时,进程会被设置为可运行。

schedule_timeout()将当前task调度出cpu,重新调度间隔为HZ。接着trace_开头的函数就会依次打印跟踪点的信息。

static void simple_thread_func(int cnt)
{
	int array[6];
	int len = cnt % 5;
	int i;

	set_current_state(TASK_INTERRUPTIBLE);
	schedule_timeout(HZ);

	for (i = 0; i < len; i++)
		array[i] = i + 1;
	array[i] = 0;

	/* Silly tracepoints */
	trace_foo_bar("hello", cnt, array, random_strings[len],
		      tsk_cpus_allowed(current));

	trace_foo_with_template_simple("HELLO", cnt);

	trace_foo_bar_with_cond("Some times print", cnt);

	trace_foo_with_template_cond("prints other times", cnt);

	trace_foo_with_template_print("I have to be different", cnt);
}

trace_foo_with_template_simple跟踪点的实现方式也是使用的TRACE_EVENT ()宏,这里不再赘述。

最后将文件编译为ko拷贝到设备上insmod后,即可看到sys目录下已经有新增的节点。

cd	/home/zhongyi/code/rk3399_linux_release_v2.5.1_20210301/kernel/samples/trace_events
make -C /home/zhongyi/code/rk3399_linux_release_v2.5.1_20210301/kernel/ M=$(pwd) modules
root@firefly:/sys/kernel/debug/tracing# cat available_events | grep sample
sample-trace:foo_bar
sample-trace:foo_bar_with_cond
race:foo_bar_with_fn
sample-trace:foo_with_template_simple
sample-trace:foo_with_template_cond
sample-trace:foo_with_template_fn
sample-trace:foo_with_template_print
power:pstate_sample
root@firefly:/sys/kernel/debug/tracing# cd events/sample-trace/
root@firefly:/sys/kernel/debug/tracing/events/sample-trace# ls
enable   foo_bar_with_cond       foo_with_template_fn
filter   foo_bar_with_fn         foo_with_template_print
foo_bar  foo_with_template_cond  foo_with_templ_simple
root@firefly:/sys/kernel/debug/tracing/events/sample-trace# echo 1 > enable 
root@firefly:/sys/kernel/debug/tracing/events/sample-trace# cat /sys/kernel/debug/tracing/trace

TRACE_EVENT_CONDITION()

在某些情况下,跟踪点只有在某个条件发生时才会被调用,类似于

if (cond)
	trace_foo();

TRACE_EVENT_CONDITION()宏就是这个作用,它和TRACE_EVENT()相比只是在参数中多加了一个cond条件。TP_CONDITION()会对条件做个判断。

TRACE_EVENT(name, proto, args, struct, assign, printk)
TRACE_EVENT_CONDITION(name, proto, args, cond, struct, assign, printk)

详细使用方法可以参考trace-events-sample.h

TRACE_EVENT_FN()

TRACE_EVENT_FN()是在跟踪点使能前和使能后分别打印一些信息。相比于TRACE_EVENT()TRACE_EVENT_FN()多了两个参数regunreg

TRACE_EVENT(name, proto, args, struct, assign, printk)
TRACE_EVENT_FN( name, proto, args, struct, assign, printk, reg, unreg)

regunreg原型为

void reg(void)

reg函数在跟踪点使能前打印,unreg函数在跟踪点使能后打印。regunreg可以根据实际情况置其中一个为NULL,也可以全部置为NULL。

详细使用方法可以参考trace-events-sample.h

本文参考

samples/trace_events

标签:ftrace,TRACE,trace,timer,sample,跟踪,foo,EVENT,调试
From: https://www.cnblogs.com/dongxb/p/17177262.html

相关文章

  • WeNet调试
    运行:参照:markdown问题:CMakeError:Error:generator:NinjaNinja:提高构建速度wenet/runtime/libtorch/fc_base/libtorch-src/lib/libtorch_cuda_cpp.so:undef......
  • 昇思MindSpore报错调试宝典(三):网络构建与训练类报错之语法问题
    欢迎前往MindSpore论坛进行该文章的浏览:https://www.hiascend.com/forum/thread-0232113194535560002-1-1.html上一期讲解了网络构建与训练类报错问题中context配置问题该......
  • [VS工程技巧]远程调试工具及dump文件来检查程序崩溃及异常等问题
    做什么之前有一次梦中所得,既然可以让vs附加到进程去调试活动的dll,那要是可以让我本地的电脑去调试别人客户端或者测试环境的DLL就好了,这样就可以不通过dbgview去一个个输......
  • Android studio 使用小米手机(Redmi K30 Pro)真机调试
    Androidstudio虚拟机调试启动很慢,而且有些应用还必须要用手机调试,还得把apk发到手机上安装,比较麻烦。所以手机通过USB直接连接电脑在线调试,方便快捷。手边有一部小米的R......
  • 蓝牙调试工具集合汇总
     BLE该部分主要分享一下常用的蓝牙调试工具,方便后续蓝牙抓包及分析。1hciconfig工具介绍:​​hciconfig​​,​​HCI​​设备配置工具命令格式:​​hciconfig​​、​​hcic......
  • react-native webView网页调试之setWebContentsDebuggingEnabled使用
    Android使用方法首先,我们需要在应用程序中调用WebView.setWebContentsDebuggingEnabled(true);文件位置:在MainActivity.java的onCreate方法内(在android/app/src/ma......
  • 问题跟踪记录
    1.redis缓存机制--donehttps://www.cnblogs.com/iknow-manage/articles/17171313.htmlredis的数据类型有哪些?应用场景有哪些?redis缓存的读写策略,常见问题有哪些,怎么解......
  • 移动端开发调试入门
    目录基础概念原理调试技巧一、基础概念1WebView(1)定义WebView,直译是网页视图,是一个基于webkit内核的,用于显示网页的控件,具备渲染Web页面的功能。Android的......
  • Servlet-Http协议、Session会话跟踪技术
    资料来源于:B站尚硅谷JavaWeb教程(全新技术栈,全程实战),本人才疏学浅,记录笔记以供日后回顾由于是多个视频内容混合在一起,因此只放了第一个链接视频链接Http协议1......
  • Goland断点调试一直进gopark
    现象使用Goland断点调试一直进gopark分析直接运行调试,不打断点,会有一个warning:undefinedbehavior-versionofDelveistoooldforGoversion1.20.0(maximu......