Annotation of freem/src/mdebug.c, revision 1.4

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

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