a linux trace/probe tool.

官网:https://sourceware.org/systemtap/

简介

SystemTap是我目前所知的最强大的内核调试工具,有些家伙甚至说它无所不能:)

(1) 发展历程

Debuted in 2005 in Red Hat Enterprise Linux 4 Update 2 as a technology preview.

After four years in development, System 1.0 was released in 2009.

As of 2011 SystemTap runs fully supported in all Linux distributions.

(2) 官方介绍

SystemTap provides free software(GPL) infrastructure to simplify the gathering of information about the

running Linux system. This assists diagnosis of a performance or functional problem. SystemTap eliminates

the need for the developer to go through the tedious and disruptive instrument, recompile, install, and reboot

sequence that may be otherwise required to collect data.

SystemTap provides a simple command line interface and scripting language for writing instrumentation for

a live running kernel plus user-space application. We are publishing samples, as well as enlarging the internal

"tapset" script library to aid reuse and abstraction.

Among other tracing/probing tools, SystemTap is the tool of choice for complex tasks that may require live analysis,

programmable on-line response, and whole-system symbolic access. SystemTap can also handle simple tracing

jobs.

Current project members include Red Hat, IBM, Hitachi, and Oracle.

(3) 获取源码

git clone git://sourceware.org/git/systemtap.git

安装

(1) Ubuntu发行版

1. 安装systemtap包

apt-get install systemtap

2. 安装依赖包

gcc:C语言编译器

elfutils:提供分析调试信息的库函数

linux-headers-generic:编译内核模块所需的内核头文件以及模块配置信息

3. 安装内核调试信息(kernel-debuginfo)

kernel-debuginfo提供了调试内核所需的符号表,如果没有安装的话SystemTap的威力就会大打折扣,

只能提供kprobes系列的功能。

下载地址:http://ddebs.ubuntu.com/pool/main/l/linux/

下载对应的内核版本,我的是linux-image-3.11.0-12-generic-dbgsym_3.11.0-12.19_amd64.ddeb

下载后安装:dpkg -i linux-image-3.11.0-12-generic-dbgsym_3.11.0-12.19_amd64.ddeb

4. 验证

stap -ve 'probe kernel.function("do_fork") { print("hello world\n") exit() }'

如果没有提示错误,就是安装成功了。

(2) CentOS/RedHat发行版

使用yum安装下列rpm包即可:

systemtap:SystemTap包

gcc:C语言编译器

elfutils:提供库函数来分析调试信息

kernel-devel:编译内核模块所需的内核头文件及模块配置信息

kernel-debuginfo:提供所需的内核调试信息来定位内核函数和变量的位置

使用

一些例子SystemTap的简单例子。

(1) stap

通常直接使用stap执行用SystemTap语法编写的脚本即可。

stap - systemtap script translator/driver

stap test.stp // .stp后缀的文件是用SystemTap语法编写的脚本

脚本主要元素:probe point + probe handler

stap [options] FILE // Run script in file

stap [options] -e SCRIPT // Run given script.

stap [options] -l PROBE // List matching probes.

stap [options] -L PROBE // List matching probes and local variables.

常用选项

-h:帮助

-g:guru模式,嵌入式C代码需要

-m:指定编译成的模块名称

-v:add verbosity to all passes

-k:不删除临时目录

-p NUM:stop after pass NUM 1-5, instead of 5 (parse, elaborate, translate, compile, run)

-b:bulk (percpu file) mode, 使用RelayFS将数据从内核空间传输到用户空间

-o FILE:输出到指定文件,而不是stdout

-c CMD:start the probes, run CMD, and exit when it finishes

stap是SystemTap的前端,当出现以下情况时退出:

1. The user interrupts the script with a CTRL-C.

2. The script executes the exit() function.

3. The script encounters a sufficient number of soft errors.

4. The monitored command started with the stap program's -c option exits.

(2) staprun

如果我们的输入不是.stp脚本,而是一个用stap生成的模块,那么就用staprun来执行。

staprun - systemtap runtime

staprun [OPTIONS] MODULE [MODULE-OPTIONS]

staprun的作用:

The staprun program is the back-end of the Systemtap tool. It expects a kernel module produced by

the front-end stap tool.

Splitting the systemtap tool into a front-end and a back-end allows a user to compile a systemtap script

on a development machine that has the kernel debugging information (need to compile the script) and

then transfer the resulting kernel module to a production machine that doesn't have any development

tools or kernel debugging information installed.

staprun is a part of the SystemTap package, dedicated to module loading and unloading and kernel-to-user

data transfer.

常用选项

-o FILE:Send output to FILE.

-D:Run in background. This requires '-o' option.

(3) 监测内核函数

一个简单脚本,每当内核函数do_fork()被调用时,显示调用它的进程名、进程ID、函数参数。

[java] view plaincopy
  1. global proc_counter
  2. probe begin {
  3. print("Started monitoring creation of new processes...Press ^C to terminate\n")
  4. printf("%-25s %-10s %-s\n", "Process Name", "Process ID", "Clone Flags")
  5. }
  6. probe kernel.function("do_fork") {
  7. proc_counter++
  8. printf("%-25s %-10d 0x%-x\n", execname(), pid(), $clone_flags)
  9. }
  10. probe end {
  11. printf("\n%d processes forked during the observed period\n", proc_counter)
  12. }

(4) 监测系统调用

一个简单脚本,显示4秒内open系统调用的信息:调用进程名、进程ID、函数参数。

[java] view plaincopy
  1. probe syscall.open
  2. {
  3. printf("%s(%d) open(%s)\n", execname(), pid(), argstr)
  4. }
  5. probe timer.ms(4000) # after 4 seconds
  6. {
  7. exit()
  8. }

(5) 监测源文件中所有函数入口和出口

括号内的探测点描述包含三个部分:

function name part:函数名

@file name part:文件名

function line part:所在行号

例如:

[java] view plaincopy
  1. probe kernel.function("*@net/socket.c") {}
  2. probe kernel.function("*@net/socket.c").return {}

这里指定函数名为任意(用*表示),指定文件名为net/socket.c,探测函数的入口和返回。

还可以用“:行号”来指定行号。

(6) 查找匹配的内核函数和变量

查找名字中包含nit的内核函数:

stap -l 'kernel.function("*nit*")'

查找名字中包含nit的内核函数和变量:

stap -L 'kernel.function("*nit*")'

(7) 自带的用例集

/root/systemtap/testsuite/systemtap.examples/,包含了许多用例脚本。

主要有几个方面:

network、io、interrupt、locks、memory、process、virtualization等

(8) 监控所有进程的收发包情况

[java] view plaincopy
  1. global recv, xmit
  2. probe begin {
  3. printf("Starting network capture...Press ^C to terminate\n")
  4. }
  5. probe netdev.receive {
  6. recv[dev_name, pid(), execname()] <<< length
  7. }
  8. probe netdev.transmit {
  9. xmit[dev_name, pid(), execname()] <<< length
  10. }
  11. probe end {
  12. printf("\nCapture terminated\n\n")
  13. printf("%-5s %-15s %-10s %-10s %-10s\n",
  14. "If", "Process", "Pid", "RcvPktCnt", "XmtPktCnt")
  15. foreach([dev, pid, name] in recv) {
  16. recvcnt = @count(recv[dev, pid, name])
  17. xmtcnt =  @count(xmit[dev, pid, name])
  18. printf("%-5s %-15s %-10d %-10d %-10d\n", dev, name, pid, recvcnt, xmtcnt)
  19. }
  20. }

(9) Systemtap usage stories and interesting demos

https://sourceware.org/systemtap/wiki/WarStories

官网提供的很多例子。

内核调试神器SystemTap — 简介与使用(一)相关推荐

  1. 内核探测工具systemtap简介

    systemtap是内核开发者必须要掌握的一个工具,本文我将简单介绍一下此工具,后续将会有系列文章介绍systemtap的用法. 什么是systemtap 假如现在有这么一个需求:需要获取正在运行的L ...

  2. ​内核调试技巧--systemtap定位丢包原因

    作者:wqiangwang,腾讯 TEG 后台开发工程师 内核收发包,可能会由于backlog队列满.内存不足.包校验失败.特性开关如rpf.路由不可达.端口未监听等等因素将包丢弃. 在内核里面,数据 ...

  3. Linux内核调试原理和工具介绍--理解静态插装/动态插装、tracepoint、ftrace、kprobe、SystemTap、Perf、eBPF

    可以将linux跟踪系统分成Tracer(跟踪数据来自哪里),数据收集分析(如"ftrace")和跟踪前端(更方便的用户态工具). 1. 数据源(Tracers) printk 是 ...

  4. Crash内核调试手段

    kdump简介 kdump是系统崩溃的时候,用来转储运行内存的一个工具. 系统一旦崩溃,内核就没法正常工作了,这个时候将由kdump提供一个用于捕获当前运行信息的内核, 该内核会将此时内存中的所有运行 ...

  5. linux内核调试指南

    Hunnad的专栏 * 条新通知 * 登录 * 注册 * 欢迎 * 退出 * 我的博客 * 配置 * 写文章 * 文章管理 * 博客首页 * * * * 空间 * 博客 * 好友 * 相册 * 留言 ...

  6. linux内核调试指南 1

    大海里的鱼有很多,而我们需要的是鱼钩一只 一些前言 作者前言 知识从哪里来 为什么撰写本文档 为什么需要汇编级调试 ***第一部分:基础知识*** 总纲:内核世界的陷阱 源码阅读的陷阱 代码调试的陷阱 ...

  7. linux 内核调试指南

    大海里的鱼有很多,而我们需要的是鱼钩一只 本文档由大家一起自由编写,修改和扩充,sniper负责维护.引用外来的文章要注明作者和来处.本文档所有命令都是在ubuntu/debian下的操作.选取的内核 ...

  8. Linux Kernel - Debug Guide (Linux内核调试指南 )

    linux内核调试指南 一些前言 作者前言 知识从哪里来 为什么撰写本文档 为什么需要汇编级调试 ***第一部分:基础知识*** 总纲:内核世界的陷阱 源码阅读的陷阱 代码调试的陷阱 原理理解的陷阱 ...

  9. 开源项目-基于Intel VT技术的Linux内核调试器

    本开源项目将硬件虚拟化技术应用在内核调试器上,使内核调试器成为VMM,将操作系统置于虚拟机中运行,即操作系统成为GuestOS,以这样的一种形式进行调试,最主要的好处就是调试器对操作系统完全透明.如下 ...

  10. 内核调试和系统调用劫持

    如何在不重新编译内核,不rmmod内核模块的情况下修改系统调用 为了解决这个问题,最终实现了两种解决方式: 1.Linux系统调用劫持 2.Kprobes内核调试技术(并非真正的修改) 下面分别说下: ...

最新文章

  1. maven生成jar包,包含第三方jar包
  2. nssl1323,jzoj(初中)2107-交流【dfs,容斥,组合数】
  3. SQLAlchemy()分页器paginate方法
  4. android:layout 冒号,android-json解析及简单例子(补汉6个汉字字).pdf
  5. JS-深入理解继承(非class方式与class继承)
  6. Oracle expdp和impdp
  7. NOIP 2014 无线网络发射器选址
  8. web网页开发-前端
  9. filezillaserver使用教程(filezilla搭建ftp服务器步骤)
  10. 阿里云服务器价格表,阿里云服务器最新收费标准大全
  11. mysql+两行+一样+筛选_Excel两行交换及两列交换,快速互换相邻表格数据的方法...
  12. Foxmail登录各种邮箱方法汇总
  13. 行行出状元,大学毕业生卖煎饼月入13万
  14. Android 左飞字幕的实现(带描边)
  15. 远程连接服务器出现channel is not opened通道未打开
  16. 计算机用鼠标画图,在电脑上用鼠标画画用那个软件好
  17. 沐阳Python扫盲01类的概念与实例
  18. 基于SSM的快递代取管理系统
  19. 万能通用!权限系统就该这么设计!
  20. 【2021情人节主题征文】| 写了一个表白网页后,我跟女神在一起啦

热门文章

  1. Android Hessian 通信
  2. Disruptor 极速体验
  3. vue组件挂载到全局方法
  4. ant 打包war 遇到的一些问题
  5. SAP在阿里云白皮书-第三章 SAP上阿里云场景介绍
  6. 解读对象存储九大关键特征
  7. 在centos x86_64里编译x32的程序
  8. Android eclipse中程序单步调试调试
  9. 国内网络安全风险评估市场与技术操作
  10. DirectX 9高层着色语言介绍4——语言基础(3)