File:  [Coherent Logic Development] / freem / src / mdebug.c
Revision 1.2: download - view: text, annotated - select for diffs
Sun Mar 9 15:20:18 2025 UTC (12 months, 3 weeks ago) by snw
Branches: MAIN
CVS tags: HEAD
Begin formatting overhaul and REUSE compliance

    1: /*
    2:  *                            *
    3:  *                           * *
    4:  *                          *   *
    5:  *                     ***************
    6:  *                      * *       * *
    7:  *                       *  MUMPS  *
    8:  *                      * *       * *
    9:  *                     ***************
   10:  *                          *   *
   11:  *                           * *
   12:  *                            *
   13:  *
   14:  *   mdebug.c
   15:  *    debugger enhancements
   16:  *
   17:  *  
   18:  *   Author: Serena Willis <snw@coherent-logic.com>
   19:  *    Copyright (C) 1998 MUG Deutschland
   20:  *    Copyright (C) 2020 Coherent Logic Development LLC
   21:  *
   22:  *
   23:  *   This file is part of FreeM.
   24:  *
   25:  *   FreeM is free software: you can redistribute it and/or modify
   26:  *   it under the terms of the GNU Affero Public License as published by
   27:  *   the Free Software Foundation, either version 3 of the License, or
   28:  *   (at your option) any later version.
   29:  *
   30:  *   FreeM is distributed in the hope that it will be useful,
   31:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   32:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   33:  *   GNU Affero Public License for more details.
   34:  *
   35:  *   You should have received a copy of the GNU Affero Public License
   36:  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
   37:  *
   38:  **/
   39: 
   40: #include <stdio.h>
   41: #include <stdlib.h>
   42: #include <string.h>
   43: 
   44: #include "mpsdef.h"
   45: #include "mdebug.h"
   46: #include "freem.h"
   47: #include "mref.h"
   48: 
   49: dbg_watch dbg_watchlist[MAXWATCH];    /* list of watchpoints */
   50: short dbg_enable_watch;               /* 0 = watches disabled, 1 = watches enabled */
   51: int dbg_pending_watches;
   52: 
   53: 
   54: void dbg_init (void)
   55: {
   56:     register int i;
   57: 
   58:     dbg_enable_watch = 0;
   59:     dbg_pending_watches = 0;
   60: 
   61:     for (i = 0; i < MAXWATCH; i++) {
   62: 
   63:         dbg_watchlist[i].varnam = NULL;
   64:         dbg_watchlist[i].chgct = 0;
   65: 
   66:     }
   67: 
   68: }
   69: 
   70: dbg_watch *dbg_add_watch (char *varnam)
   71: {
   72:     register int i;
   73:     int index = -1;
   74:     short found = 0;
   75:     dbg_watch *w;
   76: 
   77:     if ((w = dbg_find_watch (varnam)) != NULL) {
   78:         set_io (UNIX);
   79:         fprintf (stderr, "You are already watching '%s' (changed %d times).\n", dbg_get_watch_name (w->varnam), w->chgct);
   80:         set_io (MUMPS);
   81:         return NULL;
   82:     }
   83: 
   84:     for (i = 0; i < MAXWATCH; i++) {
   85:         if (dbg_watchlist[i].varnam == NULL) {
   86:             found++;
   87:             index = i;
   88:             break;
   89:         }
   90:     }
   91: 
   92:     if (!found) {
   93:         set_io (UNIX);
   94:         fprintf (stderr, "No free watchlist entries available. Try removing an existing watchpoint first.\n");
   95:         set_io (MUMPS);
   96: 
   97:         return NULL;
   98:     }
   99: 
  100:     if ((dbg_watchlist[index].varnam = (char *) malloc (256 * sizeof (char))) == NULL) {
  101:         set_io (UNIX);
  102:         fprintf (stderr, "Could not allocate memory for the new watchlist entry.\n");
  103:         set_io (MUMPS);
  104: 
  105:         return NULL;
  106:     }
  107: 
  108:     strcpy (dbg_watchlist[index].varnam, varnam);
  109:     dbg_watchlist[index].chgct = 0;
  110: 
  111:     set_io (UNIX);
  112:     fprintf (stderr, "Added '%s' to the watchlist.\n", dbg_get_watch_name (varnam));
  113:     set_io (MUMPS);
  114: 
  115:     return NULL;
  116:     
  117: }
  118: 
  119: void dbg_dump_watchlist (void) 
  120: {
  121: 	register int i;
  122: 
  123: 	for (i = 0; i < MAXWATCH; i++) {
  124: 		if (dbg_watchlist[i].firect) {
  125: 			dbg_dump_watch (dbg_watchlist[i].varnam);
  126: 		}
  127: 	}
  128: 
  129: 	dbg_pending_watches = 0;
  130: }
  131: 
  132: 
  133: void dbg_remove_watch (char *varnam)
  134: {
  135:     dbg_watch *w;
  136: 
  137:     if ((w = dbg_find_watch (varnam)) == NULL) {
  138:         set_io (UNIX);
  139:         fprintf (stderr, "'%s' is not being watched.\n", dbg_get_watch_name (varnam));
  140:         set_io (MUMPS);
  141: 
  142:         return;
  143:     }
  144: 
  145:     free (w->varnam);
  146:     
  147:     w->chgct = 0;
  148:     w->firect = 0;
  149: 
  150:     set_io (UNIX);
  151:     printf ("Removed '%s' from the watchlist.\n", dbg_get_watch_name (varnam));
  152:     set_io (MUMPS);
  153:     
  154:     return;
  155: }
  156: 
  157: void dbg_dump_watch (char *varnam)
  158: {
  159:     char *ddwbuf;
  160:     dbg_watch *w;
  161: 
  162:     ddwbuf = (char *) malloc (STRLEN * sizeof (char));
  163:     NULLPTRCHK(ddwbuf,"dbg_dump_watch");
  164: 
  165:     if ((w = dbg_find_watch (varnam)) == NULL) {
  166:         set_io (UNIX);
  167:         fprintf (stderr, "'%s' is not being watched.\n", dbg_get_watch_name (varnam));
  168:         set_io (MUMPS);
  169: 
  170:         return;
  171:     }
  172: 
  173:     w->firect = 0;
  174: 
  175:     if (varnam[0] != '^') {
  176:     	symtab (get_sym, varnam, ddwbuf);
  177:     }
  178:     else {
  179:     	if (varnam[1] == '$') {
  180:     		ssvn (get_sym, varnam, ddwbuf);
  181:     	}
  182:     	else {
  183:     		global (get_sym, varnam, ddwbuf);
  184:     	}
  185:     }
  186: 
  187:     stcnv_m2c (ddwbuf);
  188: 
  189:     set_io (UNIX);
  190:     printf (">> WATCHPOINT:  %s => '%s' (changed %d times)\n", dbg_get_watch_name (varnam), ddwbuf, w->chgct);
  191:     set_io (MUMPS);
  192: 
  193:     free (ddwbuf);
  194: 
  195: }
  196: 
  197: dbg_watch *dbg_find_watch (char *varnam)
  198: {
  199:     register int i;
  200:     
  201: 
  202:     for (i = 0; i < MAXWATCH; i++) {
  203:         if (dbg_watchlist[i].varnam != NULL) {
  204:             if (strcmp (varnam, dbg_watchlist[i].varnam) == 0) {
  205:                 return &(dbg_watchlist[i]);
  206:             }
  207: 
  208:         }
  209:     }
  210: 
  211:     return NULL;
  212: }
  213: 
  214: char *dbg_get_watch_name (char *varnam)
  215: {
  216:     freem_ref_t *r;
  217:     char *s;
  218:     
  219:     r = (freem_ref_t *) malloc (sizeof (freem_ref_t));
  220:     NULLPTRCHK(r,"dbg_get_watch_name");
  221:     
  222:     s = (char *) malloc (STRLEN * sizeof (char));
  223:     NULLPTRCHK(s,"dbg_get_watch_name");
  224:     
  225:     mref_init (r, MREF_RT_LOCAL, "");
  226:     internal_to_mref (r, varnam);
  227:     mref_to_external (r, s);
  228:         
  229:     free (r);
  230:         
  231:     return s;
  232:         
  233: }
  234: 
  235: void dbg_fire_watch (char *varnam) {
  236: 
  237: 	dbg_watch *w;
  238: 
  239: 	if ((w = dbg_find_watch (varnam)) == NULL) {
  240: 		return;
  241: 	}
  242: 
  243: 	w->chgct++;
  244: 	w->firect++;
  245: 	dbg_pending_watches++;
  246: 
  247: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>