本文参考自Lttng
的官方文档。
(1)安装Lttng
:
我使用的是CentOS
,所以按照RHEL
的文档,使用yum
方式安装:
a)构建package
相关信息:
wget -P /etc/yum.repos.d/ http://packages.efficios.com/repo.files/EfficiOS-RHEL7-x86-64.repo
rpmkeys --import http://packages.efficios.com/rhel/repo.key
yum updateinfo
b)接下来安装lttng
软件包:
yum install lttng-ust-devel #安装 lttng-ust会同时安装liburcu0
yum install kmod-lttng-modules
yum install lttng-tools-devel
yum install babeltrace-devel
(2)测试一下:
lttng create my-session
lttng enable-event --kernel --all
lttng start
lttng stop
lttng stop
lttng destroy
接着执行ls
命令:
[root@CentOS ~]# ls
anaconda-ks.cfg lttng-traces
可以看到抓的trace
都在lttng-traces
这个文件夹里
(2)使用案例
main.cpp
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <lttng/tracef.h>
int main(int argc, char *argv[])
{
int8_t count = 0;
int8_t str[26] = {0};
while(1)
{
snprintf(str, sizeof(str), "%s, %d", "hello lttng", count++);
tracef("%d:%s", count, str);
printf("%d:%s\n", count, str);
sleep(1);
}
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(tracef)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} lttng-ust dl)
(3) 跟踪
3.1 启动应用程序
./tracef
3.2 启动会话守护进程
lttng-sessiond --daemonize
3.3 查看 tracepoint
lttng list --userspace
3.4 创建自定义会话
lttng create myself-tracef
3.5 使能跟踪事件
lttng enable-event --userspace 'lttng_ust_tracef:*'
3.6 开始跟踪
lttng start
3.7 停止跟踪
开始跟踪一段时间后即可停止跟踪,然后查看 LTTng 记录的信息。
lttng destroy
3.8 查看跟踪信息
babeltrace2 ~/lttng-traces/myself-tracef*