* t/pmc/complex.t:
[parrot.git] / src / pdb.c
blobb55e56bc683b8c8bba706abae7360772889db25f
1 /*
2 Copyright (C) 2001-2008, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 pdb - The Parrot debugger
9 =head1 SYNOPSIS
11 pdb programfile
13 =head1 DESCRIPTION
15 =head2 Commands
17 =over 4
19 =item C<disassemble>
21 Disassemble the bytecode.
23 Use this if you have a PBC file but not the PASM.
25 =item C<load>
27 Load a source code file.
29 =item C<list> or C<l>
31 List the source code file.
33 =item C<run> or C<r>
35 Run the program.
37 =item C<break> or C<b>
39 Add a breakpoint.
41 =item C<watch> or C<w>
43 Add a watchpoint.
45 =item C<delete> or C<d>
47 Delete a breakpoint.
49 =item C<disable>
51 Disable a breakpoint.
53 =item C<enable>
55 Reenable a disabled breakpoint.
57 =item C<continue> or C<c>
59 Continue the program execution.
61 =item C<next> or C<n>
63 Run the next instruction
65 =item C<eval> or C<e>
67 Run an instruction.
69 =item C<trace> or C<t>
71 Trace the next instruction.
73 =item C<print> or C<p>
75 Print the interpreter registers.
77 =item C<stack> or C<s>
79 Examine the stack.
81 =item C<info>
83 Print interpreter information.
85 =item C<quit> or C<q>
87 Exit the debugger.
89 =item C<help> or C<h>
91 Print the help.
93 =back
95 =head2 Debug Ops
97 You can also debug Parrot code by using the C<debug_init>, C<debug_load>
98 and C<debug_break> ops in F<ops/debug.ops>.
100 =over 4
102 =cut
106 #include <stdio.h>
107 #include <stdlib.h>
108 #include <ctype.h>
109 #include "../compilers/imcc/imc.h"
110 #include "../compilers/imcc/parser.h"
111 #include "parrot/embed.h"
113 static void PDB_printwelcome(void);
114 static void PDB_run_code(Parrot_Interp interp, int argc, char *argv[]);
118 =item C<int main(int argc, char *argv[])>
120 Reads the PASM or PBC file from argv[1], loads it, and then calls
121 Parrot_debug().
123 =cut
127 extern void imcc_init(Parrot_Interp interp);
130 main(int argc, char *argv[])
132 Parrot_Interp debugger = Parrot_new(NULL);
133 Parrot_Interp interp = Parrot_new(debugger);
134 PDB_t *pdb = mem_allocate_zeroed_typed(PDB_t);
135 char *filename;
136 char *ext;
137 void *yyscanner;
139 /*Parrot_set_config_hash(); TODO link with cfg */
141 /* attach pdb structure */
142 debugger->pdb = pdb;
143 interp->debugger = debugger;
144 pdb->debugee = interp;
146 Parrot_block_DOD(interp);
147 Parrot_block_GC(interp);
148 imcc_init(interp);
150 do_yylex_init(interp, &yyscanner);
152 if (argc < 2) {
153 fprintf(stderr, "Usage: pdb programfile [program-options]\n");
154 Parrot_exit(interp, 1);
157 filename = argv[1];
158 ext = strrchr(filename, '.');
160 if (ext && STREQ(ext, ".pbc")) {
161 Parrot_PackFile pf = Parrot_readbc(interp, filename);
163 if (!pf)
164 return 1;
166 Parrot_loadbc(interp, pf);
168 else {
169 Parrot_PackFile pf = PackFile_new(interp, 0);
170 int pasm_file = 0;
172 Parrot_loadbc(interp, pf);
174 IMCC_push_parser_state(interp);
175 IMCC_INFO(interp)->state->file = filename;
177 if (!(imc_yyin_set(fopen(filename, "r"), yyscanner))) {
178 IMCC_fatal_standalone(interp, E_IOError,
179 "Error reading source file %s.\n",
180 filename);
183 if (ext && STREQ(ext, ".pasm"))
184 pasm_file = 1;
186 emit_open(interp, 1, NULL);
187 IMCC_INFO(interp)->state->pasm_file = pasm_file;
188 yyparse(yyscanner, interp);
189 imc_compile_all_units(interp);
191 imc_cleanup(interp, yyscanner);
193 fclose(imc_yyin_get(yyscanner));
194 PackFile_fixup_subs(interp, PBC_POSTCOMP, NULL);
197 Parrot_unblock_DOD(interp);
198 Parrot_unblock_GC(interp);
200 PDB_printwelcome();
202 PDB_run_code(interp, argc - 1, argv + 1);
205 Parrot_exit(interp, 0);
210 =item C<static void PDB_add_exception_handler(Parrot_Interp)>
212 Adds a default exception handler to PDB.
216 static void
217 PDB_run_code(Parrot_Interp interp, int argc, char *argv[])
219 Parrot_exception exp;
221 if (setjmp(exp.destination)) {
222 fprintf(stderr, "Caught exception: %s\n",
223 string_to_cstring(interp, interp->exceptions->msg));
224 return;
227 push_new_c_exception_handler(interp, &exp);
229 Parrot_runcode(interp, argc - 1, argv + 1);
234 =item C<static void PDB_printwelcome(void)>
236 Prints out the welcome string.
238 =cut
242 static void
243 PDB_printwelcome(void)
245 fprintf(stderr, "Parrot Debugger 0.4.x\n");
246 fprintf(stderr, "\nPlease note: ");
247 fprintf(stderr, "the debugger is currently under reconstruction\n");
252 =back
254 =head1 SEE ALSO
256 F<src/debug.c>, F<include/parrot/debug.h>.
258 =head1 HISTORY
260 =over 4
262 =item * Initial version by Daniel Grunblatt on 2002.5.19.
264 =item * Start of rewrite - leo 2005.02.16
266 The debugger now uses it's own interpreter. User code is run in
267 Interp* debugee. We have:
269 debug_interp->pdb->debugee->debugger
272 +------------- := -----------+
274 Debug commands are mostly run inside the C<debugger>. User code
275 runs of course in the C<debugee>.
277 =back
279 =head1 TODO
281 =over 4
283 =item * Check the user input for bad commands, it's quite easy to make
284 it bang now, try listing the source before loading or disassembling it.
286 =item * Print the interpreter info.
288 =item * Make the user interface better (add comands
289 history/completion).
291 =item * Some other things I don't remember now because it's late.
294 =back
296 =cut
302 * Local variables:
303 * c-file-style: "parrot"
304 * End:
305 * vim: expandtab shiftwidth=4: