2 Copyright (C) 2001-2010, Parrot Foundation.
7 pbc_dump - Dump or convert Parrot bytecode (PBC) files
11 pbc_dump [-tdh] [--terse|--disassemble|--header-only] file.pbc
13 pbc_dump -o converted.pbc file.pbc
17 A program to dump pack files to human readable form.
19 =head2 Command-Line Options
25 Disassemble bytecode segments.
29 Dump the bytecode header only.
35 =item C<-D> C--debug> 1-7
37 Display detailed packfile reader debugging information if
38 F<include/parrot/packfile.h> enables TRACE_PACKFILE.
40 1 print general debug info
41 2 print alignment info
44 =item C<-o converted.pbc>
46 Repacks a PBC file into the platform's native binary format for better
47 efficiency on reading non-native PBCs.
59 #include "parrot/parrot.h"
60 #include "parrot/embed.h"
61 #include "parrot/oplib/ops.h"
65 =item C<static void const_dump(PARROT_INTERP, const PackFile_Segment *segp)>
67 Dump the constant table.
74 const_dump(PARROT_INTERP
, const PackFile_Segment
*segp
)
76 Parrot_io_printf(interp
, "%Ss => [\n", segp
->name
);
77 PackFile_ConstTable_dump(interp
, (const PackFile_ConstTable
*)segp
);
78 Parrot_io_printf(interp
, "],\n");
84 =item C<static void fixup_dump(PARROT_INTERP, const PackFile_Segment *segp)>
93 fixup_dump(PARROT_INTERP
, const PackFile_Segment
*segp
)
95 Parrot_io_printf(interp
, "%Ss => [\n", segp
->name
);
96 PackFile_Fixup_dump(interp
, (const PackFile_FixupTable
*)segp
);
97 Parrot_io_printf(interp
, "],\n");
103 =item C<static void disas_dump(PARROT_INTERP, const PackFile_Segment *self)>
105 Disassemble and dump.
112 disas_dump(PARROT_INTERP
, const PackFile_Segment
*self
)
114 const opcode_t
*pc
= self
->data
;
116 Parrot_io_printf(interp
, "%Ss => [ # %d ops at offs 0x%x\n",
117 self
->name
, (int)self
->size
, (int)self
->file_offset
+ 4);
119 while (pc
< self
->data
+ self
->size
) {
120 /* n can't be const; the ADD_OP_VAR_PART macro increments it */
121 size_t n
= (size_t)interp
->op_info_table
[*pc
].op_count
;
124 /* trace_op_dump(interp, self->pf->src, pc); */
125 Parrot_io_printf(interp
, " %04x: ", (int)(pc
- self
->data
));
127 for (i
= 0; i
< 6; ++i
)
129 Parrot_io_printf(interp
, "%08lx ", (unsigned long)pc
[i
]);
131 Parrot_io_printf(interp
, " ");
133 Parrot_io_printf(interp
, "%s\n",
134 interp
->op_info_table
[*pc
].full_name
);
136 ADD_OP_VAR_PART(interp
, interp
->code
, pc
, n
);
140 Parrot_io_printf(interp
, "]\n");
146 =item C<static void nums_dump(PARROT_INTERP, const PackFile_Segment *self)>
148 Disassembles and dumps op names and line numbers only.
155 nums_dump(PARROT_INTERP
, const PackFile_Segment
*self
)
157 const STRING
*debug_name
= Parrot_str_concat(interp
, self
->name
,
158 Parrot_str_new_constant(interp
, "_DB"));
159 const PackFile_Segment
*debug
= PackFile_find_segment(interp
,
160 self
->dir
, debug_name
, 1);
162 const opcode_t
* pc
= self
->data
;
163 const opcode_t
* debug_ops
= debug
->data
;
164 const op_info_t
* const op_info
= interp
->op_info_table
;
166 while (pc
< self
->data
+ self
->size
) {
167 /* n can't be const; the ADD_OP_VAR_PART macro increments it */
168 size_t n
= (size_t)op_info
[*pc
].op_count
;
170 Parrot_io_printf(interp
, " %04x: %s\n",
171 *(debug_ops
++), op_info
[*pc
].full_name
);
173 ADD_OP_VAR_PART(interp
, interp
->code
, pc
, n
);
181 =item C<static void null_dump(PARROT_INTERP, const PackFile_Segment *self)>
183 Produces no output for the given segment type.
190 null_dump(SHIM_INTERP
, const PackFile_Segment
*self
)
198 =item C<static void null_dir_dump(PARROT_INTERP, const PackFile_Segment *self)>
200 Dumps all of the segments of the given PackFile_Directory, but produces no
201 output for the directory itself.
208 null_dir_dump(PARROT_INTERP
, const PackFile_Segment
*self
)
210 const PackFile_Directory
* const dir
= (const PackFile_Directory
*)self
;
213 for (i
= 0; i
< dir
->num_segments
; ++i
)
214 self
->pf
->PackFuncs
[dir
->segments
[i
]->type
].dump(interp
, dir
->segments
[i
]);
220 =item C<static void PackFile_header_dump(PARROT_INTERP, PackFile *pf)>
229 PackFile_header_dump(PARROT_INTERP
, PackFile
*pf
)
231 Parrot_io_printf(interp
, "HEADER => [\n");
232 Parrot_io_printf(interp
, "\twordsize = %d", pf
->header
->wordsize
);
233 Parrot_io_printf(interp
, "\t(interpreter's wordsize/INTVAL = %d/%d)\n",
234 sizeof (opcode_t
), sizeof (INTVAL
));
235 Parrot_io_printf(interp
, "\tbyteorder = %d", pf
->header
->byteorder
);
236 Parrot_io_printf(interp
, "\t(interpreter's byteorder = %d)\n",
238 Parrot_io_printf(interp
, "\tfloattype = %d", pf
->header
->floattype
);
239 Parrot_io_printf(interp
, "\t(interpreter's NUMVAL_SIZE = %d)\n",
241 Parrot_io_printf(interp
, "\tparrot-version %d.%d.%d, "
242 "bytecode-version %d.%d\n",
243 pf
->header
->major
, pf
->header
->minor
, pf
->header
->patch
,
244 pf
->header
->bc_major
, pf
->header
->bc_minor
);
245 Parrot_io_printf(interp
, "\tUUID: type = %d, size = %d",
246 pf
->header
->uuid_type
, pf
->header
->uuid_size
);
248 if (pf
->header
->uuid_size
)
249 Parrot_io_printf(interp
, ", '%s'\n", pf
->header
->uuid_data
);
251 Parrot_io_printf(interp
, "\n");
253 Parrot_io_printf(interp
, "\t%s endianize, %s opcode, %s numval transform\n",
254 pf
->need_endianize
? "**need**" : "no",
255 pf
->need_wordsize
? "**need**" : "no",
256 pf
->fetch_nv
? "**need**" : "no");
258 Parrot_io_printf(interp
, "\tdirformat = %d\n", pf
->header
->dir_format
);
259 Parrot_io_printf(interp
, "]\n");
265 =item C<static void help(void)>
267 Print out the user help info.
273 static void help(void)
275 printf("pbc_dump - dump or convert parrot bytecode (PBC) files\n");
277 printf("pbc_dump [-tdh] [--terse|--disassemble|--header-only|--line-nums]"
279 printf("pbc_dump -o converted.pbc file.pbc\n\n");
280 printf("\t-d ... disassemble bytecode segments\n");
281 printf("\t-h ... dump header only\n");
282 printf("\t-t ... terse output\n");
283 printf("\t-n ... show ops and line numbers only\n");
286 printf("\t-D<1-7> --debug debug output\n");
287 printf("\t 1 general info\n");
288 printf("\t 2 alignment\n");
289 printf("\t 4 values\n");
292 printf("\t-o converted.pbc ... repacks a PBC file into "
293 "the platform's native\n");
294 printf("\t binary format for better efficiency on reading "
295 "non native PBCs\n");
300 static struct longopt_opt_decl opt_options
[] = {
301 { 'h', 'h', OPTION_optional_FLAG
, { "--header-only" } },
302 { '?', '?', OPTION_optional_FLAG
, { "--help" } },
303 { 't', 't', OPTION_optional_FLAG
, { "--terse" } },
304 { 'n', 'n', OPTION_optional_FLAG
, { "--line-nums" } },
305 { 'd', 'd', OPTION_optional_FLAG
, { "--disassemble" } },
306 { 'o', 'o', OPTION_required_FLAG
, { "--output" } }
309 { 'D', 'D', OPTION_required_FLAG
, { "--debug" } },
316 =item C<int main(int argc, const char **argv)>
318 The run loop. Process the command-line arguments and dump accordingly.
325 main(int argc
, const char **argv
)
330 const char *file
= NULL
;
335 int options
= PFOPT_UTILS
;
337 struct longopt_opt_info opt
= LONGOPT_OPT_INFO_INIT
;
344 interp
= Parrot_new(NULL
);
346 /* init and set top of stack */
347 Parrot_init_stacktop(interp
, &status
);
349 while ((status
= longopt_get(interp
, argc
, argv
, opt_options
, &opt
)) > 0) {
350 switch (opt
.opt_id
) {
353 options
+= atoi(opt
.opt_arg
) << 2;
357 options
+= PFOPT_HEADERONLY
;
382 argc
-= opt
.opt_index
;
383 argv
+= opt
.opt_index
;
385 pf
= Parrot_pbc_read(interp
, *argv
, options
);
388 printf("Can't read PBC\n");
392 Parrot_pbc_load(interp
, pf
);
395 size_t size
= PackFile_pack_size(interp
,
396 interp
->code
->base
.pf
) * sizeof (opcode_t
);
397 opcode_t
*pack
= (opcode_t
*)Parrot_gc_allocate_memory_chunk(interp
,
402 printf("out of mem\n");
406 PackFile_pack(interp
, interp
->code
->base
.pf
, pack
);
408 if (STREQ(file
, "-"))
410 else if ((fp
= fopen(file
, "wb")) == 0) {
411 printf("Couldn't open %s\n", file
);
415 if ((1 != fwrite(pack
, size
, 1, fp
))) {
416 printf("Couldn't write %s\n", file
);
421 Parrot_gc_free_memory_chunk(interp
, pack
);
422 Parrot_exit(interp
, 0);
426 PackFile_header_dump(interp
, pf
);
428 if (options
& PFOPT_HEADERONLY
)
429 Parrot_exit(interp
, 0);
431 /* install a dumper function */
433 pf
->PackFuncs
[PF_CONST_SEG
].dump
= const_dump
;
434 pf
->PackFuncs
[PF_FIXUP_SEG
].dump
= fixup_dump
;
438 pf
->PackFuncs
[PF_BYTEC_SEG
].dump
= disas_dump
;
443 for (i
= PF_DIR_SEG
+ 1; i
< PF_MAX_SEG
; ++i
)
444 pf
->PackFuncs
[i
].dump
= null_dump
;
446 pf
->PackFuncs
[PF_DIR_SEG
].dump
= null_dir_dump
;
447 pf
->PackFuncs
[PF_BYTEC_SEG
].dump
= nums_dump
;
450 /* do a directory dump, which dumps segs then */
451 PackFile_Segment_dump(interp
, &pf
->directory
.base
);
453 Parrot_exit(interp
, 0);
471 * c-file-style: "parrot"
473 * vim: expandtab shiftwidth=4: