2014-03-19 46 views
7

(編輯:根據當前「技巧」下面的第一個答案似乎是使用Atom處理器,但我希望有些gdb大師可以回答這是否是一個基本的限制,或者是否在路線圖上添加對其他處理器的支持?)如何在GDB中運行記錄指令歷史和函數調用歷史記錄?

反向執行似乎在我的環境中工作:我可以反向繼續,看到一個似是而非的記錄日誌,並在其中走動:

(gdb) start 
...Temporary breakpoint 5 at 0x8048460: file bang.cpp, line 13. 
Starting program: /home/thomasg/temp/./bang 

Temporary breakpoint 5, main() at bang.cpp:13 
13 f(1000); 
(gdb) record 
(gdb) continue 
Continuing. 

Breakpoint 3, f (d=900) at bang.cpp:5 
5  if(d) { 
(gdb) info record 
Active record target: record-full 
Record mode: 
Lowest recorded instruction number is 1. 
Highest recorded instruction number is 1005. 
Log contains 1005 instructions. 
Max logged instructions is 200000. 
(gdb) reverse-continue 
Continuing. 

Breakpoint 3, f (d=901) at bang.cpp:5 
5  if(d) { 
(gdb) record goto end 
Go forward to insn number 1005 
#0 f (d=900) at bang.cpp:5 
5  if(d) { 

但是指令和功能的歷史不可用:

(gdb) record instruction-history 
You can't do that when your target is `record-full' 
(gdb) record function-call-history 
You can't do that when your target is `record-full' 

並且唯一可用的目標類型已滿,另一個記錄類型「btrace」會因「目標不支持分支跟蹤」而失敗。因爲它是現代主流(gdb 7.6.1-ubuntu,在amd64 Linux Mint「Petra」上運行「Intel(R)Core™」),所以它很可能只是不支持這個目標。 i5-3570「)我希望我忽略了一個關鍵的步驟或配置?

回答

5

看來除了支持它的CPU之外沒有其他解決方案。

更確切地說,您的內核必須支持英特爾處理器追蹤(Intel PT)。

grep intel_pt /proc/cpuinfo 

參見:https://unix.stackexchange.com/questions/43539/what-do-the-flags-in-proc-cpuinfo-mean

的命令只能在record btrace模式這可以在Linux中進行檢查。

在GDB源提交beab5d9,它是nat/linux-btrace.c:kernel_supports_pt,它檢查我們是否可以輸入btrace。以下檢查進行:

  • 檢查是否存在/sys/bus/event_source/devices/intel_pt/type和讀取type
  • 做一個syscall (SYS_perf_event_open, &attr, child, -1, -1, 0);type,看看如果返回>=0。 TODO:爲什麼不使用C包裝?

第一次檢查失敗:文件不存在。

內核側

CD到內核4.1源:

git grep '"intel_pt"' 

我們發現arch/x86/kernel/cpu/perf_event_intel_pt.c其中規定了該文件。特別是:

if (!test_cpu_cap(&boot_cpu_data, X86_FEATURE_INTEL_PT)) 
    goto fail; 

因此intel_pt是先決條件。

我如何找到kernel_supports_pt

爲第一的grep:

git grep 'Target does not support branch tracing.' 

這使我們btrace.c:btrace_enable。快速調試與後:

gdb -q -ex start -ex 'b btrace_enable' -ex c --args /home/ciro/git/binutils-gdb/install/bin/gdb --batch -ex start -ex 'record btrace' ./hello_world.out 

虛擬框不支持,要麼:

./sde64 -- cpuid | grep 'Intel processor trace' 
Extract execution log from gdb record in a VirtualBox VM

英特爾SDE

Intel SDE 7.21已經有這個CPU的功能,以檢查

但我不確定是否可以在其上運行Linux內核:https://superuser.com/questions/950992/how-to-run-the-linux-kernel-on-intel-software-development-emulator-sde

其他GDB方法

更通用的問題,用量少,高效的軟件解決方案:

+1

有很多輝煌的答案肉質的鏈接 - 我沒有把PT作爲同義詞/底層技術(https:// sof tware.intel.com/en-us/blogs/2013/09/18/processor-tracing) - 謝謝! –

1

至少部分答案(針對「我​​做錯了」縱橫) - 從gdb-7.6.50.20140108/gdb/NEWS

* A new record target "record-btrace" has been added. The new target 
    uses hardware support to record the control-flow of a process. It 
    does not support replaying the execution, but it implements the 
    below new commands for investigating the recorded execution log. 
    This new recording method can be enabled using: 

record btrace 

    The "record-btrace" target is only available on Intel Atom processors 
    and requires a Linux kernel 2.6.32 or later. 

* Two new commands have been added for record/replay to give information 
    about the recorded execution without having to replay the execution. 
    The commands are only supported by "record btrace". 

record instruction-history  prints the execution history at 
           instruction granularity 

record function-call-history prints the execution history at 
           function granularity

這不是經常說我羨慕的Atom處理器;-)

的所有者我將編輯該問題,重新關注解決方案或未來支持計劃的問題。