Skip to content

Commit f2e7b28

Browse files
authored
Merge pull request #93 from TOPLLab/fix/91
Fix/91
2 parents 7b1e6f0 + 24c91f5 commit f2e7b28

File tree

10 files changed

+212
-71
lines changed

10 files changed

+212
-71
lines changed

platforms/Arduino/Arduino.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ Module* m;
2121
#define UART_PIN 3
2222

2323
void startDebuggerStd(void* pvParameter) {
24+
Channel* sink = new Sink(stdout);
25+
wac->debugger->setChannel(sink);
26+
sink->open();
27+
2428
uint8_t buffer[1024] = {0};
25-
wac->debugger->setChannel(fileno(stdout));
26-
write(fileno(stdout), "Got a message ... \n", 19);
2729
while (true) {
28-
// taskYIELD();
29-
// vTaskDelay(100 / portTICK_PERIOD_MS);
3030
yield();
3131

3232
while (Serial.available()) {

platforms/CLI-Emulator/main.cpp

+52-43
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ void print_help() {
4949
"binaries (default: wat2wasm)\n");
5050
fprintf(stdout,
5151
" --file Wasm file (module) to load and execute\n");
52+
fprintf(stdout,
53+
" --no-debug Run without debug thread"
54+
"(default: false)\n");
5255
fprintf(stdout,
5356
" --no-socket Run debug on stdout"
5457
"(default: false)\n");
@@ -106,37 +109,20 @@ Module *load(WARDuino wac, const char *file_name, Options opt) {
106109
return nullptr;
107110
}
108111

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;
121116
}
122-
}
123117

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();
130119

131120
ssize_t valread;
132121
uint8_t buffer[1024] = {0};
133122
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);
140126
}
141127
}
142128
}
@@ -165,18 +151,31 @@ int connectToProxyFd(const char *proxyfd) { return open(proxyfd, O_RDWR); }
165151
WARDuino *wac = WARDuino::instance();
166152
Module *m;
167153

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);
173171
}
174172

175173
int main(int argc, const char *argv[]) {
176174
ARGV_SHIFT(); // Skip command name
177175

178176
bool return_exception = true;
179177
bool run_tests = false;
178+
bool no_debug = false;
180179
bool no_socket = false;
181180
const char *socket = "8192";
182181
bool paused = false;
@@ -207,6 +206,8 @@ int main(int argc, const char *argv[]) {
207206
ARGV_GET(asserts_file);
208207
} else if (!strcmp("--watcompiler", arg)) {
209208
ARGV_GET(watcompiler);
209+
} else if (!strcmp("--no-debug", arg)) {
210+
no_debug = true;
210211
} else if (!strcmp("--no-socket", arg)) {
211212
no_socket = true;
212213
} else if (!strcmp("--socket", arg)) {
@@ -248,24 +249,23 @@ int main(int argc, const char *argv[]) {
248249
// Run in proxy mode
249250
wac->debugger->proxify();
250251
} else if (proxy) {
251-
int connection = -1;
252-
253252
// Connect to proxy device
253+
Channel *connection = nullptr;
254254
try {
255255
int port = std::stoi(proxy);
256-
connection = connectToProxySocket(port);
256+
connection = new WebSocket(port);
257257
} catch (std::invalid_argument const &ex) {
258258
// argument is not a port
259259
// treat as filename
260-
connection = connectToProxyFd(proxy);
260+
connection = new FileDescriptorChannel(open(proxy, O_RDWR));
261261
} catch (std::out_of_range const &ex) {
262262
// argument is an integer but is out of range
263263
fprintf(stderr,
264264
"wdcli: out of range integer argument for --proxy\n");
265265
return 1;
266266
}
267267

268-
if (connection < 0) {
268+
if (connection == nullptr) {
269269
// Failed to connect stop program
270270
fprintf(stderr, "wdcli: failed to connect to proxy device\n");
271271
return 1;
@@ -275,16 +275,25 @@ int main(int argc, const char *argv[]) {
275275
wac->debugger->startProxySupervisor(connection);
276276
}
277277

278-
// Run Wasm module (new thread)
278+
// Start debugger (new thread)
279279
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);
287289
}
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+
288297
int *ptr;
289298
pthread_join(id, (void **)&ptr);
290299
}

platforms/ESP-IDF/main.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@ WARDuino* wac = WARDuino::instance();
2828
Module* m;
2929

3030
void startDebuggerStd(void* pvParameter) {
31+
Channel* duplex = new Duplex(stdin, stdout);
32+
wac->debugger->setChannel(duplex);
33+
duplex->open();
34+
3135
int valread;
3236
uint8_t buffer[1024] = {0};
33-
wac->debugger->setChannel(fileno(stdout));
3437
while (true) {
3538
taskYIELD();
3639
vTaskDelay(1000 / portTICK_PERIOD_MS);
3740

38-
while ((valread = read(fileno(stdin), buffer, 1024)) != -1) {
39-
write(fileno(stdout), "got a message ... \n", 19);
41+
while ((valread = duplex->read(buffer, 1024)) != -1) {
42+
duplex->write("got a message ... \n", 19);
4043
wac->handleInterrupt(valread - 1, buffer);
41-
write(fileno(stdout), buffer, valread);
42-
fflush(stdout);
4344
}
4445
}
4546
}

src/Debug/debugger.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
// Debugger
1919

20-
Debugger::Debugger(int address) { this->channel = new Channel(address); }
20+
Debugger::Debugger(Channel *duplex) { this->channel = duplex; }
2121

2222
// Public methods
2323

24-
void Debugger::setChannel(int address) {
24+
void Debugger::setChannel(Channel *duplex) {
2525
delete this->channel;
26-
this->channel = new Channel(address);
26+
this->channel = duplex;
2727
}
2828

2929
void Debugger::addDebugMessage(size_t len, const uint8_t *buff) {
@@ -152,6 +152,7 @@ bool Debugger::checkDebugMessages(Module *m, RunningState *program_state) {
152152
break;
153153
case interruptHALT:
154154
this->channel->write("STOP!\n");
155+
this->channel->close();
155156
free(interruptData);
156157
exit(0);
157158
case interruptPAUSE:
@@ -1014,7 +1015,7 @@ void Debugger::handleMonitorProxies(Module *m, uint8_t *interruptData) {
10141015
this->channel->write("done!\n");
10151016
}
10161017

1017-
void Debugger::startProxySupervisor(int socket) {
1018+
void Debugger::startProxySupervisor(Channel *socket) {
10181019
this->connected_to_proxy = true;
10191020
pthread_mutex_init(&this->supervisor_mutex, nullptr);
10201021
pthread_mutex_lock(&this->supervisor_mutex);
@@ -1026,7 +1027,7 @@ void Debugger::startProxySupervisor(int socket) {
10261027
bool Debugger::proxy_connected() const { return this->connected_to_proxy; }
10271028

10281029
void Debugger::disconnect_proxy() {
1029-
if (this->proxy_connected()) {
1030+
if (!this->proxy_connected()) {
10301031
return;
10311032
}
10321033
int *ptr;
@@ -1047,3 +1048,11 @@ void Debugger::updateCallbackmapping(Module *m, const char *data) {
10471048
}
10481049
}
10491050
}
1051+
1052+
// Stop the debugger
1053+
void Debugger::stop() {
1054+
if (this->channel != nullptr) {
1055+
this->channel->close();
1056+
this->channel = nullptr;
1057+
}
1058+
}

src/Debug/debugger.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,13 @@ class Debugger {
137137
nullptr; // Breakpoint to skip in the next interpretation step
138138

139139
// Constructor
140-
explicit Debugger(int address);
140+
explicit Debugger(Channel *duplex);
141141

142-
void setChannel(int address);
142+
void setChannel(Channel *duplex);
143+
144+
// Public methods
145+
146+
void stop();
143147

144148
// Interrupts
145149

@@ -174,7 +178,7 @@ class Debugger {
174178

175179
bool isProxied(uint32_t fidx) const;
176180

177-
void startProxySupervisor(int socket);
181+
void startProxySupervisor(Channel *socket);
178182

179183
bool proxy_connected() const;
180184

src/Edward/proxy_supervisor.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ Event *parseJSON(char *buff) {
5656
return new Event(*parsed.find("topic"), payload);
5757
}
5858

59-
ProxySupervisor::ProxySupervisor(int socket, pthread_mutex_t *mutex) {
59+
ProxySupervisor::ProxySupervisor(Channel *duplex, pthread_mutex_t *mutex) {
6060
printf("Started supervisor.\n");
61-
this->channel = new Channel(socket);
61+
this->channel = duplex;
6262
this->mutex = mutex;
6363

6464
pthread_create(&this->threadid, nullptr, readSocket, this);

src/Edward/proxy_supervisor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ProxySupervisor {
2020
void deserializeRFCResult(RFC *rfc);
2121

2222
public:
23-
ProxySupervisor(int socket, pthread_mutex_t *mutex);
23+
ProxySupervisor(Channel *duplex, pthread_mutex_t *mutex);
2424

2525
void startPushDebuggerSocket();
2626

src/Primitives/emulated.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def_prim(print_string, twoToNoneU32) {
268268
uint32_t size = arg0.uint32;
269269
std::string text = parse_utf8_string(m->memory.bytes, size, addr);
270270
debug("EMU: print string at %i: ", addr);
271-
printf("%s\n", text.c_str());
271+
printf("%s", text.c_str());
272272
pop_args(2);
273273
return true;
274274
}

0 commit comments

Comments
 (0)