+static void gdb_signal_reply(struct target *target, struct connection *connection)
+{
+ struct gdb_connection *gdb_connection = connection->priv;
+ char sig_reply[20];
+ char stop_reason[20];
+ int sig_reply_len;
+ int signal_var;
+
+ if (target->debug_reason == DBG_REASON_EXIT) {
+ sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "W00");
+ } else {
+ if (gdb_connection->ctrl_c) {
+ signal_var = 0x2;
+ gdb_connection->ctrl_c = 0;
+ } else
+ signal_var = gdb_last_signal(target);
+
+ stop_reason[0] = '\0';
+ if (target->debug_reason == DBG_REASON_WATCHPOINT) {
+ enum watchpoint_rw hit_wp_type;
+ uint32_t hit_wp_address;
+
+ if (watchpoint_hit(target, &hit_wp_type, &hit_wp_address) == ERROR_OK) {
+
+ switch (hit_wp_type) {
+ case WPT_WRITE:
+ snprintf(stop_reason, sizeof(stop_reason),
+ "watch:%08x;", hit_wp_address);
+ break;
+ case WPT_READ:
+ snprintf(stop_reason, sizeof(stop_reason),
+ "rwatch:%08x;", hit_wp_address);
+ break;
+ case WPT_ACCESS:
+ snprintf(stop_reason, sizeof(stop_reason),
+ "awatch:%08x;", hit_wp_address);
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "T%2.2x%s",
+ signal_var, stop_reason);
+ }
+
+ gdb_put_packet(connection, sig_reply, sig_reply_len);
+ gdb_connection->frontend_state = TARGET_HALTED;
+ rtos_update_threads(target);
+}
+
+static void gdb_fileio_reply(struct target *target, struct connection *connection)
+{
+ struct gdb_connection *gdb_connection = connection->priv;
+ char fileio_command[256];
+ int command_len;
+ bool program_exited = false;
+
+ if (strcmp(target->fileio_info->identifier, "open") == 0)
+ sprintf(fileio_command, "F%s,%x/%x,%x,%x", target->fileio_info->identifier,
+ target->fileio_info->param_1,
+ target->fileio_info->param_2,
+ target->fileio_info->param_3,
+ target->fileio_info->param_4);
+ else if (strcmp(target->fileio_info->identifier, "close") == 0)
+ sprintf(fileio_command, "F%s,%x", target->fileio_info->identifier,
+ target->fileio_info->param_1);
+ else if (strcmp(target->fileio_info->identifier, "read") == 0)
+ sprintf(fileio_command, "F%s,%x,%x,%x", target->fileio_info->identifier,
+ target->fileio_info->param_1,
+ target->fileio_info->param_2,
+ target->fileio_info->param_3);
+ else if (strcmp(target->fileio_info->identifier, "write") == 0)
+ sprintf(fileio_command, "F%s,%x,%x,%x", target->fileio_info->identifier,
+ target->fileio_info->param_1,
+ target->fileio_info->param_2,
+ target->fileio_info->param_3);
+ else if (strcmp(target->fileio_info->identifier, "lseek") == 0)
+ sprintf(fileio_command, "F%s,%x,%x,%x", target->fileio_info->identifier,
+ target->fileio_info->param_1,
+ target->fileio_info->param_2,
+ target->fileio_info->param_3);
+ else if (strcmp(target->fileio_info->identifier, "rename") == 0)
+ sprintf(fileio_command, "F%s,%x/%x,%x/%x", target->fileio_info->identifier,
+ target->fileio_info->param_1,
+ target->fileio_info->param_2,
+ target->fileio_info->param_3,
+ target->fileio_info->param_4);
+ else if (strcmp(target->fileio_info->identifier, "unlink") == 0)
+ sprintf(fileio_command, "F%s,%x/%x", target->fileio_info->identifier,
+ target->fileio_info->param_1,
+ target->fileio_info->param_2);
+ else if (strcmp(target->fileio_info->identifier, "stat") == 0)
+ sprintf(fileio_command, "F%s,%x/%x,%x", target->fileio_info->identifier,
+ target->fileio_info->param_1,
+ target->fileio_info->param_2,
+ target->fileio_info->param_3);
+ else if (strcmp(target->fileio_info->identifier, "fstat") == 0)
+ sprintf(fileio_command, "F%s,%x,%x", target->fileio_info->identifier,
+ target->fileio_info->param_1,
+ target->fileio_info->param_2);
+ else if (strcmp(target->fileio_info->identifier, "gettimeofday") == 0)
+ sprintf(fileio_command, "F%s,%x,%x", target->fileio_info->identifier,
+ target->fileio_info->param_1,
+ target->fileio_info->param_2);
+ else if (strcmp(target->fileio_info->identifier, "isatty") == 0)
+ sprintf(fileio_command, "F%s,%x", target->fileio_info->identifier,
+ target->fileio_info->param_1);
+ else if (strcmp(target->fileio_info->identifier, "system") == 0)
+ sprintf(fileio_command, "F%s,%x/%x", target->fileio_info->identifier,
+ target->fileio_info->param_1,
+ target->fileio_info->param_2);
+ else if (strcmp(target->fileio_info->identifier, "exit") == 0) {
+ /* If target hits exit syscall, report to GDB the program is terminated.
+ * In addition, let target run its own exit syscall handler. */
+ program_exited = true;
+ sprintf(fileio_command, "W%02x", target->fileio_info->param_1);
+ } else {
+ LOG_DEBUG("Unknown syscall: %s", target->fileio_info->identifier);
+
+ /* encounter unknown syscall, continue */
+ gdb_connection->frontend_state = TARGET_RUNNING;
+ target_resume(target, 1, 0x0, 0, 0);
+ return;
+ }
+
+ command_len = strlen(fileio_command);
+ gdb_put_packet(connection, fileio_command, command_len);
+
+ if (program_exited) {
+ /* Use target_resume() to let target run its own exit syscall handler. */
+ gdb_connection->frontend_state = TARGET_RUNNING;
+ target_resume(target, 1, 0x0, 0, 0);
+ } else {
+ gdb_connection->frontend_state = TARGET_HALTED;
+ rtos_update_threads(target);
+ }
+}