forked from corretge/xdebug-trace-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noutrace.class.php
443 lines (366 loc) · 11.1 KB
/
noutrace.class.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
class noutrace
{
public $logDirectory;
public $traceFormat;
public $file;
public $memoryAlarm = 0.3;
public $timeAlarm = 0.03;
public $onlyOneInstruction = '';
public $onlyOneScript = '';
public $customNamespace = 'Corretge\\';
public $filesize;
protected $defFN;
public function __construct()
{
$this->logDirectory = ini_get('xdebug.trace_output_dir');
$this->traceFormat = ini_get('xdebug.trace_format');
ini_set('xdebug.auto_trace', 'Off');
}
public function rtvFiles()
{
$ret = '';
$aFiles = array();
$files = new DirectoryIterator($this->logDirectory);
foreach ($files as $file)
{
if (substr_count($file->getFilename(), '.xt') == 0)
{
continue;
}
$date = explode('.', $file->getFilename());
$date = date('Y-m-d H:i:s', $file->getCTime());
if ($file->getFilename() == $this->file)
{
$jSel = ' selected="selected"';
}
else
{
$jSel = '';
}
$aFiles[$date . uniqid()] = '<option value="' . $file->getFilename() . '" ' . $jSel . '> ' . $date . ' - ' . str_replace('_','-',$file->getFilename()) . '-' . number_format($file->getSize() / 1024,
0,
',',
'.') . '-KB</option>';
}
ksort($aFiles);
return implode("\n", $aFiles);
}
public function aryComp($a, $b)
{
if ($a['cnt'] == $b['cnt'])
{
return 0;
}
/**
* fem-ho desc
*/
return ($a['cnt'] > $b['cnt']) ? -1 : 1;
}
public function usortByArrayKey(&$array, $key, $asc=SORT_ASC)
{
$sort_flags = array(SORT_ASC, SORT_DESC);
if (!in_array($asc, $sort_flags))
throw new InvalidArgumentException('sort flag only accepts SORT_ASC or SORT_DESC');
$cmp = function(array $a, array $b) use ($key, $asc, $sort_flags)
{
if (!is_array($key))
{ //just one key and sort direction
if (!isset($a[$key]) || !isset($b[$key]))
{
throw new Exception('attempting to sort on non-existent keys');
}
if ($a[$key] == $b[$key])
return 0;
return ($asc == SORT_ASC xor $a[$key] < $b[$key]) ? 1 : -1;
} else
{ //using multiple keys for sort and sub-sort
foreach ($key as $sub_key => $sub_asc)
{
//array can come as 'sort_key'=>SORT_ASC|SORT_DESC or just 'sort_key', so need to detect which
if (!in_array($sub_asc, $sort_flags))
{
$sub_key = $sub_asc;
$sub_asc = $asc;
}
//just like above, except 'continue' in place of return 0
if (!isset($a[$sub_key]) || !isset($b[$sub_key]))
{
throw new Exception('attempting to sort on non-existent keys');
}
if ($a[$sub_key] == $b[$sub_key])
continue;
return ($sub_asc == SORT_ASC xor $a[$sub_key] < $b[$sub_key]) ? 1 : -1;
}
return 0;
}
};
usort($array, $cmp);
}
/**
* establim els paràmetres que ens arriben del formulari.
*/
public function setParams()
{
if (isset($_GET['file']))
{
$this->file = basename($_GET['file']);
/**
* mirem que sigui un arxiu vàlid
*/
if (!file_exists($this->logDirectory . '/' . $this->file))
{
throw new Exception("Can't access to file " . $this->logDirectory . '/' . $this->file);
}
$this->filesize = filesize($this->logDirectory . '/' . $this->file);
}
if (isset($_GET['onlyOneInstruction']))
{
$this->onlyOneInstruction = ($_GET['onlyOneInstruction']);
}
if (isset($_GET['onlyOneScript']))
{
$this->onlyOneScript = ($_GET['onlyOneScript']);
}
//$this->memoryAlarm = (float) $_GET['memory'];
//$this->timeAlarm = (float) $_GET['time'];
}
/**
* la mare dels ous, la traça
*
* Sense que serveixi de precedents i per un tema de performance, aquest
* mètode escriurà a stdoutput directament.
*/
public function trace()
{
/**
* recuperem la llista de funcions, les pròpies de PHP hi
* seran sota ['internal']
*/
//$this->defFN = get_defined_functions();
/**
* counter
*/
$jCnt = 0;
/**
* Sumary
*/
$aSumary = array();
$aSumaryS = array();
/**
* inicialitzem alguns camps
*/
$prevLvl = 0;
$prevTim = 0;
$prevMem = 0;
$class = 'odd';
/**
* mirem si ens demanen iniLin
*/
if (!isset($_GET['iniLin']))
{
$_GET['iniLin'] = 0;
$ctrlPrimeraLin = false;
}
else
{
$ctrlPrimeraLin = true;
}
$iniLin = (double) $_GET['iniLin'];
$maxLin = $iniLin + 1024;
/**
* mirem si ens demanen una instrucció concreta, llavors no hi ha limit
*/
$controlDeLinies = (!empty($this->onlyOneInstruction) or !empty($this->onlyOneScript));
$controlInstruction = !empty($this->onlyOneInstruction);
$controlScript = !empty($this->onlyOneScript);
/**
* només acceptarem tipus de traça 1
*/
if ($this->traceFormat != 1)
{
throw new Exception("xdebug.trace_format in /etc/php5/conf.d/xdebug.ini must be 1");
}
$aSteps = array();
/**
* Process all lines
*/
$fh = fopen($this->logDirectory . '/' . $this->file, 'r');
$nRow = 0;
$eof = true;
while ($jReadedLine = fgets($fh))
{
$nRow++;
if (!$controlDeLinies and $nRow < $iniLin)
{
continue;
}
$jData = explode("\t", $jReadedLine);
$jDataCnt = count($jData);
/**
* si es tracta de la capçalera de l'arxiu, la mostrem com a info
*/
if ($jDataCnt == 1)
{
echo "<pre>$jReadedLine</pre>";
continue;
}
/**
* si és el registre de finalització d'una instrucció, la processem
*/
elseif ($jDataCnt == 5)
{
// list($jFLevel, $jFId, $jFPoint, $jFTime, $jFMemory) = $jData;
/**
* Si és el final de tot, no el comptarem, doncs no tenim cap id d'incic
* el mostrarem directament
*/
if ($jData[0] == '')
{
echo "<h3>TOTAL " . number_format(count($aSteps), 0) .
" function/method calls in " . number_format($jData[3], 6) . " ms with " .
number_format(((int) $jData[4]) / 1024, 3) . " KB's </h3>";
}
else
{
continue;
/**
* li restem el temps i la memòria
*/
$aSteps[$jData[1]][3] = number_format((float) $jData[3] - (float) $aSteps[$jData[1]][3],
6);
$aSteps[$jData[1]][4] = number_format((float) $jData[4] - (float) $aSteps[$jData[1]][4],
0);
}
}
/**
* En qualsevol altre cas, és un registre d'inici d'instrucció
*/
else
{
// list($jILevel, $jIId, $jIPoint, $jITime, $jIMemory, $jIFunction,
// $jIType, $jIFile, $jIFilename, $jILine, $jINumParms) = $jData;
If ($prevTim == 0)
{
$prevTim = (float) $jData[3];
$prevMem = (float) $jData[4];
if ($iniLin == 0)
{
continue;
}
}
/**
* procedim a fer la sortida
*/
/**
* si hi ha un canvi de nivell, en funció de si és
* més petit o més gran,
*/
if ($prevLvl < $jData[0])
{
if ($ctrlPrimeraLin)
{
echo str_repeat("<ul>", $jData[0]);
$ctrlPrimeraLin = false;
}
else
{
echo "<ul>";
}
}
elseif ($prevLvl > $jData[0])
{
echo str_repeat("</ul>", $prevLvl - $jData[0]);
}
$prevLvl = $jData[0];
/**
* imprimim, només si es correspon a la instrucció que han demanat.
*/
if (!$controlDeLinies or
($controlInstruction and strpos($this->onlyOneInstruction, $jData[5]) === 0) or
($controlScript and strpos($jData[8], $this->onlyOneScript) !== false)
)
{
echo "<li title=\"{$nRow}\" class=\"{$class}\">";
/**
* @todo fer-ho via CSS
*/
if ($class == 'odd')
{
$class = 'even';
}
else
{
$class = 'odd';
}
echo '<span class="line">';
echo "<a href='trace-code.php?file={$jData[8]}&line={$jData[9]}' target='trace-code'>$jData[9]</a>";
echo "</span>";
echo '<span class="time">';
// echo "ini" . number_format($prevTim, 6) . "<br />";
// echo "end" . number_format((float) $jData[3], 6) . "<br />";
$jSeconds = (float) $jData[3] - $prevTim;
echo number_format($jSeconds * 1000000, 0) . ' µs';
echo "</span>";
echo '<span class="mem">';
// echo "ini" . number_format($prevMem, 0) . "<br />";
// echo "end" . number_format((float) $jData[4], 0) . "<br />";
echo number_format((float) $jData[4] - $prevMem, 0);
echo "</span>";
// list($jILevel, $jIId, $jIPoint, $jITime, $jIMemory, $jIFunction,
// $jIType, $jIFile, $jIFilename, $jILine, $jINumParms) = $jData;
echo '<span class="func">';
echo "<b>{$jData[5]}</b><br/>";
if ($jData[10] > 0)
{
echo "<ul>";
for ($jI = 11; $jI <= 10 + $jData[10]; $jI++)
{
echo "<li class=\"parm\">{$jData[$jI]}</li>";
}
echo "</ul>";
}
elseif (!empty($jData[7]))
{
echo "<ul><li class=\"parm\">{$jData[7]}</li></ul>";
}
echo "<br/>";
echo "<i class=\"pgm\">{$jData[8]}</i>";
echo '</span>';
echo "</li>";
ob_flush();
}
/**
* Si superem el màxim de línies, sortim
*/
if (!$controlDeLinies and $nRow > $maxLin)
{
$eof = false;
break;
}
$prevTim = (float) $jData[3];
$prevMem = (float) $jData[4];
$lastLine = $nRow;
}
}
if (!$eof)
{
$_GET['iniLin'] = $lastLine + 1;
echo "<br /><br><a href=\"{$_SERVER['SCRIPT_NAME']}?";
foreach ($_GET as $parm => $val)
{
echo "{$parm}={$val}&";
}
echo "\">next 1024 lines</a>";
}
}
public function debugMem($line, $method = null)
{
echo "<!-- line {$line} memory " . number_format(memory_get_usage(true), 0);
if (isset($method))
{
echo ' method ' . $method;
}
echo " -->";
}
}