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"
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");
83 =item C<static void fixup_dump(PARROT_INTERP, const PackFile_Segment *segp)>
92 fixup_dump(PARROT_INTERP
, const PackFile_Segment
*segp
)
94 Parrot_io_printf(interp
, "%Ss => [\n", segp
->name
);
95 PackFile_Fixup_dump(interp
, (const PackFile_FixupTable
*)segp
);
96 Parrot_io_printf(interp
, "],\n");
101 =item C<static void disas_dump(PARROT_INTERP, const PackFile_Segment *self)>
103 Disassemble and dump.
110 disas_dump(PARROT_INTERP
, const PackFile_Segment
*self
)
114 Parrot_io_printf(interp
, "%Ss => [ # %d ops at offs 0x%x\n",
115 self
->name
, (int)self
->size
, (int)self
->file_offset
+ 4);
117 while (pc
< self
->data
+ self
->size
) {
118 /* trace_op_dump(interp, self->pf->src, pc); */
119 Parrot_io_printf(interp
, " %04x: ", (int) (pc
- self
->data
));
120 n
= (size_t)interp
->op_info_table
[*pc
].op_count
;
121 for (i
= 0; i
< 6; i
++)
123 Parrot_io_printf(interp
, "%08lx ", (unsigned long) pc
[i
]);
125 Parrot_io_printf(interp
, " ");
126 Parrot_io_printf(interp
, "%s\n",
127 interp
->op_info_table
[*pc
].full_name
);
128 ADD_OP_VAR_PART(interp
, interp
->code
, pc
, n
);
131 Parrot_io_printf(interp
, "]\n");
136 =item C<static void PackFile_header_dump(PARROT_INTERP, PackFile *pf)>
145 PackFile_header_dump(PARROT_INTERP
, PackFile
*pf
)
147 Parrot_io_printf(interp
, "HEADER => [\n");
148 Parrot_io_printf(interp
, "\twordsize = %d", pf
->header
->wordsize
);
149 Parrot_io_printf(interp
, "\t(interpreter's wordsize/INTVAL = %d/%d)\n",
150 sizeof (opcode_t
), sizeof (INTVAL
));
151 Parrot_io_printf(interp
, "\tbyteorder = %d", pf
->header
->byteorder
);
152 Parrot_io_printf(interp
, "\t(interpreter's byteorder = %d)\n",
154 Parrot_io_printf(interp
, "\tfloattype = %d", pf
->header
->floattype
);
155 Parrot_io_printf(interp
, "\t(interpreter's NUMVAL_SIZE = %d)\n", NUMVAL_SIZE
);
156 Parrot_io_printf(interp
, "\tparrot-version %d.%d.%d, bytecode-version %d.%d\n",
157 pf
->header
->major
, pf
->header
->minor
, pf
->header
->patch
,
158 pf
->header
->bc_major
, pf
->header
->bc_minor
);
159 Parrot_io_printf(interp
, "\tUUID: type = %d, size = %d",
160 pf
->header
->uuid_type
, pf
->header
->uuid_size
);
161 if (pf
->header
->uuid_size
)
162 Parrot_io_printf(interp
, ", '%s'\n", pf
->header
->uuid_data
);
164 Parrot_io_printf(interp
, "\n");
165 Parrot_io_printf(interp
, "\t%s endianize, %s opcode, %s numval transform\n",
166 pf
->need_endianize
? "**need**" : "no",
167 pf
->need_wordsize
? "**need**" : "no",
168 pf
->fetch_nv
? "**need**" : "no");
169 Parrot_io_printf(interp
, "\tdirformat = %d\n", pf
->header
->dir_format
);
170 Parrot_io_printf(interp
, "]\n");
175 =item C<static void help(void)>
177 Print out the user help info.
183 static void help(void)
185 printf("pbc_dump - dump or convert parrot bytecode (PBC) files\n");
187 printf("pbc_dump [-tdh] [--terse|--disassemble|--header-only] file.pbc\n");
188 printf("pbc_dump -o converted.pbc file.pbc\n\n");
189 printf("\t-d ... disassemble bytecode segments\n");
190 printf("\t-h ... dump header only\n");
191 printf("\t-t ... terse output\n");
193 printf("\t-D<1-7> --debug debug output\n");
194 printf("\t 1 general info\n");
195 printf("\t 2 alignment\n");
196 printf("\t 4 values\n");
198 printf("\t-o converted.pbc ... repacks a PBC file into "
199 "the platform's native\n");
200 printf("\t binary format for better efficiency on reading "
201 "non native PBCs\n");
205 static struct longopt_opt_decl opt_options
[] = {
206 { 'h', 'h', OPTION_optional_FLAG
, { "--header-only" } },
207 { '?', '?', OPTION_optional_FLAG
, { "--help" } },
208 { 't', 't', OPTION_optional_FLAG
, { "--terse" } },
209 { 'd', 'd', OPTION_optional_FLAG
, { "--disassemble" } },
211 { 'D', 'D', OPTION_required_FLAG
, { "--debug" } },
213 { 'o', 'o', OPTION_required_FLAG
, { "--output" } }
218 =item C<int main(int argc, const char **argv)>
220 The run loop. Process the command-line arguments and dump accordingly.
227 main(int argc
, const char **argv
)
234 int options
= PFOPT_UTILS
;
235 const char *file
= NULL
;
236 struct longopt_opt_info opt
= LONGOPT_OPT_INFO_INIT
;
242 interp
= Parrot_new(NULL
);
243 /* init and set top of stack */
244 Parrot_init_stacktop(interp
, &status
);
245 while ((status
= longopt_get(interp
,
246 argc
, argv
, opt_options
, &opt
)) > 0) {
247 switch (opt
.opt_id
) {
249 options
+= PFOPT_HEADERONLY
;
259 options
+= atoi(opt
.opt_arg
) << 2;
275 argc
-= opt
.opt_index
;
276 argv
+= opt
.opt_index
;
279 pf
= Parrot_pbc_read(interp
, *argv
, options
);
282 printf("Can't read PBC\n");
285 Parrot_pbc_load(interp
, pf
);
291 size
= PackFile_pack_size(interp
,
292 interp
->code
->base
.pf
) * sizeof (opcode_t
);
293 pack
= (opcode_t
*) Parrot_gc_allocate_memory_chunk(interp
, size
);
295 printf("out of mem\n");
298 PackFile_pack(interp
, interp
->code
->base
.pf
, pack
);
299 if (STREQ(file
, "-"))
301 else if ((fp
= fopen(file
, "wb")) == 0) {
302 printf("Couldn't open %s\n", file
);
306 if ((1 != fwrite(pack
, size
, 1, fp
))) {
307 printf("Couldn't write %s\n", file
);
311 Parrot_gc_free_memory_chunk(interp
, pack
);
312 Parrot_exit(interp
, 0);
315 PackFile_header_dump(interp
, pf
);
316 if (options
& PFOPT_HEADERONLY
) {
317 Parrot_exit(interp
, 0);
319 /* install a dumper function */
321 pf
->PackFuncs
[PF_CONST_SEG
].dump
= const_dump
;
322 pf
->PackFuncs
[PF_FIXUP_SEG
].dump
= fixup_dump
;
325 pf
->PackFuncs
[PF_BYTEC_SEG
].dump
= disas_dump
;
326 /* do a directory dump, which dumps segs then */
327 PackFile_Segment_dump(interp
, &pf
->directory
.base
);
329 Parrot_exit(interp
, 0);
346 * c-file-style: "parrot"
348 * vim: expandtab shiftwidth=4: