-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestmi.d
More file actions
225 lines (193 loc) · 4.79 KB
/
testmi.d
File metadata and controls
225 lines (193 loc) · 4.79 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
/// A simple GDB/MI interactive tester tool.
///
/// Authors: dd86k <dd@dax.moe>
/// Copyright: dd86k <dd@dax.moe>
/// License: BSD-3-Clause-Clear
module testmi;
import std;
import core.thread;
//alias splitstr = std.array.split;
//alias white = std.ascii.isWhite;
version (Windows)
{
immutable string defaultServer = "aliceserver.exe";
}
else
{
immutable string defaultServer = "./aliceserver";
}
enum Op : char
{
trace = '@',
info = '~',
important = '*',
warn = '?',
error = '!',
sending = '>',
receiving = '<',
}
__gshared
{
ProcessPipes server;
bool overbose;
int current_seq = 1;
}
void log(A...)(char op, string fmt, A args)
{
// If operating is one of those, and we don't want verbose, do not print
if (overbose == false)
switch (op) {
case Op.trace, Op.receiving, Op.sending: return;
default:
}
stderr.write("TESTER[", op, "]: ");
stderr.writefln(fmt, args);
}
int error(int code, string msg)
{
log(Op.error, msg);
return code;
}
string parseCString(string str)
{
__gshared char[1024] buffer;
return "";
}
unittest
{
}
struct MIReply
{
string body_;
char type;
}
MIReply send(string data)
{
// Wait for "(gdb)" prompt, then send command
Lwait:
string line = stripRight( server.stdout.readln() );
log(Op.receiving, line);
if (line is null)
{
MIReply eof;
eof.type = 0;
return eof;
}
if (line.length == 0)
goto Lwait;
// Prompt means server is ready for input
if (line == "(gdb)")
{
log(Op.sending, data);
server.stdin.write(data, '\n');
server.stdin.flush();
}
else
{
// Async output before prompt (e.g. events), skip
log(Op.trace, line);
}
// Read response lines until we get a result record
Lread:
string reply = stripRight( server.stdout.readln() );
log(Op.receiving, reply);
if (reply is null)
{
MIReply eof;
eof.type = 0;
return eof;
}
if (reply.length == 0)
goto Lread;
MIReply mi;
mi.type = reply[0];
mi.body_ = reply[1..$];
switch (mi.type) {
case '~': // console
goto Lread;
case '&': // logStream
case '=': // notify
case '*': // exec async
log(Op.trace, mi.body_);
goto Lread;
case '^': // result
return mi;
default: // "(gdb)" or unknown
goto Lread;
}
}
int main(string[] args)
{
string oserver;
bool otrace;
GetoptResult ores;
try
{
ores = getopt(args,
"s|server", "Server to use (default=aliceserver)", &oserver,
"t|trace", "Enable server trace messages", &otrace,
"v|verbose","Enable client verbose messages", &overbose,
);
}
catch (Exception ex)
{
log(Op.error, ex.msg);
return 1;
}
if (ores.helpWanted)
{
defaultGetoptPrinter(
`MI client tester, supports "gdb -i mi" and "lldb-mi"
OPTIONS`, ores.options);
return 0;
}
string[] svropts = void;
switch (oserver) {
case "gdb": // "gdb -i mi" alias
svropts = [ "gdb", "-i", "mi", "--quiet" ];
break;
case "lldb": // "lldb-mi" alias
svropts = [ "lldb-mi" ];
break;
case string.init: // default
// Can't see it, build it
if (exists(defaultServer) == false)
{
log(Op.important, "Server not found locally, building...");
int code = wait( spawnProcess([ "dub", "build" ]) );
if (code)
return error(code, "Compilation ended in error, aborting");
}
svropts = [ defaultServer, "-i", "mi" ];
if (otrace) svropts ~= [ "--log" ];
break;
default: // custom
svropts = [ oserver ];
}
// Spawn server, redirect all except stderr (inherits handle)
log(Op.info, "Starting %s...", oserver);
string[] launchopts = svropts ~ (args.length >= 1 ? args[1..$] : []);
server = pipeProcess(launchopts, Redirect.stdin | Redirect.stdout);
// NOTE: waitTimeout is only defined for Windows,
// despite Pid.performWait being available for POSIX
Thread.sleep(250.msecs);
if (tryWait(server.pid).terminated)
return error(2, "Could not launch server");
MIReply ver = send("show version");
if (ver.type == 0)
return error(2, "Server disconnected during version check");
log(Op.info, "Connected");
Lprompt:
write("tester> ");
string line = readln();
if (line is null) // EOF
return 0;
line = line.stripRight();
if (line.length == 0)
goto Lprompt;
MIReply reply = send(line);
// Server closed the connection (quit/exit)
if (reply.type == 0)
return 0;
goto Lprompt;
}