diff --git a/README.md b/README.md index cb4617b..b330830 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,7 @@ GDB supports the target writing files in the PC's file system. This is suppored * `int file_write(int fd, const void *buf, unsigned int count)` -* `int file_system(const char *buf)` +* `int file_system(const char *buf)`: System calls are disabled by default in GDB. To enable, use the GDB command `set remote system-call-allowed 1` For example: ```C++ diff --git a/TeensyDebug.h b/TeensyDebug.h index 0c6c5b6..1d1162b 100644 --- a/TeensyDebug.h +++ b/TeensyDebug.h @@ -103,7 +103,16 @@ public: return gdb_file_io(gdb_io); } int file_system(const char *buf) { - sprintf(gdb_io, "Fsystem,%x/%x", (unsigned int)buf, strlen(buf)); + if (*buf == 0) { + // if we send empty string, return if shell enabled or not + // to enable in gdb, use: + // set remote system-call-allowed 1 + sprintf(gdb_io, "Fsystem,0/0", (unsigned int)buf, strlen(buf)); + } + else { + // make sure to add string terminator + sprintf(gdb_io, "Fsystem,%x/%x", (unsigned int)buf, strlen(buf)+1); + } return gdb_file_io(gdb_io); } }; diff --git a/gdbstub.cpp b/gdbstub.cpp index 3bf07a6..dc449da 100644 --- a/gdbstub.cpp +++ b/gdbstub.cpp @@ -325,6 +325,18 @@ size_t gdb_out_print(const char *msg) { return gdb_out_write((const uint8_t *)msg, strlen(msg)); } +int gdb_wait_for_flag(int *flag, int timeout) { + unsigned int endtime = millis()+1000; + while(*flag) { + if (timeout && millis() > endtime) { + return -1; + } + delay(1); + yield(); + } + return 0; +} + int file_io_result; int file_io_errno; int file_io_pending = 0; @@ -333,10 +345,7 @@ int gdb_file_io(const char *cmd) { // Serial.println(cmd); file_io_pending = 1; sendResult(cmd); - while(file_io_pending) { - delay(1); - yield(); - } + gdb_wait_for_flag(&file_io_pending, 1000); // Serial.println(file_io_result); return file_io_result; } @@ -349,13 +358,10 @@ int gdb_file_io(const char *cmd) { #pragma GCC optimize ("O0") void process_onbreak() { // send the signal + halt_state = 1; sendResult(signal_text[debug_id]); // go into halt state and stay until flag is cleared - halt_state = 1; - while(halt_state) { - delay(1); - yield(); - } + gdb_wait_for_flag(&halt_state, 0); debug_id = 0; }