-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgdbrunner.js
169 lines (141 loc) · 3.84 KB
/
gdbrunner.js
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
"use strict"
const spawn = require('child_process').spawn;
const util = require('util');
const process = require('process');
const pty = require('pty.js');
const tty = require('tty');
const API = require('./API.js');
const GDBOutputParser = require('./gdboutputparser.js');
const GDB_PROMPT = "(gdb) ";
const EventEmitter = require("events");
class GDBCommandDescriptor
{
constructor(id, filter_callback)
{
this.ID = id;
this.Filter = filter_callback;
}
}
class GDBRunner
{
constructor(arg)
{
if (typeof(arg) === "string")
{
this._should_attach = false;
this._path = arg;
}
else if (typeof(arg) === "number")
{
this._should_attach = true;
this._attach_pid = arg;
}
this._CommonInitializer();
}
_CommonInitializer()
{
this._current_output = "";
this._event_emitter = new EventEmitter();
this._gdb_parser = new GDBOutputParser.GDBOutputParser();
this._saved_data = "";
this._pty = null;
this._pid =-1;
this._inferior_status = "NotStarted";
this._command_id = 0;
this._command_descriptors = {};
}
GetInferiorStatus()
{
return this._inferior_status;
}
Run(output_callback)
{
this._AllocatePTYForProgramConsole();
this._output_callback = output_callback;
if (this._should_attach)
this._process = spawn("gdb", ["-i=mi", "-tty=" + this._pty.pty, "-p", this._attach_pid]);
else
this._process = spawn("gdb", ["-i=mi", "-tty=" + this._pty.pty, this._path]);
this._process.stdout.on("data", this.ReadOutput.bind(this));
}
SendConsoleInput(command)
{
this._pty.master.write(command);
}
RunCommand(command, args, filter_callback)
{
let cmd_descriptor = new GDBCommandDescriptor(this._command_id, filter_callback)
this._command_descriptors[this._command_id] = cmd_descriptor;
this._process.stdin.write(util.format("%d%s %s\r\n", this._command_id, command, args));
this._command_id++;
}
_AllocatePTYForProgramConsole()
{
this._pty = pty.Terminal.open(80, 25);
this._pty.master.on("data", this.OnProgramConsoleOutput.bind(this));
}
_ProcessGDBOutput(gdb_output)
{
if (gdb_output == undefined)
return;
if (gdb_output.Data != null)
{
if (gdb_output.Data.stopped != undefined)
this._inferior_status = "Stopped";
if (gdb_output.Data.running != undefined)
this._inferior_status = "Running";
if(gdb_output.Data['thread-group-started'] != undefined)
{
this._pid = gdb_output.Data['thread-group-started']['pid'];
}
}
}
_CallCommandFilter(gdb_output)
{
if (gdb_output == undefined)
return;
if (gdb_output.Type != API.results.GDB_RESULT_RECORD)
return;
if (this._command_descriptors[gdb_output.ID] != undefined)
{
let callback = this._command_descriptors[gdb_output.ID].Filter;
if (callback != undefined && callback != null)
callback(gdb_output)
}
}
OnProgramConsoleOutput(data)
{
this._output_callback(new GDBOutputParser.GDBOutput(API.results.GDB_INFERIOR_OUTPUT, "", data.toString()));
}
ReadOutput(data)
{
this._saved_data += data;
while (this._saved_data.indexOf("\n") != -1) //new line!
{
let line = this._saved_data.substr(0, this._saved_data.indexOf("\n"));
if (line != GDB_PROMPT)
{
let output = this._gdb_parser.Parse(line);
this._ProcessGDBOutput(output);
this._CallCommandFilter(output);
this._output_callback(output);
}
this._saved_data = this._saved_data.substr(this._saved_data.indexOf("\n") + 1);
}
if (!this._gdb_parser.IsGDBCommand(this._saved_data))
{
// Must be inferior output. Parse it.
this._output_callback(this._gdb_parser.Parse(this._saved_data));
this._saved_data = "";
}
}
Break()
{
if (this._pid == -1)
{
return;
}
process.kill(this._pid, "SIGINT");
}
};
module.exports = GDBRunner;