summaryrefslogtreecommitdiffstats
path: root/res/XDebugParser.php
blob: acb504616cf0f56bf40261caffa0bf830b84e617 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?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'] = $this->functions[$funcNr]['memory.exit'] - $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;
    }

}