target: fix error on TCL command "return" in target event handler

The TCL command "return" always returns error code JIM_RETURN, to
indicate that the effective error code and message are elsewhere.

In the current implementation, the caller of target's event only
checks for return code JIM_OK and considers any other value,
including JIM_RETURN, as an error condition, thus dumping the
call-trace. The execution is not stopped because the error is not
further propagated, but the error message is annoying and
misleading.

It can be tested running
	openocd -f ./test.cfg
using the following script "test.cfg". You can replace the board
file in line 1, to use a board available in your lab:
  1	source [find board/st_nucleo_f4.cfg]
  2	[target current] configure -event reset-start {}
  3	[target current] configure -event reset-end {return}
  4	init
  5	proc a {} {[target current] invoke-event reset-start}
  6	proc b {} {[target current] invoke-event reset-end}
  7	proc c {} {a;b;echo "arrived at the end"}
  8	c
  9	shutdown
The execution produces:
	./test.cfg:7: Error:
	in procedure 'c' called at file "./test.cfg", line 8
	in procedure 'b' called at file "./test.cfg", line 7

	arrived at the end
that shows the call-trace but does not halt the execution.

The developer can avoid using the "return" command in the event
body by defining a TCL procedure that implements the handler and
that contains the "return" command, reducing the handler body to
a simple call to the procedure above. But this approach is either
not documented nor always intuitive while writing the handler,
causing waste of time to look for the false error.

Modify target_handle_event() to detect the specific return value
of the "return" command and to test the real error code that is,
eventually, specified to the TCL "return" command.

Change-Id: I2b860bab7233c6ed13ee4098e348d7533e1c4626
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: http://openocd.zylin.com/4974
Tested-by: jenkins
Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
bscan_optimization
Antonio Borneo 2019-02-26 09:57:14 +01:00 committed by Tomas Vanek
parent e6990bdd00
commit 1af83682e0
1 changed files with 6 additions and 1 deletions

View File

@ -4526,6 +4526,7 @@ static int target_array2mem(Jim_Interp *interp, struct target *target,
void target_handle_event(struct target *target, enum target_event e) void target_handle_event(struct target *target, enum target_event e)
{ {
struct target_event_action *teap; struct target_event_action *teap;
int retval;
for (teap = target->event_action; teap != NULL; teap = teap->next) { for (teap = target->event_action; teap != NULL; teap = teap->next) {
if (teap->event == e) { if (teap->event == e) {
@ -4544,8 +4545,12 @@ void target_handle_event(struct target *target, enum target_event e)
struct command_context *cmd_ctx = current_command_context(teap->interp); struct command_context *cmd_ctx = current_command_context(teap->interp);
struct target *saved_target_override = cmd_ctx->current_target_override; struct target *saved_target_override = cmd_ctx->current_target_override;
cmd_ctx->current_target_override = target; cmd_ctx->current_target_override = target;
retval = Jim_EvalObj(teap->interp, teap->body);
if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK) { if (retval == JIM_RETURN)
retval = teap->interp->returnCode;
if (retval != JIM_OK) {
Jim_MakeErrorMessage(teap->interp); Jim_MakeErrorMessage(teap->interp);
LOG_USER("Error executing event %s on target %s:\n%s", LOG_USER("Error executing event %s on target %s:\n%s",
Jim_Nvp_value2name_simple(nvp_target_event, e)->name, Jim_Nvp_value2name_simple(nvp_target_event, e)->name,