-
Notifications
You must be signed in to change notification settings - Fork 16
/
lldbmi2.cpp
executable file
·399 lines (361 loc) · 20.8 KB
/
lldbmi2.cpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <util.h>
#include <sys/stat.h>
#include <sys/timeb.h>
#include <stdarg.h>
#include <string.h>
#include <iostream>
#include "lldbmi2.h"
#include "engine.h"
#include "variables.h"
#include "log.h"
#include "test.h"
#include "version.h"
void help (STATE *pstate)
{
fprintf (stderr, "%s", pstate->lldbmi2Prompt);
fprintf (stderr, "Description:\n");
fprintf (stderr, " A MI2 interface to LLDB\n");
fprintf (stderr, "Authors:\n");
fprintf (stderr, " Didier Bertrand, 2015, 2016, 2018\n");
fprintf (stderr, " Eduard Matveev, 2016\n");
fprintf (stderr, " David Jenkins, 2018\n");
fprintf (stderr, "Syntax:\n");
fprintf (stderr, " lldbmi2 --version [options]\n");
fprintf (stderr, " lldbmi2 --interpreter mi2 [options]\n");
fprintf (stderr, "Arguments:\n");
fprintf (stderr, " --version: Return GDB's version (GDB 7.7.1) and exits.\n");
fprintf (stderr, " --interpreter mi2: Standard mi2 interface.\n");
fprintf (stderr, " --interpreter=mi2: Standard mi2 interface.\n");
fprintf (stderr, "Options:\n");
fprintf (stderr, " --log: Create log file in project root directory.\n");
fprintf (stderr, " --logmask mask: Select log categories. 0xFFF. See source code for values.\n");
fprintf (stderr, " --test n: Execute test sequence (to debug lldmi2).\n");
fprintf (stderr, " --script file_path: Execute test script or replay logfile (to debug lldmi2).\n");
fprintf (stderr, " --nx: Ignored.\n");
fprintf (stderr, " --frames frames: Max number of frames to display (%d).\n", FRAMES_MAX);
fprintf (stderr, " --children children: Max number of children to check for update (%d).\n", CHILDREN_MAX);
fprintf (stderr, " --walkdepth depth: Max walk depth in search for variables (%d).\n", WALK_DEPTH_MAX);
fprintf (stderr, " --changedepth depth: Max depth to check for updated variables (%d).\n", CHANGE_DEPTH_MAX);
}
LIMITS limits;
static STATE state;
int
main (int argc, char **argv, char **envp)
{
int narg;
fd_set set;
char commandLine[BIG_LINE_MAX]; // data from cdt
char consoleLine[LINE_MAX]; // data from eclipse's console
long chars;
struct timeval timeout;
int isVersion=0, isInterpreter=0;
const char *testCommand=NULL;
int isLog=0;
int logmask=LOG_ALL;
state.ptyfd = EOF;
state.gdbPrompt = "GNU gdb (GDB) 7.7.1\n";
sprintf (state.lldbmi2Prompt, "lldbmi2 version %s\n", LLDBMI2_VERSION);
state.cdtbufferB.grow(BIG_LINE_MAX);
limits.frames_max = FRAMES_MAX;
limits.children_max = CHILDREN_MAX;
limits.walk_depth_max = WALK_DEPTH_MAX;
limits.change_depth_max = CHANGE_DEPTH_MAX;
// get args
for (narg=0; narg<argc; narg++) {
logarg (argv[narg]);
if (strcmp (argv[narg],"--version") == 0)
isVersion = 1;
else if (strcmp (argv[narg],"--interpreter") == 0) {
isInterpreter = 1;
if (++narg<argc)
logarg(argv[narg]);
}
else if (strcmp (argv[narg],"--interpreter=mi2") == 0)
isInterpreter = 1;
else if ((strcmp (argv[narg],"-i") == 0) && (strcmp (argv[narg+1], "mi") == 0))
isInterpreter = 1;
else if (strcmp (argv[narg],"--test") == 0 ) {
limits.istest = true;
if (++narg<argc)
sscanf (logarg(argv[narg]), "%d", &state.test_sequence);
if (state.test_sequence)
setTestSequence (state.test_sequence);
}
else if (strcmp (argv[narg],"--script") == 0 ) {
limits.istest = true;
if (++narg<argc)
strcpy (state.test_script, logarg(argv[narg])); // no spaces allowed in the name
if (state.test_script[0])
setTestScript (state.test_script);
}
else if (strcmp (argv[narg],"--log") == 0 )
isLog = 1;
else if (strcmp (argv[narg],"--logmask") == 0 ) {
isLog = 1;
if (++narg<argc)
sscanf (logarg(argv[narg]), "%x", &logmask);
}
else if (strcmp (argv[narg],"--frames") == 0 ) {
if (++narg<argc)
sscanf (logarg(argv[narg]), "%d", &limits.frames_max);
}
else if (strcmp (argv[narg],"--children") == 0 ) {
if (++narg<argc)
sscanf (logarg(argv[narg]), "%d", &limits.children_max);
}
else if (strcmp (argv[narg],"--walkdepth") == 0 ) {
if (++narg<argc)
sscanf (logarg(argv[narg]), "%d", &limits.walk_depth_max);
}
else if (strcmp (argv[narg],"--changedepth") == 0 ) {
if (++narg<argc)
sscanf (logarg(argv[narg]), "%d", &limits.change_depth_max);
}
}
// create a log filename from program name and open log file
if (isLog) {
if (limits.istest)
setlogfile (state.logfilename, sizeof(state.logfilename), argv[0], "lldbmi2t.log");
else
setlogfile (state.logfilename, sizeof(state.logfilename), argv[0], "lldbmi2.log");
openlog (state.logfilename);
setlogmask (logmask);
}
// log program args
addlog("\n");
logprintf (LOG_ARGS, NULL);
state.envp[0] = NULL;
state.envpentries = 0;
state.envspointer = state.envs;
const char *wl = "PWD="; // want to get eclipse project_loc if any
int wll = strlen(wl);
// copy environment for tested program
for (int ienv=0; envp[ienv]; ienv++) {
addEnvironment (&state, envp[ienv]);
if (strncmp(envp[ienv], wl, wll)==0)
strcpy (state.project_loc, envp[ienv]+wll);
}
// return gdb version if --version
if (isVersion) {
writetocdt (state.gdbPrompt);
writetocdt (state.lldbmi2Prompt);
return EXIT_SUCCESS;
}
// check if --interpreter mi2
else if (!isInterpreter) {
help (&state);
return EXIT_FAILURE;
}
initializeSB (&state);
signal (SIGINT, signalHandler);
signal (SIGSTOP, signalHandler);
cdtprintf ("(gdb)\n");
// main loop
FD_ZERO (&set);
while (!state.eof) {
if (limits.istest)
logprintf (LOG_NONE, "main loop\n");
// get inputs
timeout.tv_sec = 0;
timeout.tv_usec = 200000;
// check command from CDT
FD_SET (STDIN_FILENO, &set);
if (state.ptyfd != EOF) {
// check data from Eclipse's console
FD_SET (state.ptyfd, &set);
select(state.ptyfd+1, &set, NULL, NULL, &timeout);
}
else
select(STDIN_FILENO+1, &set, NULL, NULL, &timeout);
if (FD_ISSET(STDIN_FILENO, &set) && !state.eof && !limits.istest) {
logprintf (LOG_NONE, "read in\n");
chars = read (STDIN_FILENO, commandLine, sizeof(commandLine)-1);
logprintf (LOG_NONE, "read out %d chars\n", chars);
if (chars>0) {
commandLine[chars] = '\0';
while (fromCDT (&state,commandLine,sizeof(commandLine)) == MORE_DATA)
commandLine[0] = '\0';
}
else
state.eof = true;
}
if (state.ptyfd!=EOF && state.isrunning) { // input from user to program
if (FD_ISSET(state.ptyfd, &set) && !state.eof && !limits.istest) {
logprintf (LOG_NONE, "pty read in\n");
chars = read (state.ptyfd, consoleLine, sizeof(consoleLine)-1);
logprintf (LOG_NONE, "pty read out %d chars\n", chars);
if (chars>0) {
logprintf (LOG_PROG_OUT, "pty read %d chars\n", chars);
consoleLine[chars] = '\0';
SBProcess process = state.process;
if (process.IsValid())
process.PutSTDIN (consoleLine, chars);
}
}
}
// execute test command if test mode
if (!state.eof && limits.istest && !state.isrunning) {
if ((testCommand=getTestCommand ())!=NULL) {
snprintf (commandLine, sizeof(commandLine), "%s\n", testCommand);
fromCDT (&state, commandLine, sizeof(commandLine));
}
}
// execute stacked commands if many command arrived once
if (!state.eof && state.cdtbufferB.size()>0) {
commandLine[0] = '\0';
while (fromCDT (&state, commandLine, sizeof(commandLine)) == MORE_DATA)
;
}
}
if (state.ptyfd != EOF)
close (state.ptyfd);
terminateSB ();
logprintf (LOG_INFO, "main exit\n");
closelog ();
return EXIT_SUCCESS;
}
// log an argument and return the argument
const char *
logarg (const char *arg) {
addlog (arg);
addlog (" ");
return arg;
}
void
writetocdt (const char *line)
{
logprintf (LOG_NONE, "writetocdt (...)\n", line);
logdata (LOG_CDT_OUT, line, strlen(line));
write (STDOUT_FILENO, line, strlen(line));
}
void
cdtprintf ( const char *format, ... )
{
logprintf (LOG_NONE, "cdtprintf (...)\n");
static StringB buffer(BIG_LINE_MAX);
va_list args;
if (format!=NULL) {
va_start (args, format);
buffer.vosprintf (0, format, args);
va_end (args);
if ((buffer.c_str()[0] == '0') && (buffer.c_str()[1] == '^'))
buffer.clear(1,0);
writetocdt (buffer.c_str());
}
}
std::string ReplaceString(std::string subject, const std::string& search,
const std::string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}
void
strrecprintf (const char *typestr, const char *format, va_list args)
{
logprintf (LOG_NONE, "srcprintf (...)\n");
static StringB buffer(BIG_LINE_MAX);
static StringB lineout(BIG_LINE_MAX);
static StringB prepend(10);
buffer.vosprintf (0, format, args);
prepend.clear();
prepend.append(typestr);
prepend.append("\"");
lineout.clear();
lineout.append(prepend.c_str());
for (int ndx=0; ndx<buffer.size(); ndx++) {
if (buffer.c_str()[ndx] == '\"')
lineout.append("\\\"");
else if (buffer.c_str()[ndx] == '\n') {
lineout.append("\\n\"\n");
writetocdt(lineout.c_str());
lineout.clear();
lineout.append(prepend.c_str());
}
else
lineout.append(buffer.c_str()[ndx]);
}
if (lineout.size() > 2) {
lineout.append("\n\"");
writetocdt (lineout.c_str());
}
}
void
srcprintf (const char *format, ... )
{
va_list args;
if (format!=NULL) {
va_start (args, format);
strrecprintf("&", format, args);
va_end (args);
}
}
void
srlprintf (const char *format, ... )
{
va_list args;
if (format!=NULL) {
va_start (args, format);
strrecprintf("~", format, args);
va_end (args);
}
}
static int signals_received=0;
void
signalHandler (int signo)
{
logprintf (LOG_TRACE, "signalHandler (%d)\n", signo);
if (signo==SIGINT)
logprintf (LOG_INFO, "signal SIGINT\n");
else
logprintf (LOG_INFO, "signal %s\n", signo);
if (signo==SIGINT) {
if (state.process.IsValid() && signals_received==0) {
int selfPID = getpid();
int processPID = state.process.GetProcessID();
logprintf (LOG_INFO, "signal_handler: signal SIGINT. self PID = %d, process pid = %d\n", selfPID, processPID);
logprintf (LOG_INFO, "send signal SIGSTOP to process %d\n", processPID);
// state.process.Signal (SIGSTOP);
logprintf (LOG_INFO, "Stop process\n");
state.process.Stop();
// ++signals_received;
}
else
state.debugger.DispatchInputInterrupt();
}
}
/*
BUTTON PAUSE (SIGSTOP or ^Z)