summaryrefslogtreecommitdiffstats
path: root/useful/ultrastrace.php
blob: f0a3f0da63e1e03626b5435b735f0e8454db8719 (plain) (blame)
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
#!/usr/bin/env php
<?php

if ($argc < 2) die("$argv[0] <tracefile>\n");

function parseTime($time)
{
	if (isset($time['ts'])) return $time['ts'];
	return $time['h'] * 3600 + $time['m'] * 60 + $time['s'];
}

$failed = array();
$found = array();

array_shift($argv);

$unique = false;
if ($argv[0] == '-u') {
	array_shift($argv);
	$unique = true;
}

$brief = false;
if ($argv[0] == '-b') {
	array_shift($argv);
	$brief = true;
}

foreach ($argv as $fname) {
	$fh = fopen($fname, 'r');
	if ($fh === false) die("File not found: $fname\n");
	echo "Reading $fname...\n";
	$lineNo = 0;
	unset($lastLine);

	while (!feof($fh)) {
		$lineNo++;
		$line = fgets($fh);
		// open("/lib/x86_64-linux-gnu/tls/x86_64/libkdeui.so.5", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
		if (preg_match('#^(\d+\s+)?((?<h>\d+):(?<m>\d+):(?<s>\d+(\.\d+)?)|(?<ts>\d+\.\d+))\s#', $line, $out) >= 1) {
			$time = parseTime($out);
			if (isset($lastLine)) {
				if ($time < $lastTime) $lastTime -= 86400;
				$delay = (int)($time - $lastTime);
				if ($delay > 3) {
					echo "\n------------- $delay s Delay detected in $fname at line $lineNo:\n$lastLine$line";
				}
			}
			$lastLine = $line;
			$lastTime = $time;
		}
		if (preg_match('#^(\d+\s+)?(\d+:\d+:\d+\.?\d*\s+|\d+\.\d+\s+)?(?<action>access|stat|lstat|stat64|lstat64|readlink|open|openat|execve)\("(?<lib>[^"]*[^"]*[^/])".*\)\s+=\s+(?<ret>\S+)(\s+(?<err>\S+)\s+|$)#', $line, $out) < 1) continue;
		//echo "Match: {$out['lib']} -> {$out['ret']} ({$out['err']})\n";
		$file = basename($out['lib']);
		$file = preg_replace('/\.so(\.\d+)*$/', '', $file);
		if ($out['ret'] < 0) {
			$str = ':-(    ' . $out['lib'] . ' *** (' . (isset($out['err']) ? $out['err'] : 'Unknown Error') . ') (' . $out[    'action'] . ')';
			if (isset($found[$file])) {
				if (!$unique || !in_array($str, $found[$file]))
					$found[$file][] = $str;
			} else {
				if (!$unique || !isset($failed[$file]) || !in_array($str, $failed[$file]))
					$failed[$file][] = $str;
			}
		} else {
			if (isset($failed[$file])) {
				$found[$file] = $failed[$file];
				unset($failed[$file]);
			}
			$str = ':-)     ' . $out['lib'] . ' (' . $out['action'] . ')';
			if (!$unique || !isset($found[$file]) || !in_array($str, $found[$file]))
				$found[$file][] = $str;
		}
	}
	fclose($fh);
}

if ($brief) {

	foreach ($found as $key => $val) {
		echo "Found: $key\n";
	}
	foreach ($failed as $key => $val) {
		echo "Missing: $key\n";
	}

} else {

	echo "│\n\n\n│ ******* Found libraries *******************************************************\n";
	foreach ($found as $key => $val) {
		echo "│\n├──┬ $key\n";
		$count = count($val);
		$i = 1;
		foreach ($val as $loc) {
			if ($i == $count)	echo "│  ╰── $loc\n";
			else 			echo "│  ├── $loc\n";
			++$i;
		}
	}

	echo "│\n\n\n│ ******* Missing libraries *****************************************************\n";
	foreach ($failed as $key => $val) {
		echo "│\n├──┬ $key\n";
		$count = count($val);
		$i = 1;
		foreach ($val as $loc) {
			if ($i == $count)	echo "│  ╰── $loc\n";
			else 			echo "│  ├── $loc\n";
			++$i;
		}
	}

}