1 /*
2  *  A Z-Machine
3  *  Copyright (C) 2000 Andrew Hunter
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 of the License, or (at your option) any later version.
9  *
10  *  This library 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 GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 /*
21  * The debugger
22  */
23 
24 #ifndef __DEBUG_H
25 #define __DEBUG_H
26 
27 #include "zmachine.h"
28 #include "file.h"
29 #include "hash.h"
30 
31 /* === Debug data structures === */
32 
33 typedef struct debug_breakpoint debug_breakpoint;
34 typedef struct debug_symbols    debug_symbols;
35 typedef struct debug_symbol     debug_symbol;
36 typedef struct debug_file       debug_file;
37 typedef struct debug_line       debug_line;
38 
39 /* Symbols */
40 typedef struct debug_class      debug_class;
41 typedef struct debug_object     debug_object;
42 typedef struct debug_global     debug_global;
43 typedef struct debug_array      debug_array;
44 typedef struct debug_attr       debug_attr;
45 typedef struct debug_prop       debug_prop;
46 typedef struct debug_fakeact    debug_fakeact;
47 typedef struct debug_action     debug_action;
48 typedef struct debug_routine    debug_routine;
49 
50 /* Information structures */
51 typedef struct debug_address    debug_address;
52 
53 /* External debuggers */
54 typedef void(*debug_breakpoint_handler)(ZDWord pc);
55 typedef enum debug_step_type {
56 	debug_step_over,
57 	debug_step_into,
58 	debug_step_out
59 } debug_step_type;
60 
61 struct debug_file
62 {
63   int number;
64   char* name;
65   char* realname;
66 
67   char*  data;
68   int    nlines;
69   char** line;
70 };
71 
72 struct debug_class
73 {
74   char* name;
75 
76   int st_fl, st_ln, st_ch;
77   int end_fl, end_ln, end_ch;
78 };
79 
80 struct debug_object
81 {
82   int number;
83   char* name;
84 
85   int st_fl, st_ln, st_ch;
86   int end_fl, end_ln, end_ch;
87 };
88 
89 struct debug_global
90 {
91   int   number;
92   char* name;
93 };
94 
95 struct debug_array
96 {
97   int offset;
98   char* name;
99 };
100 
101 struct debug_attr
102 {
103   int   number;
104   char* name;
105 };
106 
107 struct debug_prop
108 {
109   int   number;
110   char* name;
111 };
112 
113 struct debug_fakeact
114 {
115   int number;
116   char* name;
117 };
118 
119 struct debug_action
120 {
121   int number;
122   char* name;
123 };
124 
125 struct debug_line
126 {
127   int fl, ln, ch;
128   ZDWord address;
129 };
130 
131 struct debug_routine
132 {
133   int number;
134 
135   int defn_fl, defn_ln, defn_ch;
136   ZDWord start;
137 
138   ZDWord end;
139   int end_fl, end_ln, end_ch;
140 
141   char* name;
142 
143   int    nvars;
144   char** var;
145 
146   int         nlines;
147   debug_line* line;
148 };
149 
150 struct debug_symbol
151 {
152   enum
153     {
154       dbg_class,
155       dbg_object,
156       dbg_global,
157       dbg_attr,
158       dbg_prop,
159       dbg_action,
160       dbg_fakeact,
161       dbg_array,
162       dbg_routine
163     }
164   type;
165 
166   union
167   {
168     debug_class    class;
169     debug_object   object;
170     debug_global   global;
171     debug_attr     attr;
172     debug_prop     prop;
173     debug_action   action;
174     debug_fakeact  fakeact;
175     debug_array    array;
176     int            routine;
177   } data;
178 
179   debug_symbol* next;
180 };
181 
182 struct debug_breakpoint
183 {
184   ZDWord address;
185   ZByte  original;
186 
187   int    usage;
188   int    temporary;
189   int    funcbp;
190 };
191 
192 struct debug_symbols
193 {
194   int nsymbols;
195   hash symbol;
196   hash file;
197 
198   debug_symbol* first_symbol;
199 
200   debug_routine* routine;
201   int            nroutines;
202   debug_file*    files;
203   int            nfiles;
204 
205   ZDWord         codearea;
206   ZDWord		 stringarea;
207   ZDWord		 largest_object;
208 };
209 
210 struct debug_address
211 {
212   debug_routine* routine;
213   debug_line*    line;
214 
215   int            line_no;
216 };
217 
218 extern debug_breakpoint* debug_bplist;
219 extern int               debug_nbps;
220 extern debug_symbols     debug_syms;
221 extern int               debug_eval_result;
222 extern char*             debug_eval_type;
223 extern const char*       debug_error;
224 
225 extern int*				 debug_expr;
226 extern int				 debug_expr_pos;
227 extern debug_routine*    debug_expr_routine;
228 
229 #define DEBUG_EOF_DBR 0
230 #define DEBUG_FILE_DBR 1
231 #define DEBUG_CLASS_DBR 2
232 #define DEBUG_OBJECT_DBR 3
233 #define DEBUG_GLOBAL_DBR 4
234 #define DEBUG_ATTR_DBR 5
235 #define DEBUG_PROP_DBR 6
236 #define DEBUG_FAKEACT_DBR 7
237 #define DEBUG_ACTION_DBR 8
238 #define DEBUG_HEADER_DBR 9
239 #define DEBUG_LINEREF_DBR 10
240 #define DEBUG_ROUTINE_DBR 11
241 #define DEBUG_ARRAY_DBR 12
242 #define DEBUG_MAP_DBR 13
243 #define DEBUG_ROUTINE_END_DBR 14
244 
245 /* === Debug functions === */
246 
247 /* Breakpoints */
248 extern int               debug_set_breakpoint  (int address,
249 						int temporary,
250 						int funcbp);
251 extern int               debug_clear_breakpoint(debug_breakpoint* bp);
252 extern debug_breakpoint* debug_get_breakpoint  (int address);
253 
254 extern void              debug_run_breakpoint(ZDWord pc);
255 
256 /* === Inform debug file functions === */
257 
258 /* Initialisation/loading */
259 extern void              debug_load_symbols    (char* filename,
260 						char* pathname);
261 
262 /* Information retrieval */
263 extern debug_address     debug_find_address      (int   address);
264 extern int               debug_find_named_address(const char* name);
265 extern char*             debug_address_string    (debug_address addr,
266 						  int pc,
267 						  int format);
268 
269 /* === Expression evaluation === */
270 extern int               debug_eval_parse  (void);
271 extern void              debug_eval_error  (const char*);
272 extern int               debug_eval_lex    (void);
273 extern ZWord             debug_symbol_value(const char*    symbol,
274 					    debug_routine* r);
275 extern char*             debug_print_value (ZWord          value,
276 					    char*          type);
277 
278 /* === Alternative breakpoint handling === */
279 extern void debug_set_bp_handler(debug_breakpoint_handler handler);
280 extern void debug_set_temp_breakpoints(debug_step_type step);
281 
282 #endif
283 
284