--- freem/src/mdebug.c 2025/05/01 17:02:30 1.11 +++ freem/src/mdebug.c 2025/05/20 18:07:41 1.13 @@ -1,5 +1,5 @@ /* - * $Id: mdebug.c,v 1.11 2025/05/01 17:02:30 snw Exp $ + * $Id: mdebug.c,v 1.13 2025/05/20 18:07:41 snw Exp $ * debugger enhancements * * @@ -24,6 +24,12 @@ * along with FreeM. If not, see . * * $Log: mdebug.c,v $ + * Revision 1.13 2025/05/20 18:07:41 snw + * Add completion to debugger + * + * Revision 1.12 2025/05/01 21:02:31 snw + * Documentation updates + * * Revision 1.11 2025/05/01 17:02:30 snw * Further debugging improvements * @@ -95,6 +101,51 @@ extern int read_history (); #include "freem.h" #include "mref.h" +#ifdef HAVE_LIBREADLINE +char *dbg_commands[] = { + "backtrace", + "continue", + "examine", + "exit", + "halt", + "next", + "quit", + "step", + "trace", + "watch", + NULL +}; + +char **dbg_completion(const char *, int, int); +char *dbg_generator(const char *, int); + +char **dbg_completion(const char *text, int start, int end) +{ + if (start > 0) return NULL; + rl_attempted_completion_over = 1; + return rl_completion_matches (text, dbg_generator); +} + +char *dbg_generator(const char *text, int state) +{ + static int list_index, len; + char *name; + + if (!state) { + list_index = 0; + len = strlen(text); + } + + while ((name = dbg_commands[list_index++])) { + if (strncmp (name, text, len) == 0) { + return strdup (name); + } + } + + return NULL; +} +#endif + dbg_watch dbg_watchlist[MAXWATCH]; /* list of watchpoints */ short dbg_enable_watch; /* 0 = watches disabled, 1 = watches enabled */ int dbg_pending_watches; @@ -139,7 +190,8 @@ int debugger (int entry_mode, char *entr char tbuf2[512]; register int i; register int j; - + + rl_attempted_completion_function = dbg_completion; set_io (UNIX); if (first_entry) { @@ -206,7 +258,7 @@ int debugger (int entry_mode, char *entr } snprintf (dbg_prompt, sizeof (dbg_prompt) - 1, "%s $STACK=%d [DEBUG]> ", rouname_buf, nstx); - + if (dbg_enable_watch && dbg_pending_watches) dbg_dump_watchlist (); dbg_buf = readline (dbg_prompt); if (!dbg_buf) continue; @@ -347,6 +399,48 @@ int debugger (int entry_mode, char *entr printf ("ZTRAP:\t%s\n", ecbuf); } + else if ((strcmp (dbg_cmd, "halt") == 0) || (strcmp (dbg_cmd, "h") == 0)) { + printf ("debug: forcing halt\n"); + exit (0); + } + else if ((strcmp (dbg_cmd, "watch") == 0) || (strcmp (dbg_cmd, "w") == 0)) { + char watchmode; + char *glvn; + + if (dbg_argc == 1) { + switch (dbg_enable_watch) { + case 0: + dbg_enable_watch = 1; + printf ("debug: watchpoints enabled\n"); + break; + case 1: + dbg_enable_watch = 0; + printf ("debug: watchpoints disabled\n"); + break; + } + } + else { + watchmode = dbarg[0]; + glvn = &(dbarg[1]); + + switch (watchmode) { + case '+': + dbg_add_watch (glvn); + break; + case '-': + dbg_remove_watch (glvn); + break; + case '?': + dbg_dump_watch (glvn); + break; + default: + printf ("debug: watch mode must be +, -, or ?\n"); + break; + } + + set_io (UNIX); + } + } else { printf ("DEBUG: invalid command\r\n"); } @@ -390,7 +484,7 @@ dbg_watch *dbg_add_watch (char *varnam) return NULL; } - if ((dbg_watchlist[index].varnam = (char *) malloc (256 * sizeof (char))) == NULL) { + if ((dbg_watchlist[index].varnam = (char *) calloc (256, sizeof (char))) == NULL) { set_io (UNIX); fprintf (stderr, "Could not allocate memory for the new watchlist entry.\n"); set_io (MUMPS); @@ -402,7 +496,7 @@ dbg_watch *dbg_add_watch (char *varnam) dbg_watchlist[index].chgct = 0; set_io (UNIX); - fprintf (stderr, "Added '%s' to the watchlist.\n", dbg_get_watch_name (varnam)); + fprintf (stderr, "Added '%s' to the watchlist.\n", varnam); set_io (MUMPS); return NULL;