使用 GDB 除錯內建函式
從 V8 v6.9 開始,可以在 GDB(以及其他除錯器)中建立中斷點,以除錯 CSA/ASM/Torque 內建函式。
(gdb) tb i::Isolate::Init
Temporary breakpoint 1 at 0x7ffff706742b: i::Isolate::Init. (2 locations)
(gdb) r
Thread 1 "d8" hit Temporary breakpoint 1, 0x00007ffff7c55bc0 in Isolate::Init
(gdb) br Builtins_RegExpPrototypeExec
Breakpoint 2 at 0x7ffff7ac8784
(gdb) c
Thread 1 "d8" hit Breakpoint 2, 0x00007ffff7ac8784 in Builtins_RegExpPrototypeExec ()
請注意,使用臨時中斷點(GDB 中的捷徑為 tb
)會比使用一般中斷點(br
)更適合這個情況,因為你只需要在程序啟動時使用它。
內建函式也會顯示在堆疊追蹤中
(gdb) bt
#0 0x00007ffff7ac8784 in Builtins_RegExpPrototypeExec ()
#1 0x00007ffff78f5066 in Builtins_ArgumentsAdaptorTrampoline ()
#2 0x000039751d2825b1 in ?? ()
#3 0x000037ef23a0fa59 in ?? ()
#4 0x0000000000000000 in ?? ()
注意事項
僅適用於嵌入式內建函式。
中斷點只能設定在內建函式的開頭。
在設定內建函式中斷點之前,需要在
Isolate::Init
中設定初始中斷點,因為 GDB 會修改二進位檔,而我們會在啟動時驗證二進位檔中內建函式區段的雜湊值。否則,V8 會抱怨雜湊值不符# Fatal error in ../../src/isolate.cc, line 117 # Check failed: d.Hash() == d.CreateHash() (11095509419988753467 vs. 3539781814546519144).