-
Notifications
You must be signed in to change notification settings - Fork 654
Expand file tree
/
Copy pathGdbCommandHandler.cc
More file actions
238 lines (200 loc) · 6.42 KB
/
GdbCommandHandler.cc
File metadata and controls
238 lines (200 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* -*- Mode: C++; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
#include "GdbCommandHandler.h"
#include "GdbCommand.h"
#include "log.h"
#include <sstream>
#include <vector>
using namespace std;
namespace rr {
// HashMap would be better here but the unordered_map API is annoying
// and linear search is fine.
static vector<GdbCommand*>* gdb_command_list;
static string gdb_macro_binding(const GdbCommand& cmd) {
string auto_args_str = "[";
for (size_t i = 0; i < cmd.auto_args().size(); i++) {
if (i > 0) {
auto_args_str += ", ";
}
auto_args_str += "'" + cmd.auto_args()[i] + "'";
}
auto_args_str += "]";
string ret = "python RRCmd('" + cmd.name() + "', " + auto_args_str + ")\n";
if (!cmd.docs().empty()) {
ret += "document " + cmd.name() + "\n" + cmd.docs() + "\nend\n";
}
return ret;
}
/* static */ string GdbCommandHandler::gdb_macros() {
GdbCommand::init_auto_args();
stringstream ss;
ss << string(R"Delimiter(
set python print-stack full
python
import re
def gdb_unescape(string):
result = ""
pos = 0
while pos < len(string):
result += chr(int(string[pos:pos+2], 16))
pos += 2
return result
def gdb_escape(string):
result = ""
pos = 0
for curr_char in string:
result += format(ord(curr_char), '02x')
return result
class RRWhere(gdb.Command):
"""Helper to get the location for checkpoints/history. Used by auto-args"""
def __init__(self):
gdb.Command.__init__(self, 'rr-where',
gdb.COMMAND_USER, gdb.COMPLETE_NONE, False)
def invoke(self, arg, from_tty):
#Get the symbol name from 'frame 0' in the format:
# '#0 0x00007f9d81a04c46 in _dl_start (arg=0x7ffee1f1c740) at rtld.c:356
# 356 in rtld.c'
try:
rv = gdb.execute('frame 0', to_string=True)
except:
rv = "???" # This may occurs if we're not running
m = re.match("#0\w*(.*)", rv);
if m:
rv = m.group(1)
else:
rv = rv + "???"
gdb.write(rv)
RRWhere()
class RRCmd(gdb.Command):
def __init__(self, name, auto_args):
gdb.Command.__init__(self, name,
gdb.COMMAND_USER, gdb.COMPLETE_NONE, False)
self.cmd_name = name
self.auto_args = auto_args
def invoke(self, arg, from_tty):
args = gdb.string_to_argv(arg)
self.rr_cmd(args)
def rr_cmd(self, args):
cmd_prefix = "maint packet qRRCmd:" + gdb_escape(self.cmd_name)
argStr = ""
for auto_arg in self.auto_args:
argStr += ":" + gdb_escape(gdb.execute(auto_arg, to_string=True))
for arg in args:
argStr += ":" + gdb_escape(arg)
rv = gdb.execute(cmd_prefix + argStr, to_string=True);
rv_match = re.search('received: "(.*)"', rv, re.MULTILINE);
if not rv_match:
gdb.write("Response error: " + rv)
return
response = gdb_unescape(rv_match.group(1))
gdb.write(response)
def history_push(p):
gdb.execute("rr-history-push", to_string=True)
rr_suppress_run_hook = False
class RRHookRun(gdb.Command):
def __init__(self):
gdb.Command.__init__(self, 'rr-hook-run',
gdb.COMMAND_USER, gdb.COMPLETE_NONE, False)
def invoke(self, arg, from_tty):
thread = int(gdb.parse_and_eval("$_thread"))
if thread != 0 and not rr_suppress_run_hook:
gdb.execute("stepi")
class RRSetSuppressRunHook(gdb.Command):
def __init__(self):
gdb.Command.__init__(self, 'rr-set-suppress-run-hook',
gdb.COMMAND_USER, gdb.COMPLETE_NONE, False)
def invoke(self, arg, from_tty):
rr_suppress_run_hook = arg == '1'
RRHookRun()
RRSetSuppressRunHook()
#Automatically push an history entry when the program execution stops
#(signal, breakpoint).This is fired before an interactive prompt is shown.
#Disabled for now since it's not fully working.
#gdb.events.stop.connect(history_push)
end
)Delimiter");
if (gdb_command_list) {
for (auto& it : *gdb_command_list) {
ss << gdb_macro_binding(*it);
}
}
ss << string(R"Delimiter(
define hookpost-back
frame
end
define hookpost-forward
frame
end
)Delimiter");
return ss.str();
}
/*static*/ GdbCommand* GdbCommandHandler::command_for_name(const string& name) {
if (!gdb_command_list) {
return nullptr;
}
for (auto& it : *gdb_command_list) {
if (it->name() == name) {
return it;
}
}
return nullptr;
}
void GdbCommandHandler::register_command(GdbCommand& cmd) {
LOG(debug) << "registering command: " << cmd.name();
if (!gdb_command_list) {
gdb_command_list = new vector<GdbCommand*>();
}
gdb_command_list->push_back(&cmd);
}
// Use the simplest two hex character by byte encoding
static string gdb_escape(const string& str) {
stringstream ss;
ss << hex;
for (size_t i = 0; i < str.size(); i++) {
int chr = (int)str.at(i);
if (chr < 16) {
ss << "0";
}
ss << chr;
}
return ss.str();
}
static string gdb_unescape(const string& str) {
stringstream ss;
for (size_t i = 0; i < str.size(); i += 2) {
ss << (char)stoul(str.substr(i, 2), nullptr, 16);
}
return ss.str();
}
static vector<string> parse_cmd(string& str) {
vector<string> args;
size_t pos = 0;
string delimiter = ":";
while ((pos = str.find(delimiter)) != string::npos) {
args.push_back(gdb_unescape(str.substr(0, pos)));
str.erase(0, pos + delimiter.length());
}
args.push_back(gdb_unescape(str));
return args;
}
/* static */ string GdbCommandHandler::process_command(GdbServer& gdb_server,
Task* t,
string payload) {
const vector<string> args = parse_cmd(payload);
GdbCommand* cmd = command_for_name(args[0]);
if (!cmd) {
return gdb_escape(string() + "Command '" + args[0] + "' not found.\n");
}
LOG(debug) << "invoking command: " << cmd->name();
string resp = cmd->invoke(gdb_server, t, args);
if (resp == GdbCommandHandler::cmd_end_diversion()) {
LOG(debug) << "cmd must run outside of diversion (" << resp << ")";
if (gdb_server.remote_supports_replay_diversion()) {
return gdb_escape(string() + "Command '" + cmd->name() +
"' cannot run in a replay diversion.\n");
}
return resp;
}
LOG(debug) << "cmd response: " << resp;
return gdb_escape(resp + "\n");
}
} // namespace rr