/*
* $Id: mdebug.c,v 1.10 2025/05/01 04:45:15 snw Exp $
* debugger enhancements
*
*
* Author: Serena Willis <snw@coherent-logic.com>
* Copyright (C) 1998 MUG Deutschland
* Copyright (C) 2020, 2025 Coherent Logic Development LLC
*
*
* This file is part of FreeM.
*
* FreeM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero Public License for more details.
*
* You should have received a copy of the GNU Affero Public License
* along with FreeM. If not, see <https://www.gnu.org/licenses/>.
*
* $Log: mdebug.c,v $
* Revision 1.10 2025/05/01 04:45:15 snw
* Improve backtraces
*
* Revision 1.9 2025/05/01 03:56:29 snw
* -m
*
* Revision 1.8 2025/04/30 20:03:09 snw
* Work on entryref parser
*
* Revision 1.7 2025/04/30 17:19:16 snw
* Improve backtraces in debugger
*
* Revision 1.6 2025/04/30 14:41:03 snw
* Further debugger work
*
* Revision 1.5 2025/04/29 18:46:17 snw
* Begin work on interactive debugger
*
* Revision 1.4 2025/04/28 19:38:55 snw
* Add trace mode
*
* Revision 1.3 2025/03/09 19:50:47 snw
* Second phase of REUSE compliance and header reformat
*
*
* SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
* SPDX-License-Identifier: AGPL-3.0-or-later
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#ifdef HAVE_LIBREADLINE
# if defined(HAVE_READLINE_READLINE_H)
# include <readline/readline.h>
# elif defined(HAVE_READLINE_H)
# include <readline.h>
# else /* !defined(HAVE_READLINE_H) */
extern char *readline ();
# endif /* !defined(HAVE_READLINE_H) */
/*char *cmdline = NULL;*/
#else /* !defined(HAVE_READLINE_READLINE_H) */
/* no readline */
#endif /* HAVE_LIBREADLINE */
#ifdef HAVE_READLINE_HISTORY
# if defined(HAVE_READLINE_HISTORY_H)
# include <readline/history.h>
# elif defined(HAVE_HISTORY_H)
# include <history.h>
# else /* !defined(HAVE_HISTORY_H) */
extern void add_history ();
extern int write_history ();
extern int read_history ();
# endif /* defined(HAVE_READLINE_HISTORY_H) */
/* no history */
#endif /* HAVE_READLINE_HISTORY */
#include "mpsdef.h"
#include "mdebug.h"
#include "freem.h"
#include "mref.h"
dbg_watch dbg_watchlist[MAXWATCH]; /* list of watchpoints */
short dbg_enable_watch; /* 0 = watches disabled, 1 = watches enabled */
int dbg_pending_watches;
void dbg_init (void)
{
register int i;
dbg_enable_watch = 0;
dbg_pending_watches = 0;
for (i = 0; i < MAXWATCH; i++) {
dbg_watchlist[i].varnam = NULL;
dbg_watchlist[i].chgct = 0;
}
}
int debugger (int entry_mode, char *curcode)
{
#if defined(HAVE_LIBREADLINE)
static int first_entry = TRUE;
static int stepmode = STEPMODE_NONE;
char *dbg_buf;
char dbg_prompt[256];
HIST_ENTRY **dbg_hist;
int dbg_hist_idx;
HIST_ENTRY *dbg_hist_ent;
char rouname_buf[256];
char *savptr;
char *dbg_cmd;
int do_prompt = FALSE;
register int i;
register int j;
set_io (UNIX);
if (first_entry) {
first_entry = FALSE;
printf ("\nEntering debug mode; M commands unavailable.\n");
printf ("Type 'exit' to return to direct mode; 'help' for help.\n\n");
do_prompt = TRUE;
}
else {
switch (entry_mode) {
case DEBENTRY_CMD:
if (stepmode == STEPMODE_CMD) {
do_prompt = TRUE;
}
else {
do_prompt = FALSE;
}
break;
case DEBENTRY_LINE:
if (stepmode == STEPMODE_CMD || stepmode == STEPMODE_LINE) {
do_prompt = TRUE;
}
else {
do_prompt = FALSE;
}
break;
case DEBENTRY_BREAKPOINT:
do_prompt = TRUE;
break;
case DEBENTRY_SIGINT:
break;
}
}
if (!do_prompt) return TRUE;
while (1) {
if (nstx == 0) {
stcpy (rouname_buf, rou_name);
}
else {
char tbuf[256];
getraddress (tbuf, nstx);
stcpy (rouname_buf, &(tbuf[3]));
}
stcnv_m2c (rouname_buf);
snprintf (dbg_prompt, sizeof (dbg_prompt) - 1, "%s $STACK=%d [DEBUG]> ", rouname_buf, nstx);
if ((stepmode == STEPMODE_LINE) || (stepmode == STEPMODE_CMD)) {
char codbuf[512];
stcpy (codbuf, curcode);
stcnv_m2c (codbuf);
printf ("%s\n", codbuf);
}
dbg_buf = readline (dbg_prompt);
if (!dbg_buf) continue;
savptr = dbg_buf;
dbg_cmd = strtok_r (dbg_buf, " ", &savptr);
if ((strcmp (dbg_cmd, "exit") == 0) || (strcmp (dbg_cmd, "quit") == 0)) {
first_entry = TRUE;
stepmode = STEPMODE_NONE;
printf ("\n\nExiting debug mode.\n\n");
set_io (MUMPS);
return FALSE;
}
else if ((strcmp (dbg_cmd, "step") == 0) || (strcmp (dbg_cmd, "s") == 0)) {
stepmode = STEPMODE_CMD;
return TRUE;
}
else if ((strcmp (dbg_cmd, "next") == 0) || (strcmp (dbg_cmd, "n") == 0)) {
stepmode = STEPMODE_LINE;
return TRUE;
}
else if ((strcmp (dbg_cmd, "continue") == 0) || (strcmp (dbg_cmd, "cont") == 0) || (strcmp(dbg_cmd, "c") == 0)) {
stepmode = STEPMODE_CONT;
return TRUE;
}
else if ((strcmp (dbg_cmd, "backtrace") == 0) || (strcmp (dbg_cmd, "bt") == 0)) {
char tmpbuf[1024];
char ecbuf[256];
char m_cmd[10];
char lref[1024];
char bt_mcode[1024];
printf ("%-10s%-10s%s\n", "$STACK", "COMMAND", "PLACE");
printf ("%-10s%-10s%s\n", "======", "=======", "=====");
for (i = 1; i <= nstx; i++) getraddress (callerr[i], i);
for (i = nstx; i > 0; i--) {
stcpy (tmpbuf, callerr[i]);
stcnv_m2c (tmpbuf);
strcpy (ecbuf, &(tmpbuf[1]));
for (j = 0; j < strlen (ecbuf); j++) {
if (ecbuf[j] == ')') {
ecbuf[j] = '\0';
break;
}
}
switch (ecbuf[0]) {
case 'F':
sprintf (m_cmd, "FOR");
break;
case 'D':
sprintf (m_cmd, "DO");
break;
}
printf ("%-10d%-10s%s\n", i, m_cmd, &(tmpbuf[3]));
stcpy (lref, &(tmpbuf[3]));
stcnv_m2c (lref);
if (routine_get_line (lref, bt_mcode) != NULL) {
stcnv_m2c (bt_mcode);
printf ("%-20s%s\n", " ", bt_mcode);
}
}
stcpy (ecbuf, etrap);
stcnv_m2c (ecbuf);
printf ("ETRAP:\t%s\n", ecbuf);
stcpy (ecbuf, ztrap[nstx]);
stcnv_m2c (ecbuf);
printf ("ZTRAP:\t%s\n", ecbuf);
}
else {
printf ("DEBUG: invalid command\r\n");
}
}
return TRUE;
#else
return FALSE;
#endif
}
dbg_watch *dbg_add_watch (char *varnam)
{
register int i;
int index = -1;
short found = 0;
dbg_watch *w;
if ((w = dbg_find_watch (varnam)) != NULL) {
set_io (UNIX);
fprintf (stderr, "You are already watching '%s' (changed %d times).\n", dbg_get_watch_name (w->varnam), w->chgct);
set_io (MUMPS);
return NULL;
}
for (i = 0; i < MAXWATCH; i++) {
if (dbg_watchlist[i].varnam == NULL) {
found++;
index = i;
break;
}
}
if (!found) {
set_io (UNIX);
fprintf (stderr, "No free watchlist entries available. Try removing an existing watchpoint first.\n");
set_io (MUMPS);
return NULL;
}
if ((dbg_watchlist[index].varnam = (char *) malloc (256 * sizeof (char))) == NULL) {
set_io (UNIX);
fprintf (stderr, "Could not allocate memory for the new watchlist entry.\n");
set_io (MUMPS);
return NULL;
}
strcpy (dbg_watchlist[index].varnam, varnam);
dbg_watchlist[index].chgct = 0;
set_io (UNIX);
fprintf (stderr, "Added '%s' to the watchlist.\n", dbg_get_watch_name (varnam));
set_io (MUMPS);
return NULL;
}
void dbg_dump_watchlist (void)
{
register int i;
for (i = 0; i < MAXWATCH; i++) {
if (dbg_watchlist[i].firect) {
dbg_dump_watch (dbg_watchlist[i].varnam);
}
}
dbg_pending_watches = 0;
}
void dbg_remove_watch (char *varnam)
{
dbg_watch *w;
if ((w = dbg_find_watch (varnam)) == NULL) {
set_io (UNIX);
fprintf (stderr, "'%s' is not being watched.\n", dbg_get_watch_name (varnam));
set_io (MUMPS);
return;
}
free (w->varnam);
w->chgct = 0;
w->firect = 0;
set_io (UNIX);
printf ("Removed '%s' from the watchlist.\n", dbg_get_watch_name (varnam));
set_io (MUMPS);
return;
}
void dbg_dump_watch (char *varnam)
{
char *ddwbuf;
dbg_watch *w;
ddwbuf = (char *) malloc (STRLEN * sizeof (char));
NULLPTRCHK(ddwbuf,"dbg_dump_watch");
if ((w = dbg_find_watch (varnam)) == NULL) {
set_io (UNIX);
fprintf (stderr, "'%s' is not being watched.\n", dbg_get_watch_name (varnam));
set_io (MUMPS);
return;
}
w->firect = 0;
if (varnam[0] != '^') {
symtab (get_sym, varnam, ddwbuf);
}
else {
if (varnam[1] == '$') {
ssvn (get_sym, varnam, ddwbuf);
}
else {
global (get_sym, varnam, ddwbuf);
}
}
stcnv_m2c (ddwbuf);
set_io (UNIX);
printf (">> WATCHPOINT: %s => '%s' (changed %d times)\n", dbg_get_watch_name (varnam), ddwbuf, w->chgct);
set_io (MUMPS);
free (ddwbuf);
}
dbg_watch *dbg_find_watch (char *varnam)
{
register int i;
for (i = 0; i < MAXWATCH; i++) {
if (dbg_watchlist[i].varnam != NULL) {
if (strcmp (varnam, dbg_watchlist[i].varnam) == 0) {
return &(dbg_watchlist[i]);
}
}
}
return NULL;
}
char *dbg_get_watch_name (char *varnam)
{
freem_ref_t *r;
char *s;
r = (freem_ref_t *) malloc (sizeof (freem_ref_t));
NULLPTRCHK(r,"dbg_get_watch_name");
s = (char *) malloc (STRLEN * sizeof (char));
NULLPTRCHK(s,"dbg_get_watch_name");
mref_init (r, MREF_RT_LOCAL, "");
internal_to_mref (r, varnam);
mref_to_external (r, s);
free (r);
return s;
}
void dbg_fire_watch (char *varnam) {
dbg_watch *w;
if ((w = dbg_find_watch (varnam)) == NULL) {
return;
}
w->chgct++;
w->firect++;
dbg_pending_watches++;
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>