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->second) << end();
82 }
83
84 :(before "End Globals")
85 string Watch_this_effective_address;
86 :(after "Run One Instruction")
87 Watch_this_effective_address = "";
88 if (contains_key(Symbol_name, EIP) && starts_with(get(Symbol_name, EIP), "$watch-"))
89 Watch_this_effective_address = get(Symbol_name, EIP);
90 :(after "Found effective_address(addr)")
91 if (!Watch_this_effective_address.empty()) {
92 dbg << "now watching " << HEXWORD << addr << " for " << Watch_this_effective_address << end();
93 put(Watch_points, Watch_this_effective_address, addr);
94 }
95
96
97
98
99 :(after "Run One Instruction")
100 if (contains_key(Symbol_name, EIP) && get(Symbol_name, EIP) == "$dump-stream-at-EAX")
101 dump_stream_at(Reg[EAX].u);
102 :(code)
103 void dump_stream_at(uint32_t stream_start) {
104 int32_t stream_length = read_mem_i32(stream_start + 8);
105 dbg << "stream length: " << std::dec << stream_length << end();
106 for (int i = 0; i < stream_length + 12; ++i)
107 dbg << "0x" << HEXWORD << (stream_start+i) << ": " << HEXBYTE << NUM(read_mem_u8(stream_start+i)) << end();
108 }
109
110
111
112 :(code)
113 string hacky_squeeze_out_whitespace(const string& s) {
114
115 string::const_iterator first = s.begin();
116 while (first != s.end() && isspace(*first))
117 ++first;
118 if (first == s.end()) return "";
119
120
121 string::const_iterator last = --s.end();
122 while (last != s.begin() && isspace(*last))
123 --last;
124 ++last;
125
126
127 TODO
128
129
130
131
132 ostringstream out;
133 bool previous_was_space = false;
134 bool in_comment_or_string = false;
135 for (string::const_iterator curr = first; curr != last; ++curr) {
136 if (in_comment_or_string)
137 out << *curr;
138 else if (isspace(*curr) || *curr == '.')
139 previous_was_space = true;
140 else {
141 if (previous_was_space)
142 out << ' ';
143 out << *curr;
144 previous_was_space = false;
145 if (*curr == '#' || *curr == '"') in_comment_or_string = true;
146 }
147 }
148 return out.str();
149 }