| 1 | #! /usr/bin/php |
|---|
| 2 | <?php |
|---|
| 3 | |
|---|
| 4 | $srcdir = dirname(__FILE__); |
|---|
| 5 | require_once("$srcdir/Decompiler.class.php"); |
|---|
| 6 | if (file_exists("$srcdir/phpdc.debug.php")) { |
|---|
| 7 | include("$srcdir/phpdc.debug.php"); |
|---|
| 8 | } |
|---|
| 9 | |
|---|
| 10 | function get_op($op) |
|---|
| 11 | { |
|---|
| 12 | switch ($op['op_type']) { |
|---|
| 13 | case 1: // CONST |
|---|
| 14 | return var_export($op['constant'], true); |
|---|
| 15 | |
|---|
| 16 | case 2: // IS_TMP_VAR |
|---|
| 17 | return 't@' . $op['var']; |
|---|
| 18 | |
|---|
| 19 | case 4: |
|---|
| 20 | return 'v$' . $op['var']; |
|---|
| 21 | |
|---|
| 22 | case 8: // UNUSED |
|---|
| 23 | if (isset($op['opline_num'])) { |
|---|
| 24 | return 'l#' . $op['opline_num']; |
|---|
| 25 | } |
|---|
| 26 | else { |
|---|
| 27 | return '-'; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | default: |
|---|
| 31 | return $op['op_type'] . $op['var']; |
|---|
| 32 | } |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | function dump_opcodes($opcodes, $indent = '') |
|---|
| 36 | { |
|---|
| 37 | global $decompiler; |
|---|
| 38 | |
|---|
| 39 | $types = array('result' => 5, 'op1' => 20, 'op2' => 20); |
|---|
| 40 | foreach ($decompiler->fixOpcode($opcodes) as $line => $op) { |
|---|
| 41 | echo $indent; |
|---|
| 42 | echo sprintf("%3d ", $op['lineno']); |
|---|
| 43 | echo sprintf("%3x ", $line); |
|---|
| 44 | $name = xcache_get_opcode($op['opcode']); |
|---|
| 45 | |
|---|
| 46 | if (substr($name, 0, 5) == 'ZEND_') { |
|---|
| 47 | $name = substr($name, 5); |
|---|
| 48 | } |
|---|
| 49 | echo str_pad($name, 25); |
|---|
| 50 | |
|---|
| 51 | foreach ($types as $t => $len) { |
|---|
| 52 | echo str_pad(isset($op[$t]) ? get_op($op[$t]) : "", $len); |
|---|
| 53 | } |
|---|
| 54 | printf("%5s", isset($op['extended_value']) ? $op['extended_value'] : ""); |
|---|
| 55 | |
|---|
| 56 | echo "\n"; |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | function dump_function($name, $func, $indent = '') |
|---|
| 61 | { |
|---|
| 62 | if (isset($func['op_array'])) { |
|---|
| 63 | $op_array = $func['op_array']; |
|---|
| 64 | unset($func['op_array']); |
|---|
| 65 | } |
|---|
| 66 | else { |
|---|
| 67 | $op_array = null; |
|---|
| 68 | } |
|---|
| 69 | var_dump($func); |
|---|
| 70 | echo $indent, 'function ', $name, "\n"; |
|---|
| 71 | if (isset($op_array)) { |
|---|
| 72 | dump_opcodes($op_array['opcodes'], " " . $indent); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | function dump_class($name, $class, $indent = '') |
|---|
| 77 | { |
|---|
| 78 | if (isset($class['function_table'])) { |
|---|
| 79 | $funcs = $class['function_table']; |
|---|
| 80 | unset($class['function_table']); |
|---|
| 81 | } |
|---|
| 82 | else { |
|---|
| 83 | $funcs = null; |
|---|
| 84 | } |
|---|
| 85 | echo $indent, 'class ', $name, "\n"; |
|---|
| 86 | if (isset($funcs)) { |
|---|
| 87 | foreach ($funcs as $name => $func) { |
|---|
| 88 | dump_function($name, $func, " " . $indent); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | if (!isset($argv[1])) { |
|---|
| 94 | die("Usage: $argv[0] <file>\n"); |
|---|
| 95 | } |
|---|
| 96 | $decompiler = new Decompiler(); |
|---|
| 97 | if (isset($argv[2])) { |
|---|
| 98 | eval('$pk = ' . file_get_contents($argv[2]) . ';'); |
|---|
| 99 | } |
|---|
| 100 | else { |
|---|
| 101 | $pk = xcache_dasm_file($argv[1]); |
|---|
| 102 | } |
|---|
| 103 | $op_array = $funcs = $classes = null; |
|---|
| 104 | if (isset($pk['op_array'])) { |
|---|
| 105 | $op_array = $pk['op_array']; |
|---|
| 106 | unset($pk['op_array']); |
|---|
| 107 | } |
|---|
| 108 | if (isset($pk['function_table'])) { |
|---|
| 109 | $funcs = $pk['function_table']; |
|---|
| 110 | unset($pk['function_table']); |
|---|
| 111 | } |
|---|
| 112 | if (isset($pk['class_table'])) { |
|---|
| 113 | $classes = $pk['class_table']; |
|---|
| 114 | unset($pk['class_table']); |
|---|
| 115 | } |
|---|
| 116 | var_dump($pk); |
|---|
| 117 | if (isset($classes)) { |
|---|
| 118 | foreach ($classes as $name => $class) { |
|---|
| 119 | dump_class($name, $class); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | if (isset($funcs)) { |
|---|
| 123 | foreach ($funcs as $name => $func) { |
|---|
| 124 | dump_function($name, $func); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | if (isset($op_array)) { |
|---|
| 128 | dump_opcodes($op_array['opcodes']); |
|---|
| 129 | } |
|---|
| 130 | |
|---|