Hello all

I would like to share an idea regarding improving debug information
for ZEND_ASSERT and exceptions.

Why has this become relevant to me? At this stage of development,
asynchronous core code tends to expose hidden bugs that are not
detected in the usual way. I am considering adding a telemetry service
to PHP with the ability to collect information, since in such cases
logging is the only reliable solution.
But this post is not about that.

## The idea

When a regular PHP exception or ASSERT is triggered, output two
backtraces: one for PHP and one for C.
To avoid breaking existing tests, this output could be optionally
suppressed via `php.ini`.

To achieve this, https://github.com/ianlancetaylor/libbacktrace is
used — almost an industry standard!

## How it looks:

```text
edmond@Edmond:~/php-src$ ./sapi/cli/php -d
zend.exception_c_backtrace=1 -r 'throw new RuntimeException("test");'

>>> RuntimeException
    at /home/edmond/php-src/Zend/zend_exceptions.c:178

--- C backtrace ---
  #0  zend_throw_exception_internal
/home/edmond/php-src/Zend/zend_exceptions.c:178
  #1  zend_throw_exception_object
/home/edmond/php-src/Zend/zend_exceptions.c:1043
  #2  ZEND_THROW_SPEC_TMP_HANDLER
/home/edmond/php-src/Zend/zend_vm_execute.h:17281
  #3  execute_ex
/home/edmond/php-src/Zend/zend_vm_execute.h:112392
  #4  zend_execute
/home/edmond/php-src/Zend/zend_vm_execute.h:115483
  #5  zend_eval_stringl
/home/edmond/php-src/Zend/zend_execute_API.c:1491
  #6  zend_eval_stringl_ex
/home/edmond/php-src/Zend/zend_execute_API.c:1533
  #7  zend_eval_string_ex
/home/edmond/php-src/Zend/zend_execute_API.c:1543
  #8  do_cli                         /home/edmond/php-src/sapi/cli/php_cli.c:988
  #9  main
/home/edmond/php-src/sapi/cli/php_cli.c:1369
  #10 __libc_start_call_main         ../sysdeps/nptl/libc_start_call_main.h:58
  #11 __libc_start_main_impl         ../csu/libc-start.c:360
  #12 0x55c9f5e0b444                ??:0
  #13 0xffffffffffffffff            ??:0

--- PHP backtrace ---
  #0  Command line code:1 {main}()

Fatal error: Uncaught RuntimeException: test in Command line code:1
Stack trace:
#0 {main}
  thrown in Command line code on line 1

For Windows, a different library will be required. Maybe someone has ideas?

Thanks

Reply via email to