-
Notifications
You must be signed in to change notification settings - Fork 328
/
jdwp_loader.go
197 lines (173 loc) · 6.12 KB
/
jdwp_loader.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
// 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"
"fmt"
"io"
"net"
"reflect"
"time"
"github.com/google/gapid/core/event/task"
"github.com/google/gapid/core/java/jdbg"
"github.com/google/gapid/core/java/jdwp"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/android/adb"
"github.com/google/gapid/core/os/device"
"github.com/google/gapid/gapidapk"
)
func expect(r io.Reader, expected []byte) error {
got := make([]byte, len(expected))
if _, err := io.ReadFull(r, got); err != nil {
return err
}
if !reflect.DeepEqual(expected, got) {
return fmt.Errorf("Expected %v, got %v", expected, got)
}
return nil
}
// waitForOnCreate waits for android.app.Application.onCreate to be called, and
// then suspends the thread.
func waitForOnCreate(ctx context.Context, conn *jdwp.Connection, wakeup jdwp.ThreadID) (*jdwp.EventMethodEntry, error) {
app, err := conn.GetClassBySignature("Landroid/app/Application;")
if err != nil {
return nil, err
}
onCreate, err := conn.GetClassMethod(app.ClassID(), "onCreate", "()V")
if err != nil {
return nil, err
}
return conn.WaitForMethodEntry(ctx, app.ClassID(), onCreate.ID, wakeup)
}
// waitForVulkanLoad for android.app.ApplicationLoaders.getClassLoader to be called,
// and then suspends the thread.
// This function is what is used to tell the vulkan loader where to search for
// layers.
func waitForVulkanLoad(ctx context.Context, conn *jdwp.Connection) (*jdwp.EventMethodEntry, error) {
loaders, err := conn.GetClassBySignature("Landroid/app/ApplicationLoaders;")
if err != nil {
return nil, err
}
getClassLoader, err := conn.GetClassMethod(loaders.ClassID(), "getClassLoader",
"(Ljava/lang/String;IZLjava/lang/String;Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;")
if err != nil {
return nil, err
}
return conn.WaitForMethodEntry(ctx, loaders.ClassID(), getClassLoader.ID, 0)
}
// loadLibrariesViaJDWP connects to the application waiting for a JDWP
// connection with the specified process id, sends a number of JDWP commands to
// load the list of libraries.
func loadLibrariesViaJDWP(ctx context.Context, gapidAPK *gapidapk.APK, pid int, d adb.Device) error {
const (
reconnectAttempts = 10
reconnectDelay = time.Second
)
ctx, stop := task.WithCancel(ctx)
defer stop()
jdwpPort, err := adb.LocalFreeTCPPort()
if err != nil {
return log.Err(ctx, err, "Finding free port")
}
ctx = log.V{"jdwpPort": jdwpPort}.Bind(ctx)
log.I(ctx, "Forwarding TCP port %v -> JDWP pid %v", jdwpPort, pid)
if err := d.Forward(ctx, adb.TCPPort(jdwpPort), adb.Jdwp(pid)); err != nil {
return log.Err(ctx, err, "Setting up JDWP port forwarding")
}
defer d.RemoveForward(ctx, adb.TCPPort(jdwpPort))
log.I(ctx, "Connecting to JDWP")
// Create a JDWP connection with the application.
var sock net.Conn
var conn *jdwp.Connection
for i := 0; i < reconnectAttempts; i++ {
if sock, err = net.Dial("tcp", fmt.Sprintf("localhost:%v", jdwpPort)); err == nil {
if conn, err = jdwp.Open(ctx, sock); err == nil {
break
}
sock.Close()
}
log.I(ctx, "Failed to connect: %v", err)
time.Sleep(reconnectDelay)
}
if err != nil {
return log.Err(ctx, err, "Connecting to JDWP")
}
defer func() {
stop()
sock.Close()
}()
classLoaderThread := jdwp.ThreadID(0)
processABI := func(j *jdbg.JDbg) (*device.ABI, error) {
abiName := j.Class("android.os.Build").Field("CPU_ABI").Get().(string)
abi := device.ABIByName(abiName)
if abi == nil {
return nil, fmt.Errorf("Unknown ABI %v", abiName)
}
return abi, nil
}
loadGAPII := func(j *jdbg.JDbg) error {
abi, err := processABI(j)
if err != nil {
return err
}
interceptorPath := gapidAPK.LibInterceptorPath(abi)
gapiiPath := gapidAPK.LibGAPIIPath(abi)
ctx = log.V{"gapii.so": gapiiPath, "process abi": abi.Name}.Bind(ctx)
// Load the library.
log.D(ctx, "Loading GAPII library...")
// Work around for loading libraries in the N previews. See b/29441142.
j.Class("java.lang.Runtime").Call("getRuntime").Call("doLoad", interceptorPath, nil)
j.Class("java.lang.Runtime").Call("getRuntime").Call("doLoad", gapiiPath, nil)
log.D(ctx, "Library loaded")
return nil
}
log.I(ctx, "Waiting for ApplicationLoaders.getClassLoader()")
if getClassLoader, err := waitForVulkanLoad(ctx, conn); err == nil {
// If err != nil that means we could not find or break in getClassLoader
// so we have no vulkan support.
classLoaderThread = getClassLoader.Thread
err = jdbg.Do(conn, getClassLoader.Thread, func(j *jdbg.JDbg) error {
abi, err := processABI(j)
if err != nil {
return err
}
libsPath := gapidAPK.LibsPath(abi)
newLibraryPath := j.String(":" + libsPath)
obj := j.GetStackObject("librarySearchPath").Call("concat", newLibraryPath)
j.SetStackObject("librarySearchPath", obj)
// If successfully loaded vulkan support, then we should be good to go
// load libgapii and friends here.
return loadGAPII(j)
})
if err != nil {
return log.Err(ctx, err, "JDWP failure")
}
}
// If we did not have vulkan support, then we should try to load with
// Application.onCreate().
if classLoaderThread == jdwp.ThreadID(0) {
// Wait for Application.onCreate to be called.
log.I(ctx, "Waiting for Application.onCreate()")
onCreate, err := waitForOnCreate(ctx, conn, classLoaderThread)
if err != nil {
return log.Err(ctx, err, "Waiting for Application.OnCreate")
}
// Create a JDbg session to install and load the libraries.
log.I(ctx, "Installing interceptor libraries")
if err := jdbg.Do(conn, onCreate.Thread, loadGAPII); err != nil {
return log.Err(ctx, err, "JDWP failure")
}
}
return nil
}