command: Prepend logs during command capture 02/7902/3
authorMarek Vrbka <marek.vrbka@codasip.com>
Mon, 18 Sep 2023 12:32:44 +0000 (14:32 +0200)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sat, 14 Oct 2023 12:00:16 +0000 (12:00 +0000)
Previously, if you ran a tcl command in capture like so:
"capture { reg 0x1000 hw }"
Such command did overwrite the tcl result if LOG_LVL_INFO or
lower was logged during it.

This patch changes it by prepending the log to the tcl result instead.
As the tcl results should not be lost during capture.

Change-Id: I37381b45e15c931ba2844d65c9d38f6ed2f6e4fd
Signed-off-by: Marek Vrbka <marek.vrbka@codasip.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/7902
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
Tested-by: jenkins
Reviewed-by: Jan Matyas <jan.matyas@codasip.com>
doc/openocd.texi
src/helper/command.c

index 2d59238b804726aaab87492d34c001dd49bad5e0..6ec280ad237a8095984c7b50c2b3092fbb257fed 100644 (file)
@@ -12475,7 +12475,7 @@ Return information about the flash banks
 @item @b{capture} <@var{command}>
 
 Run <@var{command}> and return full log output that was produced during
-its execution. Example:
+its execution together with the command output. Example:
 
 @example
 > capture "reset init"
index 945b890b3a41178514c85c419c02e94749d8dcc0..ef50ab5bd0efcb2382298e7561e49e94f229f994 100644 (file)
@@ -99,8 +99,7 @@ static struct log_capture_state *command_log_capture_start(Jim_Interp *interp)
  * The tcl return value is empty for openocd commands that provide
  * progress output.
  *
- * Therefore we set the tcl return value only if we actually
- * captured output.
+ * For other commands, we prepend the logs to the tcl return value.
  */
 static void command_log_capture_finish(struct log_capture_state *state)
 {
@@ -109,15 +108,18 @@ static void command_log_capture_finish(struct log_capture_state *state)
 
        log_remove_callback(tcl_output, state);
 
-       int length;
-       Jim_GetString(state->output, &length);
+       int loglen;
+       const char *log_result = Jim_GetString(state->output, &loglen);
+       int reslen;
+       const char *cmd_result = Jim_GetString(Jim_GetResult(state->interp), &reslen);
 
-       if (length > 0)
-               Jim_SetResult(state->interp, state->output);
-       else {
-               /* No output captured, use tcl return value (which could
-                * be empty too). */
-       }
+       // Just in case the log doesn't end with a newline, we add it
+       if (loglen != 0 && reslen != 0 && log_result[loglen - 1] != '\n')
+               Jim_AppendString(state->interp, state->output, "\n", 1);
+
+       Jim_AppendString(state->interp, state->output, cmd_result, reslen);
+
+       Jim_SetResult(state->interp, state->output);
        Jim_DecrRefCount(state->interp, state->output);
 
        free(state);
@@ -691,8 +693,8 @@ COMMAND_HANDLER(handle_echo)
        return ERROR_OK;
 }
 
-/* Capture progress output and return as tcl return value. If the
- * progress output was empty, return tcl return value.
+/* Return both the progress output (LOG_INFO and higher)
+ * and the tcl return value of a command.
  */
 static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 {

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)