Skip to content

Generating a trace file using Xdebug

Simon Frost edited this page Mar 16, 2019 · 2 revisions

Configure Xdebug

Enable the following settings:

; xdebug.auto_trace = 1
; Type: boolean, Default value: 0
; When this setting is set to on, the tracing of function calls will be enabled just before the script is run.

xdebug.trace_enable_trigger = 1
; Type: boolean, Default value: 0
; When this setting is set to 1, you can trigger the generation of trace files by using the XDEBUG_TRACE GET/POST parameter, 
; or set a cookie with the name XDEBUG_TRACE. This will then write the trace data to defined directory. 
; In order to prevent Xdebug to generate trace files for each request, you need to set xdebug.auto_trace to 0. 
; Access to the trigger itself can be configured through xdebug.trace_enable_trigger_value.

auto_prepend_file = /var/www/html/watson/watson-safari/xdebug_filter_trace_auto_prepend_file.php
; Type: string, Default value: null
; Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require function, so include_path is used.
; The special value none disables auto-prepending.
; @see https://secure.php.net/manual/en/ini.core.php#ini.auto-prepend-file

xdebug.trace_output_dir = /var/www/html/watson/watson-safari/xdebug-trace-files
; Type: string, Default value: /tmp
; The directory where the tracing files will be written to, make sure that the user who the PHP will be running as has write permissions to that directory.

xdebug.trace_output_name = xdebug_trace.watson-pathfinder.%s
; This setting determines the name of the file that is used to dump traces into.
; Specifier     Meaning                         Example Format  Example Filename
; %H            $_SERVER['HTTP_HOST']           trace.%H        trace.kossu.xt
; %u		timestamp (microseconds)	trace.%u	trace.1179434749_642382.xt

xdebug.trace_format = 1
; Type: integer, Default value: 1
; The format of the trace file.
; 
; Value	Description
; 0	Shows a human readable indented trace file with: time index, memory usage, memory delta (if the setting xdebug.show_mem_delta is enabled), level, function name, 
;	function parameters (if the setting xdebug.collect_params is enabled), filename and line number.
; 1	Writes a computer readable format which has two different records. There are different records for entering a stack frame, and leaving a stack frame. 
;	The table below lists the fields in each type of record. Fields are tab separated.
; 2	Writes a trace formatted in (simple) HTML.
; Fields for the computerized format:
;
; Record type	1	2		3		4		5		6		7						8					9		10	11	12 - ...
; Entry		level	function #	always '0'	time index	memory usage	function name	user-defined (1) or internal function (0)	name of the include/require file	filename	line number	no. of parameters	parameters (as many as specified in field 11) - tab separated
; Exit		level	function #	always '1'	time index	memory usage	empty
; Return	level	function #	always 'R'	empty		return value	empty
;
; See the introduction of Function Traces for a few examples.

Enable the tracing for this request

If you are profiling a web request, drop the XDEBUG_TRACE cookie. If you are profiling a CLI request, enable the xdebug.auto_trace feature.

Trace the request

Run the script as normal. The trace files should appear in the location defined in xdebug.trace_output_dir. The files can become extremely big very quickly. More so if the more verbose reporting options are defined.

Trace filtering

We are not interested in any third-party libraries, or tools used in the intialisation of the request we're tracing, so these are filtered out by Xdebug.

The filtering uses a whitelisting approach, so only files in Magento-specific directories are added to the trace file.

The filtering is configured in xdebug_filter_trace_auto_prepend_file.php.

<?php

/**
 * @see https://xdebug.org/docs/all_functions#xdebug_set_filter
 */
xdebug_set_filter(
    XDEBUG_FILTER_TRACING,
    XDEBUG_PATH_WHITELIST,
    [
        '/var/www/html/magento225-example-modules/htdocs/app/',
        '/var/www/html/magento225-example-modules/htdocs/bin/',
        '/var/www/html/magento225-example-modules/htdocs/generated/',
        '/var/www/html/magento225-example-modules/htdocs/lib/',
        '/var/www/html/magento225-example-modules/htdocs/pub/',
        '/var/www/html/magento225-example-modules/htdocs/setup/',
        '/var/www/html/magento225-example-modules/htdocs/update/',
        '/var/www/html/magento225-example-modules/htdocs/var/',
        '/var/www/html/magento225-example-modules/htdocs/vendor/magento/',
    ]
);