-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathHahaHelper.java
More file actions
214 lines (190 loc) · 7.36 KB
/
HahaHelper.java
File metadata and controls
214 lines (190 loc) · 7.36 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
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
/*
* Copyright (C) 2015 Square, 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 com.hprof.bitmap;
import com.squareup.haha.perflib.ArrayInstance;
import com.squareup.haha.perflib.ClassInstance;
import com.squareup.haha.perflib.ClassObj;
import com.squareup.haha.perflib.Instance;
import com.squareup.haha.perflib.Type;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static java.util.Arrays.asList;
public final class HahaHelper {
private static final Set<String> WRAPPER_TYPES = new HashSet<>(
asList(Boolean.class.getName(), Character.class.getName(), Float.class.getName(),
Double.class.getName(), Byte.class.getName(), Short.class.getName(),
Integer.class.getName(), Long.class.getName()));
static String threadName(Instance holder) {
List<ClassInstance.FieldValue> values = classInstanceValues(holder);
Object nameField = fieldValue(values, "name");
if (nameField == null) {
// Sometimes we can't find the String at the expected memory address in the heap dump.
// See https://github.com/square/leakcanary/issues/417 .
return "Thread name not available";
}
return asString(nameField);
}
static boolean extendsThread(ClassObj clazz) {
boolean extendsThread = false;
ClassObj parentClass = clazz;
while (parentClass.getSuperClassObj() != null) {
if (parentClass.getClassName().equals(Thread.class.getName())) {
extendsThread = true;
break;
}
parentClass = parentClass.getSuperClassObj();
}
return extendsThread;
}
/**
* This returns a string representation of any object or value passed in.
*/
static String valueAsString(Object value) {
String stringValue;
if (value == null) {
stringValue = "null";
} else if (value instanceof ClassInstance) {
String valueClassName = ((ClassInstance) value).getClassObj().getClassName();
if (valueClassName.equals(String.class.getName())) {
stringValue = '"' + asString(value) + '"';
} else {
stringValue = value.toString();
}
} else {
stringValue = value.toString();
}
return stringValue;
}
/** Given a string instance from the heap dump, this returns its actual string value. */
static String asString(Object stringObject) {
Instance instance = (Instance) stringObject;
List<ClassInstance.FieldValue> values = classInstanceValues(instance);
Integer count = fieldValue(values, "count");
if (count == 0) {
return "";
}
Object value = fieldValue(values, "value");
Integer offset;
ArrayInstance array;
if (isCharArray(value)) {
array = (ArrayInstance) value;
offset = 0;
// < API 23
// As of Marshmallow, substrings no longer share their parent strings' char arrays
// eliminating the need for String.offset
// https://android-review.googlesource.com/#/c/83611/
if (hasField(values, "offset")) {
offset = fieldValue(values, "offset");
}
char[] chars = array.asCharArray(offset, count);
return new String(chars);
} else if (isByteArray(value)) {
// In API 26, Strings are now internally represented as byte arrays.
array = (ArrayInstance) value;
// HACK - remove when HAHA's perflib is updated to https://goo.gl/Oe7ZwO.
try {
Method asRawByteArray =
ArrayInstance.class.getDeclaredMethod("asRawByteArray", int.class, int.class);
asRawByteArray.setAccessible(true);
byte[] rawByteArray = (byte[]) asRawByteArray.invoke(array, 0, count);
return new String(rawByteArray, Charset.forName("UTF-8"));
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
} else {
throw new UnsupportedOperationException("Could not find char array in " + instance);
}
}
public static boolean isPrimitiveWrapper(Object value) {
if (!(value instanceof ClassInstance)) {
return false;
}
return WRAPPER_TYPES.contains(((ClassInstance) value).getClassObj().getClassName());
}
public static boolean isPrimitiveOrWrapperArray(Object value) {
if (!(value instanceof ArrayInstance)) {
return false;
}
ArrayInstance arrayInstance = (ArrayInstance) value;
if (arrayInstance.getArrayType() != Type.OBJECT) {
return true;
}
return WRAPPER_TYPES.contains(arrayInstance.getClassObj().getClassName());
}
private static boolean isCharArray(Object value) {
return value instanceof ArrayInstance && ((ArrayInstance) value).getArrayType() == Type.CHAR;
}
public static boolean isByteArray(Object value) {
return value instanceof ArrayInstance && ((ArrayInstance) value).getArrayType() == Type.BYTE;
}
static List<ClassInstance.FieldValue> classInstanceValues(Instance instance) {
ClassInstance classInstance = (ClassInstance) instance;
return classInstance.getValues();
}
@SuppressWarnings({ "unchecked", "TypeParameterUnusedInFormals" })
static <T> T fieldValue(List<ClassInstance.FieldValue> values, String fieldName) {
for (ClassInstance.FieldValue fieldValue : values) {
if (fieldValue.getField().getName().equals(fieldName)) {
return (T) fieldValue.getValue();
}
}
throw new IllegalArgumentException("Field " + fieldName + " does not exists");
}
static boolean hasField(List<ClassInstance.FieldValue> values, String fieldName) {
for (ClassInstance.FieldValue fieldValue : values) {
if (fieldValue.getField().getName().equals(fieldName)) {
//noinspection unchecked
return true;
}
}
return false;
}
private HahaHelper() {
throw new AssertionError();
}
public static byte[] getByteArray(Object arrayInstance){
if(isByteArray(arrayInstance)){
try {
Method asRawByteArray =
ArrayInstance.class.getDeclaredMethod("asRawByteArray", int.class, int.class);
asRawByteArray.setAccessible(true);
Field length = ArrayInstance.class.getDeclaredField("mLength");
length.setAccessible(true);
int lengthValue = (int)length.get(arrayInstance);
byte[] rawByteArray = (byte[]) asRawByteArray.invoke(arrayInstance, 0, lengthValue);
return rawByteArray;
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchFieldException e){
throw new RuntimeException(e);
}
}
return null;
}
}