2 Copyright (C) 2001-2007, The Perl Foundation.
7 src/debug.c - Parrot debugging
11 This file implements Parrot debugging and is used by C<pdb>, the Parrot
12 debugger, and the C<debug> ops.
24 #include "parrot/parrot.h"
25 #include "interp_guts.h"
26 #include "parrot/oplib.h"
28 #include "parrot/debug.h"
29 #include "parrot/oplib/ops.h"
32 /* Not sure how we want to handle this sort of cross-project header */
35 IMCC_warning(PARROT_INTERP
, ARGIN(const char *fmt
), ...);
36 extern void imcc_init(PARROT_INTERP
);
40 /* HEADERIZER HFILE: include/parrot/debug.h */
42 /* HEADERIZER BEGIN: static */
44 static void dump_string(PARROT_INTERP
, ARGIN_NULLOK(const STRING
*s
))
45 __attribute__nonnull__(1);
47 static int GDB_B(PARROT_INTERP
, NOTNULL(char *s
))
48 __attribute__nonnull__(1)
49 __attribute__nonnull__(2);
51 PARROT_WARN_UNUSED_RESULT
52 PARROT_CANNOT_RETURN_NULL
53 static const char* GDB_P(PARROT_INTERP
, ARGIN(const char *s
))
54 __attribute__nonnull__(1)
55 __attribute__nonnull__(2);
57 PARROT_CAN_RETURN_NULL
58 PARROT_WARN_UNUSED_RESULT
59 static char const * nextarg(NOTNULL(char const *command
))
60 __attribute__nonnull__(1);
62 PARROT_CAN_RETURN_NULL
63 PARROT_WARN_UNUSED_RESULT
64 static const char * parse_command(
65 ARGIN(const char *command
),
66 NOTNULL(unsigned long *cmdP
))
67 __attribute__nonnull__(1)
68 __attribute__nonnull__(2);
70 PARROT_CANNOT_RETURN_NULL
71 PARROT_WARN_UNUSED_RESULT
72 static const char * parse_int(ARGIN(const char *str
), ARGOUT(int *intP
))
73 __attribute__nonnull__(1)
74 __attribute__nonnull__(2);
76 PARROT_CAN_RETURN_NULL
77 PARROT_WARN_UNUSED_RESULT
78 static const char* parse_key(PARROT_INTERP
,
79 ARGIN(const char *str
),
81 __attribute__nonnull__(1)
82 __attribute__nonnull__(2)
83 __attribute__nonnull__(3);
85 PARROT_CAN_RETURN_NULL
86 PARROT_WARN_UNUSED_RESULT
87 static const char * parse_string(PARROT_INTERP
,
88 ARGIN(const char *str
),
89 ARGOUT(STRING
**strP
))
90 __attribute__nonnull__(1)
91 __attribute__nonnull__(2)
92 __attribute__nonnull__(3);
94 PARROT_CANNOT_RETURN_NULL
95 static const char * skip_command(ARGIN(const char *str
))
96 __attribute__nonnull__(1);
98 PARROT_CANNOT_RETURN_NULL
99 PARROT_WARN_UNUSED_RESULT
100 static const char * skip_ws(ARGIN(const char *str
))
101 __attribute__nonnull__(1);
103 /* HEADERIZER END: static */
108 =item C<static char const * nextarg>
110 Returns the position just past the current argument in the PASM instruction
111 C<command>. This is not the same as C<skip_command()>, which is intended for
112 debugger commands. This function is used for C<eval>.
118 PARROT_CAN_RETURN_NULL
119 PARROT_WARN_UNUSED_RESULT
121 nextarg(NOTNULL(char const *command
))
123 /* as long as the character pointed to by command is not NULL,
124 * and it is either alphanumeric, a comma or a closing bracket,
125 * continue looking for the next argument.
127 while (*command
&& (isalnum((unsigned char) *command
) || *command
== ',' ||
131 /* eat as much space as possible */
132 while (*command
&& isspace((unsigned char) *command
))
140 =item C<static const char * skip_ws>
142 Returns the pointer past any whitespace.
148 PARROT_CANNOT_RETURN_NULL
149 PARROT_WARN_UNUSED_RESULT
151 skip_ws(ARGIN(const char *str
))
153 /* as long as str is not NULL and it contains space, skip it */
154 while (*str
&& isspace((unsigned char) *str
))
162 =item C<static const char * skip_command>
164 Returns the pointer past the current debugger command. (This is an
165 alternative to the C<skip_command()> macro above.)
171 PARROT_CANNOT_RETURN_NULL
173 skip_command(ARGIN(const char *str
))
175 /* while str is not null and it contains a command (no spaces),
178 while (*str
&& !isspace((unsigned char) *str
))
181 /* eat all space after that */
182 while (*str
&& isspace((unsigned char) *str
))
190 =item C<static const char * parse_int>
192 Parse an C<int> out of a string and return a pointer to just after the C<int>.
193 The output parameter C<intP> contains the parsed value.
199 PARROT_CANNOT_RETURN_NULL
200 PARROT_WARN_UNUSED_RESULT
202 parse_int(ARGIN(const char *str
), ARGOUT(int *intP
))
206 *intP
= strtol(str
, &end
, 0);
213 =item C<static const char * parse_string>
215 Parse a double-quoted string out of a C string and return a pointer to
216 just after the string. The parsed string is converted to a Parrot
217 C<STRING> and placed in the output parameter C<strP>.
223 PARROT_CAN_RETURN_NULL
224 PARROT_WARN_UNUSED_RESULT
226 parse_string(PARROT_INTERP
, ARGIN(const char *str
), ARGOUT(STRING
**strP
))
228 const char *string_start
;
230 /* if this is not a quoted string, there's nothing to parse */
239 /* parse while there's no closing quote */
240 while (*str
&& *str
!= '"') {
241 /* skip any potentially escaped quotes */
242 if (*str
== '\\' && str
[1])
248 /* create the output STRING */
249 *strP
= string_make(interp
, string_start
, str
- string_start
, NULL
, 0);
251 /* skip the closing quote */
260 =item C<static const char* parse_key>
262 Parse an aggregate key out of a string and return a pointer to just
263 after the key. Currently only string and integer keys are allowed.
269 PARROT_CAN_RETURN_NULL
270 PARROT_WARN_UNUSED_RESULT
272 parse_key(PARROT_INTERP
, ARGIN(const char *str
), ARGOUT(PMC
**keyP
))
274 /* clear output parameter */
277 /* make sure it's a key */
284 /* if this is a string key, create a Parrot STRING */
286 STRING
*parrot_string
;
287 str
= parse_string(interp
, str
, &parrot_string
);
288 *keyP
= key_new_string(interp
, parrot_string
);
290 /* if this is a numeric key */
291 else if (isdigit((unsigned char) *str
)) {
293 str
= parse_int(str
, &value
);
294 *keyP
= key_new_integer(interp
, (INTVAL
) value
);
296 /* unsupported case; neither a string nor a numeric key */
301 /* hm, but if this doesn't match, it's probably an error */
302 /* XXX str can be NULL from parse_string() */
306 /* skip the closing brace on the key */
312 =item C<static const char * parse_command>
314 Convert the command at the beginning of a string into a numeric value
315 that can be used as a switch key for fast lookup.
321 PARROT_CAN_RETURN_NULL
322 PARROT_WARN_UNUSED_RESULT
324 parse_command(ARGIN(const char *command
), NOTNULL(unsigned long *cmdP
))
329 if (*command
== '\0') {
334 for (i
= 0; *command
&& isalpha((unsigned char) *command
); command
++, i
++)
335 c
+= (tolower((unsigned char) *command
) + (i
+ 1)) * ((i
+ 1) * 255);
337 /* Nonempty and did not start with a letter */
339 c
= (unsigned long)-1;
348 =item C<void PDB_get_command>
350 Get a command from the user input to execute.
352 It saves the last command executed (in C<< pdb->last_command >>), so it
353 first frees the old one and updates it with the current one.
355 Also prints the next line to run if the program is still active.
357 The user input can't be longer than 255 characters.
359 The input is saved in C<< pdb->cur_command >>.
366 PDB_get_command(PARROT_INTERP
)
371 PDB_t
* const pdb
= interp
->pdb
;
373 /* flush the buffered data */
376 /* not used any more */
377 if (pdb
->last_command
&& *pdb
->cur_command
) {
378 mem_sys_free(pdb
->last_command
);
379 pdb
->last_command
= NULL
;
382 /* update the last command */
383 if (pdb
->cur_command
&& *pdb
->cur_command
)
384 pdb
->last_command
= pdb
->cur_command
;
386 /* if the program is stopped and running show the next line to run */
387 if ((pdb
->state
& PDB_STOPPED
) && (pdb
->state
& PDB_RUNNING
)) {
388 PDB_line_t
*line
= pdb
->file
->line
;
390 while (pdb
->cur_opcode
!= line
->opcode
)
393 PIO_eprintf(interp
, "%li ", line
->number
);
394 c
= pdb
->file
->source
+ line
->source_offset
;
396 while (c
&& (*c
!= '\n'))
397 PIO_eprintf(interp
, "%c", *(c
++));
402 /* RT#46109 who frees that */
403 /* need to allocate 256 chars as string is null-terminated i.e. 255 + 1*/
404 c
= (char *)mem_sys_allocate(256);
406 PIO_eprintf(interp
, "\n(pdb) ");
408 /* skip leading whitespace */
411 } while (isspace((unsigned char)ch
) && ch
!= '\n');
413 /* generate string (no more than 255 chars) */
414 while (ch
!= EOF
&& ch
!= '\n' && (i
< 255)) {
424 pdb
->cur_command
= c
;
429 =item C<void PDB_script_file>
431 Interprets the contents of a file as user input commands
438 PDB_script_file(PARROT_INTERP
, ARGIN(const char *command
))
441 const char *ptr
= (const char *)&buf
;
445 command
= nextarg(command
);
447 fd
= fopen(command
, "r");
449 IMCC_warning(interp
, "script_file: "
450 "Error reading script file %s.\n",
458 fgets(buf
, 1024, fd
);
461 for (ptr
=(char *)&buf
;*ptr
&&isspace((unsigned char)*ptr
);ptr
=ptr
+1);
463 /* avoid null blank and commented lines */
464 if (*buf
== '\0' || *buf
== '#')
467 buf
[strlen(buf
)-1]='\0';
468 /* RT#46117: handle command error and print out script line
469 * PDB_run_command should return non-void value?
470 * stop execution of script if fails
471 * RT#46115: avoid this verbose output? add -v flag? */
472 if (PDB_run_command(interp
, buf
)) {
473 IMCC_warning(interp
, "script_file: "
474 "Error interpreting command at line %d (%s).\n",
484 =item C<int PDB_run_command>
488 Hash the command to make a simple switch calling the correct handler.
494 PARROT_IGNORABLE_RESULT
496 PDB_run_command(PARROT_INTERP
, ARGIN(const char *command
))
499 PDB_t
* const pdb
= interp
->pdb
;
500 const char * const original_command
= command
;
502 /* keep a pointer to the command, in case we need to report an error */
504 /* get a number from what the user typed */
505 command
= parse_command(original_command
, &c
);
508 skip_command(command
);
514 PDB_script_file(interp
, command
);
517 PDB_disassemble(interp
, command
);
520 PDB_load_source(interp
, command
);
524 PDB_list(interp
, command
);
528 PDB_set_break(interp
, command
);
532 PDB_watchpoint(interp
, command
);
536 PDB_delete_breakpoint(interp
, command
);
539 PDB_disable_breakpoint(interp
, command
);
542 PDB_enable_breakpoint(interp
, command
);
546 PDB_init(interp
, command
);
547 PDB_continue(interp
, NULL
);
551 PDB_continue(interp
, command
);
555 PDB_print(interp
, command
);
559 PDB_next(interp
, command
);
563 PDB_trace(interp
, command
);
567 PDB_eval(interp
, command
);
574 PDB_help(interp
, command
);
578 pdb
->state
|= PDB_EXIT
;
581 if (pdb
->last_command
)
582 PDB_run_command(interp
, pdb
->last_command
);
586 "Undefined command: \"%s\". Try \"help\".", original_command
);
594 =item C<void PDB_next>
596 Execute the next N operation(s).
598 Inits the program if needed, runs the next N >= 1 operations and stops.
605 PDB_next(PARROT_INTERP
, ARGIN_NULLOK(const char *command
))
608 PDB_t
* const pdb
= interp
->pdb
;
610 /* Init the program if it's not running */
611 if (!(pdb
->state
& PDB_RUNNING
))
612 PDB_init(interp
, command
);
614 command
= nextarg(command
);
615 /* Get the number of operations to execute if any */
616 if (command
&& isdigit((unsigned char) *command
))
619 /* Erase the stopped flag */
620 pdb
->state
&= ~PDB_STOPPED
;
623 for (; n
&& pdb
->cur_opcode
; n
--)
624 DO_OP(pdb
->cur_opcode
, pdb
->debugee
);
626 /* Set the stopped flag */
627 pdb
->state
|= PDB_STOPPED
;
629 /* If program ended */
632 * RT#46119 this doesn't handle resume opcodes
634 if (!pdb
->cur_opcode
)
635 (void)PDB_program_end(interp
);
640 =item C<void PDB_trace>
642 Execute the next N operations; if no number is specified, it defaults to 1.
649 PDB_trace(PARROT_INTERP
, ARGIN_NULLOK(const char *command
))
652 PDB_t
* const pdb
= interp
->pdb
;
655 /* if debugger is not running yet, initialize */
656 if (!(pdb
->state
& PDB_RUNNING
))
657 PDB_init(interp
, command
);
659 command
= nextarg(command
);
660 /* if the number of ops to run is specified, convert to a long */
661 if (command
&& isdigit((unsigned char) *command
))
664 /* clear the PDB_STOPPED flag, we'll be running n ops now */
665 pdb
->state
&= ~PDB_STOPPED
;
666 debugee
= pdb
->debugee
;
669 for (; n
&& pdb
->cur_opcode
; n
--) {
671 debugee
->code
->base
.data
,
672 debugee
->code
->base
.data
+
673 debugee
->code
->base
.size
,
674 debugee
->pdb
->cur_opcode
);
675 DO_OP(pdb
->cur_opcode
, debugee
);
678 /* we just stopped */
679 pdb
->state
|= PDB_STOPPED
;
681 /* If program ended */
682 if (!pdb
->cur_opcode
)
683 (void)PDB_program_end(interp
);
688 =item C<PDB_condition_t * PDB_cond>
690 Analyzes a condition from the user input.
696 PARROT_CAN_RETURN_NULL
698 PDB_cond(PARROT_INTERP
, ARGIN(const char *command
))
700 PDB_condition_t
*condition
;
704 /* Return if no more arguments */
705 if (!(command
&& *command
)) {
706 PIO_eprintf(interp
, "No condition specified\n");
710 /* Allocate new condition */
711 condition
= mem_allocate_typed(PDB_condition_t
);
716 condition
->type
= PDB_cond_int
;
720 condition
->type
= PDB_cond_num
;
724 condition
->type
= PDB_cond_str
;
728 condition
->type
= PDB_cond_pmc
;
731 PIO_eprintf(interp
, "First argument must be a register\n");
732 mem_sys_free(condition
);
736 /* get the register number */
737 condition
->reg
= (unsigned char)atoi(++command
);
739 /* the next argument might have no spaces between the register and the
743 /* RT#46121 Does /this/ have to do with the fact that PASM registers used to have
744 * maximum of 2 digits? If so, there should be a while loop, I think.
746 if (condition
->reg
> 9)
750 skip_command(command
);
752 /* Now the condition */
755 if (*(command
+ 1) == '=')
756 condition
->type
|= PDB_cond_ge
;
757 else if (*(command
+ 1) == ' ')
758 condition
->type
|= PDB_cond_gt
;
763 if (*(command
+ 1) == '=')
764 condition
->type
|= PDB_cond_le
;
765 else if (*(command
+ 1) == ' ')
766 condition
->type
|= PDB_cond_lt
;
771 if (*(command
+ 1) == '=')
772 condition
->type
|= PDB_cond_eq
;
777 if (*(command
+ 1) == '=')
778 condition
->type
|= PDB_cond_ne
;
783 INV_COND
: PIO_eprintf(interp
, "Invalid condition\n");
784 mem_sys_free(condition
);
788 /* if there's an '=', skip it */
789 if (*(command
+ 1) == '=')
795 skip_command(command
);
797 /* return if no more arguments */
798 if (!(command
&& *command
)) {
799 PIO_eprintf(interp
, "Can't compare a register with nothing\n");
800 mem_sys_free(condition
);
804 if (isalpha((unsigned char)*command
)) {
805 /* It's a register - we first check that it's the correct type */
809 if (!(condition
->type
& PDB_cond_int
))
814 if (!(condition
->type
& PDB_cond_num
))
819 if (!(condition
->type
& PDB_cond_str
))
824 if (!(condition
->type
& PDB_cond_pmc
))
828 WRONG_REG
: PIO_eprintf(interp
, "Register types don't agree\n");
829 mem_sys_free(condition
);
833 /* Now we check and store the register number */
834 reg_number
= (int)atoi(++command
);
836 if (reg_number
< 0) {
837 PIO_eprintf(interp
, "Out-of-bounds register\n");
838 mem_sys_free(condition
);
842 condition
->value
= mem_allocate_typed(int);
843 *(int *)condition
->value
= reg_number
;
845 /* If the first argument was an integer */
846 else if (condition
->type
& PDB_cond_int
) {
847 /* This must be either an integer constant or register */
848 condition
->value
= mem_allocate_typed(INTVAL
);
849 *(INTVAL
*)condition
->value
= (INTVAL
)atoi(command
);
850 condition
->type
|= PDB_cond_const
;
852 else if (condition
->type
& PDB_cond_num
) {
853 condition
->value
= mem_allocate_typed(FLOATVAL
);
854 *(FLOATVAL
*)condition
->value
= (FLOATVAL
)atof(command
);
855 condition
->type
|= PDB_cond_const
;
857 else if (condition
->type
& PDB_cond_str
) {
858 for (i
= 1; ((command
[i
] != '"') && (i
< 255)); i
++)
859 str
[i
- 1] = command
[i
];
861 condition
->value
= string_make(interp
,
862 str
, i
- 1, NULL
, PObj_external_FLAG
);
863 condition
->type
|= PDB_cond_const
;
865 else if (condition
->type
& PDB_cond_pmc
) {
866 /* RT#46123 Need to figure out what to do in this case.
867 * For the time being, we just bail. */
868 PIO_eprintf(interp
, "Can't compare PMC with constant\n");
869 mem_sys_free(condition
);
873 /* We're not part of a list yet */
874 condition
->next
= NULL
;
881 =item C<void PDB_watchpoint>
890 PDB_watchpoint(PARROT_INTERP
, ARGIN(const char *command
))
892 PDB_t
* const pdb
= interp
->pdb
;
893 PDB_condition_t
* const condition
= PDB_cond(interp
, command
);
898 /* Add it to the head of the list */
900 condition
->next
= pdb
->watchpoint
;
902 pdb
->watchpoint
= condition
;
907 =item C<void PDB_set_break>
909 Set a break point, the source code file must be loaded.
916 PDB_set_break(PARROT_INTERP
, ARGIN(const char *command
))
918 PDB_t
* const pdb
= interp
->pdb
;
919 PDB_breakpoint_t
*newbreak
= NULL
;
920 PDB_breakpoint_t
*sbreak
;
921 PDB_condition_t
*condition
;
925 command
= nextarg(command
);
926 /* If no line number was specified, set it at the current line */
927 if (command
&& *command
) {
928 const long ln
= atol(command
);
930 /* Move to the line where we will set the break point */
931 line
= pdb
->file
->line
;
933 for (i
= 1; ((i
< ln
) && (line
->next
)); i
++)
936 /* Abort if the line number provided doesn't exist */
939 "Can't set a breakpoint at line number %li\n", ln
);
944 /* Get the line to set it */
945 line
= pdb
->file
->line
;
947 while (line
->opcode
!= pdb
->cur_opcode
) {
951 "No current line found and no line number specified\n");
957 /* Skip lines that are not related to an opcode */
958 while (!line
->opcode
)
961 /* Allocate the new break point */
962 newbreak
= mem_allocate_typed(PDB_breakpoint_t
);
965 skip_command(command
);
968 real_exception(interp
, NULL
, 1, "NULL command passed to PDB_set_break");
972 /* if there is another argument to break, besides the line number,
973 * it should be an 'if', so we call another handler. */
974 if (command
&& *command
) {
975 skip_command(command
);
976 if ((condition
= PDB_cond(interp
, command
)))
977 newbreak
->condition
= condition
;
980 /* If there are no other arguments, or if there isn't a valid condition,
981 then condition will be NULL */
983 newbreak
->condition
= NULL
;
985 /* Set the address where to stop */
986 newbreak
->pc
= line
->opcode
;
988 /* No next breakpoint */
989 newbreak
->next
= NULL
;
991 /* Don't skip (at least initially) */
994 /* Add the breakpoint to the end of the list */
996 sbreak
= pdb
->breakpoint
;
1000 sbreak
= sbreak
->next
;
1002 newbreak
->prev
= sbreak
;
1003 sbreak
->next
= newbreak
;
1004 i
= sbreak
->next
->id
= sbreak
->id
+ 1;
1007 newbreak
->prev
= NULL
;
1008 pdb
->breakpoint
= newbreak
;
1009 i
= pdb
->breakpoint
->id
= 0;
1012 PIO_eprintf(interp
, "Breakpoint %li at line %li\n", i
, line
->number
);
1017 =item C<void PDB_init>
1026 PDB_init(PARROT_INTERP
, SHIM(const char *command
))
1028 PDB_t
* const pdb
= interp
->pdb
;
1030 /* Restart if we are already running */
1031 if (pdb
->state
& PDB_RUNNING
)
1032 PIO_eprintf(interp
, "Restarting\n");
1034 /* Add the RUNNING state */
1035 pdb
->state
|= PDB_RUNNING
;
1040 =item C<void PDB_continue>
1042 Continue running the program. If a number is specified, skip that many
1050 PDB_continue(PARROT_INTERP
, ARGIN_NULLOK(const char *command
))
1052 PDB_t
*pdb
= interp
->pdb
;
1054 /* Skip any breakpoint? */
1055 if (command
&& *command
) {
1057 if (!pdb
->breakpoint
) {
1058 PIO_eprintf(interp
, "No breakpoints to skip\n");
1062 command
= nextarg(command
);
1064 PDB_skip_breakpoint(interp
, ln
);
1067 /* Run while no break point is reached */
1068 while (!PDB_break(interp
))
1069 DO_OP(pdb
->cur_opcode
, pdb
->debugee
);
1074 =item C<PDB_breakpoint_t * PDB_find_breakpoint>
1076 Find breakpoint number N; returns C<NULL> if the breakpoint doesn't
1077 exist or if no breakpoint was specified.
1083 PARROT_CAN_RETURN_NULL
1084 PARROT_WARN_UNUSED_RESULT
1086 PDB_find_breakpoint(PARROT_INTERP
, ARGIN(const char *command
))
1088 command
= nextarg(command
);
1089 if (isdigit((unsigned char) *command
)) {
1090 const long n
= atol(command
);
1091 PDB_breakpoint_t
*breakpoint
= interp
->pdb
->breakpoint
;
1093 while (breakpoint
&& breakpoint
->id
!= n
)
1094 breakpoint
= breakpoint
->next
;
1097 PIO_eprintf(interp
, "No breakpoint number %ld", n
);
1104 /* Report an appropriate error */
1106 PIO_eprintf(interp
, "Not a valid breakpoint");
1108 PIO_eprintf(interp
, "No breakpoint specified");
1116 =item C<void PDB_disable_breakpoint>
1118 Disable a breakpoint; it can be reenabled with the enable command.
1125 PDB_disable_breakpoint(PARROT_INTERP
, ARGIN(const char *command
))
1127 PDB_breakpoint_t
* const breakpoint
= PDB_find_breakpoint(interp
, command
);
1129 /* if the breakpoint exists, disable it. */
1131 breakpoint
->skip
= -1;
1136 =item C<void PDB_enable_breakpoint>
1138 Reenable a disabled breakpoint; if the breakpoint was not disabled, has
1146 PDB_enable_breakpoint(PARROT_INTERP
, ARGIN(const char *command
))
1148 PDB_breakpoint_t
* const breakpoint
= PDB_find_breakpoint(interp
, command
);
1150 /* if the breakpoint exists, and it was disabled, enable it. */
1151 if (breakpoint
&& breakpoint
->skip
== -1)
1152 breakpoint
->skip
= 0;
1157 =item C<void PDB_delete_breakpoint>
1159 Delete a breakpoint.
1166 PDB_delete_breakpoint(PARROT_INTERP
, ARGIN(const char *command
))
1168 PDB_breakpoint_t
* const breakpoint
= PDB_find_breakpoint(interp
, command
);
1171 PDB_line_t
*line
= interp
->pdb
->file
->line
;
1173 while (line
->opcode
!= breakpoint
->pc
)
1176 /* Delete the condition structure, if there is one */
1177 if (breakpoint
->condition
) {
1178 PDB_delete_condition(interp
, breakpoint
);
1179 breakpoint
->condition
= NULL
;
1182 /* Remove the breakpoint from the list */
1183 if (breakpoint
->prev
&& breakpoint
->next
) {
1184 breakpoint
->prev
->next
= breakpoint
->next
;
1185 breakpoint
->next
->prev
= breakpoint
->prev
;
1187 else if (breakpoint
->prev
&& !breakpoint
->next
) {
1188 breakpoint
->prev
->next
= NULL
;
1190 else if (!breakpoint
->prev
&& breakpoint
->next
) {
1191 breakpoint
->next
->prev
= NULL
;
1192 interp
->pdb
->breakpoint
= breakpoint
->next
;
1195 interp
->pdb
->breakpoint
= NULL
;
1198 /* Kill the breakpoint */
1199 mem_sys_free(breakpoint
);
1205 =item C<void PDB_delete_condition>
1207 Delete a condition associated with a breakpoint.
1214 PDB_delete_condition(SHIM_INTERP
, NOTNULL(PDB_breakpoint_t
*breakpoint
))
1216 if (breakpoint
->condition
->value
) {
1217 if (breakpoint
->condition
->type
& PDB_cond_str
) {
1218 /* 'value' is a string, so we need to be careful */
1219 PObj_external_CLEAR((STRING
*)breakpoint
->condition
->value
);
1220 PObj_on_free_list_SET((STRING
*)breakpoint
->condition
->value
);
1221 /* it should now be properly garbage collected after
1222 we destroy the condition */
1225 /* 'value' is a float or an int, so we can just free it */
1226 mem_sys_free(breakpoint
->condition
->value
);
1227 breakpoint
->condition
->value
= NULL
;
1231 mem_sys_free(breakpoint
->condition
);
1232 breakpoint
->condition
= NULL
;
1237 =item C<void PDB_skip_breakpoint>
1239 Skip C<i> times all breakpoints.
1246 PDB_skip_breakpoint(PARROT_INTERP
, long i
)
1248 interp
->pdb
->breakpoint_skip
= i
? i
-1 : i
;
1253 =item C<char PDB_program_end>
1262 PDB_program_end(PARROT_INTERP
)
1264 PDB_t
* const pdb
= interp
->pdb
;
1266 /* Remove the RUNNING state */
1267 pdb
->state
&= ~PDB_RUNNING
;
1269 PIO_eprintf(interp
, "Program exited.\n");
1275 =item C<char PDB_check_condition>
1277 Returns true if the condition was met.
1283 PARROT_WARN_UNUSED_RESULT
1285 PDB_check_condition(PARROT_INTERP
, NOTNULL(PDB_condition_t
*condition
))
1287 if (condition
->type
& PDB_cond_int
) {
1290 * RT#46125 verify register is in range
1292 i
= REG_INT(interp
, condition
->reg
);
1294 if (condition
->type
& PDB_cond_const
)
1295 j
= *(INTVAL
*)condition
->value
;
1297 j
= REG_INT(interp
, *(int *)condition
->value
);
1299 if (((condition
->type
& PDB_cond_gt
) && (i
> j
)) ||
1300 ((condition
->type
& PDB_cond_ge
) && (i
>= j
)) ||
1301 ((condition
->type
& PDB_cond_eq
) && (i
== j
)) ||
1302 ((condition
->type
& PDB_cond_ne
) && (i
!= j
)) ||
1303 ((condition
->type
& PDB_cond_le
) && (i
<= j
)) ||
1304 ((condition
->type
& PDB_cond_lt
) && (i
< j
)))
1309 else if (condition
->type
& PDB_cond_num
) {
1312 k
= REG_NUM(interp
, condition
->reg
);
1314 if (condition
->type
& PDB_cond_const
)
1315 l
= *(FLOATVAL
*)condition
->value
;
1317 l
= REG_NUM(interp
, *(int *)condition
->value
);
1319 if (((condition
->type
& PDB_cond_gt
) && (k
> l
)) ||
1320 ((condition
->type
& PDB_cond_ge
) && (k
>= l
)) ||
1321 ((condition
->type
& PDB_cond_eq
) && (k
== l
)) ||
1322 ((condition
->type
& PDB_cond_ne
) && (k
!= l
)) ||
1323 ((condition
->type
& PDB_cond_le
) && (k
<= l
)) ||
1324 ((condition
->type
& PDB_cond_lt
) && (k
< l
)))
1329 else if (condition
->type
& PDB_cond_str
) {
1332 m
= REG_STR(interp
, condition
->reg
);
1334 if (condition
->type
& PDB_cond_const
)
1335 n
= (STRING
*)condition
->value
;
1337 n
= REG_STR(interp
, *(int *)condition
->value
);
1339 if (((condition
->type
& PDB_cond_gt
) &&
1340 (string_compare(interp
, m
, n
) > 0)) ||
1341 ((condition
->type
& PDB_cond_ge
) &&
1342 (string_compare(interp
, m
, n
) >= 0)) ||
1343 ((condition
->type
& PDB_cond_eq
) &&
1344 (string_compare(interp
, m
, n
) == 0)) ||
1345 ((condition
->type
& PDB_cond_ne
) &&
1346 (string_compare(interp
, m
, n
) != 0)) ||
1347 ((condition
->type
& PDB_cond_le
) &&
1348 (string_compare(interp
, m
, n
) <= 0)) ||
1349 ((condition
->type
& PDB_cond_lt
) &&
1350 (string_compare(interp
, m
, n
) < 0)))
1361 =item C<char PDB_break>
1363 Returns true if we have to stop running.
1369 PARROT_WARN_UNUSED_RESULT
1371 PDB_break(PARROT_INTERP
)
1373 PDB_t
* const pdb
= interp
->pdb
;
1374 PDB_breakpoint_t
*breakpoint
= pdb
->breakpoint
;
1375 PDB_condition_t
*watchpoint
= pdb
->watchpoint
;
1377 /* Check the watchpoints first. */
1378 while (watchpoint
) {
1379 if (PDB_check_condition(interp
, watchpoint
)) {
1380 pdb
->state
|= PDB_STOPPED
;
1384 watchpoint
= watchpoint
->next
;
1387 /* If program ended */
1388 if (!pdb
->cur_opcode
)
1389 return PDB_program_end(interp
);
1391 /* If the program is STOPPED allow it to continue */
1392 if (pdb
->state
& PDB_STOPPED
) {
1393 pdb
->state
&= ~PDB_STOPPED
;
1397 /* If we have to skip breakpoints, do so. */
1398 if (pdb
->breakpoint_skip
) {
1399 pdb
->breakpoint_skip
--;
1403 while (breakpoint
) {
1404 /* if we are in a break point */
1405 if (pdb
->cur_opcode
== breakpoint
->pc
) {
1406 if (breakpoint
->skip
< 0)
1409 /* Check if there is a condition for this breakpoint */
1410 if ((breakpoint
->condition
) &&
1411 (!PDB_check_condition(interp
, breakpoint
->condition
)))
1414 /* Add the STOPPED state and stop */
1415 pdb
->state
|= PDB_STOPPED
;
1418 breakpoint
= breakpoint
->next
;
1426 =item C<char * PDB_escape>
1428 Escapes C<">, C<\r>, C<\n>, C<\t>, C<\a> and C<\\>.
1434 PARROT_WARN_UNUSED_RESULT
1435 PARROT_CAN_RETURN_NULL
1437 PDB_escape(ARGIN(const char *string
), INTVAL length
)
1442 length
= length
> 20 ? 20 : length
;
1443 end
= string
+ length
;
1445 /* Return if there is no string to escape*/
1449 fill
= _new
= (char *)mem_sys_allocate(length
* 2 + 1);
1451 for (; string
< end
; string
++) {
1482 *(fill
++) = *string
;
1494 =item C<int PDB_unescape>
1496 Do inplace unescape of C<\r>, C<\n>, C<\t>, C<\a> and C<\\>.
1503 PDB_unescape(NOTNULL(char *string
))
1507 for (; *string
; string
++) {
1510 if (*string
== '\\') {
1514 switch (string
[1]) {
1536 for (i
= 1; fill
[i
+ 1]; i
++)
1537 fill
[i
] = fill
[i
+ 1];
1548 =item C<size_t PDB_disassemble_op>
1557 PDB_disassemble_op(PARROT_INTERP
, ARGOUT(char *dest
), int space
,
1558 NOTNULL(op_info_t
*info
), NOTNULL(opcode_t
*op
),
1559 NULLOK(PDB_file_t
*file
), NULLOK(opcode_t
*code_start
), int full_name
)
1564 /* Write the opcode name */
1565 const char * const p
= full_name
? info
->full_name
: info
->name
;
1571 /* Concat the arguments */
1572 for (j
= 1; j
< info
->op_count
; j
++) {
1578 PARROT_ASSERT(size
+ 2 < space
);
1580 switch (info
->types
[j
-1]) {
1594 /* If the opcode jumps and this is the last argument,
1595 that means this is a label */
1596 if ((j
== info
->op_count
- 1) &&
1597 (info
->jump
& PARROT_JUMP_RELATIVE
)) {
1600 i
= PDB_add_label(file
, op
, op
[j
]);
1602 else if (code_start
) {
1605 i
= op
[j
] + (op
- code_start
);
1614 /* Convert the integer to a string */
1619 PARROT_ASSERT(size
+ 20 < space
);
1621 size
+= sprintf(&dest
[size
], INTVAL_FMT
, i
);
1623 /* If this is a constant dispatch arg to an "infix" op, then show
1624 the corresponding symbolic op name. */
1625 if (j
== 1 && info
->types
[j
-1] == PARROT_ARG_IC
1626 && (strcmp(info
->name
, "infix") == 0
1627 || strcmp(info
->name
, "n_infix") == 0)) {
1628 PARROT_ASSERT(size
+ 20 < space
);
1630 size
+= sprintf(&dest
[size
], " [%s]",
1631 /* [kludge: the "2+" skips the leading underscores. --
1633 2 + Parrot_MMD_method_name(interp
, op
[j
]));
1637 /* Convert the float to a string */
1638 f
= interp
->code
->const_table
->constants
[op
[j
]]->u
.number
;
1639 Parrot_snprintf(interp
, buf
, sizeof (buf
), FLOATVAL_FMT
, f
);
1640 strcpy(&dest
[size
], buf
);
1641 size
+= strlen(buf
);
1645 if (interp
->code
->const_table
->constants
[op
[j
]]->
1648 char * const escaped
=
1649 PDB_escape(interp
->code
->const_table
->
1650 constants
[op
[j
]]->u
.string
->strstart
,
1651 interp
->code
->const_table
->
1652 constants
[op
[j
]]->u
.string
->strlen
);
1654 strcpy(&dest
[size
], escaped
);
1655 size
+= strlen(escaped
);
1656 mem_sys_free(escaped
);
1662 Parrot_snprintf(interp
, buf
, sizeof (buf
), "PMC_CONST(%d)", op
[j
]);
1663 strcpy(&dest
[size
], buf
);
1664 size
+= strlen(buf
);
1667 dest
[size
-1] = '['; Parrot_snprintf(interp
, buf
, sizeof (buf
),
1668 "P" INTVAL_FMT
, op
[j
]);
1669 strcpy(&dest
[size
], buf
);
1670 size
+= strlen(buf
);
1675 k
= interp
->code
->const_table
->constants
[op
[j
]]->u
.key
;
1677 switch (PObj_get_FLAGS(k
)) {
1680 case KEY_integer_FLAG
:
1681 Parrot_snprintf(interp
, buf
, sizeof (buf
),
1682 INTVAL_FMT
, PMC_int_val(k
));
1683 strcpy(&dest
[size
], buf
);
1684 size
+= strlen(buf
);
1686 case KEY_number_FLAG
:
1687 Parrot_snprintf(interp
, buf
, sizeof (buf
),
1688 FLOATVAL_FMT
, PMC_num_val(k
));
1689 strcpy(&dest
[size
], buf
);
1690 size
+= strlen(buf
);
1692 case KEY_string_FLAG
:
1696 temp
= string_to_cstring(interp
, PMC_str_val(k
));
1697 strcpy(&dest
[size
], temp
);
1698 string_cstring_free(temp
);
1700 size
+= string_length(interp
, PMC_str_val(k
));
1703 case KEY_integer_FLAG
|KEY_register_FLAG
:
1704 Parrot_snprintf(interp
, buf
, sizeof (buf
),
1705 "I" INTVAL_FMT
, PMC_int_val(k
));
1706 strcpy(&dest
[size
], buf
);
1707 size
+= strlen(buf
);
1709 case KEY_number_FLAG
|KEY_register_FLAG
:
1710 Parrot_snprintf(interp
, buf
, sizeof (buf
),
1711 "N" INTVAL_FMT
, PMC_int_val(k
));
1712 strcpy(&dest
[size
], buf
);
1713 size
+= strlen(buf
);
1715 case KEY_string_FLAG
|KEY_register_FLAG
:
1716 Parrot_snprintf(interp
, buf
, sizeof (buf
),
1717 "S" INTVAL_FMT
, PMC_int_val(k
));
1718 strcpy(&dest
[size
], buf
);
1719 size
+= strlen(buf
);
1721 case KEY_pmc_FLAG
|KEY_register_FLAG
:
1722 Parrot_snprintf(interp
, buf
, sizeof (buf
),
1723 "P" INTVAL_FMT
, PMC_int_val(k
));
1724 strcpy(&dest
[size
], buf
);
1725 size
+= strlen(buf
);
1731 k
= PMC_data_typed(k
, PMC
*);
1738 dest
[size
- 1] = '[';
1739 Parrot_snprintf(interp
, buf
, sizeof (buf
), "I" INTVAL_FMT
, op
[j
]);
1740 strcpy(&dest
[size
], buf
);
1741 size
+= strlen(buf
);
1744 case PARROT_ARG_KIC
:
1745 dest
[size
- 1] = '[';
1746 Parrot_snprintf(interp
, buf
, sizeof (buf
), INTVAL_FMT
, op
[j
]);
1747 strcpy(&dest
[size
], buf
);
1748 size
+= strlen(buf
);
1752 real_exception(interp
, NULL
, 1, "Unknown opcode type");
1755 if (j
!= info
->op_count
- 1)
1759 /* Special decoding for the signature used in args/returns. Such ops have
1760 one fixed parameter (the signature vector), plus a varying number of
1761 registers/constants. For each arg/return, we show the register and its
1762 flags using PIR syntax. */
1763 if (*(op
) == PARROT_OP_set_args_pc
||
1764 *(op
) == PARROT_OP_get_results_pc
||
1765 *(op
) == PARROT_OP_get_params_pc
||
1766 *(op
) == PARROT_OP_set_returns_pc
) {
1768 PMC
* const sig
= interp
->code
->const_table
->constants
[op
[1]]->u
.key
;
1769 int n_values
= SIG_ELEMS(sig
);
1770 /* The flag_names strings come from Call_bits_enum_t (with which it
1771 should probably be colocated); they name the bits from LSB to MSB.
1772 The two least significant bits are not flags; they are the register
1773 type, which is decoded elsewhere. We also want to show unused bits,
1774 which could indicate problems.
1776 const char *flag_names
[] = { "",
1781 " :flat", /* should be :slurpy for args */
1788 /* Register decoding. It would be good to abstract this, too. */
1789 const char *regs
= "ISPN";
1791 for (j
= 0; j
< n_values
; j
++) {
1792 unsigned int idx
= 0;
1793 int sig_value
= VTABLE_get_integer_keyed_int(interp
, sig
, j
);
1795 /* Print the register name, e.g. P37. */
1798 buf
[idx
++] = regs
[sig_value
& PARROT_ARG_TYPE_MASK
];
1799 Parrot_snprintf(interp
, &buf
[idx
], sizeof(buf
)-idx
,
1800 INTVAL_FMT
, op
[j
+2]);
1803 /* Add flags, if we have any. */
1807 int flags
= sig_value
;
1809 /* End when we run out of flags, off the end of flag_names, or
1810 get too close to the end of buf. */
1811 while (flags
&& idx
< sizeof(buf
)-100) {
1812 const char *flag_string
= flag_names
[flag_idx
];
1815 if (flags
& 1 && *flag_string
) {
1816 int n
= strlen(flag_string
);
1817 strcpy(&buf
[idx
], flag_string
);
1825 /* Add it to dest. */
1827 strcpy(&dest
[size
], buf
);
1828 size
+= strlen(buf
);
1838 =item C<void PDB_disassemble>
1840 Disassemble the bytecode.
1847 PDB_disassemble(PARROT_INTERP
, SHIM(const char *command
))
1849 PDB_t
*pdb
= interp
->pdb
;
1851 PDB_line_t
*pline
, *newline
;
1854 opcode_t
*pc
= interp
->code
->base
.data
;
1856 const unsigned int default_size
= 32768;
1857 size_t space
; /* How much space do we have? */
1858 size_t size
, alloced
, n
;
1860 pfile
= mem_allocate_typed(PDB_file_t
);
1861 pline
= mem_allocate_typed(PDB_line_t
);
1863 /* If we already got a source, free it */
1865 PDB_free_file(interp
);
1868 pline
->label
= NULL
;
1869 pfile
->line
= pline
;
1870 pfile
->label
= NULL
;
1872 pfile
->source
= (char *)mem_sys_allocate(default_size
);
1873 pline
->source_offset
= 0;
1875 alloced
= space
= default_size
;
1876 code_end
= pc
+ interp
->code
->base
.size
;
1878 while (pc
!= code_end
) {
1880 if (space
< default_size
) {
1881 alloced
+= default_size
;
1882 space
+= default_size
;
1883 pfile
->source
= (char *)mem_sys_realloc(pfile
->source
, alloced
);
1886 size
= PDB_disassemble_op(interp
, pfile
->source
+ pfile
->size
,
1887 space
, &interp
->op_info_table
[*pc
], pc
, pfile
, NULL
, 1);
1889 pfile
->size
+= size
;
1890 pfile
->source
[pfile
->size
- 1] = '\n';
1892 /* Store the opcode of this line */
1894 n
= interp
->op_info_table
[*pc
].op_count
;
1896 ADD_OP_VAR_PART(interp
, interp
->code
, pc
, n
);
1899 /* Prepare for next line */
1900 newline
= mem_allocate_typed(PDB_line_t
);
1901 newline
->label
= NULL
;
1902 newline
->next
= NULL
;
1903 newline
->number
= pline
->number
+ 1;
1904 pline
->next
= newline
;
1906 pline
->source_offset
= pfile
->size
;
1909 /* Add labels to the lines they belong to */
1910 label
= pfile
->label
;
1913 /* Get the line to apply the label */
1914 pline
= pfile
->line
;
1916 while (pline
&& pline
->opcode
!= label
->opcode
)
1917 pline
= pline
->next
;
1921 "Label number %li out of bounds.\n", label
->number
);
1922 /* RT#46127: free allocated memory */
1926 pline
->label
= label
;
1928 label
= label
->next
;
1931 pdb
->state
|= PDB_SRC_LOADED
;
1937 =item C<long PDB_add_label>
1939 Add a label to the label list.
1946 PDB_add_label(NOTNULL(PDB_file_t
*file
), NOTNULL(opcode_t
*cur_opcode
), opcode_t offset
)
1949 PDB_label_t
*label
= file
->label
;
1951 /* See if there is already a label at this line */
1953 if (label
->opcode
== cur_opcode
+ offset
)
1954 return label
->number
;
1955 label
= label
->next
;
1958 /* Allocate a new label */
1959 label
= file
->label
;
1960 _new
= mem_allocate_typed(PDB_label_t
);
1961 _new
->opcode
= cur_opcode
+ offset
;
1966 label
= label
->next
;
1968 _new
->number
= label
->number
+ 1;
1976 return _new
->number
;
1981 =item C<void PDB_free_file>
1983 Frees any allocated source files.
1990 PDB_free_file(PARROT_INTERP
)
1992 PDB_file_t
*file
= interp
->pdb
->file
;
1995 /* Free all of the allocated line structures */
1996 PDB_line_t
*line
= file
->line
;
2001 PDB_line_t
* const nline
= line
->next
;
2006 /* Free all of the allocated label structures */
2007 label
= file
->label
;
2010 PDB_label_t
* const nlabel
= label
->next
;
2012 mem_sys_free(label
);
2016 /* Free the remaining allocated portions of the file structure */
2017 if (file
->sourcefilename
)
2018 mem_sys_free(file
->sourcefilename
);
2021 mem_sys_free(file
->source
);
2028 /* Make sure we don't end up pointing at garbage memory */
2029 interp
->pdb
->file
= NULL
;
2034 =item C<void PDB_load_source>
2036 Load a source code file.
2043 PDB_load_source(PARROT_INTERP
, ARGIN(const char *command
))
2049 PDB_line_t
*pline
, *newline
;
2050 PDB_t
*pdb
= interp
->pdb
;
2051 opcode_t
*pc
= pdb
->cur_opcode
;
2052 unsigned long size
= 0;
2054 /* If there was a file already loaded or the bytecode was
2055 disassembled, free it */
2057 PDB_free_file(interp
);
2059 /* Get the name of the file */
2060 for (i
= 0; command
[i
]; i
++)
2066 file
= fopen(f
, "r");
2068 /* abort if fopen failed */
2070 PIO_eprintf(interp
, "Unable to load %s\n", f
);
2074 pfile
= mem_allocate_zeroed_typed(PDB_file_t
);
2075 pline
= mem_allocate_zeroed_typed(PDB_line_t
);
2077 pfile
->source
= (char *)mem_sys_allocate(1024);
2078 pfile
->line
= pline
;
2081 while ((c
= fgetc(file
)) != EOF
) {
2083 if (++size
== 1024) {
2084 pfile
->source
= (char *)mem_sys_realloc(pfile
->source
,
2085 (size_t)pfile
->size
+ 1024);
2088 pfile
->source
[pfile
->size
] = (char)c
;
2093 /* If the line has an opcode move to the next one,
2094 otherwise leave it with NULL to skip it. */
2095 if (PDB_hasinstruction(pfile
->source
+ pline
->source_offset
)) {
2098 n
= interp
->op_info_table
[*pc
].op_count
;
2099 ADD_OP_VAR_PART(interp
, interp
->code
, pc
, n
);
2102 newline
= mem_allocate_zeroed_typed(PDB_line_t
);
2103 newline
->number
= pline
->number
+ 1;
2104 pline
->next
= newline
;
2106 pline
->source_offset
= pfile
->size
;
2107 pline
->opcode
= NULL
;
2108 pline
->label
= NULL
;
2112 pdb
->state
|= PDB_SRC_LOADED
;
2118 =item C<char PDB_hasinstruction>
2120 Return true if the line has an instruction.
2126 =item * This should take the line, get an instruction, get the opcode for
2127 that instruction and check that is the correct one.
2129 =item * Decide what to do with macros if anything.
2137 PARROT_WARN_UNUSED_RESULT
2138 PARROT_PURE_FUNCTION
2140 PDB_hasinstruction(ARGIN(const char *c
))
2144 /* as long as c is not NULL, we're not looking at a comment (#...) or a '\n'... */
2145 while (*c
&& *c
!= '#' && *c
!= '\n') {
2146 /* ... and c is alphanumeric or a quoted string then the line contains
2147 * an instruction. */
2148 if (isalnum((unsigned char) *c
) || *c
== '"') {
2151 else if (*c
== ':') {
2152 /* this is a label. RT#46137 right? */
2164 =item C<void PDB_list>
2166 Show lines from the source code file.
2173 PDB_list(PARROT_INTERP
, ARGIN(const char *command
))
2179 PDB_t
*pdb
= interp
->pdb
;
2180 unsigned long n
= 10;
2183 PIO_eprintf(interp
, "No source file loaded\n");
2187 command
= nextarg(command
);
2188 /* set the list line if provided */
2189 if (isdigit((unsigned char) *command
)) {
2190 line_number
= atol(command
) - 1;
2191 if (line_number
< 0)
2192 pdb
->file
->list_line
= 0;
2194 pdb
->file
->list_line
= (unsigned long) line_number
;
2196 skip_command(command
);
2199 pdb
->file
->list_line
= 0;
2202 /* set the number of lines to print */
2203 if (isdigit((unsigned char) *command
)) {
2205 skip_command(command
);
2208 /* if n is zero, we simply return, as we don't have to print anything */
2212 line
= pdb
->file
->line
;
2214 for (i
= 0; i
< pdb
->file
->list_line
&& line
->next
; i
++)
2218 while (line
->next
) {
2219 PIO_eprintf(interp
, "%li ", pdb
->file
->list_line
+ i
);
2220 /* If it has a label print it */
2222 PIO_eprintf(interp
, "L%li:\t", line
->label
->number
);
2224 c
= pdb
->file
->source
+ line
->source_offset
;
2227 PIO_eprintf(interp
, "%c", *(c
++));
2229 PIO_eprintf(interp
, "\n");
2238 pdb
->file
->list_line
= 0;
2240 pdb
->file
->list_line
+= n
;
2245 =item C<void PDB_eval>
2247 C<eval>s an instruction.
2254 PDB_eval(PARROT_INTERP
, ARGIN(const char *command
))
2256 /* This code is almost certainly wrong. The Parrot debugger needs love. */
2257 opcode_t
*run
= PDB_compile(interp
, command
);
2265 =item C<opcode_t * PDB_compile>
2267 Compiles instructions with the PASM compiler.
2269 Appends an C<end> op.
2271 This may be called from C<PDB_eval> above or from the compile opcode
2272 which generates a malloced string.
2278 PARROT_CAN_RETURN_NULL
2280 PDB_compile(PARROT_INTERP
, ARGIN(const char *command
))
2283 const char *end
= "\nend\n";
2284 STRING
*key
= const_string(interp
, "PASM");
2285 PMC
*compreg_hash
= VTABLE_get_pmc_keyed_int(interp
,
2286 interp
->iglobals
, IGLOBALS_COMPREG_HASH
);
2287 PMC
*compiler
= VTABLE_get_pmc_keyed_str(interp
, compreg_hash
, key
);
2289 if (!VTABLE_defined(interp
, compiler
)) {
2290 fprintf(stderr
, "Couldn't find PASM compiler");
2294 buf
= Parrot_sprintf_c(interp
, "%s%s", command
, end
);
2296 return VTABLE_invoke(interp
, compiler
, buf
);
2301 =item C<int PDB_extend_const_table>
2303 Extend the constant table.
2310 PDB_extend_const_table(PARROT_INTERP
)
2312 int k
= ++interp
->code
->const_table
->const_count
;
2314 /* Update the constant count and reallocate */
2315 if (interp
->code
->const_table
->constants
) {
2316 interp
->code
->const_table
->constants
=
2317 (PackFile_Constant
**)mem_sys_realloc(interp
->code
->const_table
->constants
,
2318 k
* sizeof (PackFile_Constant
*));
2321 interp
->code
->const_table
->constants
=
2322 (PackFile_Constant
**)mem_sys_allocate(k
* sizeof (PackFile_Constant
*));
2325 /* Allocate a new constant */
2326 interp
->code
->const_table
->constants
[--k
] =
2327 PackFile_Constant_new(interp
);
2334 =item C<static void dump_string>
2336 Dumps the buflen, flags, bufused, strlen, and offset associated with a string
2337 and the string itself.
2344 dump_string(PARROT_INTERP
, ARGIN_NULLOK(const STRING
*s
))
2349 PIO_eprintf(interp
, "\tBuflen =\t%12ld\n", PObj_buflen(s
));
2350 PIO_eprintf(interp
, "\tFlags =\t%12ld\n", PObj_get_FLAGS(s
));
2351 PIO_eprintf(interp
, "\tBufused =\t%12ld\n", s
->bufused
);
2352 PIO_eprintf(interp
, "\tStrlen =\t%12ld\n", s
->strlen
);
2353 PIO_eprintf(interp
, "\tOffset =\t%12ld\n",
2354 (char*) s
->strstart
- (char*) PObj_bufstart(s
));
2355 PIO_eprintf(interp
, "\tString =\t%S\n", s
);
2360 =item C<void PDB_print_user_stack>
2362 Print an entry from the user stack.
2369 PDB_print_user_stack(PARROT_INTERP
, ARGIN(const char *command
))
2371 Stack_Entry_t
*entry
;
2373 Stack_Chunk_t
* const chunk
= CONTEXT(interp
->ctx
)->user_stack
;
2375 command
= nextarg(command
);
2377 depth
= atol(command
);
2379 entry
= stack_entry(interp
, chunk
, (INTVAL
)depth
);
2382 PIO_eprintf(interp
, "No such entry on stack\n");
2386 switch (entry
->entry_type
) {
2387 case STACK_ENTRY_INT
:
2388 PIO_eprintf(interp
, "Integer\t=\t%8vi\n", UVal_int(entry
->entry
));
2390 case STACK_ENTRY_FLOAT
:
2391 PIO_eprintf(interp
, "Float\t=\t%8.4vf\n", UVal_num(entry
->entry
));
2393 case STACK_ENTRY_STRING
:
2394 PIO_eprintf(interp
, "String =\n");
2395 dump_string(interp
, UVal_str(entry
->entry
));
2397 case STACK_ENTRY_PMC
:
2398 PIO_eprintf(interp
, "PMC =\n%PS\n", UVal_ptr(entry
->entry
));
2400 case STACK_ENTRY_POINTER
:
2401 PIO_eprintf(interp
, "POINTER\n");
2403 case STACK_ENTRY_DESTINATION
:
2404 PIO_eprintf(interp
, "DESTINATION\n");
2407 PIO_eprintf(interp
, "Invalid stack_entry_type!\n");
2414 =item C<void PDB_print>
2416 Print interp registers.
2423 PDB_print(PARROT_INTERP
, ARGIN(const char *command
))
2425 const char * const s
= GDB_P(interp
->pdb
->debugee
, command
);
2426 PIO_eprintf(interp
, "%s\n", s
);
2432 =item C<void PDB_info>
2434 Print the interpreter info.
2441 PDB_info(PARROT_INTERP
)
2443 PIO_eprintf(interp
, "Total memory allocated = %ld\n",
2444 interpinfo(interp
, TOTAL_MEM_ALLOC
));
2445 PIO_eprintf(interp
, "DOD runs = %ld\n",
2446 interpinfo(interp
, DOD_RUNS
));
2447 PIO_eprintf(interp
, "Lazy DOD runs = %ld\n",
2448 interpinfo(interp
, LAZY_DOD_RUNS
));
2449 PIO_eprintf(interp
, "Collect runs = %ld\n",
2450 interpinfo(interp
, COLLECT_RUNS
));
2451 PIO_eprintf(interp
, "Collect memory = %ld\n",
2452 interpinfo(interp
, TOTAL_COPIED
));
2453 PIO_eprintf(interp
, "Active PMCs = %ld\n",
2454 interpinfo(interp
, ACTIVE_PMCS
));
2455 PIO_eprintf(interp
, "Extended PMCs = %ld\n",
2456 interpinfo(interp
, EXTENDED_PMCS
));
2457 PIO_eprintf(interp
, "Timely DOD PMCs = %ld\n",
2458 interpinfo(interp
, IMPATIENT_PMCS
));
2459 PIO_eprintf(interp
, "Total PMCs = %ld\n",
2460 interpinfo(interp
, TOTAL_PMCS
));
2461 PIO_eprintf(interp
, "Active buffers = %ld\n",
2462 interpinfo(interp
, ACTIVE_BUFFERS
));
2463 PIO_eprintf(interp
, "Total buffers = %ld\n",
2464 interpinfo(interp
, TOTAL_BUFFERS
));
2465 PIO_eprintf(interp
, "Header allocations since last collect = %ld\n",
2466 interpinfo(interp
, HEADER_ALLOCS_SINCE_COLLECT
));
2467 PIO_eprintf(interp
, "Memory allocations since last collect = %ld\n",
2468 interpinfo(interp
, MEM_ALLOCS_SINCE_COLLECT
));
2473 =item C<void PDB_help>
2475 Print the help text. "Help" with no arguments prints a list of commands.
2476 "Help xxx" prints information on command xxx.
2483 PDB_help(PARROT_INTERP
, ARGIN(const char *command
))
2486 const char *temp
= command
;
2488 command
= parse_command(command
, &c
);
2492 PIO_eprintf(interp
, "No documentation yet");
2495 PIO_eprintf(interp
, "No documentation yet");
2499 "List the source code.\n\n\
2500 Optionally specify the line number to begin the listing from and the number\n\
2501 of lines to display.\n");
2505 "Run (or restart) the program being debugged.\n\n\
2506 Arguments specified after \"run\" are passed as command line arguments to\n\
2511 "Set a breakpoint at a given line number (which must be specified).\n\n\
2512 Optionally, specify a condition, in which case the breakpoint will only\n\
2513 activate if the condition is met. Conditions take the form:\n\n\
2514 if [REGISTER] [COMPARISON] [REGISTER or CONSTANT]\n\n\
2517 break 10 if I4 > I3\n\n\
2518 break 45 if S1 == \"foo\"\n\n\
2519 The command returns a number which is the breakpoint identifier.");
2522 PIO_eprintf(interp
, "Interprets a file.\n\
2524 (pdb) script file.script\n");
2527 PIO_eprintf(interp
, "No documentation yet");
2531 "Delete a breakpoint.\n\n\
2532 The breakpoint to delete must be specified by its breakpoint number.\n\
2533 Deleted breakpoints are gone completely. If instead you want to\n\
2534 temporarily disable a breakpoint, use \"disable\".\n");
2538 "Disable a breakpoint.\n\n\
2539 The breakpoint to disable must be specified by its breakpoint number.\n\
2540 Disabled breakpoints are not forgotten, but have no effect until re-enabled\n\
2541 with the \"enable\" command.\n");
2544 PIO_eprintf(interp
, "Re-enable a disabled breakpoint.\n");
2548 "Continue the program execution.\n\n\
2549 Without arguments, the program runs until a breakpoint is found\n\
2550 (or until the program terminates for some other reason).\n\n\
2551 If a number is specified, then skip that many breakpoints.\n\n\
2552 If the program has terminated, then \"continue\" will do nothing;\n\
2553 use \"run\" to re-run the program.\n");
2557 "Execute a specified number of instructions.\n\n\
2558 If a number is specified with the command (e.g. \"next 5\"), then\n\
2559 execute that number of instructions, unless the program reaches a\n\
2560 breakpoint, or stops for some other reason.\n\n\
2561 If no number is specified, it defaults to 1.\n");
2564 PIO_eprintf(interp
, "No documentation yet");
2568 "Similar to \"next\", but prints additional trace information.\n\
2569 This is the same as the information you get when running Parrot with\n\
2573 PIO_eprintf(interp
, "Print register: e.g. p I2\n");
2577 "Print information about the current interpreter\n");
2580 PIO_eprintf(interp
, "Exit the debugger.\n");
2583 PIO_eprintf(interp
, "Print a list of available commands.\n");
2586 /* C89: strings need to be 509 chars or less */
2587 PIO_eprintf(interp
, "\
2588 List of commands:\n\
2589 disassemble -- disassemble the bytecode\n\
2590 load -- load a source code file\n\
2591 list (l) -- list the source code file\n\
2592 run (r) -- run the program\n\
2593 break (b) -- add a breakpoint\n\
2594 script (f) -- interprets a file as user commands\n\
2595 watch (w) -- add a watchpoint\n\
2596 delete (d) -- delete a breakpoint\n\
2597 disable -- disable a breakpoint\n\
2598 enable -- reenable a disabled breakpoint\n\
2599 continue (c) -- continue the program execution\n");
2600 PIO_eprintf(interp
, "\
2601 next (n) -- run the next instruction\n\
2602 eval (e) -- run an instruction\n\
2603 trace (t) -- trace the next instruction\n\
2604 print (p) -- print the interpreter registers\n\
2605 stack (s) -- examine the stack\n\
2606 info -- print interpreter information\n\
2607 quit (q) -- exit the debugger\n\
2608 help (h) -- print this help\n\n\
2609 Type \"help\" followed by a command name for full documentation.\n\n");
2612 PIO_eprintf(interp
, "Unknown command: \"%s\".", temp
);
2619 =item C<void PDB_backtrace>
2621 Prints a backtrace of the interp's call chain.
2628 PDB_backtrace(PARROT_INTERP
)
2634 /* information about the current sub */
2635 PMC
*sub
= interpinfo_p(interp
, CURRENT_SUB
);
2636 parrot_context_t
*ctx
= CONTEXT(interp
->ctx
);
2638 if (!PMC_IS_NULL(sub
)) {
2639 str
= Parrot_Context_infostr(interp
, ctx
);
2641 PIO_eprintf(interp
, "%Ss\n", str
);
2644 /* backtrace: follow the continuation chain */
2646 Parrot_cont
*sub_cont
;
2647 sub
= ctx
->current_cont
;
2652 sub_cont
= PMC_cont(sub
);
2657 str
= Parrot_Context_infostr(interp
, sub_cont
->to_ctx
);
2662 /* recursion detection */
2663 if (!PMC_IS_NULL(old
) && PMC_cont(old
) &&
2664 PMC_cont(old
)->to_ctx
->current_pc
==
2665 PMC_cont(sub
)->to_ctx
->current_pc
&&
2666 PMC_cont(old
)->to_ctx
->current_sub
==
2667 PMC_cont(sub
)->to_ctx
->current_sub
) {
2670 else if (rec_level
!= 0) {
2671 PIO_eprintf(interp
, "... call repeated %d times\n", rec_level
);
2675 /* print the context description */
2677 PIO_eprintf(interp
, "%Ss\n", str
);
2679 /* get the next Continuation */
2680 ctx
= PMC_cont(sub
)->to_ctx
;
2688 PIO_eprintf(interp
, "... call repeated %d times\n", rec_level
);
2694 * GDB_P gdb> pp $I0 print register I0 value
2696 * RT46139 more, more
2701 =item C<static const char* GDB_P>
2703 RT#48260: Not yet documented!!!
2709 PARROT_WARN_UNUSED_RESULT
2710 PARROT_CANNOT_RETURN_NULL
2712 GDB_P(PARROT_INTERP
, ARGIN(const char *s
))
2716 case 'I': t
= REGNO_INT
; break;
2717 case 'N': t
= REGNO_NUM
; break;
2718 case 'S': t
= REGNO_STR
; break;
2719 case 'P': t
= REGNO_PMC
; break;
2720 default: return "no such reg";
2722 if (s
[1] && isdigit((unsigned char)s
[1]))
2725 return "no such reg";
2727 if (n
>= 0 && n
< CONTEXT(interp
->ctx
)->n_regs_used
[t
]) {
2730 return string_from_int(interp
, REG_INT(interp
, n
))->strstart
;
2732 return string_from_num(interp
, REG_NUM(interp
, n
))->strstart
;
2734 return REG_STR(interp
, n
)->strstart
;
2736 /* prints directly */
2737 trace_pmc_dump(interp
, REG_PMC(interp
, n
));
2743 return "no such reg";
2746 /* RT#46141 move these to debugger interpreter
2748 static PDB_breakpoint_t
*gdb_bps
;
2751 * GDB_pb gdb> pb 244 # set breakpoint at opcode 244
2753 * RT#46143 We can't remove the breakpoint yet, executing the next ins
2754 * most likely fails, as the length of the debug-brk stmt doesn't
2755 * match the old opcode
2756 * Setting a breakpoint will also fail, if the bytecode os r/o
2761 =item C<static int GDB_B>
2763 RT#48260: Not yet documented!!!
2770 GDB_B(PARROT_INTERP
, NOTNULL(char *s
)) {
2773 PDB_breakpoint_t
*bp
, *newbreak
;
2775 if ((unsigned long)s
< 0x10000) {
2776 /* HACK alarm pb 45 is passed as the integer not a string */
2777 /* RT#46145 check if in bounds */
2778 pc
= interp
->code
->base
.data
+ (unsigned long)s
;
2782 newbreak
= mem_allocate_typed(PDB_breakpoint_t
);
2783 newbreak
->prev
= NULL
;
2784 newbreak
->next
= NULL
;
2788 /* create new one */
2789 for (nr
= 0, bp
= gdb_bps
; ; bp
= bp
->next
, ++nr
) {
2798 newbreak
= mem_allocate_typed(PDB_breakpoint_t
);
2799 newbreak
->prev
= bp
;
2800 newbreak
->next
= NULL
;
2801 bp
->next
= newbreak
;
2806 *pc
= PARROT_OP_trap
;
2820 F<include/parrot/debug.h>, F<src/pdb.c> and F<ops/debug.ops>.
2826 =item Initial version by Daniel Grunblatt on 2002.5.19.
2828 =item Start of rewrite - leo 2005.02.16
2830 The debugger now uses its own interpreter. User code is run in
2831 Interp *debugee. We have:
2833 debug_interp->pdb->debugee->debugger
2836 +------------- := -----------+
2838 Debug commands are mostly run inside the C<debugger>. User code
2839 runs of course in the C<debugee>.
2850 * c-file-style: "parrot"
2852 * vim: expandtab shiftwidth=4: