diff --git a/src/server/server.c b/src/server/server.c index 2be90451f7..76bd2767cf 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -752,6 +752,11 @@ bool openocd_is_shutdown_pending(void) return shutdown_openocd != CONTINUE_MAIN_LOOP; } +bool openocd_shutdown_by_signal(void) +{ + return shutdown_openocd == SHUTDOWN_WITH_SIGNAL_CODE; +} + /* tell the server we want to shut down */ COMMAND_HANDLER(handle_shutdown_command) { diff --git a/src/server/server.h b/src/server/server.h index c9d4698af8..bf38c73f1c 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -106,6 +106,8 @@ int connection_read(struct connection *connection, void *data, int len); bool openocd_is_shutdown_pending(void); +bool openocd_shutdown_by_signal(void); + /** * Defines an extended command handler function declaration to enable * access to (and manipulation of) the server port number. diff --git a/src/target/image.c b/src/target/image.c index ad2d856b54..08f77a81ca 100644 --- a/src/target/image.c +++ b/src/target/image.c @@ -24,6 +24,7 @@ #include "image.h" #include "target.h" #include +#include /* convert ELF header field to host endianness */ #define field16(elf, field) \ @@ -1287,6 +1288,8 @@ int image_calculate_checksum(const uint8_t *buffer, uint32_t nbytes, uint32_t *c crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255]; } keep_alive(); + if (openocd_shutdown_by_signal()) + return ERROR_FAIL; } LOG_DEBUG("Calculating checksum done; checksum=0x%" PRIx32, crc); diff --git a/src/target/riscv/batch.c b/src/target/riscv/batch.c index b68d38ea14..1b8c98c740 100644 --- a/src/target/riscv/batch.c +++ b/src/target/riscv/batch.c @@ -110,6 +110,8 @@ int riscv_batch_run(struct riscv_batch *batch) } keep_alive(); + if (openocd_shutdown_by_signal()) + return ERROR_FAIL; if (bscan_tunnel_ir_width != 0) { /* need to right-shift "in" by one bit, because of clock skew between BSCAN TAP and DM TAP */ diff --git a/src/target/riscv/program.c b/src/target/riscv/program.c index 1494845d9f..525c829529 100644 --- a/src/target/riscv/program.c +++ b/src/target/riscv/program.c @@ -45,6 +45,8 @@ int riscv_program_write(struct riscv_program *program) int riscv_program_exec(struct riscv_program *p, struct target *t) { keep_alive(); + if (openocd_shutdown_by_signal()) + return ERROR_FAIL; riscv_reg_t saved_registers[GDB_REGNO_XPR31 + 1]; for (size_t i = GDB_REGNO_ZERO + 1; i <= GDB_REGNO_XPR31; ++i) { diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index 8a837952fd..aedfbe3ea9 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -591,6 +591,8 @@ static int dmi_op_timeout(struct target *target, uint32_t *data_in, } keep_alive(); + if (openocd_shutdown_by_signal()) + return ERROR_FAIL; time_t start = time(NULL); /* This first loop performs the request. Note that if for some reason this @@ -3193,6 +3195,9 @@ static int read_memory_bus_v1(struct target *target, target_addr_t address, return ERROR_FAIL; } keep_alive(); + if (openocd_shutdown_by_signal()) + return ERROR_FAIL; + dmi_status_t status = dmi_scan(target, NULL, &sbvalue[next_read_j], DMI_OP_READ, sbdata[j], 0, false); /* By reading from sbdata0, we have just initiated another system bus read. diff --git a/src/target/riscv/riscv.c b/src/target/riscv/riscv.c index b2ab3e3b43..7ef47b99a6 100644 --- a/src/target/riscv/riscv.c +++ b/src/target/riscv/riscv.c @@ -5140,6 +5140,8 @@ static int riscv_set_or_write_register(struct target *target, assert(r->set_register); keep_alive(); + if (openocd_shutdown_by_signal()) + return ERROR_FAIL; if (regid == GDB_REGNO_PC) { return riscv_set_or_write_register(target, GDB_REGNO_DPC, value, write_through); @@ -5237,6 +5239,8 @@ int riscv_get_register(struct target *target, riscv_reg_t *value, assert(r->get_register); keep_alive(); + if (openocd_shutdown_by_signal()) + return ERROR_FAIL; if (regid == GDB_REGNO_PC) { return riscv_get_register(target, value, GDB_REGNO_DPC); diff --git a/src/target/target.c b/src/target/target.c index 167b1ea5d3..02c20d72d9 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -1095,6 +1095,10 @@ int target_run_flash_async_algorithm(struct target *target, /* Avoid GDB timeouts */ keep_alive(); + if (openocd_shutdown_by_signal()) { + retval = ERROR_FAIL; + break; + } } if (retval != ERROR_OK) { @@ -1244,6 +1248,10 @@ int target_run_read_async_algorithm(struct target *target, /* Avoid GDB timeouts */ keep_alive(); + if (openocd_shutdown_by_signal()) { + retval = ERROR_FAIL; + break; + } } @@ -1879,6 +1887,8 @@ static int target_call_timer_callbacks_check_time(int checktime) callback_processing = true; keep_alive(); + if (openocd_shutdown_by_signal()) + return ERROR_FAIL; int64_t now = timeval_ms(); @@ -3250,8 +3260,11 @@ int target_wait_state(struct target *target, enum target_state state, unsigned i nvp_value2name(nvp_target_state, state)->name); } - if (cur-then > 500) + if (cur - then > 500) { keep_alive(); + if (openocd_shutdown_by_signal()) + return ERROR_FAIL; + } if ((cur-then) > ms) { LOG_ERROR("timed out while waiting for target %s", @@ -3534,6 +3547,10 @@ static int target_fill_mem(struct target *target, break; /* avoid GDB timeouts */ keep_alive(); + if (openocd_shutdown_by_signal()) { + retval = ERROR_FAIL; + break; + } } free(target_buf); @@ -3876,6 +3893,12 @@ static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode ver } } keep_alive(); + if (openocd_shutdown_by_signal()) { + retval = ERROR_FAIL; + free(data); + free(buffer); + goto done; + } } } free(data);