https://github.com/akkartik/mu/blob/master/039debug.cc
1
2
3
4
5
6
7 :(before "End Globals")
8 map<uint32_t, string> Symbol_name;
9 map<uint32_t, string> Source_line;
10 :(before "End --trace Settings")
11 load_labels();
12 load_source_lines();
13 :(code)
14 void load_labels() {
15 ifstream fin("labels");
16 if (fin.fail()) return;
17 fin >> std::hex;
18 while (has_data(fin)) {
19 uint32_t addr = 0;
20 fin >> addr;
21 string name;
22 fin >> name;
23 put(Symbol_name, addr, name);
24 }
25 }
26
27 void load_source_lines() {
28 ifstream fin("source_lines");
29 if (fin.fail()) return;
30 fin >> std::hex;
31 while (has_data(fin)) {
32 uint32_t addr = 0;
33 fin >> addr;
34 string line;
35 getline(fin, line);
36 put(Source_line, addr, hacky_squeeze_out_whitespace(line));
37 }
38 }
39
40 :(after "Run One Instruction")
41 if (contains_key(Symbol_name, EIP))
42 trace(Callstack_depth, "run") << "== label " << get(Symbol_name, EIP) << end();
43 if (contains_key(Source_line, EIP))
44 trace(Callstack_depth, "run") << "inst: " << get(Source_line, EIP) << end();
45 else
46
47 trace(Callstack_depth, "run") << "inst: " << debug_info(EIP) << end();
48
49 :(code)
50 string debug_info(uint32_t inst_address) {
51 uint8_t op = read_mem_u8(inst_address);
52 if (op != 0xe8) {
53 ostringstream out;
54 out << HEXBYTE << NUM(op);
55 return out.str();
56 }
57 int32_t offset = read_mem_i32(inst_address+1);
58 uint32_t next_eip = inst_address+5+offset;
59 if (contains_key(Symbol_name, next_eip))
60 return "e8/call "+get(Symbol_name, next_eip);
61 ostringstream out;
62 out << "e8/call 0x" << HEXWORD << next_eip;
63 return out.str();
64 }
65
66
67
68
69
70 :(after "Run One Instruction")
71 dump_watch_points();
72 :(before "End Globals")
73 map<string, uint32_t> Watch_points;
74 :(before "End Reset")
75 Watch_points.clear();
76 :(code)
77 void dump_watch_points() {
78 if (Watch_points.empty()) return;
79 trace(Callstack_depth, "dbg") << "watch points:" << end();
80 for (map<string, uint32_t>::iterator p = Watch_points.begin(); p != Watch_points.end(); ++p)
81 trace(Callstack_depth, "dbg") << " " << p->first << ": " << HEXWORD << p->second << " -> " << HEXWORD << read_mem_u32(p->