From 8ba17d839eeca97bbf3947ce1995350b0858f233 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Fri, 12 Jul 2019 23:14:13 -0700 Subject: [PATCH] . Snapshot at a random moment, showing a new debugging trick: hacking on the C++ level to dump memory contents on specific labels. For some reason label 'x' doesn't have a segment assigned by the time we get to compute-addresses. --- subx/039debug.cc | 15 ++++++++++++ subx/apps/survey.subx | 57 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/subx/039debug.cc b/subx/039debug.cc index a592e198..49c0b8b6 100644 --- a/subx/039debug.cc +++ b/subx/039debug.cc @@ -91,6 +91,21 @@ if (!Watch_this_effective_address.empty()) { put(Watch_points, Watch_this_effective_address, addr); } +//: Special label that dumps regions of memory. +//: Not a general mechanism; by the time you get here you're willing to hack +//: on the emulator. +:(after "Run One Instruction") +if (contains_key(Symbol_name, EIP) && get(Symbol_name, EIP) == "$dump-stream-at-ESI") + dump_stream_at_ESI(); +:(code) +void dump_stream_at_ESI() { + uint32_t stream_start = Reg[ESI].i; + int32_t stream_length = read_mem_i32(stream_start + 8); + dbg << "stream length: " << std::dec << stream_length << end(); + for (int i = 0; i < stream_length + 12; ++i) + dbg << "0x" << HEXWORD << (stream_start+i) << ": " << HEXBYTE << NUM(read_mem_u8(stream_start+i)) << end(); +} + //: helpers :(code) diff --git a/subx/apps/survey.subx b/subx/apps/survey.subx index a3e5cb5a..deedebef 100644 --- a/subx/apps/survey.subx +++ b/subx/apps/survey.subx @@ -1174,6 +1174,7 @@ $compute-addresses:segment-loop: $compute-addresses:segment-break: # ESI = labels 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 0xc/disp8 . # copy *(EBP+12) to ESI +#? $dump-stream-at-ESI: # lrow/EAX = labels->data 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 0xc/disp8 . # copy ESI+12 to EAX # max/ECX = labels->data + labels->write @@ -1182,9 +1183,61 @@ $compute-addresses:segment-break: $compute-addresses:label-loop: # if (lrow >= max) break 39/compare 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # compare EAX with ECX - 73/jump-if-greater-or-equal-unsigned $compute-addresses:end/disp8 + 0f 83/jump-if-greater-or-equal-unsigned $compute-addresses:end/disp32 + # dump lrow->key {{{ + # . write(2/stderr, "label: ") + # . . push args + 68/push "label: "/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write(2/stderr, lrow->key) + # . . push args + ff 6/subop/push 0/mod/indirect 0/rm32/EAX . . . . . . # push *EAX + 68/push 2/imm32/stderr + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write(2/stderr, "$\n") + # . . push args + 68/push "$\n"/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # }}} # seg-name/EDX = lrow->segment-name 8b/copy 1/mod/*+disp8 0/rm32/EAX . . . 2/r32/EDX 4/disp8 . # copy *EAX to EDX + # dump seg-name {{{ + # . write(2/stderr, "compute-addresses: seg-name: ") + # . . push args + 68/push "seg-name: "/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write(2/stderr, seg-name) + # . . push args + 52/push-EDX + 68/push 2/imm32/stderr + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write(2/stderr, "$\n") + # . . push args + 68/push "$\n"/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # }}} # label-seg/EDX : (address segment-info) = get(segments, seg-name, row-size=16) # . save EAX 50/push-EAX @@ -1220,7 +1273,7 @@ $compute-addresses:label-loop: 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP # lrow += 16 # size of row 05/add-to-EAX 0x10/imm32 - eb/jump $compute-addresses:label-loop/disp8 + e9/jump $compute-addresses:label-loop/disp32 $compute-addresses:end: # . restore registers 5f/pop-to-EDI