The new 'stack' command displays the openocd C stack from TCL as a
debugging aid.  Takes an optional parameter: the depth of the stack
or 'fault' (which creates an immediate segfault by dereferencing
a bad pointer).  This demonstrates the new segfault handling nicely.

Signed-off-by: Zachary T Welch <z...@superlucidity.net>
---
 src/helper/command.c |   40 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/src/helper/command.c b/src/helper/command.c
index 1cbedae..ca7418b 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -42,6 +42,7 @@
 #include "log.h"
 #include "time_support.h"
 #include "jim-eventloop.h"
+#include "stack.h"
 
 
 /* nice short description of source file */
@@ -1201,6 +1202,36 @@ COMMAND_HANDLER(handle_sleep_command)
        return ERROR_OK;
 }
 
+static int stack_command_walker(struct stack_walk_state *state, intptr_t data)
+{
+       struct command_context *cmd_ctx = (struct command_context *)data;
+       command_print(cmd_ctx, "%d: %s", state->level, state->frame);
+       return 0;
+}
+
+COMMAND_HANDLER(handle_stack_command)
+{
+       if (CMD_ARGC > 1)
+               return ERROR_INVALID_ARGUMENTS;
+
+       unsigned depth = 10;
+       if (CMD_ARGC == 1)
+       {
+               if (strcmp(CMD_ARGV[0], "fault") == 0)
+               {
+                       long *bad_ptr = (void*)-1;
+                       *bad_ptr = 0xDEADBEEF;
+                       // unreachable... we should have just caused a crash!
+                       LOG_ERROR("unable to fake a segmentation fault?!?!");
+               }
+               COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], depth);
+               if (depth > 200)
+                       return ERROR_INVALID_ARGUMENTS;
+       }
+
+       return stack_walk(depth, &stack_command_walker, (intptr_t)CMD_CTX);
+}
+
 static const struct command_registration command_subcommand_handlers[] = {
        {
                .name = "mode",
@@ -1246,6 +1277,15 @@ static const struct command_registration 
command_builtin_handlers[] = {
                .usage = "<n> [busy]",
        },
        {
+               .name = "stack",
+               .mode = COMMAND_ANY,
+               .handler = &handle_stack_command,
+               .usage = "[<depth>]",
+               .help = "Display the C stack up to <depth>, with the "
+                       "default of 10 and limit of 200.",
+
+       },
+       {
                .name = "help",
                .handler = &handle_help_command,
                .mode = COMMAND_ANY,
-- 
1.6.4.4

_______________________________________________
Openocd-development mailing list
Openocd-development@lists.berlios.de
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to