forked from jasta/android-dev-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproclogcat
More file actions
executable file
·120 lines (99 loc) · 3.18 KB
/
Copy pathproclogcat
File metadata and controls
executable file
·120 lines (99 loc) · 3.18 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env perl
###############################################################################
##
## Simple script designed to insert after "adb logcat" in a pipeline to track a
## specific package's logcat messages.
##
## I typically invoke this script as a function in my profile:
##
## function plogcat() {
## adb logcat | proclogcat $* | coloredlogcat.py
## }
##
## Then run as:
##
## $ plogcat org.devtcg.five
##
###############################################################################
use strict;
use Data::Dumper;
###############################################################################
@ARGV > 0 and not -t STDIN or usage($0);
# We support tracking multiple process names. Fill @trackedNames with the
# process names to match (via regular expressions). The process IDs that we
# are currently tracking will be stored in %trackedPids.
my @trackedNames = @ARGV;
my %trackedPids = ();
# Flush all writes immediately. This is necessary as we expect this script to
# be placed between two other programs in a pipe which outputs text very slowly
# (adb logcat outputs only when events happen), so it's rare to fill up the
# buffer quickly. Without this, the normal buffering that occurs between piped
# programs not directly attached to a pty would prevent the user from seeing
# messages as they arrive.
$| = 1;
# Lookup the pids of the processes before we start. From then on, rely on
# the ActivityManager to tell us as the processes dies and starts.
my $numPids = get_pids();
if ($numPids == 0) {
print "- waiting for process ", join(' or ', @trackedNames), " -\n";
}
while (<STDIN>) {
my $line = $_;
my ($level, $tag, $pid, $message) = $line =~
m/^([A-Z])\/(.*?)\(\s*(\d+)\s*\): (.*)$/;
chomp $message;
if ($tag eq 'ActivityManager') {
if ($message =~ m/^Start proc (.*?) .*?: pid=(\d+) /) {
if (match_name($1)) {
$trackedPids{$2} = 1;
print $line;
}
} elsif ($message =~ m/Process (.*?) \(pid (\d+)\) has died./) {
if (match_name($1)) {
$trackedPids{$2} = 0;
print $line;
}
}
} elsif ($trackedPids{$pid}) {
print $line;
}
}
###############################################################################
sub get_pids {
my @ps = qx{adb shell ps};
if (@ps == 0) {
return -1;
}
my @columns = split /\s+/, (shift @ps);
# There's a "STATE" column slipped in between WCHAN and NAME that has no
# room for a column...
splice @columns, $#columns, 0, 'STATE';
my $numFound = 0;
foreach (@ps) {
s/\s+$//;
my @data = split /\s+/, $_, scalar @columns;
my %row = map { $_ => (shift @data) } @columns;
if (match_name($row{NAME})) {
$trackedPids{$row{PID}} = 1;
$numFound++;
}
}
return $numFound;
}
sub match_name {
my $name = shift;
foreach (@trackedNames) {
return 1 if $_ and $name =~ $_;
}
return 0;
}
sub usage {
my $prog = shift;
die <<"EOF"
Usage: adb logcat | $0 <process-name-regex> [<process2-name-regex> ...]
Process names will be matched against the given regular expression(s),
and only the matching processes will be shown in the output.
To find processes you're interested in, type `adb shell ps' and look
through the list.
EOF
}