-
Notifications
You must be signed in to change notification settings - Fork 327
/
jdbg.go
405 lines (340 loc) · 11.4 KB
/
jdbg.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// 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 jdbg provides a simpler interface to the jdwp package, offering
// simple type resolving and method invoking functions.
package jdbg
import (
"fmt"
"strings"
"github.com/google/gapid/core/java/jdwp"
)
type cache struct {
arrays map[string]*Array
classes map[string]*Class
idToSig map[jdwp.ReferenceTypeID]string
objTy *Class
stringTy *Class
numberTy *Class
boolObjTy *Class
byteObjTy *Class
charObjTy *Class
shortObjTy *Class
intObjTy *Class
longObjTy *Class
floatObjTy *Class
doubleObjTy *Class
boolTy *Simple
byteTy *Simple
charTy *Simple
shortTy *Simple
intTy *Simple
longTy *Simple
floatTy *Simple
doubleTy *Simple
voidTy *Simple
}
// JDbg is a wrapper around a JDWP connection that provides an easier interface
// for usage.
type JDbg struct {
conn *jdwp.Connection
thread jdwp.ThreadID
cache cache
objects []jdwp.ObjectID // Objects created that have GC disabled
}
// Do calls f with a JDbg instance, returning the error returned by f.
// If any JDWP errors are raised during the call to f, then execution of f is
// immediately terminated, and the JDWP error is returned.
func Do(conn *jdwp.Connection, thread jdwp.ThreadID, f func(jdbg *JDbg) error) error {
j := &JDbg{
conn: conn,
thread: thread,
cache: cache{
arrays: map[string]*Array{},
classes: map[string]*Class{},
idToSig: map[jdwp.ReferenceTypeID]string{},
},
}
defer func() {
// Reenable GC for all objects used during the call to f()
for _, o := range j.objects {
conn.EnableGC(o)
}
}()
return Try(func() error {
// Prime the cache with basic types.
j.cache.voidTy = &Simple{j: j, ty: jdwp.TagVoid}
j.cache.boolTy = &Simple{j: j, ty: jdwp.TagBoolean}
j.cache.byteTy = &Simple{j: j, ty: jdwp.TagByte}
j.cache.charTy = &Simple{j: j, ty: jdwp.TagChar}
j.cache.shortTy = &Simple{j: j, ty: jdwp.TagShort}
j.cache.intTy = &Simple{j: j, ty: jdwp.TagInt}
j.cache.longTy = &Simple{j: j, ty: jdwp.TagLong}
j.cache.floatTy = &Simple{j: j, ty: jdwp.TagFloat}
j.cache.doubleTy = &Simple{j: j, ty: jdwp.TagDouble}
j.cache.objTy = j.Class("java.lang.Object")
j.cache.stringTy = j.Class("java.lang.String")
j.cache.numberTy = j.Class("java.lang.Number")
j.cache.boolObjTy = j.Class("java.lang.Boolean")
j.cache.byteObjTy = j.Class("java.lang.Byte")
j.cache.charObjTy = j.Class("java.lang.Character")
j.cache.shortObjTy = j.Class("java.lang.Short")
j.cache.intObjTy = j.Class("java.lang.Integer")
j.cache.longObjTy = j.Class("java.lang.Long")
j.cache.floatObjTy = j.Class("java.lang.Float")
j.cache.doubleObjTy = j.Class("java.lang.Double")
// Call f
return f(j)
})
}
// Connection returns the JDWP connection.
func (j *JDbg) Connection() *jdwp.Connection { return j.conn }
// ObjectType returns the Java java.lang.Object type.
func (j *JDbg) ObjectType() *Class { return j.cache.objTy }
// StringType returns the Java java.lang.String type.
func (j *JDbg) StringType() *Class { return j.cache.stringTy }
// NumberType returns the Java java.lang.Number type.
func (j *JDbg) NumberType() *Class { return j.cache.numberTy }
// BoolObjectType returns the Java java.lang.Boolean type.
func (j *JDbg) BoolObjectType() *Class { return j.cache.boolObjTy }
// ByteObjectType returns the Java java.lang.Byte type.
func (j *JDbg) ByteObjectType() *Class { return j.cache.byteObjTy }
// CharObjectType returns the Java java.lang.Character type.
func (j *JDbg) CharObjectType() *Class { return j.cache.charObjTy }
// ShortObjectType returns the Java java.lang.Short type.
func (j *JDbg) ShortObjectType() *Class { return j.cache.shortObjTy }
// IntObjectType returns the Java java.lang.Integer type.
func (j *JDbg) IntObjectType() *Class { return j.cache.intObjTy }
// LongObjectType returns the Java java.lang.Long type.
func (j *JDbg) LongObjectType() *Class { return j.cache.longObjTy }
// FloatObjectType returns the Java java.lang.Float type.
func (j *JDbg) FloatObjectType() *Class { return j.cache.floatObjTy }
// DoubleObjectType returns the Java java.lang.Double type.
func (j *JDbg) DoubleObjectType() *Class { return j.cache.doubleObjTy }
// BoolType returns the Java java.lang.Boolean type.
func (j *JDbg) BoolType() *Simple { return j.cache.boolTy }
// ByteType returns the Java byte type.
func (j *JDbg) ByteType() *Simple { return j.cache.byteTy }
// CharType returns the Java char type.
func (j *JDbg) CharType() *Simple { return j.cache.charTy }
// ShortType returns the Java short type.
func (j *JDbg) ShortType() *Simple { return j.cache.shortTy }
// IntType returns the Java int type.
func (j *JDbg) IntType() *Simple { return j.cache.intTy }
// LongType returns the Java long type.
func (j *JDbg) LongType() *Simple { return j.cache.longTy }
// FloatType returns the Java float type.
func (j *JDbg) FloatType() *Simple { return j.cache.floatTy }
// DoubleType returns the Java double type.
func (j *JDbg) DoubleType() *Simple { return j.cache.doubleTy }
// Type looks up the specified type by signature.
// For example: "Ljava/io/File;"
func (j *JDbg) Type(sig string) Type {
offset := 0
ty, err := j.parseSignature(sig, &offset)
if err != nil {
j.fail("Failed to parse signature: %v", err)
}
return ty
}
// Class looks up the specified class by name.
// For example: "java.io.File"
func (j *JDbg) Class(name string) *Class {
ty := j.Type(fmt.Sprintf("L%s;", strings.Replace(name, ".", "/", -1)))
if class, ok := ty.(*Class); ok {
return class
}
j.fail("Resolved type was not array but %T", ty)
return nil
}
// AllClasses returns all the loaded classes.
func (j *JDbg) AllClasses() []*Class {
classes, err := j.conn.GetAllClasses()
if err != nil {
j.fail("Couldn't get all classes: %v", err)
}
out := []*Class{}
for _, class := range classes {
c, err := j.class(class)
if err != nil {
j.fail("Couldn't get class '%v': %v", class.Signature, err)
}
out = append(out, c)
}
return out
}
// ArrayOf returns the type of the array with specified element type.
func (j *JDbg) ArrayOf(elTy Type) *Array {
ty := j.Type("[" + elTy.Signature())
if array, ok := ty.(*Array); ok {
return array
}
j.fail("Resolved type was not array but %T", ty)
return nil
}
// classFromSig looks up the specified class type by signature.
func (j *JDbg) classFromSig(sig string) (*Class, error) {
if class, ok := j.cache.classes[sig]; ok {
return class, nil
}
class, err := j.conn.GetClassBySignature(sig)
if err != nil {
return nil, err
}
return j.class(class)
}
func (j *JDbg) class(class jdwp.ClassInfo) (*Class, error) {
sig := class.Signature
if cached, ok := j.cache.classes[class.Signature]; ok {
return cached, nil
}
name := strings.Replace(strings.TrimRight(strings.TrimLeft(sig, "[L"), ";"), "/", ".", -1)
ty := &Class{j: j, signature: sig, name: name, class: class}
j.cache.classes[sig] = ty
j.cache.idToSig[class.TypeID] = sig
superid, err := j.conn.GetSuperClass(class.ClassID())
if err != nil {
return nil, err
}
if superid != 0 {
ty.super = j.typeFromID(jdwp.ReferenceTypeID(superid)).(*Class)
}
implementsids, err := j.conn.GetImplemented(class.TypeID)
if err != nil {
return nil, err
}
ty.implements = make([]*Class, len(implementsids))
for i, id := range implementsids {
ty.implements[i] = j.typeFromID(jdwp.ReferenceTypeID(id)).(*Class)
}
ty.fields, err = j.conn.GetFields(class.TypeID)
if err != nil {
return nil, err
}
return ty, nil
}
func (j *JDbg) typeFromID(id jdwp.ReferenceTypeID) Type {
sig, ok := j.cache.idToSig[id]
if !ok {
var err error
sig, err = j.conn.GetTypeSignature(id)
if err != nil {
j.fail("GetTypeSignature() returned: %v", err)
}
j.cache.idToSig[id] = sig
}
return j.Type(sig)
}
// This returns the this object for the current stack frame.
func (j *JDbg) This() Value {
frames, err := j.conn.GetFrames(j.thread, 0, 1)
if err != nil {
j.fail("GetFrames() returned: %v", err)
}
this, err := j.conn.GetThisObject(j.thread, frames[0].Frame)
if err != nil {
j.fail("GetThisObject() returned: %v", err)
}
return j.object(this.Object)
}
func (j *JDbg) String(val string) Value {
str, err := j.conn.CreateString(val)
if err != nil {
j.fail("CreateString() returned: %v", err)
}
return j.object(str)
}
// findArg finds the argument with the given name/index in the given frame
func (j *JDbg) findArg(name string, index int, frame jdwp.FrameInfo) jdwp.VariableRequest {
table, err := j.conn.VariableTable(
jdwp.ReferenceTypeID(frame.Location.Class),
frame.Location.Method)
if err != nil {
j.fail("VariableTable returned: %v", err)
}
variable := jdwp.VariableRequest{-1, 0}
for _, slot := range table.Slots {
if name == slot.Name {
variable.Index = slot.Slot
variable.Tag = slot.Signature[0]
}
}
if variable.Index != -1 {
return variable
}
// Fallback to looking for the argument by index.
slots := table.ArgumentSlots()
// Find the "this" argument. It is always labeled and the first argument slot.
thisSlot := -1
for i, slot := range slots {
if slot.Name == "this" {
thisSlot = i
break
}
}
if thisSlot < 0 {
j.fail("Could not find argument with name %s (no 'this' found)", name)
}
if thisSlot+1+index >= len(slots) {
j.fail("Could not find argument with name %s (not enough slots)", name)
}
variable.Index = slots[thisSlot+1+index].Slot
variable.Tag = slots[thisSlot+1+index].Signature[0]
return variable
}
// GetArgument returns the method argument of the given name and index. First,
// this attempts to retrieve the argument by name, but falls back to looking for
// the argument by index (e.g. in the case the names have been stripped from the
// debug info).
func (j *JDbg) GetArgument(name string, index int) Variable {
frames, err := j.conn.GetFrames(j.thread, 0, 1)
if err != nil {
j.fail("GetFrames() returned: %v", err)
}
variable := j.findArg(name, index, frames[0])
values, err := j.conn.GetValues(j.thread, frames[0].Frame, []jdwp.VariableRequest{variable})
if err != nil {
j.fail("GetValues() returned: %v", err)
}
return Variable{j.value(values[0]), variable}
}
// SetVariable sets the value of the given variable.
func (j *JDbg) SetVariable(variable Variable, val Value) {
frames, err := j.conn.GetFrames(j.thread, 0, 1)
if err != nil {
j.fail("GetFrames() returned: %v", err)
}
v := val.val.(jdwp.Value)
assign := jdwp.VariableAssignmentRequest{variable.variable.Index, v}
err = j.conn.SetValues(j.thread, frames[0].Frame, []jdwp.VariableAssignmentRequest{assign})
if err != nil {
j.fail("GetValues() returned: %v", err)
}
}
func (j *JDbg) object(id jdwp.Object) Value {
tyID, err := j.conn.GetObjectType(id.ID())
if err != nil {
j.fail("GetObjectType() returned: %v", err)
}
ty := j.typeFromID(tyID.Type)
return newValue(ty, id)
}
func (j *JDbg) value(o interface{}) Value {
switch v := o.(type) {
case jdwp.Object:
return j.object(v)
default:
j.fail("Unhandled variable type %T", o)
return Value{}
}
}