systemtap使用笔记

安装

使用之前需要安装一些内核调试包
安装systemtap

sudo yum install systemtap systemtap-runtime  

可以用这个命令看下需要的包

stap-prep

正常的安装流程.假设之前相关的包都没有装
先看一下自己的系统环境,安装包必须要跟内核版本对应

$uname -r
3.10.0-327.el7.x86_64

需要安装的三个包

  • kernel-debuginfo
  • kernel-debuginfo-common
  • kernel-devel

后面加上自己环境的版本信息即可。比如kernel-debuginfo-3.10.0-327.el7.x86_64。安装包可以去 http://rpm.pbone.net这里找。

安装完成后可以输入以下命令来验证是否系统已经支持systemtap:

$sudo stap -ve 'probe begin { log("hello world") exit() }'
Pass 1: parsed user script and 116 library script(s) using 227408virt/38928res/3192shr/36024data kb, in 190usr/10sys/210real ms.
Pass 2: analyzed script: 1 probe(s), 2 function(s), 0 embed(s), 0 global(s) using 228068virt/39984res/3456shr/36684data kb, in 10usr/0sys/5real ms.
Pass 3: translated to C into "/tmp/stapIFvNry/stap_ce820c0cec98f129396f1b5044b5f319_1112_src.c" using 228192virt/40188res/3640shr/36808data kb, in 0usr/0sys/0real ms.
Pass 4: compiled C into "stap_ce820c0cec98f129396f1b5044b5f319_1112.ko" in 1500usr/450sys/1859real ms.
Pass 5: starting run.
hello world
Pass 5: run completed in 0usr/30sys/343real ms.

如果能输出类似上面的信息,说明已经安装完成可以使用了。

使用

stap -l 可以查看某个函数在哪个文件的哪一行定义的,可以是内核代码也可以是用户态代码。

$stap -l 'process("/usr/lib64/libjemalloc.so.2").function("malloc")'
process("/usr/lib64/libjemalloc.so.2").function("malloc@/usr/include/stdlib.h:465")

stap -L 可以看函数中的哪些变量可以被看到,后面写脚本时可以直接引用这些变量

$stap -L 'process("/usr/lib64/libjemalloc.so.2").function("malloc")'
process("/usr/lib64/libjemalloc.so.2").function("malloc@/usr/include/stdlib.h:465") $size:size_t $usize:size_t

复杂一点的打点、统计之类的可以写stap脚本来完成,语法类似c语言,也比较简答,这里贴一个做内存分析的脚本。

probe begin {
    printf("=============begin============\n")
}

//记录每次内存分配的size
global g_mem_ref_tbl
//记录内存分配的堆栈
global g_mem_bt_tbl
global cnt
global m_cnt
global str
probe process("/usr/lib64/libjemalloc.so.2").function("malloc").return {
    if (target() == pid()) {
      cnt++
      if ($size > 1024*1024) { // 1M
        g_mem_ref_tbl[$return] = $size
        g_mem_bt_tbl[$return] = sprint_ubacktrace()
        //g_mem_bt_tbl[$return] = sprint_ubacktrace_brief()
        //str = sprint_ubacktrace()
        //g_mem_bt_tbl[$return] = sprint_ustack(ubacktrace)
        m_cnt++
      }
    }
}

probe process("/usr/local/lib/libjemalloc.so.2").function("free").call {
    if (target() == pid()) {
      //内存释放就将这两个变量删除
      g_mem_ref_tbl[$ptr] = 0
      delete g_mem_ref_tbl[$ptr]
      delete g_mem_bt_tbl[$ptr]
    }
}

probe end {
    //最后输出未释放内存的分配堆栈
    total_size=0
    printf("=============end============\n")
    printf("total alloc count %d, mcnt=%d\n", cnt, m_cnt)
    foreach(mem in g_mem_bt_tbl) {
      if (g_mem_ref_tbl[mem] > 1024 * 1024) {
        total_size+=g_mem_ref_tbl[mem]
        printf("---------%d--------\n", g_mem_ref_tbl[mem])
        printf("%s\n", g_mem_bt_tbl[mem])
        printf("--------end--------\n")
      } else {
        printf("error %d, %s\n", g_mem_ref_tbl[mem], g_mem_bt_tbl[mem])
      }
    }
    printf("total size = %d\n", total_size)
}

//程序计时功能,60s后退出
probe timer.ms(60000)
{
    exit()
}

变量无需指明类型,程序会自己识别。如果是全局的,前面加上global。

官方有很多函数,需要的可以去查一下。 另外如果安装了systemstap在下面目录下也有一些例子可以参考一下。
https://sourceware.org/systemtap/tapsets/
/usr/share/systemtap/tapset 例子

错误分析

parser error

这个错误一般是debuginfo包没有安装好,如果确定已经安装完成,可以卸载掉重新安装试一下。

semantic error

这个错误分好多种,这里举例我遇到过的

  1. 如下,我将process错写为processy
semantic error: while resolving probe point: identifier 'processy' at mem.stap:12:7
        source: probe processy("/usr/lib64/libjemalloc.so.2").function("malloc").return {
  1. 将探测函数名写错,malloc错写为mmalloc,这时在下面有提示“similar functions: malloc, calloc, mallocx, valloc, realloc”, 可以根据这个来检查一下是否写错。
$semantic error: while resolving probe point: identifier 'process' at mem.stap:12:7
        source: probe process("/usr/lib64/libjemalloc.so.2").function("mmalloc").return {
                      ^

semantic error: no match (similar functions: malloc, calloc, mallocx, valloc, realloc)

OVER FLOW

这个是运行时出现的错误,主要是由于systemtap设置了一些限制,可以用一些参数来配置。
下面举三个运行出错的例子以及解决方式,命令中加入对应的参数。

ERROR: probe overhead exceeded threshold

-DSTP_NO_OVERLOAD

ERROR: Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.

-DMAXSKIPPED=102400

ERROR: Array overflow, check MAXMAPENTRIES near identifier....

-DMAXMAPENTRIES=1024000

man stap可以看到RESOURCE LIMITS这一栏下面有很多资源配置的参数。

MAXNESTING
Maximum number of nested function calls. Default determined by script analysis, with a bonus 10 slots added for recursive scripts.

MAXSTRINGLEN
Maximum length of strings, default 128.

MAXTRYLOCK
Maximum number of iterations to wait for locks on global variables before declaring possible deadlock and skipping the probe, default 1000.

MAXACTION
Maximum number of statements to execute during any single probe hit (with interrupts disabled), default 1000.

MAXACTION_INTERRUPTIBLE
Maximum number of statements to execute during any single probe hit which is executed with interrupts enabled (such as begin/end probes),
default (MAXACTION * 10).

MAXMAPENTRIES
Maximum number of rows in any single global array, default 2048.

MAXERRORS
Maximum number of soft errors before an exit is triggered, default 0, which means that the first error will exit the script.

MAXSKIPPED
Maximum number of skipped probes before an exit is triggered, default 100. Running systemtap with -t (timing) mode gives more details about
skipped probes. With the default -DINTERRUPTIBLE=1 setting, probes skipped due to reentrancy are not accumulated against this limit.

MINSTACKSPACE
Minimum number of free kernel stack bytes required in order to run a probe handler, default 1024. This number should be large enough for
the probe handler’s own needs, plus a safety margin.

MAXUPROBES
Maximum number of concurrently armed user-space probes (uprobes), default somewhat larger than the number of user-space probe points named
in the script. This pool needs to be potentialy large because individual uprobe objects (about 64 bytes each) are allocated for each
process for each matching script-level probe.
STP_MAXMEMORY
Maximum amount of memory (in kilobytes) that the systemtap module should use, default unlimited. The memory size includes the size of the
module itself, plus any additional allocations. This only tracks direct allocations by the systemtap runtime. This does not track indirect
allocations (as done by kprobes/uprobes/etc. internals).

TASK_FINDER_VMA_ENTRY_ITEMS
Maximum number of VMA pages that will be tracked at runtime. This might get exhausted for system wide probes inspecting shared library vari‐
ables and/or user backtraces. Defaults to 1536.

STP_PROCFS_BUFSIZE
Size of procfs probe read buffers (in bytes). Defaults to MAXSTRINGLEN. This value can be overridden on a per-procfs file basis using the
procfs read probe .maxsize(MAXSIZE) parameter.

先这样,后续用到一些不错的技能再继续补充。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • 1:InputChannel提供函数创建底层的Pipe对象 2: 1)客户端需要新建窗口 2)new ViewRo...
    自由人是工程师阅读 5,096评论 0 18
  • 如果你看完书中的所有例子,你很可能已经做完你的实验和在已经越狱的iPhone上的研究。因为和许多人一样,几乎所有的...
    fishmai0阅读 15,296评论 2 42
  • 一、温故而知新 1. 内存不够怎么办 内存简单分配策略的问题地址空间不隔离内存使用效率低程序运行的地址不确定 关于...
    SeanCST阅读 7,669评论 0 27
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,018评论 2 34
  • 一、入口 https://developer.android.google.cn -> API指南 -> 库 ->...
    捡之阅读 8,939评论 4 8