Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enumag/7a0bf35f020c0174059d7de37e3151f9 to your computer and use it in GitHub Desktop.
Save enumag/7a0bf35f020c0174059d7de37e3151f9 to your computer and use it in GitHub Desktop.
Debugging segmentation faults in PHP in a docker container.
git clone git@github.com:docker-library/php.git docker-library-php
## Go to the specific version you're interested in
cd docker-library-php/7.1/fpm/alpine
## Edit the .Dockerfile.
## Change
## ENV PHP_EXTRA_CONFIGURE_ARGS --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data
## to
## ENV PHP_EXTRA_CONFIGURE_ARGS --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --enable-debug
## Comment out or delete:
## && { find /usr/local/bin /usr/local/sbin -type f -perm +0111 -exec strip --strip-all '{}' + || true; } \
## && docker-php-source delete \
## Now build the container with a tag that make sense.
docker build -t sh41/php:php-7.1.6-fpm-alpine-debug .
## and optionally push to a repo.
## (might need to docker login)
docker push sh41/php:php-7.1.6-fpm-alpine-debug
## now use this newly built container in place of the non-debug one.
## Set the location of the core dump to /tmp
## On linux host
echo "/tmp/core-%e.%p" > /proc/sys/kernel/core_pattern
## On Docker for Windows this requires a bit of a dodgy hack on the MobyVM
## Get a privliged prompt in a container (but accessing the host)
docker run --net=host --ipc=host --uts=host --pid=host -it --security-opt=seccomp=unconfined --privileged --rm -v /:/host alpine sh
echo "/tmp/core-%e.%p" > /proc/sys/kernel/core_pattern
exit
## For CLI segfaults get a shell in the container
## Compose
## docker-compose exec php-fpm sh
## Standalone
docker run sh41/php:php-7.1.6-fpm-alpine-debug sh
## Set core size as per https://bugs.php.net/bugs-generating-backtrace.php
ulimit -c unlimited
## check that the container has correct core location
cat /proc/sys/kernel/core_pattern
## /tmp/core-%e.%p
## Check that PHP has symbols (look for "not stripped" in the following:
file `which php`
#/usr/local/bin/php: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, not stripped
## Install gdb (example for alpine other distros differ
apk add --no-cache gdb
## Run the seg faulting program
/usr/local/bin/php somedyingfile.php
## find the core
ls -lrt /tmp/core-php*
## Do some debugging
gdb /usr/local/bin/php /tmp/core-php.123
## in gdb import php helper macros
(gdb) source /usr/src/php/.gdbinit
## use zbacktrace to get a sensible backtrace
(gdb) zbacktrace
[0x7f7af1815ff0] Symfony\Component\VarDumper\Cloner\VarCloner->doClone(array(7)[0x7f7af1816040]) /var/www/html/vendor/symfony/symfony/src/Symfony/Component/VarDumper/Cloner/VarCloner.php:123
[0x7f7af1815f50] Symfony\Component\VarDumper\Cloner\AbstractCloner->cloneVar(array(7)[0x7f7af1815fa0]) /var/www/html/vendor/symfony/symfony/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php:213
[0x7f7af1815ed0] Symfony\Component\HttpKernel\DataCollector\DataCollector->cloneVar(array(7)[0x7f7af1815f20]) /var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php:81
...etc.
@enumag
Copy link
Author

enumag commented Mar 30, 2022

Screenshot from 2022-03-30 12-13-45
Screenshot from 2022-03-30 12-13-52
Screenshot from 2022-03-30 12-14-15

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