forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace-sandbox-viewer.py
212 lines (179 loc) · 5.86 KB
/
trace-sandbox-viewer.py
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
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Use Chromium tracing to associate mojo endpoints with sandboxes.
This script provides a rich view of where mojo interfaces are hosted
and consumed when Chromium is build with mojo tracing enabled, and
provides a coarser view for default builds or packages such as Chrome
Canary.
## With Mojo Tracing Enabled
1. Enable detailed mojo tracing:
gn args ./out/Default
...
enable_mojo_tracing = true
...
autoninja -C ./out/Default chrome
2. Collect a trace:
chrome.exe --trace-startup=-*,disabled-by-default-sandbox,startup,mojom ^
--trace-startup-file=c:/src/temp/tracing/foo.log ^
--trace-startup-duration=0
Or visit chrome://tracing and enable categories above, save file.
3. Run this script:
trace-sandbox-viewer.py file.log
trace-sandbox-viewer.py file.json.gz
### Output
{
"None(Browser)": [
"(Impl)IPC::mojom::Channel::GetAssociatedInterface",
"(Impl)IPC::mojom::Channel::Receive",
"(Impl)IPC::mojom::Channel::SetPeerPid",
"(Impl)apps::mojom::AppService::RegisterPublisher",
"(Impl)apps::mojom::AppService::RegisterSubscriber",
"(Impl)apps::mojom::Publisher::Connect",
...
"data_decoder::mojom::DataDecoderService::BindJsonParser",
"data_decoder::mojom::JsonParser::Parse",
"data_decoder::mojom::JsonParser::ParseCallback",
...
],
"Network": [
...
],
"Renderer": [
"(Impl)IPC::mojom::Channel::GetAssociatedInterface",
"(Impl)IPC::mojom::Channel::Receive",
"(Impl)IPC::mojom::Channel::SetPeerPid",
"(Impl)autofill::mojom::AutofillAgent::FieldTypePredictionsAvailable",
"(Impl)autofill::mojom::PasswordAutofillAgent::SetLoggingState",
"(Impl)blink::mojom::AppCacheFrontend::CacheSelected",
...
],
"Utility": [
"(Impl)IPC::mojom::Channel::SetPeerPid",
"(Impl)content::mojom::ChildHistogramFetcherFactory::CreateFetcher",
"(Impl)content::mojom::ChildProcess::BindReceiver",
"(Impl)content::mojom::ChildProcess::BindServiceInterface",
"(Impl)content::mojom::ChildProcess::BootstrapLegacyIpc",
"(Impl)content::mojom::ChildProcess::GetBackgroundTracingAgentProvider",
"(Impl)content::mojom::ChildProcess::Initialize",
"(Impl)data_decoder::mojom::DataDecoderService::BindImageDecoder",
"(Impl)data_decoder::mojom::DataDecoderService::BindJsonParser",
"(Impl)data_decoder::mojom::ImageDecoder::DecodeImage",
"(Impl)data_decoder::mojom::ImageDecoder::DecodeImageCallback",
"(Impl)data_decoder::mojom::JsonParser::Parse",
"(Impl)data_decoder::mojom::JsonParser::ParseCallback",
...
],
}
```
## Default Builds
This script is also useful for default builds of Chromium but will
show interfaces in both hosting and consuming processes (as mojom
tracing is limited). Run using the same arguments as above.
### Output.
Note interfaces appear in both host and client contexts:
```
{
"None(Browser)": [
"IPC Channel",
"apps.mojom.AppService",
"apps.mojom.Publisher",
...
"data_decoder.mojom.DataDecoderService",
"data_decoder.mojom.JsonParser",
...
],
"Utility": [
"IPC Channel",
"content.mojom.ChildHistogramFetcherFactory",
"content.mojom.ChildProcess",
"data_decoder.mojom.DataDecoderService",
"data_decoder.mojom.JsonParser",
...
],
...
"Renderer": [
"IPC Channel",
"blink.mojom.AppCacheFrontend",
...
"network.mojom.URLLoaderClient",
"safe_browsing.mojom.PhishingModelSetter",
"safe_browsing.mojom.SafeBrowsing",
...
],
...
}
```
"""
import argparse
import gzip
import json
import sys
def guess_open_file(filename):
if filename.endswith('.gz'):
return gzip.open(filename, mode='rb')
else:
return open(filename, 'rb')
def read_json_events(files, categories):
events = []
for filename in files:
with guess_open_file(filename) as fp:
objs = json.load(fp)
for event in objs['traceEvents']:
if event['cat'] in categories:
events.append(event)
return events
def assign_interfaces_to_sandboxes(events):
# Returns SandboxType:set(Interfaces).
intmap = {}
# Running map of PID:SandboxType
pidmap = {}
# running map of PID:ProcName
procmap = {}
# Assumes events are sorted by timestamp.
for event in events:
# Don't yet know how to remove processes when they finish
# (i.e. which event to match).
if event['cat'] == 'mojom':
pid = event['pid']
interface = event['name']
# If there is a sandbox, use that.
if pid in pidmap:
sbox = pidmap[pid]
if not sbox in intmap:
intmap[sbox] = set()
intmap[sbox].add(interface)
# Otherwise if we saw a process, use that.
elif pid in procmap:
sbox = "None(" + procmap[pid] + ")"
if not sbox in intmap:
intmap[sbox] = set()
intmap[sbox].add(interface)
elif event['cat'] == '__metadata' and event['name'] == 'process_name':
pid = event['pid']
proc = event['args']['name']
procmap[pid] = proc
elif event['cat'] == 'disabled-by-default-sandbox':
pid = int(event['args']['policy']['processIds'][0])
sbox = event['args']['sandboxType']
pidmap[pid] = sbox
return intmap
def output_as_json(interfaces):
dumpable = {}
for host in interfaces:
dumpable[host] = sorted(interfaces[host])
return json.dumps(dumpable, indent=2)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('files', nargs='*', help='file file...')
args = parser.parse_args()
if len(args.files) < 1:
print("Need at least one file")
return 1
events = read_json_events(
args.files,
categories=['mojom', 'disabled-by-default-sandbox', '__metadata'])
interfaces = assign_interfaces_to_sandboxes(events)
print(output_as_json(interfaces))
if __name__ == '__main__':
sys.exit(main())