|
8 | 8 | use PHPJava\Core\JVM\ConstantPool; |
9 | 9 | use PHPJava\Core\JVM\Validations\MagicByte; |
10 | 10 | use PHPJava\Exceptions\ValidatorException; |
| 11 | +use PHPJava\Kernel\Maps\AccessFlag; |
11 | 12 | use PHPJava\Kernel\Structures\_Utf8; |
| 13 | +use PHPJava\Utilities\Formatter; |
12 | 14 |
|
13 | 15 | class JavaClass |
14 | 16 | { |
@@ -53,6 +55,8 @@ class JavaClass |
53 | 55 | */ |
54 | 56 | private $className = null; |
55 | 57 |
|
| 58 | + private $debugTraces = []; |
| 59 | + |
56 | 60 | /** |
57 | 61 | * JavaClass constructor. |
58 | 62 | * @param JavaClassReader $reader |
@@ -138,4 +142,121 @@ public function getInvoker(): JavaClassInvoker |
138 | 142 | { |
139 | 143 | return new JavaClassInvoker($this); |
140 | 144 | } |
| 145 | + |
| 146 | + public function appendDebug($log) |
| 147 | + { |
| 148 | + $this->debugTraces[] = $log; |
| 149 | + return $this; |
| 150 | + } |
| 151 | + |
| 152 | + public function debug(): void |
| 153 | + { |
| 154 | + $cpInfo = $this->getConstantPool()->getEntries(); |
| 155 | + foreach ($this->debugTraces as $debugTraces) { |
| 156 | + $methodAccessFlags = $debugTraces['method']->getAccessFlag(); |
| 157 | + $accessFlags = []; |
| 158 | + $accessFlag = new AccessFlag(); |
| 159 | + foreach ($accessFlag->getValues() as $value) { |
| 160 | + if (($methodAccessFlags & $value) !== 0) { |
| 161 | + $accessFlags[] = strtolower(str_replace('_', '', $accessFlag->getName($value))); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + $methodName = $cpInfo[$debugTraces['method']->getNameIndex()]->getString(); |
| 166 | + $descriptor = Formatter::parseSignature($cpInfo[$debugTraces['method']->getDescriptorIndex()]->getString()); |
| 167 | + $formattedArguments = str_replace( |
| 168 | + '/', |
| 169 | + '.', |
| 170 | + implode( |
| 171 | + ', ', |
| 172 | + array_map( |
| 173 | + function ($argument) { |
| 174 | + $arrayBrackets = str_repeat('[]', $argument['deep_array']); |
| 175 | + if ($argument['type'] === 'class') { |
| 176 | + return $argument['class_name'] . $arrayBrackets; |
| 177 | + } |
| 178 | + return $argument['type'] . $arrayBrackets; |
| 179 | + }, |
| 180 | + $descriptor['arguments'] |
| 181 | + ) |
| 182 | + ) |
| 183 | + ); |
| 184 | + |
| 185 | + |
| 186 | + $type = $descriptor[0]['type']; |
| 187 | + if ($type === 'class') { |
| 188 | + $type = $descriptor[0]['class_name']; |
| 189 | + } |
| 190 | + |
| 191 | + $type = str_replace('/', '.', $type); |
| 192 | + |
| 193 | + $methodAccessibility = implode(' ', $accessFlags); |
| 194 | + |
| 195 | + printf("[method]\n"); |
| 196 | + printf(ltrim("$methodAccessibility $type $methodName($formattedArguments)\n", ' ')); |
| 197 | + printf("\n"); |
| 198 | + printf("[code]\n"); |
| 199 | + |
| 200 | + $codeCounter = 0; |
| 201 | + printf( |
| 202 | + "%s\n", |
| 203 | + implode( |
| 204 | + "\n", |
| 205 | + array_map( |
| 206 | + function ($codes) use (&$codeCounter, &$debugTraces) { |
| 207 | + return implode( |
| 208 | + ' ', |
| 209 | + array_map( |
| 210 | + function ($code) use (&$codeCounter, &$debugTraces) { |
| 211 | + $isMnemonic = in_array($codeCounter, $debugTraces['mnemonic_indexes']); |
| 212 | + $codeCounter++; |
| 213 | + return ($isMnemonic ? "\e[1m\e[35m" : "") . "<0x{$code}>" . ($isMnemonic ? "\e[m" : ""); |
| 214 | + }, |
| 215 | + $codes |
| 216 | + ) |
| 217 | + ); |
| 218 | + }, |
| 219 | + array_chunk(str_split(bin2hex($debugTraces['raw_code']), 2), 20) |
| 220 | + ) |
| 221 | + ) |
| 222 | + ); |
| 223 | + printf("\n"); |
| 224 | + printf("[executed]\n"); |
| 225 | + |
| 226 | + printf( |
| 227 | + "% 8s | %-6.6s | %-20.20s | %-10.10s | %-15.15s\n", |
| 228 | + "PC", |
| 229 | + "OPCODE", |
| 230 | + "MNEMONIC", |
| 231 | + "OPERANDS", |
| 232 | + "LOCAL STORAGE" |
| 233 | + ); |
| 234 | + |
| 235 | + $line = sprintf( |
| 236 | + "%8s+%8s+%22s+%12s+%17s\n", |
| 237 | + "---------", |
| 238 | + "--------", |
| 239 | + "----------------------", |
| 240 | + "------------", |
| 241 | + "-----------------" |
| 242 | + ); |
| 243 | + |
| 244 | + printf($line); |
| 245 | + |
| 246 | + foreach ($debugTraces['executed'] as [$opcode, $mnemonic, $localStorage, $stacks, $pointer]) { |
| 247 | + printf( |
| 248 | + "% 8s | 0x%02X | %-20.20s | %-10.10s | %-15.15s\n", |
| 249 | + (int) $pointer, |
| 250 | + $opcode, |
| 251 | + // Remove prefix |
| 252 | + ltrim($mnemonic, '_'), |
| 253 | + count($stacks), |
| 254 | + count($localStorage) |
| 255 | + ); |
| 256 | + } |
| 257 | + |
| 258 | + printf($line); |
| 259 | + printf("\n"); |
| 260 | + } |
| 261 | + } |
141 | 262 | } |
0 commit comments