-
Notifications
You must be signed in to change notification settings - Fork 328
/
adb.go
236 lines (206 loc) · 7.48 KB
/
adb.go
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
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"context"
"net"
"time"
"github.com/google/gapid/core/app"
"github.com/google/gapid/core/context/keys"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/android"
"github.com/google/gapid/core/os/android/adb"
"github.com/google/gapid/core/os/device"
"github.com/google/gapid/core/os/device/bind"
"github.com/google/gapid/core/os/flock"
"github.com/google/gapid/core/text"
"github.com/google/gapid/gapidapk"
"github.com/pkg/errors"
)
const (
// getPidRetries is the number of retries for getting the pid of the process
// our newly-started activity runs in.
getPidRetries = 7
// vkImplicitLayersProp is the name of the system property that contains implicit
// Vulkan layers to be loaded by Vulkan loader on Android
vkImplicitLayersProp = "debug.vulkan.layers"
)
// Process represents a running process to capture.
type Process struct {
// The local host port used to connect to GAPII.
Port int
// Information about the target device.
Device bind.Device
// The options used for the capture.
Options Options
// The connection
conn net.Conn
}
// Start launches an activity on an android device with the GAPII interceptor
// enabled using the gapid.apk built for the ABI matching the specified action and device.
// GAPII will attempt to connect back on the returned host port to write the trace.
func Start(ctx context.Context, p *android.InstalledPackage, a *android.ActivityAction, o Options) (*Process, app.Cleanup, error) {
ctx = log.Enter(ctx, "start")
if a != nil {
ctx = log.V{"activity": a.Activity}.Bind(ctx)
}
ctx = log.V{"on": p.Name}.Bind(ctx)
d := p.Device.(adb.Device)
abi := p.ABI
if abi.SameAs(device.UnknownABI) {
abi = p.Device.Instance().GetConfiguration().PreferredABI(nil)
}
// For NativeBridge emulated devices opt for the native ABI of the emulator.
abi = d.NativeBridgeABI(ctx, abi)
ctx = log.V{"abi": abi.Name}.Bind(ctx)
log.I(ctx, "Unlocking device screen")
unlocked, err := d.UnlockScreen(ctx)
if err != nil {
log.W(ctx, "Failed to determine lock state: %s", err)
} else if !unlocked {
return nil, nil, log.Err(ctx, nil, "Please unlock your device screen: GAPID can automatically unlock the screen only when no PIN/password/pattern is needed")
}
port, err := adb.LocalFreeTCPPort()
if err != nil {
return nil, nil, log.Err(ctx, err, "Finding free port for gapii")
}
log.I(ctx, "Checking gapid.apk is installed")
apk, err := gapidapk.EnsureInstalled(ctx, d, abi)
if err != nil {
return nil, nil, log.Err(ctx, err, "Installing gapid.apk")
}
ctx = log.V{"port": port}.Bind(ctx)
log.I(ctx, "Forwarding")
pipe := "gapii"
if o.PipeName != "" {
pipe = o.PipeName
}
if err := d.Forward(ctx, adb.TCPPort(port), adb.NamedAbstractSocket(pipe)); err != nil {
return nil, nil, log.Err(ctx, err, "Setting up port forwarding for gapii")
}
cleanup := app.Cleanup(func(ctx context.Context) {
d.RemoveForward(ctx, port)
})
isVulkan := o.APIs&VulkanAPI != uint32(0)
useLayers := android.SupportsLayersViaSystemSettings(d)
if useLayers {
log.I(ctx, "Setting up Layer")
cu, err := android.SetupLayer(ctx, d, p.Name, gapidapk.PackageName(abi), gapidapk.LayerName(isVulkan), isVulkan)
if err != nil {
return nil, cleanup.Invoke(ctx), log.Err(ctx, err, "Setting up the layer")
}
cleanup = cleanup.Then(cu)
} else if isVulkan {
m, err := reserveVulkanDevice(ctx, d)
if err != nil {
return nil, cleanup.Invoke(ctx), log.Err(ctx, err, "Setting up for tracing Vulkan")
}
cleanup = cleanup.Then(func(ctx context.Context) {
releaseVulkanDevice(ctx, d, m)
})
}
var additionalArgs []android.ActionExtra
if o.AdditionalFlags != "" {
additionalArgs = append(additionalArgs, android.CustomExtras(text.Quote(text.SplitArgs(o.AdditionalFlags))))
}
if a != nil {
if useLayers {
log.I(ctx, "Starting activity")
if err := d.StartActivity(ctx, *a, additionalArgs...); err != nil {
return nil, cleanup.Invoke(ctx), log.Err(ctx, err, "Starting activity")
}
} else {
log.I(ctx, "Starting activity in debug mode")
if err := d.StartActivityForDebug(ctx, *a, additionalArgs...); err != nil {
return nil, cleanup.Invoke(ctx), log.Err(ctx, err, "Starting activity in debug mode")
}
}
} else {
log.I(ctx, "No start activity selected - trying to attach...")
}
var pid int
err = android.ErrProcessNotFound
for attempt := 0; attempt <= getPidRetries && errors.Cause(err) == android.ErrProcessNotFound; attempt++ {
time.Sleep(time.Duration(attempt*100) * time.Millisecond)
pid, err = p.Pid(ctx)
}
if err != nil {
return nil, cleanup.Invoke(ctx), log.Err(ctx, err, "Getting pid")
}
ctx = log.V{"pid": pid}.Bind(ctx)
process := &Process{
Port: int(port),
Device: d,
Options: o,
}
if !useLayers {
if err := process.loadAndConnectViaJDWP(ctx, apk, pid, d); err != nil {
return nil, cleanup.Invoke(ctx), err
}
}
return process, cleanup, nil
}
// Connect connects to an app that is already setup to trace. This is similar to
// Start(...), except that it skips some steps as it is assumed that the loading
// of libgapii is done manually and the app is waiting for a connection from the
// host.
func Connect(ctx context.Context, d adb.Device, abi *device.ABI, pipe string, o Options) (*Process, error) {
port, err := adb.LocalFreeTCPPort()
if err != nil {
return nil, log.Err(ctx, err, "Finding free port for gapii")
}
ctx = log.V{"port": port}.Bind(ctx)
log.I(ctx, "Checking gapid.apk is installed")
apk, err := gapidapk.EnsureInstalled(ctx, d, abi)
if err != nil {
return nil, log.Err(ctx, err, "Installing gapid.apk")
}
log.I(ctx, "Forwarding")
if err := d.Forward(ctx, adb.TCPPort(port), adb.NamedAbstractSocket(pipe)); err != nil {
return nil, log.Err(ctx, err, "Setting up port forwarding for gapii")
}
process := &Process{
Port: int(port),
Device: d,
Options: o,
}
interceptorPath := apk.LibInterceptorPath(abi)
if err := process.connect(ctx, 0, interceptorPath); err != nil {
return nil, err
}
return process, nil
}
// reserveVulkanDevice reserves the given device for starting Vulkan trace and
// set the implicit Vulkan layers property to let the Vulkan loader loads
// GraphicsSpy layer. It returns the mutex which reserves the device and error.
func reserveVulkanDevice(ctx context.Context, d adb.Device) (*flock.Mutex, error) {
m := flock.Lock(d.Instance().GetSerial())
if err := d.SetSystemProperty(ctx, vkImplicitLayersProp, "GraphicsSpy"); err != nil {
return nil, log.Err(ctx, err, "Setting up vulkan layer")
}
return m, nil
}
// releaseVulkanDevice checks if the given mutex is nil, and if not, unsets the
// implicit Vulkan layers property on the given Android device and release the
// lock in the given mutex.
func releaseVulkanDevice(ctx context.Context, d adb.Device, m *flock.Mutex) error {
if m != nil {
ctx = keys.Clone(context.Background(), ctx)
if err := d.SetSystemProperty(ctx, vkImplicitLayersProp, ""); err != nil {
return err
}
m.Unlock()
}
return nil
}