GDB スタブを書いてみよう その5
スタブをほぼそのまま移植して動かしてみる。Mutex のテストコードの main.cpp で直接 debug 例外を上げる。
int main(int argc, char *argv[]) { asm("int $3;"); testSyscallMutex(); testClassMutex(); TEST_RESULTS(syscal_mutex); return 0; }
するとなぜか fault 0d ハンドラに制御が行く(これは間違いだと思うのであとで調べる)。そこから _catchException14 を今回は手動で呼び出す。
すると最終的に handle_exception に制御が渡り gdb との通信が始まる。なお gdb で remote デバッグ時のデバッグメッセージを有効にしておくとやりとりされたパケットが見える。
(gdb) set debug remote 1 (gdb) target remote localhost:43770 Remote debugging using localhost:43770 Sending packet: $qSupported#37...Sending packet: $qSupported#37...Sending packet: $qSupported#37...Sending packet: $qSupporte d#37...Packet received: T0b4:98cf1000;5:b4cf1000;8:40012100; Packet qSupported (supported-packets) is supported warning: unrecognized item "T0b4:98cf1000" in "qSupported" response warning: unrecognized item "5:b4cf1000" in "qSupported" response warning: unrecognized item "8:40012100" in "qSupported" response Sending packet: $Hg0#df...Ack Packet received: Sending packet: $?#3f...Packet instead of Ack, ignoring it Packet instead of Ack, ignoring it Packet instead of Ack, ignoring it (いくつか繰り返し) Packet received: Packet qAttached (query-attached) is NOT supported Sending packet: $qOffsets#4b...Ack Packet received: warning: Invalid remote reply: Packet received: warning: Invalid remote reply:
となって gdb 側で処理が止まってしまう。Mona 側の handle_exception では 'q', 'H', '?', 'H', 'q', 'q', 'q' のパケットが来ていた。
どうも観察するに Mona 側が ACK を返していないように思えたので '+' や '?' を返してみたが変わらなかった。
試しに handle_exception の先頭あたりで 1 byte だけあらかじめパケットを空読みすると '+' が来ていて、それでうまく gdb が止まるようになった。これは正しいんだろうか。
なんとなくうまくいく例
target remote localhost:43770 Remote debugging using localhost:43770 Sending packet: $qSupported#37...Sending packet: $qSupported#37...Sending packet: $qSupported#37...Sending packet: $qSupporte d#37...Packet received: T0b4:98cf1000;5:b4cf1000;8:40012100; Packet qSupported (supported-packets) is supported warning: unrecognized item "T0b4:98cf1000" in "qSupported" response warning: unrecognized item "5:b4cf1000" in "qSupported" response warning: unrecognized item "8:40012100" in "qSupported" response Sending packet: $Hg0#df...Packet instead of Ack, ignoring it Packet instead of Ack, ignoring it(何回か繰り返し) Ack Packet received: Sending packet: $?#3f...Ack Packet received: S0b Sending packet: $Hc-1#09...Ack Packet received: Sending packet: $qC#b4...Ack Packet received: Sending packet: $qAttached#8f...Ack Packet received: Packet qAttached (query-attached) is NOT supported Sending packet: $qOffsets#4b...Ack Packet received: Sending packet: $g#67...Ack Packet received: 000017001000000040012100909c000098cf1000b4cf1000504a2100c0ffffef400121002b000000007d00001800000010000000100000000000000000000000 Sending packet: $m210140,1#f2...Ack Packet received: 48 Sending packet: $m210140,8#f9...Ack Packet received: 488d010020035802 Sending packet: $m210140,7#f8...Ack Packet received: 488d0100200358 0x00210140 in ?? () Sending packet: $qSymbol::#5b...Ack Packet received: Packet qSymbol (symbol-lookup) is NOT supported (gdb) bt #0 0x00210140 in ?? () #1 0x0001272f in ?? () #2 0xa00219af in ?? () #3 0xa000104a in _fu0__monapi_memory_initialized () #4 0xa0001fc0 in testSyscallMutex() () #5 0xa0038424 in ?? () Ignoring packet error, continuing... Reply contains invalid hex digit 116 (gdb) info reg eax 0x170000 1507328 ecx 0x10 16 edx 0x210140 2163008 ebx 0x9c90 40080 esp 0x10cf98 0x10cf98 ebp 0x10cfb4 0x10cfb4 esi 0x214a50 2181712 edi 0xefffffc0 -268435520 eip 0x210140 0x210140 eflags 0x2b [ CF #3 #5 ] cs 0x7d00 32000 ss 0x18 24 ds 0x10 16 es 0x10 16 fs 0x0 0 gs 0x0 0