Skip to content

Stdlib: Throwable getTrace()/getTraceAsString()/__toString() — VM introspection missing (zend_exceptions.c) #7159

Description

@PurHur

Category

stdlib | runtime

Problem

Builtin Throwable / Exception / Error objects are missing core introspection methods that Zend exposes on every thrown object:

Method Zend PHP 8.x This compiler VM (2026-06-06)
getTrace() true (method_exists) false
getTraceAsString() true false
__toString() echo $e prints message + stack Error: Object of class Exception could not be converted to string

ExceptionTrace::captureOnThrow() already stores a trace property on throw (#3351), but there is no public getTrace() accessor. getPrevious() landed in lib/VM/Builtin/ExceptionGetPrevious.php (#5104 partial) — this issue covers the remaining Throwable surface.

Nested exception string formatting (Next Exception: chain) also depends on __toString() + getPrevious() working together.

php-src reference

Repro

Save as test/repro-maintainer/throwable_introspection.php:

<?php
declare(strict_types=1);

$inner = new Exception('inner');
$outer = new Exception('outer', 0, $inner);

echo 'getTrace ', method_exists($outer, 'getTrace') ? 'yes' : 'no', "\n";
echo 'getTraceAsString ', method_exists($outer, 'getTraceAsString') ? 'yes' : 'no', "\n";

try {
    throw new Exception('x');
} catch (Exception $e) {
    echo 'trace_is_array ', is_array($e->getTrace()) ? 'yes' : 'no', "\n";
    echo 'trace_str ', substr($e->getTraceAsString(), 0, 9), "\n";
}

echo $outer, "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro-maintainer/throwable_introspection.php
php bin/vm.php test/repro-maintainer/throwable_introspection.php'
Check Zend VM today
method_exists(..., 'getTrace') yes no
getTrace() after catch array undefined method
getTraceAsString() prefix #0 undefined method
echo $outer chained text with Next Exception fatal: could not be converted to string

Scope (this repo)

Path Work
lib/VM/Builtin/ExceptionGetTrace.php new — return captured trace property array
lib/VM/Builtin/ExceptionGetTraceAsString.php new — format like debug_backtrace() string renderer
lib/VM/Builtin/ExceptionToString.php new — __toString with previous-chain formatting
lib/VM/BuiltinClasses.php / ext/standard/ThrowableManifest.php register methods on Throwable hierarchy
lib/VM/ExceptionTrace.php ensure trace populated for Error subclasses too
JIT/AOT VM fallback acceptable v1; note in capabilities if deferred
Tests test/compliance/cases/stdlib/throwable_get_trace.phpt, throwable_to_string_chain.phpt

PHP-in-PHP: implement in lib/VM/Builtin/* + manifest; do not add new C in lib/AOT/runtime/.

Done when

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && ./vendor/bin/phpunit --filter throwable_get_trace'

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions