server/gdb: fix gdb remote monitor cmd on multi-target

Commit 5ebb1bdea1 ("server/gdb: fix return of gdb remote monitor
command") replaces the call to command_run_line() with call to
Jim_EvalObj() but does not properly set the "context".
In multi-target environment, his can cause the erroneously
execution of the command on the wrong target.

Copy from the code in command_run_line() the proper setup before
executing Jim_EvalObj().

Change-Id: I56738c80779082ca146a06c01bc30e28bc835fd3
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reported-by: Bohdan Tymkiv <bohdan200@gmail.com>
Fixes: 5ebb1bdea1 ("server/gdb: fix return of gdb remote monitor command")
Reviewed-on: https://review.openocd.org/c/openocd/+/6966
Tested-by: jenkins
Reviewed-by: Bohdan Tymkiv <bohdan200@gmail.com>
Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
Reviewed-by: Tim Newsome <tim@sifive.com>
This commit is contained in:
Antonio Borneo 2022-05-04 16:17:08 +02:00
parent b4f8d99c8d
commit f88a7dde6a
1 changed files with 17 additions and 5 deletions

View File

@ -2750,6 +2750,7 @@ static int gdb_query_packet(struct connection *connection,
if (strncmp(packet, "qRcmd,", 6) == 0) {
if (packet_size > 6) {
Jim_Interp *interp = cmd_ctx->interp;
char *cmd;
cmd = malloc((packet_size - 6) / 2 + 1);
size_t len = unhexify((uint8_t *)cmd, packet + 6, (packet_size - 6) / 2);
@ -2761,20 +2762,31 @@ static int gdb_query_packet(struct connection *connection,
/* some commands need to know the GDB connection, make note of current
* GDB connection. */
current_gdb_connection = gdb_connection;
struct target *saved_target_override = cmd_ctx->current_target_override;
cmd_ctx->current_target_override = target;
int retval = Jim_EvalObj(cmd_ctx->interp, Jim_NewStringObj(cmd_ctx->interp, cmd, -1));
struct target *saved_target_override = cmd_ctx->current_target_override;
cmd_ctx->current_target_override = NULL;
struct command_context *old_context = Jim_GetAssocData(interp, "context");
Jim_DeleteAssocData(interp, "context");
int retval = Jim_SetAssocData(interp, "context", NULL, cmd_ctx);
if (retval == JIM_OK) {
retval = Jim_EvalObj(interp, Jim_NewStringObj(interp, cmd, -1));
Jim_DeleteAssocData(interp, "context");
}
int inner_retval = Jim_SetAssocData(interp, "context", NULL, old_context);
if (retval == JIM_OK)
retval = inner_retval;
cmd_ctx->current_target_override = saved_target_override;
current_gdb_connection = NULL;
target_call_timer_callbacks_now();
gdb_connection->output_flag = GDB_OUTPUT_NO;
free(cmd);
if (retval == JIM_RETURN)
retval = cmd_ctx->interp->returnCode;
retval = interp->returnCode;
int lenmsg;
const char *cretmsg = Jim_GetString(Jim_GetResult(cmd_ctx->interp), &lenmsg);
const char *cretmsg = Jim_GetString(Jim_GetResult(interp), &lenmsg);
char *retmsg;
if (lenmsg && cretmsg[lenmsg - 1] != '\n') {
retmsg = alloc_printf("%s\n", cretmsg);