Skip to content

Instantly share code, notes, and snippets.

@sysang
Last active March 25, 2020 10:25
Show Gist options
  • Save sysang/3af3f480502541a6d20f7e7506470d64 to your computer and use it in GitHub Desktop.
Save sysang/3af3f480502541a6d20f7e7506470d64 to your computer and use it in GitHub Desktop.
Very simple XDebug trace web GUI, forked from https://github.com/splitbrain/xdebug-trace-tree

XDebug Trace Tree

This is a simple PHP script to display XDebug traces in a tree like visualization. It can collapse parts that you're not interested in when analysing the trace, making it much easier to follow the program flow.

Installation is simple. Just clone this dierectory to somewhere in your webserver and it should automatically list all available trace files.

Important: this is meant a personal debugging - it should not be installed on a public webserver (Its passing full file paths).

Screenshot

Screenshot

Recommended xdebug.ini setup:

Refer to https://xdebug.org/docs/stack_trace for detailled xdebug setup info.

xdebug.trace_enable_trigger=1
xdebug.trace_output_dir=/tmp/
xdebug.trace_output_name=xdebug.trace.%t.%R
xdebug.show_mem_delta=1
xdebug.collect_params=4
xdebug.collect_return=1
xdebug.trace_format=1

License

The MIT License (MIT)
Copyright (c) 2016 Andreas Gohr andi@splitbrain.org

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!doctype html>
<html>
<head>
<title>XDebug Trace Tree</title>
<link rel="stylesheet" href="res/style.css">
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<script src="res/script.js"></script>
</head>
<body>
<form method="post" class="load">
<label for="file">File:</label>
<select name="file" id="file">
<?php
$dir = ini_get('xdebug.trace_output_dir');
if (!$dir) {
$dir = '/tmp/';
}
$files = glob("$dir/*.xt");
foreach ($files as $file) {
$checked = ($file == $_REQUEST['file']) ? 'selected="selected"' : '';
echo '<option value="' . htmlspecialchars($file) . '" '.$checked.'>' . htmlspecialchars(basename($file)) . '</option>';
}
?>
</select>
<button type="submit">Load</button>
<br/>
<p>Files are read from <code>xdebug.trace_output_dir = <?php echo htmlspecialchars($dir) ?></code></p>
</form>
<ul class="help">
<li>load a trace file from the dropdown</li>
<li>click a left margin to collapse a whole sub tree</li>
<li>click a function name to collapse all calls to the same function</li>
<li>click the parameter list to expand it</li>
<li>click the return list to expand it</li>
<li>click the time to mark the line important</li>
<li>use checkboxes to hide all PHP internal functions or limit to important lines</li>
</ul>
<form class="options">
<input type="checkbox" value="1" checked="checked" id="internal">
<label for="internal">Show internal functions</label>
<input type="checkbox" value="1" id="marked">
<label for="marked">Show important only (slow)</label>
</form>
<?php
if (!empty($_REQUEST['file'])) {
require_once 'res/XDebugParser.php';
$parser = new XDebugParser($_REQUEST['file']);
$parser->parse();
echo $parser->getTraceHTML();
}
?>
<a href="https://github.com/splitbrain/xdebug-trace-tree">
<img style="position: absolute; top: 0; right: 0; border: 0;"
src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67"
alt="Fork me on GitHub"
data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png">
</a>
</body>
</html>
$(function(){
/* hide blocks */
$('div.d, div.f').click(function (e) {
if (e.target !== this) return;
$(this).toggleClass('hide');
e.preventDefault();
e.stopPropagation();
});
/* collapse parameters */
$('span.params, span.return').click(function (e) {
if (e.target !== this) return;
$(this).toggleClass('short');
e.preventDefault();
e.stopPropagation();
});
/* hide internal funcs */
$('#internal').change(function(){
$('div.i').toggle();
});
/* hide functions */
$('span.name').click(function(e){
if (e.target !== this) return;
var $fn = $(this);
var name = $(this).text();
$("span.name:contains('"+name+"')").closest('div.f').toggleClass('hide');
e.preventDefault();
e.stopPropagation();
});
/* mark important */
$('span.time').click(function(e){
if (e.target !== this) return;
$(this).closest('div.f').toggleClass('mark');
e.preventDefault();
e.stopPropagation();
});
/* hide internal funcs */
$('#marked').change(function(){
$('div.f').toggle();
$('div.f.mark').show();
});
});
label {
cursor: pointer;
}
div.d {
padding-left: 2em;
border-left: 1px solid #ccc;
cursor: pointer;
}
div.d.hide {
height: 5px;
margin-top: 1px;
background-color: #ccc;
}
div.d.hide * {
display: none;
}
div.f.hide {
height: 5px;
margin-top: 1px;
background-color: #a1b7cc;
cursor: pointer;
}
div.f {
cursor: auto;
display: flex;
flex-wrap: nowrap;
width: 100%;
}
div.f.hide * {
display: none;
}
div.f.mark {
background-color: #ffeced;
}
div.f div {
padding: 5px 0;
}
div.f div.func {
flex-grow: 1;
}
div.f div.data {
flex-grow: 0;
width: 600px;
min-width: 600px;
max-width: 600px;
}
span {
display: inline-block;
line-height: 1.1em;
vertical-align: bottom;
}
span.short {
max-height: 1.1em;
overflow: hidden;
word-break: break-all;
}
span.name {
font-weight: bold;
cursor: pointer;
}
span.params {
color: #468b5e;
cursor: pointer;
}
span.return {
color: #8a2200;
cursor: pointer;
}
span.file,
span.time,
span.memorydiff,
span.timediff {
overflow: hidden;
width: 150px;
text-align: right;
white-space: nowrap;
}
span.time {
cursor: pointer;
}
div.header {
width: 100%;
font-weight: bold;
background-color: antiquewhite;
}
form.load {
width: 50%;
float: left;
}
select {
width: 80%;
}
ul.help {
margin: 0;
width: 45%;
float: left;
}
form.options {
clear:both;
}
<?php
class XDebugParser
{
protected $handle;
protected $functions = array();
public function __construct($fileName)
{
$this->handle = fopen($fileName, 'r');
if (!$this->handle) {
throw new Exception("Can't open '$fileName'");
}
$header1 = fgets($this->handle);
$header2 = fgets($this->handle);
if (!preg_match('@Version: [23].*@', $header1) || !preg_match('@File format: [2-4]@', $header2)) {
throw new Exception("This file is not an Xdebug trace file made with format option '1' and version 2 to 4.");
}
}
public function parse()
{
$c = 0;
$size = fstat($this->handle);
$size = $size['size'];
$read = 0;
while (!feof($this->handle)) {
$buffer = fgets($this->handle, 4096);
$read += strlen($buffer);
$this->parseLine($buffer);
$c++;
}
}
function parseLine($line)
{
$parts = explode("\t", $line);
if (count($parts) < 5) {
return;
}
$funcNr = (int) $parts[1];
$type = $parts[2];
switch ($type) {
case '0': // Function enter
$this->functions[$funcNr] = array();
$this->functions[$funcNr]['depth'] = (int) $parts[0];
$this->functions[$funcNr]['time.enter'] = $parts[3];
$this->functions[$funcNr]['memory.enter'] = $parts[4];
$this->functions[$funcNr]['name'] = $parts[5];
$this->functions[$funcNr]['internal'] = !(bool) $parts[6];
$this->functions[$funcNr]['file'] = $parts[8];
$this->functions[$funcNr]['line'] = $parts[9];
if ($parts[7]) {
$this->functions[$funcNr]['params'] = array($parts[7]);
} else {
$this->functions[$funcNr]['params'] = array_slice($parts, 11);
}
// these are set later
$this->functions[$funcNr]['time.exit'] = '';
$this->functions[$funcNr]['memory.exit'] = '';
$this->functions[$funcNr]['time.diff'] = '';
$this->functions[$funcNr]['memory.diff'] = '';
$this->functions[$funcNr]['return'] = '';
break;
case '1': // Function exit
$this->functions[$funcNr]['time.exit'] = $parts[3];
$this->functions[$funcNr]['memory.exit'] = $parts[4];
$this->functions[$funcNr]['time.diff'] = $this->functions[$funcNr]['time.exit'] - $this->functions[$funcNr]['time.enter'];
$this->functions[$funcNr]['memory.diff'] = (int) $this->functions[$funcNr]['memory.exit'] - (int) $this->functions[$funcNr]['memory.enter'];
break;
case 'R'; // Function return
$this->functions[$funcNr]['return'] = $parts[5];
break;
}
}
function getTrace()
{
return $this->functions;
}
function getTraceHTML()
{
ob_start();
echo '<div class="f header">';
echo '<div class="func">Function Call</div>';
echo '<div class="data">';
echo '<span class="file">File:Line</span>';
echo '<span class="timediff">ΔTime</span>';
echo '<span class="memorydiff">ΔMemory</span>';
echo '<span class="time">Time</span>';
echo '</div>';
echo '</div>';
$level = 0;
foreach ($this->functions as $func) {
// depth wrapper
if ($func['depth'] > $level) {
for ($i = $level; $i < $func['depth']; $i++) {
echo '<div class="d">';
}
} elseif ($func['depth'] < $level) {
for ($i = $func['depth']; $i < $level; $i++) {
echo '</div>';
}
}
$level = $func['depth'];
$class = 'f';
if ($func['internal']) {
$class .= ' i';
}
echo '<div class="' . $class . '">';
echo '<div class="func">';
echo '<span class="name">' . htmlspecialchars($func['name']) . '</span>';
echo '(<span class="params short">' . htmlspecialchars(join(",", $func['params'])) . '</span>) ';
if ($func['return'] !== '') {
echo '→ <span class="return short">' . htmlspecialchars($func['return']) . '</span>';
}
echo '</div>';
echo '<div class="data">';
echo '<span class="file" title="'.htmlspecialchars($func['file'].':'.$func['line']).'">'.htmlspecialchars(basename($func['file']).':'.$func['line']).'</span>';
echo '<span class="timediff">' . sprintf('%f', $func['time.diff']) . '</span>';
echo '<span class="memorydiff">' . sprintf('%d', $func['memory.diff']) . '</span>';
echo '<span class="time">' . sprintf('%f', $func['time.enter']) . '</span>';
echo '</div>';
echo '</div>';
}
if ($level > 0) {
for ($i = 0; $i < $level; $i++) {
echo '</div>';
}
}
$html = ob_get_contents();
ob_end_clean();
return $html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment