2 Copyright (C) 2001-2009, 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"
66 const_dump(PARROT_INTERP, const PackFile_Segment *segp)>
68 Dump the constant table.
75 const_dump(PARROT_INTERP
, const PackFile_Segment
*segp
)
77 Parrot_io_printf(interp
, "%Ss => [\n", segp
->name
);
78 PackFile_ConstTable_dump(interp
, (const PackFile_ConstTable
*)segp
);
79 Parrot_io_printf(interp
, "],\n");
85 fixup_dump(PARROT_INTERP, const PackFile_Segment *segp)>
94 fixup_dump(PARROT_INTERP
, const PackFile_Segment
*segp
)
96 Parrot_io_printf(interp
, "%Ss => [\n", segp
->name
);
97 PackFile_Fixup_dump(interp
, (const PackFile_FixupTable
*)segp
);
98 Parrot_io_printf(interp
, "],\n");
104 disas_dump(PARROT_INTERP, const PackFile_Segment *self)>
106 Disassemble and dump.
113 disas_dump(PARROT_INTERP
, const PackFile_Segment
*self
)
117 Parrot_io_printf(interp
, "%Ss => [ # %d ops at offs 0x%x\n",
118 self
->name
, (int)self
->size
, (int)self
->file_offset
+ 4);
120 while (pc
< self
->data
+ self
->size
) {
121 /* trace_op_dump(interp, self->pf->src, pc); */
122 Parrot_io_printf(interp
, " %04x: ", (int) (pc
- self
->data
));
123 n
= (size_t)interp
->op_info_table
[*pc
].op_count
;
124 for (i
= 0; i
< 6; i
++)
126 Parrot_io_printf(interp
, "%08lx ", (unsigned long) pc
[i
]);
128 Parrot_io_printf(interp
, " ");
129 Parrot_io_printf(interp
, "%s\n",
130 interp
->op_info_table
[*pc
].full_name
);
131 ADD_OP_VAR_PART(interp
, interp
->code
, pc
, n
);
134 Parrot_io_printf(interp
, "]\n");
140 PackFile_header_dump(PARROT_INTERP, PackFile *pf)>
149 PackFile_header_dump(PARROT_INTERP
, PackFile
*pf
)
151 Parrot_io_printf(interp
, "HEADER => [\n");
152 Parrot_io_printf(interp
, "\twordsize = %d", pf
->header
->wordsize
);
153 Parrot_io_printf(interp
, "\t(interpreter's wordsize/INTVAL = %d/%d)\n",
154 sizeof (opcode_t
), sizeof (INTVAL
));
155 Parrot_io_printf(interp
, "\tbyteorder = %d", pf
->header
->byteorder
);
156 Parrot_io_printf(interp
, "\t(interpreter's byteorder = %d)\n",
158 Parrot_io_printf(interp
, "\tfloattype = %d", pf
->header
->floattype
);
159 Parrot_io_printf(interp
, "\t(interpreter's NUMVAL_SIZE = %d)\n", NUMVAL_SIZE
);
160 Parrot_io_printf(interp
, "\tparrot-version %d.%d.%d, bytecode-version %d.%d\n",
161 pf
->header
->major
, pf
->header
->minor
, pf
->header
->patch
,
162 pf
->header
->bc_major
, pf
->header
->bc_minor
);
163 Parrot_io_printf(interp
, "\tUUID: type = %d, size = %d",
164 pf
->header
->uuid_type
, pf
->header
->uuid_size
);
165 if (pf
->header
->uuid_size
)
166 Parrot_io_printf(interp
, ", '%s'\n", pf
->header
->uuid_data
);
168 Parrot_io_printf(interp
, "\n");
169 Parrot_io_printf(interp
, "\t%s endianize, %s opcode, %s numval transform\n",
170 pf
->need_endianize
? "**need**" : "no",
171 pf
->need_wordsize
? "**need**" : "no",
172 pf
->fetch_nv
? "**need**" : "no");
173 Parrot_io_printf(interp
, "\tdirformat = %d\n", pf
->header
->dir_format
);
174 Parrot_io_printf(interp
, "]\n");
179 =item C<static void help(void)>
181 Print out the user help info.
187 static void help(void)
189 printf("pbc_dump - dump or convert parrot bytecode (PBC) files\n");
191 printf("pbc_dump [-tdh] [--terse|--disassemble|--header-only] file.pbc\n");
192 printf("pbc_dump -o converted.pbc file.pbc\n\n");
193 printf("\t-d ... disassemble bytecode segments\n");
194 printf("\t-h ... dump header only\n");
195 printf("\t-t ... terse output\n");
197 printf("\t-D<1-7> --debug debug output\n");
198 printf("\t 1 general info\n");
199 printf("\t 2 alignment\n");
200 printf("\t 4 values\n");
202 printf("\t-o converted.pbc ... repacks a PBC file into "
203 "the platform's native\n");
204 printf("\t binary format for better efficiency on reading "
205 "non native PBCs\n");
209 static struct longopt_opt_decl opt_options
[] = {
210 { 'h', 'h', OPTION_optional_FLAG
, { "--header-only" } },
211 { '?', '?', OPTION_optional_FLAG
, { "--help" } },
212 { 't', 't', OPTION_optional_FLAG
, { "--terse" } },
213 { 'd', 'd', OPTION_optional_FLAG
, { "--disassemble" } },
215 { 'D', 'D', OPTION_required_FLAG
, { "--debug" } },
217 { 'o', 'o', OPTION_required_FLAG
, { "--output" } }
222 =item C<int main(int argc, const char **argv)>
224 The run loop. Process the command-line arguments and dump accordingly.
231 main(int argc
, const char **argv
)
238 int options
= PFOPT_UTILS
;
239 const char *file
= NULL
;
240 struct longopt_opt_info opt
= LONGOPT_OPT_INFO_INIT
;
246 interp
= Parrot_new(NULL
);
247 /* init and set top of stack */
248 Parrot_init_stacktop(interp
, &status
);
249 while ((status
= longopt_get(interp
,
250 argc
, argv
, opt_options
, &opt
)) > 0) {
251 switch (opt
.opt_id
) {
253 options
+= PFOPT_HEADERONLY
;
263 options
+= atoi(opt
.opt_arg
) << 2;
279 argc
-= opt
.opt_index
;
280 argv
+= opt
.opt_index
;
283 pf
= Parrot_pbc_read(interp
, *argv
, options
);
286 printf("Can't read PBC\n");
289 Parrot_pbc_load(interp
, pf
);
295 size
= PackFile_pack_size(interp
,
296 interp
->code
->base
.pf
) * sizeof (opcode_t
);
297 pack
= (opcode_t
*) mem_sys_allocate(size
);
299 printf("out of mem\n");
302 PackFile_pack(interp
, interp
->code
->base
.pf
, pack
);
303 if (STREQ(file
, "-"))
305 else if ((fp
= fopen(file
, "wb")) == 0) {
306 printf("Couldn't open %s\n", file
);
310 if ((1 != fwrite(pack
, size
, 1, fp
))) {
311 printf("Couldn't write %s\n", file
);
316 Parrot_exit(interp
, 0);
319 PackFile_header_dump(interp
, pf
);
320 if (options
& PFOPT_HEADERONLY
) {
321 Parrot_exit(interp
, 0);
323 /* install a dumper function */
325 pf
->PackFuncs
[PF_CONST_SEG
].dump
= const_dump
;
326 pf
->PackFuncs
[PF_FIXUP_SEG
].dump
= fixup_dump
;
329 pf
->PackFuncs
[PF_BYTEC_SEG
].dump
= disas_dump
;
330 /* do a directory dump, which dumps segs then */
331 PackFile_Segment_dump(interp
, &pf
->directory
.base
);
333 Parrot_exit(interp
, 0);
350 * c-file-style: "parrot"
352 * vim: expandtab shiftwidth=4: