-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Callstack collection and dump & positions for invokable
- Loading branch information
Showing
35 changed files
with
363 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GoPhp\Debug; | ||
|
||
use function array_pop; | ||
use function array_reverse; | ||
use function array_shift; | ||
use function count; | ||
|
||
final class CallStack | ||
{ | ||
/** | ||
* @var list<CallTrace> | ||
*/ | ||
private array $callTraces = []; | ||
|
||
public function __construct( | ||
private readonly int $limit, | ||
) {} | ||
|
||
public function add(CallTrace $callTrace): void | ||
{ | ||
if (count($this->callTraces) > $this->limit) { | ||
array_shift($this->callTraces); | ||
} | ||
|
||
$this->callTraces[] = $callTrace; | ||
} | ||
|
||
public function pop(): void | ||
{ | ||
array_pop($this->callTraces); | ||
} | ||
|
||
/** | ||
* @return list<CallTrace> | ||
*/ | ||
public function getCallTraces(): array | ||
{ | ||
return array_reverse($this->callTraces); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->callTraces); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GoPhp\Debug; | ||
|
||
use GoPhp\Error\InternalError; | ||
use GoPhp\GoValue\AddressableValue; | ||
use GoPhp\GoValue\BuiltinFuncValue; | ||
use GoPhp\InvokableCall; | ||
|
||
final class CallStackCollectorDebugger implements Debugger | ||
{ | ||
public const int DEFAULT_STACK_TRACE_DEPTH = 128; | ||
|
||
private readonly CallStack $callStack; | ||
|
||
public function __construct(int $maxTraceDepth = self::DEFAULT_STACK_TRACE_DEPTH) | ||
{ | ||
$this->callStack = new CallStack($maxTraceDepth); | ||
} | ||
|
||
public function addStackTrace(InvokableCall $call): void | ||
{ | ||
$name = match (true) { | ||
$call->func instanceof AddressableValue => $call->func->getQualifiedName(), | ||
$call->func instanceof BuiltinFuncValue => $call->func->getName(), | ||
default => throw new InternalError(sprintf('unknown call stack value %s', $call->func::class)), | ||
}; | ||
|
||
$this->callStack->add(new CallTrace($name, $call->getPosition())); | ||
} | ||
|
||
public function releaseLastStackTrace(): void | ||
{ | ||
$this->callStack->pop(); | ||
} | ||
|
||
public function getCallStack(): CallStack | ||
{ | ||
return $this->callStack; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GoPhp\Debug; | ||
|
||
use GoParser\Lexer\Position; | ||
|
||
final class CallTrace | ||
{ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly ?Position $position, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GoPhp\Debug; | ||
|
||
use GoPhp\InvokableCall; | ||
|
||
interface Debugger | ||
{ | ||
public function addStackTrace(InvokableCall $call): void; | ||
|
||
public function releaseLastStackTrace(): void; | ||
|
||
public function getCallStack(): CallStack; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GoPhp\Debug; | ||
|
||
use GoParser\Lexer\Position; | ||
|
||
use function realpath; | ||
use function sprintf; | ||
|
||
use const STDERR; | ||
|
||
/** | ||
* Dumps the call stack to STDERR. | ||
*/ | ||
function dump_call_stack(CallStack $callStack): void | ||
{ | ||
$positionDumper = fn(?Position $position): string => $position === null | ||
? 'unknown' | ||
: sprintf( | ||
"%s:%d:%d", | ||
$position->filename === null | ||
? '' | ||
: realpath($position->filename), | ||
$position->line, | ||
$position->offset, | ||
); | ||
|
||
$callTraces = $callStack->getCallTraces(); | ||
$length = $callStack->count(); | ||
|
||
for ($i = 0; $i < $length; $i++) { | ||
if ($i === 0) { | ||
continue; | ||
} | ||
|
||
fwrite(STDERR, sprintf( | ||
"%s(...)\n\t%s\n", | ||
$callTraces[$i]->name, | ||
$positionDumper($callTraces[$i - 1]->position), | ||
)); | ||
} | ||
} |
Oops, something went wrong.