| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.SystemApi; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.os.Build; |
| 24 |
|
| 25 |
import java.io.BufferedReader; |
| 26 |
import java.io.FileReader; |
| 27 |
import java.io.IOException; |
| 28 |
import java.io.UnsupportedEncodingException; |
| 29 |
import java.nio.BufferUnderflowException; |
| 30 |
import java.nio.ByteBuffer; |
| 31 |
import java.nio.ByteOrder; |
| 32 |
import java.util.Arrays; |
| 33 |
import java.util.Collection; |
| 34 |
import java.util.HashMap; |
| 35 |
import java.util.regex.Matcher; |
| 36 |
import java.util.regex.Pattern; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class EventLog { |
| 52 |
public EventLog() {} |
| 53 |
|
| 54 |
private static final String TAG = "EventLog"; |
| 55 |
|
| 56 |
private static final String TAGS_FILE = "/system/etc/event-log-tags"; |
| 57 |
private static final String COMMENT_PATTERN = "^\\s*(#.*)?$"; |
| 58 |
private static final String TAG_PATTERN = "^\\s*(\\d+)\\s+(\\w+)\\s*(\\(.*\\))?\\s*$"; |
| 59 |
private static HashMap<String, Integer> sTagCodes = null; |
| 60 |
private static HashMap<Integer, String> sTagNames = null; |
| 61 |
|
| 62 |
|
| 63 |
public static final class Event { |
| 64 |
private final ByteBuffer mBuffer; |
| 65 |
private Exception mLastWtf; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private static final int LENGTH_OFFSET = 0; |
| 70 |
private static final int HEADER_SIZE_OFFSET = 2; |
| 71 |
private static final int PROCESS_OFFSET = 4; |
| 72 |
private static final int THREAD_OFFSET = 8; |
| 73 |
private static final int SECONDS_OFFSET = 12; |
| 74 |
private static final int NANOSECONDS_OFFSET = 16; |
| 75 |
private static final int UID_OFFSET = 24; |
| 76 |
|
| 77 |
|
| 78 |
private static final int V1_PAYLOAD_START = 20; |
| 79 |
private static final int TAG_LENGTH = 4; |
| 80 |
|
| 81 |
|
| 82 |
private static final byte INT_TYPE = 0; |
| 83 |
private static final byte LONG_TYPE = 1; |
| 84 |
private static final byte STRING_TYPE = 2; |
| 85 |
private static final byte LIST_TYPE = 3; |
| 86 |
private static final byte FLOAT_TYPE = 4; |
| 87 |
|
| 88 |
|
| 89 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 90 |
Event(byte[] data) { |
| 91 |
mBuffer = ByteBuffer.wrap(data); |
| 92 |
mBuffer.order(ByteOrder.nativeOrder()); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public int getProcessId() { |
| 97 |
return mBuffer.getInt(PROCESS_OFFSET); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@SystemApi |
| 105 |
public int getUid() { |
| 106 |
try { |
| 107 |
return mBuffer.getInt(UID_OFFSET); |
| 108 |
} catch (IndexOutOfBoundsException e) { |
| 109 |
|
| 110 |
return -1; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
public int getThreadId() { |
| 116 |
return mBuffer.getInt(THREAD_OFFSET); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
public long getTimeNanos() { |
| 121 |
return mBuffer.getInt(SECONDS_OFFSET) * 1000000000l |
| 122 |
+ mBuffer.getInt(NANOSECONDS_OFFSET); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
public int getTag() { |
| 127 |
return mBuffer.getInt(getHeaderSize()); |
| 128 |
} |
| 129 |
|
| 130 |
private int getHeaderSize() { |
| 131 |
int length = mBuffer.getShort(HEADER_SIZE_OFFSET); |
| 132 |
if (length != 0) { |
| 133 |
return length; |
| 134 |
} |
| 135 |
return V1_PAYLOAD_START; |
| 136 |
} |
| 137 |
|
| 138 |
public synchronized Object getData() { |
| 139 |
try { |
| 140 |
int offset = getHeaderSize(); |
| 141 |
mBuffer.limit(offset + mBuffer.getShort(LENGTH_OFFSET)); |
| 142 |
if ((offset + TAG_LENGTH) >= mBuffer.limit()) { |
| 143 |
|
| 144 |
return null; |
| 145 |
} |
| 146 |
mBuffer.position(offset + TAG_LENGTH); |
| 147 |
return decodeObject(); |
| 148 |
} catch (IllegalArgumentException e) { |
| 149 |
Log.wtf(TAG, "Illegal entry payload: tag=" + getTag(), e); |
| 150 |
mLastWtf = e; |
| 151 |
return null; |
| 152 |
} catch (BufferUnderflowException e) { |
| 153 |
Log.wtf(TAG, "Truncated entry payload: tag=" + getTag(), e); |
| 154 |
mLastWtf = e; |
| 155 |
return null; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public Event withNewData(@Nullable Object object) { |
| 165 |
byte[] payload = encodeObject(object); |
| 166 |
if (payload.length > 65535 - TAG_LENGTH) { |
| 167 |
throw new IllegalArgumentException("Payload too long"); |
| 168 |
} |
| 169 |
int headerLength = getHeaderSize(); |
| 170 |
byte[] newBytes = new byte[headerLength + TAG_LENGTH + payload.length]; |
| 171 |
|
| 172 |
System.arraycopy(mBuffer.array(), 0, newBytes, 0, headerLength + TAG_LENGTH); |
| 173 |
|
| 174 |
System.arraycopy(payload, 0, newBytes, headerLength + TAG_LENGTH, payload.length); |
| 175 |
Event result = new Event(newBytes); |
| 176 |
|
| 177 |
result.mBuffer.putShort(LENGTH_OFFSET, (short) (payload.length + TAG_LENGTH)); |
| 178 |
return result; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
private Object decodeObject() { |
| 183 |
byte type = mBuffer.get(); |
| 184 |
switch (type) { |
| 185 |
case INT_TYPE: |
| 186 |
return mBuffer.getInt(); |
| 187 |
|
| 188 |
case LONG_TYPE: |
| 189 |
return mBuffer.getLong(); |
| 190 |
|
| 191 |
case FLOAT_TYPE: |
| 192 |
return mBuffer.getFloat(); |
| 193 |
|
| 194 |
case STRING_TYPE: |
| 195 |
try { |
| 196 |
int length = mBuffer.getInt(); |
| 197 |
int start = mBuffer.position(); |
| 198 |
mBuffer.position(start + length); |
| 199 |
return new String(mBuffer.array(), start, length, "UTF-8"); |
| 200 |
} catch (UnsupportedEncodingException e) { |
| 201 |
Log.wtf(TAG, "UTF-8 is not supported", e); |
| 202 |
mLastWtf = e; |
| 203 |
return null; |
| 204 |
} |
| 205 |
|
| 206 |
case LIST_TYPE: |
| 207 |
int length = mBuffer.get(); |
| 208 |
if (length < 0) length += 256; |
| 209 |
Object[] array = new Object[length]; |
| 210 |
for (int i = 0; i < length; ++i) array[i] = decodeObject(); |
| 211 |
return array; |
| 212 |
|
| 213 |
default: |
| 214 |
throw new IllegalArgumentException("Unknown entry type: " + type); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
private static @NonNull byte[] encodeObject(@Nullable Object object) { |
| 219 |
if (object == null) { |
| 220 |
return new byte[0]; |
| 221 |
} |
| 222 |
if (object instanceof Integer) { |
| 223 |
return ByteBuffer.allocate(1 + 4) |
| 224 |
.order(ByteOrder.nativeOrder()) |
| 225 |
.put(INT_TYPE) |
| 226 |
.putInt((Integer) object) |
| 227 |
.array(); |
| 228 |
} else if (object instanceof Long) { |
| 229 |
return ByteBuffer.allocate(1 + 8) |
| 230 |
.order(ByteOrder.nativeOrder()) |
| 231 |
.put(LONG_TYPE) |
| 232 |
.putLong((Long) object) |
| 233 |
.array(); |
| 234 |
} else if (object instanceof Float) { |
| 235 |
return ByteBuffer.allocate(1 + 4) |
| 236 |
.order(ByteOrder.nativeOrder()) |
| 237 |
.put(FLOAT_TYPE) |
| 238 |
.putFloat((Float) object) |
| 239 |
.array(); |
| 240 |
} else if (object instanceof String) { |
| 241 |
String string = (String) object; |
| 242 |
byte[] bytes; |
| 243 |
try { |
| 244 |
bytes = string.getBytes("UTF-8"); |
| 245 |
} catch (UnsupportedEncodingException e) { |
| 246 |
bytes = new byte[0]; |
| 247 |
} |
| 248 |
return ByteBuffer.allocate(1 + 4 + bytes.length) |
| 249 |
.order(ByteOrder.nativeOrder()) |
| 250 |
.put(STRING_TYPE) |
| 251 |
.putInt(bytes.length) |
| 252 |
.put(bytes) |
| 253 |
.array(); |
| 254 |
} else if (object instanceof Object[]) { |
| 255 |
Object[] objects = (Object[]) object; |
| 256 |
if (objects.length > 255) { |
| 257 |
throw new IllegalArgumentException("Object array too long"); |
| 258 |
} |
| 259 |
byte[][] bytes = new byte[objects.length][]; |
| 260 |
int totalLength = 0; |
| 261 |
for (int i = 0; i < objects.length; i++) { |
| 262 |
bytes[i] = encodeObject(objects[i]); |
| 263 |
totalLength += bytes[i].length; |
| 264 |
} |
| 265 |
ByteBuffer buffer = ByteBuffer.allocate(1 + 1 + totalLength) |
| 266 |
.order(ByteOrder.nativeOrder()) |
| 267 |
.put(LIST_TYPE) |
| 268 |
.put((byte) objects.length); |
| 269 |
for (int i = 0; i < objects.length; i++) { |
| 270 |
buffer.put(bytes[i]); |
| 271 |
} |
| 272 |
return buffer.array(); |
| 273 |
} else { |
| 274 |
throw new IllegalArgumentException("Unknown object type " + object); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
public static Event fromBytes(byte[] data) { |
| 280 |
return new Event(data); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
public byte[] getBytes() { |
| 285 |
byte[] bytes = mBuffer.array(); |
| 286 |
return Arrays.copyOf(bytes, bytes.length); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public Exception getLastError() { |
| 295 |
return mLastWtf; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public void clearError() { |
| 304 |
mLastWtf = null; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
@Override |
| 311 |
public boolean equals(@Nullable Object o) { |
| 312 |
|
| 313 |
|
| 314 |
if (this == o) return true; |
| 315 |
if (o == null || getClass() != o.getClass()) return false; |
| 316 |
Event other = (Event) o; |
| 317 |
return Arrays.equals(mBuffer.array(), other.mBuffer.array()); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
@Override |
| 324 |
public int hashCode() { |
| 325 |
|
| 326 |
|
| 327 |
return Arrays.hashCode(mBuffer.array()); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public static native int writeEvent(int tag, int value); |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public static native int writeEvent(int tag, long value); |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public static native int writeEvent(int tag, float value); |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public static native int writeEvent(int tag, String str); |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public static native int writeEvent(int tag, Object... list); |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
public static native void readEvents(int[] tags, Collection<Event> output) |
| 380 |
throws IOException; |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
@SystemApi |
| 391 |
public static native void readEventsOnWrapping(int[] tags, long timestamp, |
| 392 |
Collection<Event> output) |
| 393 |
throws IOException; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public static String getTagName(int tag) { |
| 401 |
readTagsFile(); |
| 402 |
return sTagNames.get(tag); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public static int getTagCode(String name) { |
| 411 |
readTagsFile(); |
| 412 |
Integer code = sTagCodes.get(name); |
| 413 |
return code != null ? code : -1; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
private static synchronized void readTagsFile() { |
| 420 |
if (sTagCodes != null && sTagNames != null) return; |
| 421 |
|
| 422 |
sTagCodes = new HashMap<String, Integer>(); |
| 423 |
sTagNames = new HashMap<Integer, String>(); |
| 424 |
|
| 425 |
Pattern comment = Pattern.compile(COMMENT_PATTERN); |
| 426 |
Pattern tag = Pattern.compile(TAG_PATTERN); |
| 427 |
BufferedReader reader = null; |
| 428 |
String line; |
| 429 |
|
| 430 |
try { |
| 431 |
reader = new BufferedReader(new FileReader(TAGS_FILE), 256); |
| 432 |
while ((line = reader.readLine()) != null) { |
| 433 |
if (comment.matcher(line).matches()) continue; |
| 434 |
|
| 435 |
Matcher m = tag.matcher(line); |
| 436 |
if (!m.matches()) { |
| 437 |
Log.wtf(TAG, "Bad entry in " + TAGS_FILE + ": " + line); |
| 438 |
continue; |
| 439 |
} |
| 440 |
|
| 441 |
try { |
| 442 |
int num = Integer.parseInt(m.group(1)); |
| 443 |
String name = m.group(2); |
| 444 |
sTagCodes.put(name, num); |
| 445 |
sTagNames.put(num, name); |
| 446 |
} catch (NumberFormatException e) { |
| 447 |
Log.wtf(TAG, "Error in " + TAGS_FILE + ": " + line, e); |
| 448 |
} |
| 449 |
} |
| 450 |
} catch (IOException e) { |
| 451 |
Log.wtf(TAG, "Error reading " + TAGS_FILE, e); |
| 452 |
|
| 453 |
} finally { |
| 454 |
try { if (reader != null) reader.close(); } catch (IOException e) {} |
| 455 |
} |
| 456 |
} |
| 457 |
} |