This is an automated email from Gerrit.

"Antonio Borneo <borneo.anto...@gmail.com>" just uploaded a new patch set to 
Gerrit, which you can find at https://review.openocd.org/c/openocd/+/8830

-- gerrit

commit 5f142fa001e0ed349f75ca0658e6cc54840a23e5
Author: Antonio Borneo <borneo.anto...@gmail.com>
Date:   Sun Dec 3 22:54:51 2023 +0100

    rtos: rework rtos_create()
    
    To simplify the caller of rtos_create(), convert the code from
    jimtcl oriented to OpenOCD commands.
    
    While there, fix inconsistencies in almost every rtos create()
    method and reset rtos_auto_detect to better cooperate on run-time
    rtos configuration.
    
    Change-Id: I59c443aaed77a48174facdfc86db75d6b28c8480
    Signed-off-by: Antonio Borneo <borneo.anto...@gmail.com>

diff --git a/src/rtos/chibios.c b/src/rtos/chibios.c
index af590c2cb1..0cd99e4720 100644
--- a/src/rtos/chibios.c
+++ b/src/rtos/chibios.c
@@ -515,10 +515,10 @@ static int chibios_create(struct target *target)
        for (unsigned int i = 0; i < ARRAY_SIZE(chibios_params_list); i++)
                if (strcmp(chibios_params_list[i].target_name, 
target_type_name(target)) == 0) {
                        target->rtos->rtos_specific_params = (void 
*)&chibios_params_list[i];
-                       return 0;
+                       return ERROR_OK;
                }
 
        LOG_WARNING("Could not find target \"%s\" in ChibiOS compatibility "
                                "list", target_type_name(target));
-       return -1;
+       return ERROR_FAIL;
 }
diff --git a/src/rtos/ecos.c b/src/rtos/ecos.c
index 7048b006e0..a70084b4fb 100644
--- a/src/rtos/ecos.c
+++ b/src/rtos/ecos.c
@@ -1213,12 +1213,12 @@ static int ecos_create(struct target *target)
                                target->rtos->gdb_thread_packet = 
ecos_packet_hook;
                                /* We do not currently use the 
target->rtos->gdb_target_for_threadid
                                 * hook. */
-                               return 0;
+                               return ERROR_OK;
                        }
                        tnames++;
                }
        }
 
        LOG_ERROR("Could not find target in eCos compatibility list");
-       return -1;
+       return ERROR_FAIL;
 }
diff --git a/src/rtos/embkernel.c b/src/rtos/embkernel.c
index 7e6de7902a..7ad937be59 100644
--- a/src/rtos/embkernel.c
+++ b/src/rtos/embkernel.c
@@ -115,11 +115,11 @@ static int embkernel_create(struct target *target)
        if (i >= ARRAY_SIZE(embkernel_params_list)) {
                LOG_WARNING("Could not find target \"%s\" in embKernel 
compatibility "
                                "list", target_type_name(target));
-               return -1;
+               return ERROR_FAIL;
        }
 
        target->rtos->rtos_specific_params = (void *) &embkernel_params_list[i];
-       return 0;
+       return ERROR_OK;
 }
 
 static int embkernel_get_tasks_details(struct rtos *rtos, int64_t iterable, 
const struct embkernel_params *param,
diff --git a/src/rtos/freertos.c b/src/rtos/freertos.c
index 20977e07e9..bd7b4220e3 100644
--- a/src/rtos/freertos.c
+++ b/src/rtos/freertos.c
@@ -534,9 +534,9 @@ static int freertos_create(struct target *target)
        for (unsigned int i = 0; i < ARRAY_SIZE(freertos_params_list); i++)
                if (strcmp(freertos_params_list[i].target_name, 
target_type_name(target)) == 0) {
                        target->rtos->rtos_specific_params = (void 
*)&freertos_params_list[i];
-                       return 0;
+                       return ERROR_OK;
                }
 
        LOG_ERROR("Could not find target in FreeRTOS compatibility list");
-       return -1;
+       return ERROR_FAIL;
 }
diff --git a/src/rtos/hwthread.c b/src/rtos/hwthread.c
index 6332bd8ad0..76571c6b60 100644
--- a/src/rtos/hwthread.c
+++ b/src/rtos/hwthread.c
@@ -412,7 +412,7 @@ static int hwthread_create(struct target *target)
        target->rtos->thread_details = NULL;
        target->rtos->gdb_target_for_threadid = hwthread_target_for_threadid;
        target->rtos->gdb_thread_packet = hwthread_thread_packet;
-       return 0;
+       return ERROR_OK;
 }
 
 static int hwthread_read_buffer(struct rtos *rtos, target_addr_t address,
diff --git a/src/rtos/linux.c b/src/rtos/linux.c
index 5efdc9f609..4ca02605ed 100644
--- a/src/rtos/linux.c
+++ b/src/rtos/linux.c
@@ -1424,7 +1424,7 @@ static int linux_os_create(struct target *target)
        /*  initialize a default virt 2 phys translation */
        os_linux->phys_mask = ~0xc0000000;
        os_linux->phys_base = 0x0;
-       return JIM_OK;
+       return ERROR_OK;
 }
 
 static char *linux_ps_command(struct target *target)
diff --git a/src/rtos/mqx.c b/src/rtos/mqx.c
index 017fd2b01c..dbb48952f2 100644
--- a/src/rtos/mqx.c
+++ b/src/rtos/mqx.c
@@ -251,11 +251,11 @@ static int mqx_create(
                if (strcmp(mqx_params_list[i].target_name, 
target_type_name(target)) == 0) {
                        target->rtos->rtos_specific_params = (void 
*)&mqx_params_list[i];
                        /* LOG_DEBUG("MQX RTOS - valid architecture: %s", 
target_type_name(target)); */
-                       return 0;
+                       return ERROR_OK;
                }
        }
        LOG_ERROR("MQX RTOS - could not find target \"%s\" in MQX compatibility 
list", target_type_name(target));
-       return -1;
+       return ERROR_FAIL;
 }
 
 /*
diff --git a/src/rtos/nuttx.c b/src/rtos/nuttx.c
index 821e550885..27bf086c83 100644
--- a/src/rtos/nuttx.c
+++ b/src/rtos/nuttx.c
@@ -129,13 +129,13 @@ static int nuttx_create(struct target *target)
 
        if (i >= ARRAY_SIZE(nuttx_params_list)) {
                LOG_ERROR("Could not find \"%s\" target in NuttX compatibility 
list", target_type_name(target));
-               return JIM_ERR;
+               return ERROR_FAIL;
        }
 
        /* We found a target in our list, copy its reference. */
        target->rtos->rtos_specific_params = (void *)param;
 
-       return JIM_OK;
+       return ERROR_OK;
 }
 
 static int nuttx_smp_init(struct target *target)
diff --git a/src/rtos/rtkernel.c b/src/rtos/rtkernel.c
index aebbf3d94d..2be1996fcd 100644
--- a/src/rtos/rtkernel.c
+++ b/src/rtos/rtkernel.c
@@ -364,12 +364,12 @@ static int rtkernel_create(struct target *target)
        for (size_t i = 0; i < ARRAY_SIZE(rtkernel_params_list); i++) {
                if (strcmp(rtkernel_params_list[i].target_name, 
target_type_name(target)) == 0) {
                        target->rtos->rtos_specific_params = (void 
*)&rtkernel_params_list[i];
-                       return 0;
+                       return ERROR_OK;
                }
        }
 
        LOG_ERROR("Could not find target in rt-kernel compatibility list");
-       return -1;
+       return ERROR_FAIL;
 }
 
 const struct rtos_type rtkernel_rtos = {
diff --git a/src/rtos/rtos.c b/src/rtos/rtos.c
index 2c563d522b..5cac316ed3 100644
--- a/src/rtos/rtos.c
+++ b/src/rtos/rtos.c
@@ -57,7 +57,7 @@ static int os_alloc(struct target *target, const struct 
rtos_type *ostype)
        struct rtos *os = target->rtos = calloc(1, sizeof(struct rtos));
 
        if (!os)
-               return JIM_ERR;
+               return ERROR_FAIL;
 
        os->type = ostype;
        os->current_threadid = -1;
@@ -69,7 +69,7 @@ static int os_alloc(struct target *target, const struct 
rtos_type *ostype)
        os->gdb_thread_packet = rtos_thread_packet;
        os->gdb_target_for_threadid = rtos_target_for_threadid;
 
-       return JIM_OK;
+       return ERROR_OK;
 }
 
 static void os_free(struct target *target)
@@ -86,38 +86,26 @@ static void os_free(struct target *target)
 static int os_alloc_create(struct target *target, const struct rtos_type 
*ostype)
 {
        int ret = os_alloc(target, ostype);
+       if (ret != ERROR_OK)
+               return ret;
 
-       if (ret == JIM_OK) {
-               ret = target->rtos->type->create(target);
-               if (ret != JIM_OK)
-                       os_free(target);
-       }
+       ret = target->rtos->type->create(target);
+       if (ret != ERROR_OK)
+               os_free(target);
 
        return ret;
 }
 
-int rtos_create(struct jim_getopt_info *goi, struct target *target)
+int rtos_create(struct command_invocation *cmd, struct target *target,
+               const char *rtos_name)
 {
-       int x;
-       const char *cp;
-       Jim_Obj *res;
-       int e;
-
-       if (!goi->is_configure && goi->argc != 0) {
-               Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO 
PARAMS");
-               return JIM_ERR;
-       }
-
        os_free(target);
+       target->rtos_auto_detect = false;
 
-       e = jim_getopt_string(goi, &cp, NULL);
-       if (e != JIM_OK)
-               return e;
-
-       if (strcmp(cp, "none") == 0)
-               return JIM_OK;
+       if (strcmp(rtos_name, "none") == 0)
+               return ERROR_OK;
 
-       if (strcmp(cp, "auto") == 0) {
+       if (strcmp(rtos_name, "auto") == 0) {
                /* Auto detect tries to look up all symbols for each RTOS,
                 * and runs the RTOS driver's _detect() function when GDB
                 * finds all symbols for any RTOS. See rtos_qsymbol(). */
@@ -128,17 +116,29 @@ int rtos_create(struct jim_getopt_info *goi, struct 
target *target)
                return os_alloc(target, rtos_types[0]);
        }
 
-       for (x = 0; rtos_types[x]; x++)
-               if (strcmp(cp, rtos_types[x]->name) == 0)
+       for (int x = 0; rtos_types[x]; x++)
+               if (strcmp(rtos_name, rtos_types[x]->name) == 0)
                        return os_alloc_create(target, rtos_types[x]);
 
-       Jim_SetResultFormatted(goi->interp, "Unknown RTOS type %s, try one of: 
", cp);
-       res = Jim_GetResult(goi->interp);
-       for (x = 0; rtos_types[x]; x++)
-               Jim_AppendStrings(goi->interp, res, rtos_types[x]->name, ", ", 
NULL);
-       Jim_AppendStrings(goi->interp, res, ", auto or none", NULL);
+       char *all = NULL;
+       for (int x = 0; rtos_types[x]; x++) {
+               char *prev = all;
+               if (all)
+                       all = alloc_printf("%s, %s", all, rtos_types[x]->name);
+               else
+                       all = alloc_printf("%s", rtos_types[x]->name);
+               free(prev);
+               if (!all) {
+                       LOG_ERROR("Out of memory");
+                       return ERROR_FAIL;
+               }
+       }
+
+       command_print(cmd, "Unknown RTOS type %s, try one of: %s, auto or none",
+               rtos_name, all);
+       free(all);
 
-       return JIM_ERR;
+       return ERROR_COMMAND_ARGUMENT_INVALID;
 }
 
 void rtos_destroy(struct target *target)
diff --git a/src/rtos/rtos.h b/src/rtos/rtos.h
index 5ba8b26945..05beab1459 100644
--- a/src/rtos/rtos.h
+++ b/src/rtos/rtos.h
@@ -10,7 +10,6 @@
 
 #include "server/server.h"
 #include "target/target.h"
-#include <helper/jim-nvp.h>
 
 typedef int64_t threadid_t;
 typedef int64_t symbol_address_t;
@@ -113,7 +112,8 @@ struct rtos_register_stacking {
 
 #define GDB_THREAD_PACKET_NOT_CONSUMED (-40)
 
-int rtos_create(struct jim_getopt_info *goi, struct target *target);
+int rtos_create(struct command_invocation *cmd, struct target *target,
+               const char *rtos_name);
 void rtos_destroy(struct target *target);
 int rtos_set_reg(struct connection *connection, int reg_num,
                uint8_t *reg_value);
diff --git a/src/rtos/threadx.c b/src/rtos/threadx.c
index 61c49264e8..620f43cc8e 100644
--- a/src/rtos/threadx.c
+++ b/src/rtos/threadx.c
@@ -611,9 +611,9 @@ static int threadx_create(struct target *target)
                        target->rtos->rtos_specific_params = (void 
*)&threadx_params_list[i];
                        target->rtos->current_thread = 0;
                        target->rtos->thread_details = NULL;
-                       return 0;
+                       return ERROR_OK;
                }
 
        LOG_ERROR("Could not find target in ThreadX compatibility list");
-       return -1;
+       return ERROR_FAIL;
 }
diff --git a/src/target/target.c b/src/target/target.c
index 99481622db..ea8f000c29 100644
--- a/src/target/target.c
+++ b/src/target/target.c
@@ -5176,17 +5176,10 @@ static COMMAND_HELPER(target_configure, struct target 
*target, unsigned int inde
                                        command_print(CMD, "missing argument to 
%s", CMD_ARGV[index - 1]);
                                        return ERROR_COMMAND_ARGUMENT_INVALID;
                                }
-                               struct jim_getopt_info goi;
-                               jim_getopt_setup(&goi, CMD_CTX->interp, 
CMD_ARGC - index, CMD_JIMTCL_ARGV + index);
+                               retval = rtos_create(CMD, target, 
CMD_ARGV[index]);
+                               if (retval != ERROR_OK)
+                                       return retval;
                                index++;
-                               goi.is_configure = true;
-                               int resval = rtos_create(&goi, target);
-                               int reslen;
-                               const char *result = 
Jim_GetString(Jim_GetResult(CMD_CTX->interp), &reslen);
-                               if (reslen > 0)
-                                       command_print(CMD, "%s", result);
-                               if (resval != JIM_OK)
-                                       return ERROR_FAIL;
                        } else {
                                if (index != CMD_ARGC)
                                        return ERROR_COMMAND_SYNTAX_ERROR;

-- 

Reply via email to