/
parrot_debugger.c
375 lines (240 loc) · 7.53 KB
/
parrot_debugger.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Copyright (C) 2001-2009, Parrot Foundation.
$Id$
History:
Renamed from pdb.c in 2008.7.15
=head1 NAME
parrot_debugger - The Parrot debugger
=head1 SYNOPSIS
parrot_debugger programfile
parrot_debugger --script scriptfile programfile
=head1 DESCRIPTION
=head2 Commands
=over 4
=item C<disassemble>
Disassemble the bytecode.
Use this if you have a PBC file but not the PASM.
=item C<load>
Load a source code file.
=item C<list> or C<l>
List the source code file.
=item C<run> or C<r>
Run the program.
=item C<break> or C<b>
Add a breakpoint at the line number given or the current line if none is given.
(pdb) b
Breakpoint 1 at pos 0
(pdb) b 10
Breakpoint 1 at pos 10
=item C<watch> or C<w>
Add a watchpoint.
=item C<delete> or C<d>
Given a number n, deletes the n-th breakpoint. To delete the first breakpoint:
(pdb) d 1
Breakpoint 1 deleted
=item C<disable>
Disable a breakpoint.
=item C<enable>
Reenable a disabled breakpoint.
=item C<continue> or C<c>
Continue the program execution.
=item C<next> or C<n>
Run the next instruction
=item C<eval> or C<e>
Run an instruction.
=item C<trace> or C<t>
Trace the next instruction. This is equivalent to printing the source of the
next instruction and then executing it.
=item C<print> or C<p>
Print an interpreter register. If a register I0 has been used, this
would look like:
(pdb) p I0
2
If no register number is given then all registers of that type will be printed.
If the two registers I0 and I1 have been used, then this would look like:
(pdb) p I
I0 = 2
I1 = 5
It would be nice if "p" with no arguments printed all registers, but this is
currently not the case.
=item C<stack> or C<s>
Examine the stack.
=item C<info>
Print interpreter information relating to memory allocation and garbage
collection.
=item C<gcdebug>
Toggle garbage collection debugging mode. In gcdebug mode a garbage collection
cycle is run before each opcocde, which is the same as using the gcdebug core.
=item C<quit> or C<q>
Exit the debugger.
=item C<help> or C<h>
Print the help.
=back
=head2 Debug Ops
You can also debug Parrot code by using the C<debug_init>, C<debug_load>
and C<debug_break> ops in F<ops/debug.ops>.
=over 4
=cut
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../compilers/imcc/imc.h"
#include "../compilers/imcc/parser.h"
#include "parrot/embed.h"
#include "parrot/debugger.h"
#include "parrot/runcore_api.h"
static void PDB_printwelcome(void);
static void PDB_run_code(PARROT_INTERP, int argc, char *argv[]);
/*
=item C<int main(int argc, char *argv[])>
Reads the PIR, PASM or PBC file from argv[1], loads it, and then calls
Parrot_debug().
=cut
*/
int
main(int argc, char *argv[])
{
int nextarg;
Parrot_Interp interp;
PDB_t *pdb;
const char *scriptname = NULL;
Parrot_set_config_hash();
interp = Parrot_new(NULL);
Parrot_set_executable_name(interp, Parrot_str_new(interp, argv[0], 0));
Parrot_debugger_init(interp);
pdb = interp->pdb;
/*Parrot_set_config_hash(); TODO link with cfg */
pdb->state = PDB_ENTER;
Parrot_block_GC_mark(interp);
Parrot_block_GC_sweep(interp);
nextarg = 1;
if (argv[nextarg] && strcmp(argv[nextarg], "--script") == 0)
{
scriptname = argv [++nextarg];
++nextarg;
}
if (argv[nextarg]) {
const char *filename = argv[nextarg];
const char *ext = strrchr(filename, '.');
if (ext && STREQ(ext, ".pbc")) {
Parrot_PackFile pf = Parrot_pbc_read(interp, filename, 0);
if (!pf)
return 1;
Parrot_pbc_load(interp, pf);
PackFile_fixup_subs(interp, PBC_MAIN, NULL);
}
else {
void *yyscanner;
Parrot_PackFile pf = PackFile_new(interp, 0);
int pasm_file = 0;
do_yylex_init(interp, &yyscanner);
Parrot_pbc_load(interp, pf);
IMCC_push_parser_state(interp);
IMCC_INFO(interp)->state->file = mem_sys_strdup(filename);
if (!(imc_yyin_set(fopen(filename, "r"), yyscanner))) {
IMCC_fatal_standalone(interp, EXCEPTION_PIO_ERROR,
"Error reading source file %s.\n",
filename);
}
if (ext && STREQ(ext, ".pasm"))
pasm_file = 1;
emit_open(interp, 1, NULL);
IMCC_INFO(interp)->state->pasm_file = pasm_file;
yyparse(yyscanner, interp);
imc_compile_all_units(interp);
imc_cleanup(interp, yyscanner);
fclose(imc_yyin_get(yyscanner));
PackFile_fixup_subs(interp, PBC_POSTCOMP, NULL);
/* load the source for debugger list */
PDB_load_source(interp, filename);
PackFile_fixup_subs(interp, PBC_MAIN, NULL);
}
}
else {
/* Generate some code to be able to enter into runloop */
STRING *compiler = Parrot_str_new_constant(interp, "PIR");
STRING *errstr = NULL;
const char source []= ".sub aux :main\nexit 0\n.end\n";
PMC *code = Parrot_compile_string(interp, compiler, source, &errstr);
if (!STRING_IS_NULL(errstr))
Parrot_io_eprintf(interp, "%Ss\n", errstr);
}
Parrot_unblock_GC_mark(interp);
Parrot_unblock_GC_sweep(interp);
if (scriptname)
PDB_script_file(interp, scriptname);
else
PDB_printwelcome();
Parrot_runcore_switch(interp, Parrot_str_new_constant(interp, "debugger"));
PDB_run_code(interp, argc - nextarg, argv + nextarg);
Parrot_exit(interp, 0);
}
/*
=item C<static void PDB_run_code(PARROT_INTERP, int argc, char *argv[])>
Run the code, catching exceptions if they are left unhandled.
=cut
*/
static void
PDB_run_code(PARROT_INTERP, int argc, char *argv[])
{
new_runloop_jump_point(interp);
if (setjmp(interp->current_runloop->resume)) {
free_runloop_jump_point(interp);
fprintf(stderr, "Caught exception\n");
return;
}
/* Loop to avoid exiting at program end */
do {
Parrot_runcode(interp, argc, argv);
interp->pdb->state |= PDB_STOPPED;
} while (! (interp->pdb->state & PDB_EXIT));
free_runloop_jump_point(interp);
}
/*
=item C<static void PDB_printwelcome(void)>
Prints out the welcome string.
=cut
*/
static void
PDB_printwelcome(void)
{
fprintf(stderr,
"Parrot " PARROT_VERSION " Debugger\n"
"\nPlease note: the debugger is currently under reconstruction\n");
}
/*
=back
=head1 SEE ALSO
F<src/debug.c>, F<include/parrot/debug.h>.
=head1 HISTORY
=over 4
=item * Initial version by Daniel Grunblatt on 2002.5.19.
=item * Start of rewrite - leo 2005.02.16
The debugger now uses it's own interpreter. User code is run in
Interp* debugee. We have:
debug_interp->pdb->debugee->debugger
^ |
| v
+------------- := -----------+
Debug commands are mostly run inside the C<debugger>. User code
runs of course in the C<debugee>.
=back
=head1 TODO
=over 4
=item * Check the user input for bad commands, it's quite easy to make
it bang now, try listing the source before loading or disassembling it.
=item * Print the interpreter info.
=item * Make the user interface better (add comands
history/completion).
=item * Some other things I don't remember now because it's late.
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/