我正在使用基于“ubuntu”标签的Docker容器,无法获取linux perf工具来显示调试符号。你如何获得在Docker容器中的linux perf工具中工作的调试符号?
这是我正在做的事情来证明问题。
首先我启动一个容器,这里有一个交互式shell。
docker run -t -i ubuntu:14.04 /bin/bash
然后从容器提示我安装linux perf工具。
apt-get update
apt-get install -y linux-tools-common linux-tools-generic linux-tools-`uname -r`
我现在可以使用perf
工具。我的内核是3.16.0-77-generic
。
现在我将安装gcc
,编译一个测试程序,并尝试运行它在perf record
下。
apt-get install -y gcc
我贴在测试程序为test.c
:
#include <stdio.h>
int function(int i) {
int j;
for(j = 2; j <= i/2; j++) {
if (i % j == 0) {
return 0;
}
}
return 1;
}
int main() {
int i;
for(i = 2; i < 100000; i++) {
if(function(i)) {
printf("%d\n", i);
}
}
}
然后编译,运行和报告: gcc -g -O0 test.c && perf record ./a.out && perf report
输出看起来是这样的: 72.38% a.out a.out [.] 0x0000000000000544 8.37% a.out a.out [.] 0x000000000000055a 8.30% a.out a.out [.] 0x000000000000053d 7.81% a.out a.out [.] 0x0000000000000551 0.40% a.out a.out [.] 0x0000000000000540
这没有符号,即使可执行文件有符号l信息。
做同样的一般步骤外容器正常工作,并显示是这样的:通过成为根和做 96.96% a.out a.out [.] function 0.35% a.out libc-2.19.so [.] [email protected]@GLIBC_2.2.5 0.14% a.out [kernel.kallsyms] [k] update_curr 0.12% a.out [kernel.kallsyms] [k] update_cfs_shares 0.11% a.out [kernel.kallsyms] [k] _raw_spin_lock_irqsave
在主机系统我已经打开内核符号: echo 0 > /proc/sys/kernel/kptr_restrict
如何让集装箱版本正常工作并显示调试符号?
谢谢,就是这样。 要添加到解决方案,为了获得内核符号,您还需要添加: '--kallsyms =/proc/kallsyms'。 –
万一它帮助其他人:更简单的方法是用'-v/var/lib/docker /:/ var/lib/docker'调用'docker run',这样'perf'不需要任何正确解析符号的特殊参数。 –
啊,这是一个很好的。 –