Skip to content

Instantly share code, notes, and snippets.

@josue
Last active May 12, 2016 13:24
Show Gist options
  • Save josue/4b567494ad45922b246100dd1f9af3ef to your computer and use it in GitHub Desktop.
Save josue/4b567494ad45922b246100dd1f9af3ef to your computer and use it in GitHub Desktop.
xdebugger - Execute php file and outputs xdebug trace and script logs to file for further analyzing.
#/bin/bash
check_xdebug_installed () {
INSTALLED=`php -m | grep xdebug`
if [ "$INSTALLED" = "" ]; then
echo "PHP extention 'xdebug' must be installed."
exit 1
fi
}
# define params
#
ME=`basename "$0" | sed 's/.sh//g'`
OPEN_TRACE=0
PHP_FILE=$1
if [ ! -f "$PHP_FILE" ]; then
echo "PHP file does not exist: $PHP_FILE"
exit 1
fi
if [ "$1" = "-o" ]; then
OPEN_TRACE=1
PHP_FILE=$2
elif [ "$2" = "-o" ]; then
OPEN_TRACE=1
PHP_FILE=$1
fi
FILE_NAME=`echo "$PHP_FILE" | cut -f 1 -d '.'`
TRACE_FILE="$FILE_NAME.out"
LOG_FILE="$FILE_NAME.log"
OUTPUT_DIR="/tmp/xdebugger"
TRACE_DIR="$OUTPUT_DIR/cachegrinds"
LOG_DIR="$OUTPUT_DIR/logs"
TRACE_FILE_PATH="$TRACE_DIR/$TRACE_FILE"
LOG_FILE_PATH="$LOG_DIR/$LOG_FILE"
open_trace_file () {
if [ "$OPEN_TRACE" = "1" ]; then
echo -e "-------------------------------------------------------------------------------"
qcachegrind $TRACE_FILE_PATH
fi
}
run_xdebugger () {
mkdir -p {$TRACE_DIR,$LOG_DIR}
php -d xdebug.profiler_enable=On -d xdebug.profiler_output_dir=$TRACE_DIR -d xdebug.profiler_output_name=$TRACE_FILE $PHP_FILE > $LOG_FILE_PATH
echo
echo -e "Trace: $TRACE_FILE_PATH"
echo -e " Log: $LOG_FILE_PATH"
}
# run thru the actions
check_xdebug_installed
run_xdebugger
open_trace_file
echo
@josue
Copy link
Author

josue commented Apr 29, 2016

  1. Save this file to directory: /usr/local/bin
  2. Make script executable: chmod +x /usr/local/bin/xdebugger

Usage (and output):

$ xdebugger test_script.php

Trace: /tmp/xdebugger/cachegrinds/test_script.out
  Log: /tmp/xdebugger/logs/test_script.log

To open the xdebug trace output with qcachegrind GUI interface, simply call it with the -o option.

$ xdebugger -o test_script.php

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