From 8ba17d839eeca97bbf3947ce1995350b0858f233 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Fri, 12 Jul 2019 23:14:13 -0700 Subject: . 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 -- cgit 1.4.1-2-gfad0 <vc@akkartik.com> 2015-02-19 18:25:25 -0800 committer Kartik K. Agaram <vc@akkartik.com> 2015-02-19 18:25:25 -0800 796 - jump instructions done' href='/akkartik/mu/commit/cpp/015jump?h=main&id=201b2e6c7ce94c986fe7888a60f0e1ba24a05ce9'>201b2e6c ^
6f5d7864 ^


201b2e6c ^

























6f5d7864 ^
201b2e6c ^

6f5d7864 ^
201b2e6c ^






6f5d7864 ^
201b2e6c ^
6f5d7864 ^
201b2e6c ^


























6f5d7864 ^
201b2e6c ^

6f5d7864 ^
201b2e6c ^






6f5d7864 ^
201b2e6c ^

6f5d7864 ^
201b2e6c ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

















                                                                             
                        
                       
                        



                           



                              


                        

























                                                                             
                        

                              
                        






                                 
                        
                          
                        


























                                                                             
                        

                              
                        






                                     
                        

                              
                        
                             
:(before "End Globals")
// Jump ops.
const int JUMP = 10;
:(before "End Primitive Recipe Numbers")
Recipe_number["jump"] = JUMP;
Next_recipe_number++;
:(before "End Primitive Recipe Implementations")
case JUMP: {
  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
  pc += to_int(instructions[pc].ingredients[0].name);
  break;
}

:(scenario "jump_can_skip_instructions")
recipe main [
  jump 1:offset
  1:integer <- copy 1:literal
]
+run: instruction main/0
+run: ingredient 0 is 1
-run: instruction main/1
-mem: storing in location 1

:(scenario "jump_backward")
recipe main [
  jump 1:offset  // 0 -+
  jump 1:offset  //    | 1 +-+
  jump -2:offset //  2 +-->+ |
]                //       3 \/
+run: instruction main/0
+run: instruction main/2
+run: instruction main/1

:(before "End Globals")
const int JUMP_IF = 11;
:(before "End Primitive Recipe Numbers")
Recipe_number["jump-if"] = JUMP_IF;
Next_recipe_number++;
:(before "End Primitive Recipe Implementations")
case JUMP_IF: {
  vector<int> arg0 = read_memory(instructions[pc].ingredients[0]);
  assert(arg0.size() == 1);
  trace("run") << "ingredient 0 is " << arg0[0];
  if (!arg0[0]) {
    trace("run") << "jump-if fell through";
    break;
  }
  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
  pc += to_int(instructions[pc].ingredients[1].name);
  trace("run") << "jumping to instruction " << pc+1;
  break;
}

:(scenario "jump_if")
recipe main [
  jump-if 999:literal 1:offset
  1:integer <- copy 1:literal
]
+run: instruction main/0
+run: ingredient 1 is 1
+run: jumping to instruction 2
-run: instruction main/1
-mem: storing in location 1

:(scenario "jump_if_fallthrough")
recipe main [
  jump-if 0:literal 1:offset
  123:integer <- copy 1:literal
]
+run: instruction main/0
+run: jump-if fell through
+run: instruction main/1
+mem: storing in location 123

:(before "End Globals")
const int JUMP_UNLESS = 12;
:(before "End Primitive Recipe Numbers")
Recipe_number["jump-unless"] = JUMP_UNLESS;
Next_recipe_number++;
:(before "End Primitive Recipe Implementations")
case JUMP_UNLESS: {
  vector<int> arg0 = read_memory(instructions[pc].ingredients[0]);
  assert(arg0.size() == 1);
  trace("run") << "ingredient 0 is " << arg0[0];
  if (arg0[0]) {
    trace("run") << "jump-unless fell through";
    break;
  }
  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
  pc += to_int(instructions[pc].ingredients[1].name);
  trace("run") << "jumping to instruction " << pc+1;
  break;
}

:(scenario "jump_unless")
recipe main [
  jump-unless 0:literal 1:offset
  1:integer <- copy 1:literal
]
+run: instruction main/0
+run: ingredient 1 is 1
+run: jumping to instruction 2
-run: instruction main/1
-mem: storing in location 1

:(scenario "jump_unless_fallthrough")
recipe main [
  jump-unless 999:literal 1:offset
  123:integer <- copy 1:literal
]
+run: instruction main/0
+run: ingredient 0 is 999
+run: jump-unless fell through
+run: instruction main/1
+mem: storing in location 123