Skip to content

Commit

Permalink
target/espressif: register event callback
Browse files Browse the repository at this point in the history
  • Loading branch information
erhankur committed Jul 4, 2024
1 parent f1019de commit 08a7fb0
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 153 deletions.
4 changes: 0 additions & 4 deletions doc/openocd.texi
Original file line number Diff line number Diff line change
Expand Up @@ -11835,10 +11835,6 @@ which uses TDI pin for selecting SPI flash voltage.
@end itemize
@end deffn

@deffn {Command} {esp gdb_detach_handler}
Handles @code{gdb-detach} event and makes necessary cleanups. Currently, this is required to remove flash breakpoints when gdb disconnects unexpectedly.
@end deffn

@anchor{softwaredebugmessagesandtracing}
@section Software Debug Messages and Tracing
@cindex Linux-ARM DCC support
Expand Down
51 changes: 40 additions & 11 deletions src/target/espressif/esp.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define ESP_FLASH_BREAKPOINTS_MAX_NUM 32
#define ESP_ASSIST_DEBUG_INVALID_VALUE 0xFFFFFFFF

static int esp_callback_event_handler(struct target *target, enum target_event event, void *priv);

struct esp_common *target_to_esp_common(struct target *target)
{
struct xtensa *xtensa = target->arch_info;
Expand All @@ -31,7 +33,7 @@ struct esp_common *target_to_esp_common(struct target *target)
return NULL;
}

int esp_common_init(struct esp_common *esp,
int esp_common_init(struct target *target, struct esp_common *esp,
const struct esp_flash_breakpoint_ops *flash_brps_ops,
const struct esp_algorithm_hw *algo_hw)
{
Expand All @@ -41,6 +43,9 @@ int esp_common_init(struct esp_common *esp,
if (!esp->flash_brps.brps)
return ERROR_FAIL;

if (target->coreid == 0)
target_register_event_callback(esp_callback_event_handler, esp);

return ERROR_OK;
}

Expand Down Expand Up @@ -474,12 +479,9 @@ int esp_common_read_pseudo_ex_reason(struct target *target)
}

/* Generic commands for xtensa and riscv */
int esp_common_gdb_detach_command(struct command_invocation *cmd)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;

struct target *target = get_current_target(CMD_CTX);
int esp_common_gdb_detach_handler(struct target *target)
{
if (target->smp) {
struct target_list *head;
foreach_smp_target(head, target->smp_targets) {
Expand All @@ -492,12 +494,8 @@ int esp_common_gdb_detach_command(struct command_invocation *cmd)
return esp_common_handle_gdb_detach(target);
}

int esp_common_process_flash_breakpoints_command(struct command_invocation *cmd)
int esp_common_process_flash_breakpoints_handler(struct target *target)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;

struct target *target = get_current_target(CMD_CTX);
if (target->smp) {
struct target_list *head;
foreach_smp_target(head, target->smp_targets) {
Expand All @@ -509,3 +507,34 @@ int esp_common_process_flash_breakpoints_command(struct command_invocation *cmd)
}
return esp_common_handle_flash_breakpoints(target);
}

int esp_common_process_flash_breakpoints_command(struct command_invocation *cmd)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;

struct target *target = get_current_target(CMD_CTX);

return esp_common_process_flash_breakpoints_handler(target);
}

static int esp_callback_event_handler(struct target *target, enum target_event event, void *priv)
{
switch (event) {
case TARGET_EVENT_STEP_START:
case TARGET_EVENT_RESUME_START:
case TARGET_EVENT_QXFER_THREAD_READ_END:
return esp_common_process_flash_breakpoints_handler(target);
case TARGET_EVENT_GDB_DETACH:
return esp_common_gdb_detach_handler(target);
#if IS_ESPIDF
case TARGET_EVENT_EXAMINE_FAIL:
extern int examine_failed_ui_handler(struct command_invocation *cmd);
return examine_failed_ui_handler;
#endif
default:
break;
}

return ERROR_OK;
}
5 changes: 1 addition & 4 deletions src/target/espressif/esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ struct esp_ops {
};

struct esp_common *target_to_esp_common(struct target *target);
int esp_common_init(struct esp_common *esp,
int esp_common_init(struct target *target, struct esp_common *esp,
const struct esp_flash_breakpoint_ops *flash_brps_ops,
const struct esp_algorithm_hw *algo_hw);
int esp_common_flash_breakpoint_add(struct target *target,
Expand All @@ -149,11 +149,8 @@ int esp_common_flash_breakpoint_remove(struct target *target,
struct breakpoint *breakpoint);
bool esp_common_flash_breakpoint_exists(struct esp_common *esp,
struct breakpoint *breakpoint);
int esp_common_set_flash_breakpoints(struct command_invocation *cmd);
int esp_common_handle_gdb_detach(struct target *target);
int esp_common_gdb_detach_command(struct command_invocation *cmd);
int esp_common_process_flash_breakpoints_command(struct command_invocation *cmd);

int esp_dbgstubs_table_read(struct target *target, struct esp_dbg_stubs *dbg_stubs);

void esp_common_assist_debug_monitor_disable(struct target *target, uint32_t address, uint32_t *value);
Expand Down
3 changes: 1 addition & 2 deletions src/target/espressif/esp32c2.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ static int esp32c2_init_target(struct command_context *cmd_ctx,

struct esp_riscv_common *esp_riscv = target_to_esp_riscv(target);

ret = esp_riscv_init_arch_info(cmd_ctx,
target,
ret = esp_riscv_init_arch_info(target,
esp_riscv,
&esp32c2_flash_brp_ops,
&esp32c2_semihost_ops);
Expand Down
3 changes: 1 addition & 2 deletions src/target/espressif/esp32c3.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ static int esp32c3_init_target(struct command_context *cmd_ctx,

struct esp_riscv_common *esp_riscv = target_to_esp_riscv(target);

ret = esp_riscv_init_arch_info(cmd_ctx,
target,
ret = esp_riscv_init_arch_info(target,
esp_riscv,
&esp32c3_flash_brp_ops,
&esp32c3_semihost_ops);
Expand Down
3 changes: 1 addition & 2 deletions src/target/espressif/esp32c5.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ static int esp32c5_init_target(struct command_context *cmd_ctx,

struct esp_riscv_common *esp_riscv = target_to_esp_riscv(target);

ret = esp_riscv_init_arch_info(cmd_ctx,
target,
ret = esp_riscv_init_arch_info(target,
esp_riscv,
&esp32c5_flash_brp_ops,
&esp32c5_semihost_ops);
Expand Down
3 changes: 1 addition & 2 deletions src/target/espressif/esp32c6.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ static int esp32c6_init_target(struct command_context *cmd_ctx,

struct esp_riscv_common *esp_riscv = target_to_esp_riscv(target);

ret = esp_riscv_init_arch_info(cmd_ctx,
target,
ret = esp_riscv_init_arch_info(target,
esp_riscv,
&esp32c6_flash_brp_ops,
&esp32c6_semihost_ops);
Expand Down
3 changes: 1 addition & 2 deletions src/target/espressif/esp32h2.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ static int esp32h2_init_target(struct command_context *cmd_ctx,

struct esp_riscv_common *esp_riscv = target_to_esp_riscv(target);

ret = esp_riscv_init_arch_info(cmd_ctx,
target,
ret = esp_riscv_init_arch_info(target,
esp_riscv,
&esp32h2_flash_brp_ops,
&esp32h2_semihost_ops);
Expand Down
3 changes: 1 addition & 2 deletions src/target/espressif/esp32p4.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ static int esp32p4_init_target(struct command_context *cmd_ctx,

struct esp_riscv_common *esp_riscv = target_to_esp_riscv(target);

ret = esp_riscv_init_arch_info(cmd_ctx,
target,
ret = esp_riscv_init_arch_info(target,
esp_riscv,
&esp32p4_flash_brp_ops,
&esp32p4_semihost_ops);
Expand Down
19 changes: 0 additions & 19 deletions src/target/espressif/esp_riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
#include "esp_riscv.h"
#include "esp_semihosting.h"

#if IS_ESPIDF
extern int examine_failed_ui_handler(struct command_invocation *cmd);
#endif

/* Argument indexes for ESP_SEMIHOSTING_SYS_BREAKPOINT_SET */
enum {
ESP_RISCV_SET_BREAKPOINT_ARG_SET,
Expand Down Expand Up @@ -941,13 +937,6 @@ COMMAND_HANDLER(esp_riscv_halted_command)
}

const struct command_registration esp_riscv_command_handlers[] = {
{
.name = "gdb_detach_handler",
.handler = esp_common_gdb_detach_command,
.mode = COMMAND_ANY,
.help = "Handles gdb-detach events and makes necessary cleanups such as removing flash breakpoints",
.usage = "",
},
{
.name = "process_lazy_breakpoints",
.handler = esp_common_process_flash_breakpoints_command,
Expand All @@ -962,13 +951,5 @@ const struct command_registration esp_riscv_command_handlers[] = {
.help = "Handles halted event and prints exception reason",
.usage = "",
},
#if IS_ESPIDF
{
.name = "examine_failed_handler",
.handler = examine_failed_ui_handler,
.mode = COMMAND_EXEC,
.usage = "",
},
#endif
COMMAND_REGISTRATION_DONE
};
4 changes: 2 additions & 2 deletions src/target/espressif/esp_riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static inline int esp_riscv_on_reset(struct target *target)
return ERROR_OK;
}

static inline int esp_riscv_init_arch_info(struct command_context *cmd_ctx, struct target *target,
static inline int esp_riscv_init_arch_info(struct target *target,
struct esp_riscv_common *esp_riscv,
const struct esp_flash_breakpoint_ops *flash_brps_ops,
const struct esp_semihost_ops *semi_ops)
Expand All @@ -62,7 +62,7 @@ static inline int esp_riscv_init_arch_info(struct command_context *cmd_ctx, stru

INIT_LIST_HEAD(&esp_riscv->semihost.dir_map_list);

int ret = esp_common_init(&esp_riscv->esp, flash_brps_ops, &riscv_algo_hw);
int ret = esp_common_init(target, &esp_riscv->esp, flash_brps_ops, &riscv_algo_hw);
if (ret != ERROR_OK)
return ret;

Expand Down
21 changes: 1 addition & 20 deletions src/target/espressif/esp_xtensa.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
#include "esp_xtensa_apptrace.h"
#include "esp_xtensa_semihosting.h"

#if IS_ESPIDF
extern int examine_failed_ui_handler(struct command_invocation *cmd);
#endif

#define XTENSA_EXCCAUSE(reg_val) ((reg_val) & 0x3F)

static const char *xtensa_get_exception_reason(struct target *target, enum esp_xtensa_exception_cause exccause_code)
Expand Down Expand Up @@ -172,7 +168,7 @@ int esp_xtensa_init_arch_info(struct target *target,
int ret = xtensa_init_arch_info(target, &esp_xtensa->xtensa, dm_cfg);
if (ret != ERROR_OK)
return ret;
ret = esp_common_init(&esp_xtensa->esp, esp_ops->flash_brps_ops, &xtensa_algo_hw);
ret = esp_common_init(target, &esp_xtensa->esp, esp_ops->flash_brps_ops, &xtensa_algo_hw);
if (ret != ERROR_OK)
return ret;

Expand Down Expand Up @@ -389,27 +385,12 @@ int esp_xtensa_breakpoint_remove(struct target *target, struct breakpoint *break
}

const struct command_registration esp_command_handlers[] = {
{
.name = "gdb_detach_handler",
.handler = esp_common_gdb_detach_command,
.mode = COMMAND_ANY,
.help = "Handles gdb-detach events and makes necessary cleanups such as removing flash breakpoints",
.usage = "",
},
{
.name = "process_lazy_breakpoints",
.handler = esp_common_process_flash_breakpoints_command,
.mode = COMMAND_ANY,
.help = "Handles resum-start and step-start events to set/clear all waiting flash breakpoints",
.usage = "",
},
#if IS_ESPIDF
{
.name = "examine_failed_handler",
.handler = examine_failed_ui_handler,
.mode = COMMAND_ANY,
.usage = "",
},
#endif
COMMAND_REGISTRATION_DONE
};
19 changes: 0 additions & 19 deletions src/target/espressif/esp_xtensa_smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
#include "esp_xtensa_semihosting.h"
#include "esp_algorithm.h"

#if IS_ESPIDF
extern int examine_failed_ui_handler(struct command_invocation *cmd);
#endif

/*
Multiprocessor stuff common:
Expand Down Expand Up @@ -1097,28 +1093,13 @@ const struct command_registration esp_xtensa_smp_xtensa_command_handlers[] = {
};

const struct command_registration esp_xtensa_smp_esp_command_handlers[] = {
{
.name = "gdb_detach_handler",
.handler = esp_common_gdb_detach_command,
.mode = COMMAND_ANY,
.help = "Handles gdb-detach events and makes necessary cleanups such as removing flash breakpoints",
.usage = "",
},
{
.name = "process_lazy_breakpoints",
.handler = esp_common_process_flash_breakpoints_command,
.mode = COMMAND_ANY,
.help = "Handles resum-start and step-start events to set/clear all waiting flash breakpoints",
.usage = "",
},
#if IS_ESPIDF
{
.name = "examine_failed_handler",
.handler = examine_failed_ui_handler,
.mode = COMMAND_ANY,
.usage = "",
},
#endif
COMMAND_REGISTRATION_DONE
};

Expand Down
Loading

0 comments on commit 08a7fb0

Please sign in to comment.