Files
openocd/testing/tcl_commands/utils.tcl
Antonio Borneo 93f16eed4d command: fix OpenOCD commands return value for next jimtcl
JimTcl has been so far quite comfortable with new commands that
return error codes not supported by JimTcl itself.
This has been exploited by OpenOCD, allowing the OpenOCD commands
to return OpenOCD error codes mixed with JimTcl error code.

With the change [1] merged in JimTcl branch 'master' for 0.84, any
negative value returned by a command gets interpreted as a syntax
error detected at runtime by the command itself; JimTcl dumps the
correct syntax and returns a valid JimTcl error code that replaces
the negative value.
Since all OpenOCD error codes are negative values, they are all
taken as syntax errors by the new JimTcl. E.g.:
	openocd -c exit
dumps
	wrong # args: should be "exit ..."

Actually OpenOCD does not need the OpenOCD error code from the
commands, with the exception of the codes:
[a] ERROR_COMMAND_SYNTAX_ERROR, used internally by the command
    dispatcher, before returning to JimTcl;
[b] ERROR_COMMAND_CLOSE_CONNECTION, to alert the telnet server
    that the current connection should be closed.

With [a] already used internally, only [b] needs to be propagated
through JimTcl and back to the OpenOCD caller.

Map the OpenOCD error code ERROR_COMMAND_CLOSE_CONNECTION to the
existing JimTcl error code JIM_EXIT, originally used only by
JimTcl 'exit' command.

Detect JIM_EXIT in command_run_line() and return to the caller the
original ERROR_COMMAND_CLOSE_CONNECTION.

Let exec_command(), and also its caller jim_command_dispatch(),
to only return JimTcl error codes. Rename it to report the change.

Modify the test suite as now a syntax error does not returns -601
anymore.

While there, drop the association key "retval" as it's not used.

Note: after this change there is no real need to replace the
JimTcl command 'exit' with the OpenOCD version as both produce the
same result. But I prefer keeping the code as is to mask any
future change in the related JimTcl code.

Link: https://github.com/msteveb/jimtcl/commit/5669e84aad22 [1]
Change-Id: Ibd7aaeccdf4d7c9efe72aa71909aef83be5ecd27
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reported-by: Andrzej Sierżęga <asier70@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/9084
Tested-by: jenkins
2025-08-24 11:15:31 +00:00

81 lines
1.8 KiB
Tcl

# SPDX-License-Identifier: GPL-2.0-or-later
namespace eval testing_helpers {
proc test_failure message {
echo $message
shutdown error
}
proc check_for_error {expctd_code msg_ptrn script} {
set code [catch {uplevel $script} msg]
set expanded_script [uplevel subst \"$script\"]
if {!$code} {
test_failure \
"'$expanded_script' finished successfully. \
Was expecting an error."
}
if {$expctd_code ne "" && $code != $expctd_code} {
test_failure \
"'$expanded_script' returned unexpected error code $code. \
Was expecting $expctd_code. Error message: '$msg'"
}
if {$msg_ptrn ne "" && ![regexp -- $msg_ptrn $msg]} {
test_failure \
"'$expanded_script' returned unexpected error message '$msg'. \
Was expecting '$msg_ptrn'. Error code: $code"
}
}
proc check_error_matches {pattern script} {
tailcall check_for_error {} $pattern $script
}
proc check_syntax_err script {
tailcall check_for_error 1 {} $script
}
proc check_matches {pattern script} {
set result [uplevel $script]
if {[regexp $pattern $result]} {return}
test_failure \
"'$script' produced unexpected result '$result'. \
Was expecting '$pattern'."
}
namespace export check_error_matches check_syntax_err check_matches
}
namespace eval configure_testing {
variable target_idx 0
proc unique_tgt_name {} {
variable target_idx
incr target_idx
return test_target$target_idx
}
proc target_create_first_args {} {
return "target create [unique_tgt_name] testee"
}
proc simple_configure_options {} {
return {
-work-area-virt 0
-work-area-phys 0
-work-area-size 1
-work-area-backup 0
-endian little
-coreid 1
-chain-position tap.cpu
-dbgbase 0
-rtos hwthread
-gdb-port 0
-gdb-max-connections 1
}
}
namespace export target_create_first_args simple_configure_options
}