@@ -49,6 +49,9 @@ void print_help() {
49
49
" binaries (default: wat2wasm)\n " );
50
50
fprintf (stdout,
51
51
" --file Wasm file (module) to load and execute\n " );
52
+ fprintf (stdout,
53
+ " --no-debug Run without debug thread"
54
+ " (default: false)\n " );
52
55
fprintf (stdout,
53
56
" --no-socket Run debug on stdout"
54
57
" (default: false)\n " );
@@ -106,37 +109,20 @@ Module *load(WARDuino wac, const char *file_name, Options opt) {
106
109
return nullptr ;
107
110
}
108
111
109
- void startDebuggerStd (WARDuino *wac, Module *m) {
110
- int valread;
111
- uint8_t buffer[1024 ] = {0 };
112
- wac->debugger ->setChannel (fileno (stdout));
113
- while (true ) {
114
- debug (" waiting for debug command\n " );
115
- while ((valread = read (fileno (stdin), buffer, 1024 )) != -1 ) {
116
- write (fileno (stdout), " got a message ... \n " , 19 );
117
- wac->handleInterrupt (valread - 1 , buffer);
118
- write (fileno (stdout), buffer, valread);
119
- fflush (stdout);
120
- }
112
+ void *startDebuggerCommunication (void *arg) {
113
+ Channel *duplex = WARDuino::instance ()->debugger ->channel ;
114
+ if (duplex == nullptr ) {
115
+ return nullptr ;
121
116
}
122
- }
123
117
124
- void startDebuggerSocket (WARDuino *wac, Module *m, int port = 8192 ) {
125
- int socket_fd = createSocketFileDescriptor ();
126
- struct sockaddr_in address = createAddress (port);
127
- bindSocketToAddress (socket_fd, address);
128
- startListening (socket_fd);
129
- printf (" Listening on port 172.0.0.1:%i\n " , port);
118
+ duplex->open ();
130
119
131
120
ssize_t valread;
132
121
uint8_t buffer[1024 ] = {0 };
133
122
while (true ) {
134
- int socket = listenForIncomingConnection (socket_fd, address);
135
- wac->debugger ->setChannel (socket);
136
- while ((valread = read (socket, buffer, 1024 )) != -1 ) {
137
- write (socket, " got a message ... \n " , 19 );
138
- wac->handleInterrupt (valread - 1 , buffer);
139
- write (socket, buffer, valread);
123
+ while ((valread = duplex->read (buffer, 1024 )) != -1 ) {
124
+ duplex->write (" got a message ... \n " , 19 );
125
+ WARDuino::instance ()->handleInterrupt (valread - 1 , buffer);
140
126
}
141
127
}
142
128
}
@@ -165,18 +151,31 @@ int connectToProxyFd(const char *proxyfd) { return open(proxyfd, O_RDWR); }
165
151
WARDuino *wac = WARDuino::instance();
166
152
Module *m;
167
153
168
- void *runWAC (void *p) {
169
- // Print value received as argument:
170
- dbg_info (" \n === STARTED INTERPRETATION (in separate thread) ===\n " );
171
- wac->run_module (m);
172
- wac->unload_module (m);
154
+ struct debugger_options {
155
+ const char *socket;
156
+ bool no_socket;
157
+ };
158
+
159
+ void *setupDebuggerCommunication (debugger_options *options) {
160
+ dbg_info (" \n === STARTED DEBUGGER (in separate thread) ===\n " );
161
+ // Start debugger
162
+ Channel *duplex;
163
+ if (options->no_socket ) {
164
+ duplex = new Duplex (stdin, stdout);
165
+ } else {
166
+ int port = std::stoi (options->socket );
167
+ duplex = new WebSocket (port);
168
+ }
169
+
170
+ wac->debugger ->setChannel (duplex);
173
171
}
174
172
175
173
int main (int argc, const char *argv[]) {
176
174
ARGV_SHIFT (); // Skip command name
177
175
178
176
bool return_exception = true ;
179
177
bool run_tests = false ;
178
+ bool no_debug = false ;
180
179
bool no_socket = false ;
181
180
const char *socket = " 8192" ;
182
181
bool paused = false ;
@@ -207,6 +206,8 @@ int main(int argc, const char *argv[]) {
207
206
ARGV_GET (asserts_file);
208
207
} else if (!strcmp (" --watcompiler" , arg)) {
209
208
ARGV_GET (watcompiler);
209
+ } else if (!strcmp (" --no-debug" , arg)) {
210
+ no_debug = true ;
210
211
} else if (!strcmp (" --no-socket" , arg)) {
211
212
no_socket = true ;
212
213
} else if (!strcmp (" --socket" , arg)) {
@@ -248,24 +249,23 @@ int main(int argc, const char *argv[]) {
248
249
// Run in proxy mode
249
250
wac->debugger ->proxify ();
250
251
} else if (proxy) {
251
- int connection = -1 ;
252
-
253
252
// Connect to proxy device
253
+ Channel *connection = nullptr ;
254
254
try {
255
255
int port = std::stoi (proxy);
256
- connection = connectToProxySocket (port);
256
+ connection = new WebSocket (port);
257
257
} catch (std::invalid_argument const &ex) {
258
258
// argument is not a port
259
259
// treat as filename
260
- connection = connectToProxyFd ( proxy);
260
+ connection = new FileDescriptorChannel ( open ( proxy, O_RDWR) );
261
261
} catch (std::out_of_range const &ex) {
262
262
// argument is an integer but is out of range
263
263
fprintf (stderr,
264
264
" wdcli: out of range integer argument for --proxy\n " );
265
265
return 1 ;
266
266
}
267
267
268
- if (connection < 0 ) {
268
+ if (connection == nullptr ) {
269
269
// Failed to connect stop program
270
270
fprintf (stderr, " wdcli: failed to connect to proxy device\n " );
271
271
return 1 ;
@@ -275,16 +275,25 @@ int main(int argc, const char *argv[]) {
275
275
wac->debugger ->startProxySupervisor (connection);
276
276
}
277
277
278
- // Run Wasm module (new thread)
278
+ // Start debugger (new thread)
279
279
pthread_t id;
280
- pthread_create (&id, nullptr , runWAC, nullptr );
281
-
282
- // Start debugger
283
- if (no_socket) {
284
- startDebuggerStd (wac, m);
285
- } else {
286
- startDebuggerSocket (wac, m, std::stoi (socket));
280
+ if (!no_debug) {
281
+ auto *options =
282
+ (debugger_options *)malloc (sizeof (struct debugger_options ));
283
+ options->no_socket = no_socket;
284
+ options->socket = socket;
285
+ setupDebuggerCommunication (options);
286
+ free (options);
287
+
288
+ pthread_create (&id, nullptr , startDebuggerCommunication, nullptr );
287
289
}
290
+
291
+ // Run Wasm module
292
+ dbg_info (" \n === STARTED INTERPRETATION (main thread) ===\n " );
293
+ wac->run_module (m);
294
+ wac->unload_module (m);
295
+ wac->debugger ->stop ();
296
+
288
297
int *ptr;
289
298
pthread_join (id, (void **)&ptr);
290
299
}
0 commit comments