https://github.com/akkartik/mu/blob/master/subx/039debug.cc
 1 //:: Some helpers for debugging.
 2 
 3 //: Load the 'map' file generated during 'subx --map translate' when running 'subx --map --trace run'.
 4 //: (It'll only affect the trace.)
 5 
 6 :(before "End Globals")
 7 map</*address*/uint32_t, string> Symbol_name;  // used only by 'subx run'
 8 :(before "End --map Settings")
 9 load_map("map");
10 :(code)
11 void load_map(const string& map_filename) {
12   ifstream fin(map_filename.c_str());
13   fin >> std::hex;
14   while (has_data(fin)) {
15     uint32_t addr = 0;
16     fin >> addr;
17     string name;
18     fin >> name;
19     put(Symbol_name, addr, name);
20   }
21 }
22 
23 :(after "Run One Instruction")
24 if (contains_key(Symbol_name, EIP))
25   trace(Callstack_depth, "run") << "== label " << get(Symbol_name, EIP) << end();
26 
27 //: make calls in particular more salient
28 :(before "End Trace Call Instruction")
29 // at this point we've skipped past the e8 opcode, but not the offset operand
30 int32_t offset = read_mem_i32(EIP);
31 uint32_t next_eip = EIP+offset+4;
32 if (contains_key(Symbol_name, next_eip))
33   return "/call "+get(Symbol_name, next_eip);
34 
35 //: If a label starts with '$watch-', make a note of the effective address
36 //: computed by the next instruction. Start dumping out its contents to the
37 //: trace after every subsequent instruction.
38 
39 :(after "Run One Instruction")
40 dump_watch_points();
41 :(before "End Globals")
42 map<string, uint32_t> Watch_points;
43 :(before "End Reset")
44 Watch_points.clear();
45 :(code)
46 void dump_watch_points() {
47   if (Watch_points.empty()) return;
48   dbg << "watch points:" << end();
49   for (map<string, uint32_t>::iterator p = Watch_points.begin();  p != Watch_points.end();  ++p)
50     dbg << "  " << p->first << ": " << HEXWORD << p->second << " -> " << HEXWORD << read_mem_u32(p->second) << end();
51 }
52 
53 :(before "End Globals")
54 string Watch_this_effective_address;
55 :(after "Run One Instruction")
56 Watch_this_effective_address = "";
57 if (contains_key(Symbol_name, EIP) && starts_with(get(Symbol_name, EIP), "$watch-"))
58   Watch_this_effective_address = get(Symbol_name, EIP);
59 :(after "Found effective_address(addr)")
60 if (!Watch_this_effective_address.empty()) {
61   dbg << "now watching " << HEXWORD << addr << " for " << Watch_this_effective_address << end();
62   put(Watch_points, Watch_this_effective_address, addr);
63 }