前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >动态分析C语言代码生成函数调用关系的利器——perf

动态分析C语言代码生成函数调用关系的利器——perf

作者头像
方亮
发布2024-03-19 08:26:23
1240
发布2024-03-19 08:26:23
举报
文章被收录于专栏:方亮方亮

perf是一套linux操作系统上分析工具集,分析函数调用关系只是其一个子集功能。它并不像《动态分析C语言代码生成函数调用关系的利器——gprof》中介绍的需要在被分析程序的编译指令中插入新的选项(-pg),而是直接对原始编译结果进行分析。

环境准备

安装

perf工具集并不默认安装在系统中,需要进行安装。(找到你系统匹配的版本,我的是linux-tools-5.15.0-91-generic)

代码语言:javascript
复制
sudo apt install linux-tools-common linux-tools-5.15.0-91-generic

开启监控

代码语言:javascript
复制
sudo sysctl kernel.perf_event_paranoid=-1

否则会报以下错误

Error: Access to performance monitoring and observability operations is limited. Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open access to performance monitoring and observability operations for processes without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability. More information can be found at ‘Perf events and tool security’ document: https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html perf_event_paranoid setting is 4: -1: Allow use of (almost) all events by all users Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK = 0: Disallow raw and ftrace function tracepoint access = 1: Disallow CPU event access = 2: Disallow kernel profiling To make the adjusted perf_event_paranoid setting permanent preserve it in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = )

分析

我们以《动态分析C语言代码生成函数调用关系的利器——gprof》中libevent的test-time为例。这次我们只要直接编译出可执行程序即可。

代码语言:javascript
复制
gcc `find . -regextype posix-extended -regex '^./[^/]*\.c$' ! -name 'wepoll.c' ! -name 'win32select.c' ! -name 'evthread_win32.c' ! -name 'buffer_iocp.c' ! -name 'bufferevent_async.c' ! -name 'arc4random.c' ! -name 'event_iocp.c' ! -name 'bufferevent_mbedtls.c'` \
 ./test/test-time.c \
 -I./build/include/ -I./include -I./ \
 -L./build/lib/ -lcrypto -lssl \
 -DLITTLE_ENDIAN -D__clang__ \
 -UD_WIN32 -UDMBEDTLS_SSL_RENEGOTIATION \
 -o test-time

采集

代码语言:javascript
复制
sudo perf record -g -- ./test-time

-g 指令是用于开启记录调用关系。

[ perf record: Woken up 12 times to write data ] [ perf record: Captured and wrote 4.786 MB perf.data (34448 samples) ]

解析

script工具将上步采集的信息转换成文本。

代码语言:javascript
复制
sudo perf script > test-time-perf.output

可视化处理

环境准备

代码语言:javascript
复制
sudo apt-get install graphviz

转换成dot

然后使用《管理Python虚拟环境的脚本》中的脚本构建虚拟环境,并安装gprof2dot

代码语言:javascript
复制
source env.sh init
source env.sh enter
source env.sh install gprof2dot

执行下面指令将文本转换成dot格式

代码语言:javascript
复制
gprof2dot test-time-perf.output -f perf> test-time-per.dot

转换为图片

代码语言:javascript
复制
dot test-time-perf.dot -Tpng -o test-time-perf.png

参考资料

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-01-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • perf是一套linux操作系统上分析工具集,分析函数调用关系只是其一个子集功能。它并不像《动态分析C语言代码生成函数调用关系的利器——gprof》中介绍的需要在被分析程序的编译指令中插入新的选项(-pg),而是直接对原始编译结果进行分析。
  • 环境准备
    • 安装
      • 开启监控
      • 分析
        • 采集
          • 解析
          • 可视化处理
            • 环境准备
              • 转换成dot
                • 转换为图片
                • 参考资料
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档