File: event.c

package info (click to toggle)
fuse-emulator 1.5.7%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,024 kB
  • sloc: ansic: 87,196; sh: 11,456; perl: 3,916; makefile: 991; yacc: 245; lex: 139
file content (146 lines) | stat: -rw-r--r-- 3,769 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* event.c: Debugger events
   Copyright (c) 2008 Philip Kendall
   Copyright (c) 2015 Sergio BaldovĂ­

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: philip-fuse@shadowmagic.org.uk

*/

#include <config.h>

#include <string.h>
#ifdef HAVE_STRINGS_STRCASECMP
#include <strings.h>
#endif      /* #ifdef HAVE_STRINGS_STRCASECMP */

#ifdef HAVE_LIB_GLIB
#include <glib.h>
#endif				/* #ifdef HAVE_LIB_GLIB */

#include <libspectrum.h>

#include "debugger_internals.h"
#include "fuse.h"
#include "ui/ui.h"
#include "utils.h"

static GArray *registered_events;

void
debugger_event_init( void )
{
  registered_events = g_array_new( FALSE, FALSE, sizeof( debugger_event_t ) );
}

int
debugger_event_register( const char *type, const char *detail )
{
  debugger_event_t event;

  event.type = utils_safe_strdup( type );
  event.detail = utils_safe_strdup( detail );

  g_array_append_val( registered_events, event );

  return registered_events->len - 1;
}

static int
event_matches( debugger_event_t *event, const char *type, const char *detail )
{
  if( strcasecmp( type, event->type ) ) return 0;
  if( strcmp( detail, "*" ) == 0 ) return 1;
  if( strcmp( event->detail, "*" ) == 0 ) return 1;
  return strcasecmp( detail, event->detail ) == 0;
}

int
debugger_event_is_registered( const char *type, const char *detail )
{
  size_t i;

  for( i = 0; i < registered_events->len; i++ ) {
    debugger_event_t event =
      g_array_index( registered_events, debugger_event_t, i );

    if( event_matches( &event, type, detail ) ) return 1;
  }

  return 0;
}

void
debugger_event( int event_code )
{
  debugger_event_t event;
  debugger_breakpoint *bp;
  GSList *ptr, *ptr_next;

  int signal_breakpoints_updated = 0;

  if( event_code >= registered_events->len ) {
    ui_error( UI_ERROR_ERROR, "internal error: invalid debugger event %d",
	      event_code );
    fuse_abort();
  }

  event = g_array_index( registered_events, debugger_event_t, event_code );

  for( ptr = debugger_breakpoints; ptr; ptr = ptr_next ) {

    bp = ptr->data;
    ptr_next = ptr->next;

    if( bp->type != DEBUGGER_BREAKPOINT_TYPE_EVENT ) continue;

    if( event_matches( &bp->value.event, event.type, event.detail ) &&
        debugger_breakpoint_trigger( bp ) ) {
      debugger_mode = DEBUGGER_MODE_HALTED;
      debugger_command_evaluate( bp->commands );

      if( bp->life == DEBUGGER_BREAKPOINT_LIFE_ONESHOT ) {
        debugger_breakpoints = g_slist_remove( debugger_breakpoints, bp );
        libspectrum_free( bp );
        signal_breakpoints_updated = 1;
      }
    }
  }

  if( signal_breakpoints_updated )
      ui_breakpoints_updated();
}

/* Tidy-up function called at end of emulation */
void
debugger_event_end( void )
{
  int i;
  debugger_event_t event;

  if( !registered_events ) return;

  for( i = 0; i < registered_events->len; i++ ) {
    event = g_array_index( registered_events, debugger_event_t, i );
    libspectrum_free( event.detail );
    libspectrum_free( event.type );
  }

  g_array_free( registered_events, TRUE );
  registered_events = NULL;
}