-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssql.pl
More file actions
executable file
·103 lines (84 loc) · 2.21 KB
/
Copy pathssql.pl
File metadata and controls
executable file
·103 lines (84 loc) · 2.21 KB
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
#!/usr/bin/perl
# Script to convert various strace outputs into a sqlite3 database
# preferered usage: strace -Tttt -f foo
# or: strace -Tttt -f -p $PID
use strict;
use DBI;
use constant COMMIT_LIMIT => 1000;
sub get_handle {
my $filename = $_[0];
return DBI->connect("dbi:SQLite:$filename","","",
{ AutoCommit => 0 });
}
sub strip {
my $text = $_[0];
$text =~ s/\s+$//;
return $text;
}
sub parse1 {
$_ = $_[0];
# case 1 - strace output to file. with -f
if (/^(\d+) (\d+).(\d+) (.+)/) {
return ($1, $2, $3, $4);
}
# case 2 - strace to stdout with -f and a child pid.
elsif (/^\[pid (\d+)\] (\d+).(\d+) (.+)/) {
return ($1, $2, $3, $4);
}
# case 3 strace to stdout with parent pid or not -f
elsif (/^(\d+).(\d+) (.+)/) {
return ('parent', $1, $2, $3);
}
else {
warn "Unrecognised input - are you running strace with -Tttt?";
}
}
sub parse2 {
$_ = $_[0];
# regular return.
if (/^(\w+)\((.+)?= (.+) <([0-9.]+)>$/) {
return ($1, $1."(".strip($2), $3, $4);
}
# sometimes the call doesn't have a duration.
elsif (/^(\w+)\((.+)?= (.+)$/) {
return ($1, $1."(".strip($2), $3, "?");
}
# don't know what this is, log the line.
else {
return ("", $_, "", "?");
}
}
sub create_table {
my $dbh = shift;
$dbh->do(<<'EOT');
CREATE TABLE strace
(id integer primary key,
pid varchar(8),
start time,
mili integer,
syscall text,
full text,
ret text,
dur real);
EOT
}
sub main {
my $dbh = shift;
my $sth = $dbh->prepare("INSERT INTO strace (pid, start, mili, syscall, full, ret, dur) VALUES (?, ?, ?, ?, ?, ?, ?);");
my $n = 0;
while (<STDIN>) {
chomp;
my ($pid, $time, $mili, $base) = parse1($_);
next unless $pid;
my ($syscall, $text, $ret, $dur) = parse2($base);
next unless $text;
$sth->execute($pid, $time, $mili, $syscall, $text, $ret, $dur);
if ($n++ > COMMIT_LIMIT) {
$n = 0; $dbh->commit;
}
}
$dbh->commit;
}
my $dbh = get_handle($ARGV[0] ? $ARGV[0] : 'strace.db');
die "Could not open sqlite database." unless $dbh;
create_table($dbh) && main($dbh);