https://github.com/akkartik/mu/blob/master/039debug.cc
  1 //:: Some helpers for debugging.
  2 
  3 //: Load the 'map' file generated during 'subx --debug translate' when running
  4 //: 'subx --debug --trace run'.
  5 //: (It'll only affect the trace.)
  6 
  7 :(before "End Globals")
  8 map</*address*/uint32_t, string> Symbol_name;  // used only by 'subx run'
  9 map</*address*/uint32_t, string> Source_line;  // used only by 'subx run'
 10 :(before "End --debug Settings")
 11 load_labels();
 12 load_source_lines();
 13 :(code)
 14 void load_labels() {
 15   ifstream fin("labels");
 16   fin >> std::hex;
 17   while (has_data(fin)) {
 18     uint32_t addr = 0;
 19     fin >> addr;
 20     string name;
 21     fin >> name;
 22     put(Symbol_name, addr, name);
 23   }
 24 }
 25 
 26 void load_source_lines() {
 27   ifstream fin("source_lines");
 28   fin >> std::hex;
 29   while (has_data(fin)) {
 30     uint32_t addr = 0;
 31     fin >> addr;
 32     string line;
 33     getline(fin, line);
 34     put(Source_line, addr, hacky_squeeze_out_whitespace(line));
 35   }
 36 }
 37 
 38 :(after "Run One Instruction")
 39 if (contains_key(Symbol_name, EIP))
 40   trace(Callstack_depth, "run") << "== label " << get(Symbol_name, EIP) << end();
 41 if (contains_key(Source_line, EIP))
 42   trace(Callstack_depth, "run") << "0x" << HEXWORD << EIP << ": " << get(Source_line, EIP) << end();
 43 else
 44   // no source line info; do what you can
 45   trace(Callstack_depth, "run") << "0x" << HEXWORD << EIP << ": " << debug_info(EIP) << end();
 46 
 47 :(code)
 48 string debug_info(uint32_t inst_address) {
 49   uint8_t op = read_mem_u8(inst_address);
 50   if (op != 0xe8) {
 51     ostringstream out;
 52     out << HEXBYTE << NUM(op);
 53     return out.str();
 54   }
 55   int32_t offset = read_mem_i32(inst_address+/*skip op*/1);
 56   uint32_t next_eip = inst_address+/*inst length*/5+offset;
 57   if (contains_key(Symbol_name, next_eip))
 58     return "e8/call "+get(Symbol_name, next_eip);
 59   ostringstream out;
 60   out << "e8/call 0x" << HEXWORD << next_eip;
 61   return out.str();
 62 }
 63 
 64 //: If a label starts with '$watch-', make a note of the effective address
 65 //: computed by the next instruction. Start dumping out its contents to the
 66 //: trace after every subsequent instruction.
 67 
 68 :(after "Run One Instruction")
 69 dump_watch_points();
 70 :(before "End Globals")
 71 map<string, uint32_t> Watch_points;
 72 :(before "End Reset")
 73 Watch_points.clear();
 74 :(code)
 75 void dump_watch_points() {
 76   if (Watch_points.empty()) return;
 77   trace(Callstack_depth, "dbg") << "watch points:" << end();
 78   for (map<string, uint32_t>::iterator p = Watch_points.begin();  p != Watch_points.end();  ++p)
 79     trace(Callstack_depth, "dbg") << "  " << p->first << ": " << HEXWORD << p->second << " -> " << HEXWORD << read_mem_u32(p->second) << end();
 80 }
 81 
 82 :(before "End Globals")
 83 string Watch_this_effective_address;
 84 :(after "Run One Instruction")
 85 Watch_this_effective_address = "";
 86 if (contains_key(Symbol_name, EIP) && starts_with(get(Symbol_name, EIP), "$watch-"))
 87   Watch_this_effective_address = get(Symbol_name, EIP);
 88 :(after "Found effective_address(addr)")
 89 if (!Watch_this_effective_address.empty()) {
 90   dbg << "now watching " << HEXWORD << addr << " for " << Watch_this_effective_address << end();
 91   put(Watch_points, Watch_this_effective_address, addr);
 92 }
 93 
 94 //: Special label that dumps regions of memory.
 95 //: Not a general mechanism; by the time you get here you're willing to hack
 96 //: on the emulator.
 97 :(after "Run One Instruction")
 98 if (contains_key(Symbol_name, EIP) && get(Symbol_name, EIP) == "$dump-stream-at-EAX")
 99   dump_stream_at(Reg[EAX].u);
100 :(code)
101 void dump_stream_at(uint32_t stream_start) {
102   int32_t stream_length = read_mem_i32(stream_start + 8);
103   dbg << "stream length: " << std::dec << stream_length << end();
104   for (int i = 0;  i < stream_length + 12;  ++i)
105     dbg << "0x" << HEXWORD << (stream_start+i) << ": " << HEXBYTE << NUM(read_mem_u8(stream_start+i)) << end();
106 }
107 
108 //: helpers
109 
110 :(code)
111 string hacky_squeeze_out_whitespace(const string& s) {
112   // strip whitespace at start
113   string::const_iterator first = s.begin();
114   while (first != s.end() && isspace(*first))
115     ++first;
116   if (first == s.end()) return "";
117 
118   // strip whitespace at end
119   string::const_iterator last = --s.end();
120   while (last != s.begin() && isspace(*last))
121     --last;
122   ++last;
123 
124   // replace runs of spaces/dots with single space until comment or string
125   // TODO:
126   //   leave alone dots not surrounded by whitespace
127   //   leave alone '#' within word
128   //   leave alone '"' within word
129   //   squeeze spaces after end of string
130   ostringstream out;
131   bool previous_was_space = false;
132   bool in_comment_or_string = false;
133   for (string::const_iterator curr = first;  curr != last;  ++curr) {
134     if (in_comment_or_string)
135       out << *curr;
136     else if (isspace(*curr) || *curr == '.')
137       previous_was_space = true;
138     else {
139       if (previous_was_space)
140         out << ' ';
141       out << *curr;
142       previous_was_space = false;
143       if (*curr == '#' || *curr == '"') in_comment_or_string = true;
144     }
145   }
146   return out.str();
147 }