Skip to content

Commit 501c3ea

Browse files
[backport] target/riscv/riscv.c: support auto-selection of software breakpoint size
Link: riscv-collab/riscv-openocd#1288 Add auto-selection of breakpoint size for RISC-V targets. The new length depends on C extension. If the C extension is used, the auto-selected length is 2 bytes. Otherwise, the default length is 4 bytes. Change-Id: I0cf4e2a06b202b61048322ee61a06101cb55a23a Signed-off-by: Kulyatskaya Alexandra <[email protected]>
1 parent 9b6f4e8 commit 501c3ea

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

src/target/riscv/riscv.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,13 @@ int riscv_read_by_any_size(struct target *target, target_addr_t address, uint32_
15951595
return ERROR_FAIL;
15961596
}
15971597

1598+
static int riscv_get_default_breakpoint_length(struct target *target, target_addr_t addr,
1599+
uint32_t asid, int hw, unsigned int *length)
1600+
{
1601+
*length = riscv_supports_extension(target, 'c') ? 2 : 4;
1602+
return ERROR_OK;
1603+
}
1604+
15981605
int riscv_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
15991606
{
16001607
LOG_TARGET_DEBUG(target, "@0x%" TARGET_PRIxADDR, breakpoint->address);
@@ -5889,6 +5896,7 @@ struct target_type riscv_target = {
58895896
.get_gdb_reg_list = riscv_get_gdb_reg_list,
58905897
.get_gdb_reg_list_noread = riscv_get_gdb_reg_list_noread,
58915898

5899+
.get_default_breakpoint_length = riscv_get_default_breakpoint_length,
58925900
.add_breakpoint = riscv_add_breakpoint,
58935901
.remove_breakpoint = riscv_remove_breakpoint,
58945902

src/target/target.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3992,19 +3992,19 @@ static int handle_bp_command_set(struct command_invocation *cmd,
39923992
static COMMAND_HELPER(parse_bp_length, uint32_t asid, target_addr_t addr,
39933993
int hw, unsigned int *length)
39943994
{
3995-
if (strcmp(CMD_ARGV[1], "default") == 0) {
3996-
struct target *target = get_current_target(CMD_CTX);
3997-
int ret_errno = target_get_default_breakpoint_size(target, addr, asid, hw, length);
3998-
if (ret_errno == ERROR_NOT_IMPLEMENTED) {
3999-
command_print(CMD, "Default breakpoint size derivation is "
4000-
"not implemented on target %s", target_name(target));
4001-
} else if (ret_errno != ERROR_OK) {
4002-
command_print(CMD, "Unknown error when deriving default breakpoint size");
4003-
}
4004-
return ret_errno;
3995+
if (strcmp(CMD_ARGV[1], "default") != 0) {
3996+
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], *length);
3997+
return ERROR_OK;
40053998
}
4006-
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], *length);
4007-
return ERROR_OK;
3999+
struct target *target = get_current_target(CMD_CTX);
4000+
int ret_errno = target_get_default_breakpoint_size(target, addr, asid, hw, length);
4001+
if (ret_errno == ERROR_NOT_IMPLEMENTED) {
4002+
command_print(CMD, "Default breakpoint size derivation is "
4003+
"not implemented on target %s", target_name(target));
4004+
} else if (ret_errno != ERROR_OK) {
4005+
command_print(CMD, "Unknown error when deriving default breakpoint size");
4006+
}
4007+
return ret_errno;
40084008
}
40094009

40104010
COMMAND_HANDLER(handle_bp_command)
@@ -4013,6 +4013,7 @@ COMMAND_HANDLER(handle_bp_command)
40134013
uint32_t asid;
40144014
uint32_t length;
40154015
int hw = BKPT_SOFT;
4016+
int ret_errno;
40164017

40174018
switch (CMD_ARGC) {
40184019
case 0:
@@ -4021,29 +4022,37 @@ COMMAND_HANDLER(handle_bp_command)
40214022
case 2:
40224023
asid = 0;
40234024
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
4024-
CALL_COMMAND_HANDLER(parse_bp_length, asid, addr, hw, &length);
4025+
ret_errno = CALL_COMMAND_HANDLER(parse_bp_length, asid, addr, hw, &length);
4026+
if (ret_errno != ERROR_OK)
4027+
return ret_errno;
40254028
return handle_bp_command_set(CMD, addr, asid, length, hw);
40264029

40274030
case 3:
40284031
if (strcmp(CMD_ARGV[2], "hw") == 0) {
40294032
hw = BKPT_HARD;
40304033
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
40314034
asid = 0;
4032-
CALL_COMMAND_HANDLER(parse_bp_length, asid, addr, hw, &length);
4035+
ret_errno = CALL_COMMAND_HANDLER(parse_bp_length, asid, addr, hw, &length);
4036+
if (ret_errno != ERROR_OK)
4037+
return ret_errno;
40334038
return handle_bp_command_set(CMD, addr, asid, length, hw);
40344039
} else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
40354040
hw = BKPT_HARD;
40364041
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
40374042
addr = 0;
4038-
CALL_COMMAND_HANDLER(parse_bp_length, asid, addr, hw, &length);
4043+
ret_errno = CALL_COMMAND_HANDLER(parse_bp_length, asid, addr, hw, &length);
4044+
if (ret_errno != ERROR_OK)
4045+
return ret_errno;
40394046
return handle_bp_command_set(CMD, addr, asid, length, hw);
40404047
}
40414048
/* fallthrough */
40424049
case 4:
40434050
hw = BKPT_HARD;
40444051
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
40454052
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
4046-
CALL_COMMAND_HANDLER(parse_bp_length, asid, addr, hw, &length);
4053+
ret_errno = CALL_COMMAND_HANDLER(parse_bp_length, asid, addr, hw, &length);
4054+
if (ret_errno != ERROR_OK)
4055+
return ret_errno;
40474056
return handle_bp_command_set(CMD, addr, asid, length, hw);
40484057

40494058
default:

0 commit comments

Comments
 (0)