最近想用春哥写的ngx-sample-lua-bt,生成火焰图,看看线上后端代码的运行情况。就踏上了systemtap的道路,其中的坎坷就不说了,这里总结一下步骤和我出错的地方。
1.安装systemtap 这里直接给链接How to Build Systemtap 直接下载源码编译安装因为apt-get install systemtap
安装的systemtap的版本是1.6,这里需要2.0以上的版本
2.安装内核开发和调试信息包 ubuntu wiki已经给出了安装的方法Where to get debug symbols for kernel X? 根据Add repository config,添加源,apt-get install了对应的包。
在网上查的时候发现了一份检验环境的脚本,最后使用有问题的可以用这个脚本检验下环境
#!/bin/bash
distro="$(lsb_release --id --short)"
if [ "$distro" != "Debian" -a "$distro" != "Ubuntu" ]; then
echo Unsupported distro $distro
exit 1
fi
# 2.6.32-5-amd64
# 2.6.32-37-generic
abiname="$(cut -d " " -f 3 /proc/version)"
# 2.6.32
baseversion="$(echo "$abiname" | cut -d "-" -f 1)"
case "$distro" in
Debian) # 2.6.32-39
if uname -v | grep -q Debian; then
version=$(uname -v | cut -d " " -f 4)
else
version="$(cut -d " " -f 5 /proc/version | cut -d ")" -f 1)"
fi
;;
Ubuntu)
# 2.6.32-37.81
version="$(cut -d " " -f 2 /proc/version_signature | cut -d "-" -f 1-2)"
;;
esac
(
echo make >= 0
echo linux-image-$abiname = $version
echo linux-headers-$abiname = $version
echo linux-kbuild-$baseversion >= $version
case "$distro" in
Debian) echo linux-image-$abiname-dbg = $version
;;
Ubuntu) echo linux-image-$abiname-dbgsym = $version
;;
esac
) | while read package relation requiredversion; do
installedversion="$(dpkg-query -W "$package" 2> /dev/null | cut -f 2)"
if [ "$installedversion" = "" ]; then
availableversion="$(apt-cache show $package 2> /dev/null | grep ^Version: | cut -d " " -f 2)"
if [ "$availableversion" = "" ]; then
echo "You need package $package but it does not seem to be available"
if [ "$distro" = "Ubuntu" -a "$(echo $package | grep dbgsym$)" ]; then
echo " Ubuntu -dbgsym packages are typically in a separate repository"
echo " Follow https://wiki.edubuntu.org/DebuggingProgramCrash to add this repository"
elif [ "$distro" = "Debian" -a "$(echo $package | grep dbg$)" ]; then
echo " Debian does not have -dbg packages for all kernels. Consider switching to a kernel that has one."
fi
else
echo "Please install $package"
fi
elif ! dpkg --compare-versions $installedversion $relation $requiredversion; then
echo "Package $package version $installedversion does not match version of currently running kernel: $requiredversion"
echo " Consider apt-get upgrade && reboot"
fi
done
user="$(id --user --name)"
if [ "$user" != "root" ]; then
groups="$(id --groups --name)"
for i in stapusr stapdev; do
if [ "$(echo $groups | grep $i)" = "" ]; then
echo "Be root or adduser $user $i"
fi
done
fi
然后成功的执行了hello world之后
stap -e 'probe kernel.function("sys_open") {log("hello world") exit()}'
hello world
立马使用ngx-sample-lua-bt去抓取数据
root@20001:/home/game/flameGraph/nginx-systemtap-toolkit-master# ./ngx-sample-lua-bt -p 48205 --luajit20 -t 5 > tmp.bt
WARNING: Tracing 48205 (/usr/local/openresty/nginx/sbin/nginx) for LuaJIT 2.0...
ERROR: user string copy fault -14 at 0000000041398940 [man error::fault] near identifier 'user_string_n' at /opt/stap/share/systemtap/tapset/uconversions.stp:133:10
WARNING: Number of errors: 1, skipped probes: 0
WARNING: /opt/stap/bin/staprun exited with status: 1
一脸懵还有问题。。 立马google,呀~原来有人同样碰到了这个问题,并在github上提问了春哥ngx-sample-lua-bt crashes with ‘user_string_n’ error修改之后成功的跑出了火焰图 T T