GDB(GNU Debugger)
GDB是UNIX/LINUX操作系统下的、基于命令行的、功能强大的程序调试工具。 可以查看程序在执行过程中的内部
Python-gdb
通过python来运行gdb命令,其本质是利用python语言的灵活性和便利。GDB提供了一套python API,方便用户使用python脚本编写更复杂的GDB脚本 gdb python api 官方文档
GDB是UNIX/LINUX操作系统下的、基于命令行的、功能强大的程序调试工具。 可以查看程序在执行过程中的内部
通过python来运行gdb命令,其本质是利用python语言的灵活性和便利。GDB提供了一套python API,方便用户使用python脚本编写更复杂的GDB脚本 gdb python api 官方文档
使用验证器保证安全性,通过BPF映射实现内核-用户空间通信 所有与用户空间的交互都是通过 eBPF“映射”进行的,这些映射是键值存储。 每个 eBPF 程序都将在一定的有限执行时间内完成,即非图灵完备
BCC适合使用了其他库的复杂脚本、守护进程
优点: bpftrace基于内置Linux技术,不用追赶内核版本改动,稳定性更高 脚本执行速度比systemtap快(使用llvm编译成BPF)
缺点: bpftrace语言特性上没有systemtap丰富,不太能进行复杂的探测操作 探针附到函数一定偏移处不方便 无法直接获取函数的局部变量 无法直接获取结构体信息 内核版本要求较高,在较旧的发行版上难以安装
动态追踪工具在逻辑上比较简单:大多是通过类C语言创建一个脚本,通过编译器翻译成探测代码。通过一个内核地址模块加载探测代码到内核地址空间,然后patch到当前内核的二进制代码中。探针将收集的数据写到中间缓冲(这些buffers往往是lock-free的,所以他们对内核性能有较少影响,且不需要切换上下文到追踪程序)。另一个独立的实体消费者读取这些buffers,然后输出数据。
火焰图是一种分层数据可视化工具,用于直观展示程序中时间都花在了哪里。 栈采样:每秒多次,程序中的线程会被中断,同时记录下代码中的当前位置(基于线程的指令指针),以及到达该位置所调用的函数链。聚合结果。 说明: * y轴表示:堆栈深度,主函数更靠近底部 * x轴表示:涵盖所有样本,其顺序无意义。对于CPU火焰图来说,其宽度表示:该函数在 CPU 上运行或处于调用堆栈中的总时间。
强类型、无声明的过程式语言
,受dtrace和awk的启发以下列举几个常用的参数
-x用于传递PID参数给systemtap脚本,程序内部可以通过内置函数target()
获取到
e.g.:target() == pid()
本文主要通过案例演示的方式,介绍个人在使用systemtap对nginx(openresty)进行网络相关的探测的使用过程、使用感受以及过程中遇到的问题
问题: 分析openresty(nginx)在出现abort时,为什么FIN正常关闭TCP连接,而不是使用RST?
# 查看Nginx可用的探针点
sudo stap -L 'process("/usr/local/openresty/nginx/sbin/nginx").function("*")'(还有一种方式是:使用nm ./sbin/nginx,但是看不到参数和文件位置)
# 看到的输出如下:
...
process("/usr/local/openresty/nginx/sbin/nginx").function("ngx_http_variables_add_core_vars@src/http/ngx_http_variables.c:2592") $cf:ngx_conf_t*
process("/usr/local/openresty/nginx/sbin/nginx").function("ngx_http_variables_init_vars@src/http/ngx_http_variables.c:2635") $cf:ngx_conf_t* $hash:ngx_hash_init_t
process("/usr/local/openresty/nginx/sbin/nginx").function("ngx_http_wait_request_handler@src/http/ngx_http_request.c:375") $rev:ngx_event_t*
process("/usr/local/openresty/nginx/sbin/nginx").function("ngx_http_weak_etag@src/http/ngx_http_core_module.c:1703") $r:ngx_http_request_t*
process("/usr/local/openresty/nginx/sbin/nginx").function("ngx_http_write_filter@src/http/ngx_http_write_filter_module.c:48") $r:ngx_http_request_t* $in:ngx_chain_t*
process("/usr/local/openresty/nginx/sbin/nginx").function("ngx_http_write_filter_init@src/http/ngx_http_write_filter_module.c:362") $cf:ngx_conf_t*
process("/usr/local/openresty/nginx/sbin/nginx").function("ngx_http_write_request_body@src/http/ngx_http_request_body.c:484") $r:ngx_http_request_t*
process("/usr/local/openresty/nginx/sbin/nginx").function("ngx_http_writer@src/http/ngx_http_request.c:2786") $r:ngx_http_request_t*
process("/usr/local/openresty/nginx/sbin/nginx").function("ngx_http_xss_body_filter@../xss-nginx-module-0.06/src/ngx_http_xss_filter_module.c:264") $r:ngx_http_request_t* $in:ngx_chain_t* $ll:ngx_chain_t**
ngx_close_connection
sudo stap -L 'process("/usr/local/openresty/nginx/sbin/nginx").function("*")'|grep ngx_close_connection
process("/usr/local/openresty/nginx/sbin/nginx").function("ngx_close_connection@src/core/ngx_connection.c:1179") $c:ngx_connection_t*
$c
,属于结构体指针ngx_connection_t*
kprobes 机制
来设置探测点,采集数据存储到probe.out
,从而进行系统性能分析。$cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core) # 查看操作系统版本
$uname -r
3.10.0-1160.53.1.el7.x86_64 # 查看内核版本