https://github.com/akkartik/mu/blob/master/subx/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   dbg << "watch points:" << end();
 78   for (map<string, uint32_t>::iterator p = Watch_points.begin();  p != Watch_points.end();  ++p)
 79     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 //: helpers
 95 
 96 :(code)
 97 string hacky_squeeze_out_whitespace(const string& s) {
 98   // strip whitespace at start
 99   string::const_iterator first = s.begin();
100   while (first != s.end() && isspace(*first))
101     ++first;
102   if (first == s.end()) return "";
103 
104   // strip whitespace at end
105   string::const_iterator last = --s.end();
106   while (last != s.begin() && isspace(*last))
107     --last;
108   ++last;
109 
110   // replace runs of spaces/dots with single space until comment or string
111   // TODO:
112   //   leave alone dots not surrounded by whitespace
113   //   leave alone '#' within word
114   //   leave alone '"' within word
115   //   squeeze spaces after end of string
116   ostringstream out;
117   bool previous_was_space = false;
118   bool in_comment_or_string = false;
119   for (string::const_iterator curr = first;  curr != last;  ++curr) {
120     if (in_comment_or_string)
121       out << *curr;
122     else if (isspace(*curr) || *curr == '.')
123       previous_was_space = true;
124     else {
125       if (previous_was_space)
126         out << ' ';
127       out << *curr;
128       previous_was_space = false;
129       if (*curr == '#' || *curr == '"') in_comment_or_string = true;
130     }
131   }
132   return out.str();
133 }