-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathDebuggable.php
More file actions
102 lines (91 loc) Β· 3.3 KB
/
Debuggable.php
File metadata and controls
102 lines (91 loc) Β· 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace PHPJava\Core\Extended;
use PHPJava\Core\JVM\Parameters\GlobalOptions;
use PHPJava\Core\JVM\Parameters\Runtime;
use PHPJava\Exceptions\DebugTraceIsDisabledException;
use PHPJava\Utilities\Formatter;
trait Debuggable
{
/**
* @var mixed[]
*/
private $debugTraces = [];
private $debugTool;
public function appendDebug($log): self
{
$this->debugTraces[] = $log;
return $this;
}
public function debug(): void
{
$isEnabledTrace = $this->options['operations']['enable_trace'] ?? GlobalOptions::get('operations.enable_trace') ?? Runtime::OPERATIONS_ENABLE_TRACE;
if (!$isEnabledTrace) {
throw new DebugTraceIsDisabledException(
'Debug trace is disabled. If you want to show debug trace then enable to `enable_trace` option.'
);
}
$cpInfo = $this->getConstantPool();
foreach ($this->debugTraces as $debugTraces) {
printf("[method]\n");
printf(Formatter::beatifyMethodFromConstantPool($debugTraces['method'], $this->getConstantPool()) . "\n");
printf("\n");
printf("[code]\n");
$codeCounter = 0;
printf(
"%s\n",
implode(
"\n",
array_map(
function ($codes) use (&$codeCounter, &$debugTraces) {
return implode(
' ',
array_map(
function ($code) use (&$codeCounter, &$debugTraces) {
$isMnemonic = in_array($codeCounter, $debugTraces['mnemonic_indexes']);
$codeCounter++;
return ($isMnemonic ? "\e[1m\e[35m" : '') . "<0x{$code}>" . ($isMnemonic ? "\e[m" : '');
},
$codes
)
);
},
array_chunk(str_split(bin2hex($debugTraces['raw_code']), 2), 20)
)
)
);
printf("\n");
printf("[executed]\n");
printf(
"% 8s | %-6.6s | %-20.20s | %-10.10s | %-15.15s\n",
'PC',
'OPCODE',
'MNEMONIC',
'OPERANDS',
'LOCAL STORAGE'
);
$line = sprintf(
"%8s+%8s+%22s+%12s+%17s\n",
'---------',
'--------',
'----------------------',
'------------',
'-----------------'
);
printf($line);
foreach ($debugTraces['executed'] as [$opcode, $mnemonic, $localStorage, $stacks, $pointer]) {
printf(
"% 8s | 0x%02X | %-20.20s | %-10.10s | %-15.15s\n",
(int) $pointer,
$opcode,
// Remove prefix
ltrim($mnemonic, '_'),
count($stacks),
count($localStorage)
);
}
printf($line);
printf("\n");
}
}
}