1 /* event.c: Debugger events
2 Copyright (c) 2008 Philip Kendall
3 Copyright (c) 2015 Sergio Baldoví
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 Author contact information:
20
21 E-mail: philip-fuse@shadowmagic.org.uk
22
23 */
24
25 #include <config.h>
26
27 #include <string.h>
28 #ifdef HAVE_STRINGS_STRCASECMP
29 #include <strings.h>
30 #endif /* #ifdef HAVE_STRINGS_STRCASECMP */
31
32 #ifdef HAVE_LIB_GLIB
33 #include <glib.h>
34 #endif /* #ifdef HAVE_LIB_GLIB */
35
36 #include <libspectrum.h>
37
38 #include "debugger_internals.h"
39 #include "fuse.h"
40 #include "ui/ui.h"
41 #include "utils.h"
42
43 static GArray *registered_events;
44
45 void
debugger_event_init(void)46 debugger_event_init( void )
47 {
48 registered_events = g_array_new( FALSE, FALSE, sizeof( debugger_event_t ) );
49 }
50
51 int
debugger_event_register(const char * type,const char * detail)52 debugger_event_register( const char *type, const char *detail )
53 {
54 debugger_event_t event;
55
56 event.type = utils_safe_strdup( type );
57 event.detail = utils_safe_strdup( detail );
58
59 g_array_append_val( registered_events, event );
60
61 return registered_events->len - 1;
62 }
63
64 static int
event_matches(debugger_event_t * event,const char * type,const char * detail)65 event_matches( debugger_event_t *event, const char *type, const char *detail )
66 {
67 if( strcasecmp( type, event->type ) ) return 0;
68 if( strcmp( detail, "*" ) == 0 ) return 1;
69 if( strcmp( event->detail, "*" ) == 0 ) return 1;
70 return strcasecmp( detail, event->detail ) == 0;
71 }
72
73 int
debugger_event_is_registered(const char * type,const char * detail)74 debugger_event_is_registered( const char *type, const char *detail )
75 {
76 size_t i;
77
78 for( i = 0; i < registered_events->len; i++ ) {
79 debugger_event_t event =
80 g_array_index( registered_events, debugger_event_t, i );
81
82 if( event_matches( &event, type, detail ) ) return 1;
83 }
84
85 return 0;
86 }
87
88 void
debugger_event(int event_code)89 debugger_event( int event_code )
90 {
91 debugger_event_t event;
92 debugger_breakpoint *bp;
93 GSList *ptr, *ptr_next;
94
95 int signal_breakpoints_updated = 0;
96
97 if( event_code >= registered_events->len ) {
98 ui_error( UI_ERROR_ERROR, "internal error: invalid debugger event %d",
99 event_code );
100 fuse_abort();
101 }
102
103 event = g_array_index( registered_events, debugger_event_t, event_code );
104
105 for( ptr = debugger_breakpoints; ptr; ptr = ptr_next ) {
106
107 bp = ptr->data;
108 ptr_next = ptr->next;
109
110 if( bp->type != DEBUGGER_BREAKPOINT_TYPE_EVENT ) continue;
111
112 if( event_matches( &bp->value.event, event.type, event.detail ) &&
113 debugger_breakpoint_trigger( bp ) ) {
114 debugger_mode = DEBUGGER_MODE_HALTED;
115 debugger_command_evaluate( bp->commands );
116
117 if( bp->life == DEBUGGER_BREAKPOINT_LIFE_ONESHOT ) {
118 debugger_breakpoints = g_slist_remove( debugger_breakpoints, bp );
119 libspectrum_free( bp );
120 signal_breakpoints_updated = 1;
121 }
122 }
123 }
124
125 if( signal_breakpoints_updated )
126 ui_breakpoints_updated();
127 }
128
129 /* Tidy-up function called at end of emulation */
130 void
debugger_event_end(void)131 debugger_event_end( void )
132 {
133 int i;
134 debugger_event_t event;
135
136 if( !registered_events ) return;
137
138 for( i = 0; i < registered_events->len; i++ ) {
139 event = g_array_index( registered_events, debugger_event_t, i );
140 libspectrum_free( event.detail );
141 libspectrum_free( event.type );
142 }
143
144 g_array_free( registered_events, TRUE );
145 registered_events = NULL;
146 }
147