blob: 92f218b4155aa02eab7e6aafb698ec124e358789 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.util;
18
Chris Wrend09bf822016-12-02 17:43:23 -050019import android.annotation.SystemApi;
20
Dan Egnor62136d32010-01-05 19:09:08 -080021import java.io.BufferedReader;
22import java.io.FileReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.io.IOException;
24import java.io.UnsupportedEncodingException;
Dan Egnor62136d32010-01-05 19:09:08 -080025import java.nio.BufferUnderflowException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import java.nio.ByteBuffer;
27import java.nio.ByteOrder;
Rubin Xu75431fb2016-01-07 21:12:14 +000028import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import java.util.Collection;
Dan Egnor62136d32010-01-05 19:09:08 -080030import java.util.HashMap;
31import java.util.regex.Matcher;
32import java.util.regex.Pattern;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34/**
Dan Egnor62136d32010-01-05 19:09:08 -080035 * Access to the system diagnostic event record. System diagnostic events are
36 * used to record certain system-level events (such as garbage collection,
37 * activity manager state, system watchdogs, and other low level activity),
38 * which may be automatically collected and analyzed during system development.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 *
Dan Egnor62136d32010-01-05 19:09:08 -080040 * <p>This is <b>not</b> the main "logcat" debugging log ({@link android.util.Log})!
41 * These diagnostic events are for system integrators, not application authors.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 *
Dan Egnor62136d32010-01-05 19:09:08 -080043 * <p>Events use integer tag codes corresponding to /system/etc/event-log-tags.
44 * They carry a payload of one or more int, long, or String values. The
45 * event-log-tags file defines the payload contents for each type code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047public class EventLog {
Jesse Wilsona0f8bc52011-02-24 10:44:33 -080048 /** @hide */ public EventLog() {}
49
Dan Egnor62136d32010-01-05 19:09:08 -080050 private static final String TAG = "EventLog";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Dan Egnor62136d32010-01-05 19:09:08 -080052 private static final String TAGS_FILE = "/system/etc/event-log-tags";
53 private static final String COMMENT_PATTERN = "^\\s*(#.*)?$";
54 private static final String TAG_PATTERN = "^\\s*(\\d+)\\s+(\\w+)\\s*(\\(.*\\))?\\s*$";
55 private static HashMap<String, Integer> sTagCodes = null;
56 private static HashMap<Integer, String> sTagNames = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Neil Fullerbf0dc0f2015-11-24 18:19:06 +000058 /** A previously logged event read from the logs. Instances are thread safe. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 public static final class Event {
60 private final ByteBuffer mBuffer;
Chris Wrend9f3d9e2016-12-16 14:08:34 -050061 private Exception mLastWtf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Mark Salyzyn747802f2014-04-25 13:25:18 -070063 // Layout of event log entry received from Android logger.
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070064 // see system/core/include/log/log.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 private static final int LENGTH_OFFSET = 0;
Mark Salyzyn747802f2014-04-25 13:25:18 -070066 private static final int HEADER_SIZE_OFFSET = 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 private static final int PROCESS_OFFSET = 4;
68 private static final int THREAD_OFFSET = 8;
69 private static final int SECONDS_OFFSET = 12;
70 private static final int NANOSECONDS_OFFSET = 16;
Chris Wren4d6b54d2017-04-27 16:56:54 -040071 private static final int UID_OFFSET = 24;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
Mark Salyzyn747802f2014-04-25 13:25:18 -070073 // Layout for event log v1 format, v2 and v3 use HEADER_SIZE_OFFSET
74 private static final int V1_PAYLOAD_START = 20;
75 private static final int DATA_OFFSET = 4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
Dan Egnor62136d32010-01-05 19:09:08 -080077 // Value types
78 private static final byte INT_TYPE = 0;
79 private static final byte LONG_TYPE = 1;
80 private static final byte STRING_TYPE = 2;
81 private static final byte LIST_TYPE = 3;
Jeff Browne7e9cce2015-04-28 13:26:48 -070082 private static final byte FLOAT_TYPE = 4;
Dan Egnor62136d32010-01-05 19:09:08 -080083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /** @param data containing event, read from the system */
Dan Egnor62136d32010-01-05 19:09:08 -080085 /*package*/ Event(byte[] data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 mBuffer = ByteBuffer.wrap(data);
87 mBuffer.order(ByteOrder.nativeOrder());
88 }
89
Dan Egnor62136d32010-01-05 19:09:08 -080090 /** @return the process ID which wrote the log entry */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 public int getProcessId() {
92 return mBuffer.getInt(PROCESS_OFFSET);
93 }
94
Chris Wren4d6b54d2017-04-27 16:56:54 -040095 /**
96 * @return the UID which wrote the log entry
97 * @hide
98 */
99 @SystemApi
100 public int getUid() {
101 try {
102 return mBuffer.getInt(UID_OFFSET);
103 } catch (IndexOutOfBoundsException e) {
104 // buffer won't contain the UID if the caller doesn't have permission.
105 return -1;
106 }
107 }
108
Dan Egnor62136d32010-01-05 19:09:08 -0800109 /** @return the thread ID which wrote the log entry */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 public int getThreadId() {
111 return mBuffer.getInt(THREAD_OFFSET);
112 }
113
Dan Egnor62136d32010-01-05 19:09:08 -0800114 /** @return the wall clock time when the entry was written */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public long getTimeNanos() {
116 return mBuffer.getInt(SECONDS_OFFSET) * 1000000000l
117 + mBuffer.getInt(NANOSECONDS_OFFSET);
118 }
119
Dan Egnor62136d32010-01-05 19:09:08 -0800120 /** @return the type tag code of the entry */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 public int getTag() {
Mark Salyzyn747802f2014-04-25 13:25:18 -0700122 int offset = mBuffer.getShort(HEADER_SIZE_OFFSET);
123 if (offset == 0) {
124 offset = V1_PAYLOAD_START;
125 }
126 return mBuffer.getInt(offset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 }
128
Jeff Browne7e9cce2015-04-28 13:26:48 -0700129 /** @return one of Integer, Long, Float, String, null, or Object[] of same. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 public synchronized Object getData() {
Dan Egnor62136d32010-01-05 19:09:08 -0800131 try {
Mark Salyzyn747802f2014-04-25 13:25:18 -0700132 int offset = mBuffer.getShort(HEADER_SIZE_OFFSET);
133 if (offset == 0) {
134 offset = V1_PAYLOAD_START;
135 }
136 mBuffer.limit(offset + mBuffer.getShort(LENGTH_OFFSET));
Chris Wrend9f3d9e2016-12-16 14:08:34 -0500137 if ((offset + DATA_OFFSET) >= mBuffer.limit()) {
138 // no payload
139 return null;
140 }
Mark Salyzyn747802f2014-04-25 13:25:18 -0700141 mBuffer.position(offset + DATA_OFFSET); // Just after the tag.
Dan Egnor62136d32010-01-05 19:09:08 -0800142 return decodeObject();
143 } catch (IllegalArgumentException e) {
144 Log.wtf(TAG, "Illegal entry payload: tag=" + getTag(), e);
Chris Wrend9f3d9e2016-12-16 14:08:34 -0500145 mLastWtf = e;
Dan Egnor62136d32010-01-05 19:09:08 -0800146 return null;
147 } catch (BufferUnderflowException e) {
148 Log.wtf(TAG, "Truncated entry payload: tag=" + getTag(), e);
Chris Wrend9f3d9e2016-12-16 14:08:34 -0500149 mLastWtf = e;
Dan Egnor62136d32010-01-05 19:09:08 -0800150 return null;
151 }
Jim Miller30636872009-06-08 18:59:49 -0700152 }
153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /** @return the loggable item at the current position in mBuffer. */
155 private Object decodeObject() {
Dan Egnor62136d32010-01-05 19:09:08 -0800156 byte type = mBuffer.get();
157 switch (type) {
158 case INT_TYPE:
Jeff Browne7e9cce2015-04-28 13:26:48 -0700159 return mBuffer.getInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
Dan Egnor62136d32010-01-05 19:09:08 -0800161 case LONG_TYPE:
Jeff Browne7e9cce2015-04-28 13:26:48 -0700162 return mBuffer.getLong();
163
164 case FLOAT_TYPE:
165 return mBuffer.getFloat();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
Dan Egnor62136d32010-01-05 19:09:08 -0800167 case STRING_TYPE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 int length = mBuffer.getInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 int start = mBuffer.position();
171 mBuffer.position(start + length);
172 return new String(mBuffer.array(), start, length, "UTF-8");
173 } catch (UnsupportedEncodingException e) {
Dan Egnor62136d32010-01-05 19:09:08 -0800174 Log.wtf(TAG, "UTF-8 is not supported", e);
Chris Wrend9f3d9e2016-12-16 14:08:34 -0500175 mLastWtf = e;
Dan Egnor62136d32010-01-05 19:09:08 -0800176 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 }
178
Dan Egnor62136d32010-01-05 19:09:08 -0800179 case LIST_TYPE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 int length = mBuffer.get();
Dan Egnor69160892010-01-06 23:12:57 -0800181 if (length < 0) length += 256; // treat as signed byte
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 Object[] array = new Object[length];
Dan Egnor62136d32010-01-05 19:09:08 -0800183 for (int i = 0; i < length; ++i) array[i] = decodeObject();
184 return array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
186 default:
Dan Egnor62136d32010-01-05 19:09:08 -0800187 throw new IllegalArgumentException("Unknown entry type: " + type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 }
189 }
Rubin Xu75431fb2016-01-07 21:12:14 +0000190
191 /** @hide */
192 public static Event fromBytes(byte[] data) {
193 return new Event(data);
194 }
195
196 /** @hide */
197 public byte[] getBytes() {
198 byte[] bytes = mBuffer.array();
199 return Arrays.copyOf(bytes, bytes.length);
200 }
Chris Wrend9f3d9e2016-12-16 14:08:34 -0500201
202 /**
203 * Retreive the last WTF error generated by this object.
204 * @hide
205 */
206 //VisibleForTesting
207 public Exception getLastError() {
208 return mLastWtf;
209 }
210
211 /**
212 * Clear the error state for this object.
213 * @hide
214 */
215 //VisibleForTesting
216 public void clearError() {
217 mLastWtf = null;
218 }
Pavel Grafov4ce59d42017-02-25 19:45:43 +0000219
220 /**
221 * @hide
222 */
223 @Override
224 public boolean equals(Object o) {
225 // Not using ByteBuffer.equals since it takes buffer position into account and we
226 // always use absolute positions here.
227 if (this == o) return true;
228 if (o == null || getClass() != o.getClass()) return false;
229 Event other = (Event) o;
230 return Arrays.equals(mBuffer.array(), other.mBuffer.array());
231 }
232
233 /**
234 * @hide
235 */
236 @Override
237 public int hashCode() {
238 // Not using ByteBuffer.hashCode since it takes buffer position into account and we
239 // always use absolute positions here.
240 return Arrays.hashCode(mBuffer.array());
241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 }
243
244 // We assume that the native methods deal with any concurrency issues.
245
246 /**
Dan Egnor62136d32010-01-05 19:09:08 -0800247 * Record an event log message.
248 * @param tag The event type tag code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 * @param value A value to log
250 * @return The number of bytes written
251 */
252 public static native int writeEvent(int tag, int value);
253
254 /**
Dan Egnor62136d32010-01-05 19:09:08 -0800255 * Record an event log message.
256 * @param tag The event type tag code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 * @param value A value to log
258 * @return The number of bytes written
259 */
260 public static native int writeEvent(int tag, long value);
261
262 /**
Dan Egnor62136d32010-01-05 19:09:08 -0800263 * Record an event log message.
264 * @param tag The event type tag code
Jeff Browne7e9cce2015-04-28 13:26:48 -0700265 * @param value A value to log
266 * @return The number of bytes written
267 */
268 public static native int writeEvent(int tag, float value);
269
270 /**
271 * Record an event log message.
272 * @param tag The event type tag code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * @param str A value to log
274 * @return The number of bytes written
275 */
276 public static native int writeEvent(int tag, String str);
277
278 /**
Dan Egnor62136d32010-01-05 19:09:08 -0800279 * Record an event log message.
280 * @param tag The event type tag code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * @param list A list of values to log
282 * @return The number of bytes written
283 */
Dan Egnor62136d32010-01-05 19:09:08 -0800284 public static native int writeEvent(int tag, Object... list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285
286 /**
287 * Read events from the log, filtered by type.
288 * @param tags to search for
289 * @param output container to add events into
290 * @throws IOException if something goes wrong reading events
291 */
292 public static native void readEvents(int[] tags, Collection<Event> output)
293 throws IOException;
Jim Miller95ff2402009-07-01 18:03:41 -0700294
295 /**
Chris Wrend09bf822016-12-02 17:43:23 -0500296 * Read events from the log, filtered by type, blocking until logs are about to be overwritten.
297 * @param tags to search for
298 * @param timestamp timestamp allow logs before this time to be overwritten.
299 * @param output container to add events into
300 * @throws IOException if something goes wrong reading events
301 * @hide
302 */
303 @SystemApi
304 public static native void readEventsOnWrapping(int[] tags, long timestamp,
305 Collection<Event> output)
306 throws IOException;
307
308 /**
Dan Egnor62136d32010-01-05 19:09:08 -0800309 * Get the name associated with an event type tag code.
310 * @param tag code to look up
311 * @return the name of the tag, or null if no tag has that number
Jim Miller95ff2402009-07-01 18:03:41 -0700312 */
Dan Egnor62136d32010-01-05 19:09:08 -0800313 public static String getTagName(int tag) {
314 readTagsFile();
315 return sTagNames.get(tag);
316 }
317
318 /**
319 * Get the event type tag code associated with an event name.
320 * @param name of event to look up
321 * @return the tag code, or -1 if no tag has that name
322 */
323 public static int getTagCode(String name) {
324 readTagsFile();
325 Integer code = sTagCodes.get(name);
326 return code != null ? code : -1;
327 }
328
329 /**
330 * Read TAGS_FILE, populating sTagCodes and sTagNames, if not already done.
331 */
332 private static synchronized void readTagsFile() {
333 if (sTagCodes != null && sTagNames != null) return;
334
335 sTagCodes = new HashMap<String, Integer>();
336 sTagNames = new HashMap<Integer, String>();
337
338 Pattern comment = Pattern.compile(COMMENT_PATTERN);
339 Pattern tag = Pattern.compile(TAG_PATTERN);
340 BufferedReader reader = null;
341 String line;
342
343 try {
344 reader = new BufferedReader(new FileReader(TAGS_FILE), 256);
345 while ((line = reader.readLine()) != null) {
346 if (comment.matcher(line).matches()) continue;
347
348 Matcher m = tag.matcher(line);
349 if (!m.matches()) {
350 Log.wtf(TAG, "Bad entry in " + TAGS_FILE + ": " + line);
351 continue;
352 }
353
354 try {
355 int num = Integer.parseInt(m.group(1));
356 String name = m.group(2);
357 sTagCodes.put(name, num);
358 sTagNames.put(num, name);
359 } catch (NumberFormatException e) {
360 Log.wtf(TAG, "Error in " + TAGS_FILE + ": " + line, e);
361 }
362 }
363 } catch (IOException e) {
364 Log.wtf(TAG, "Error reading " + TAGS_FILE, e);
365 // Leave the maps existing but unpopulated
366 } finally {
367 try { if (reader != null) reader.close(); } catch (IOException e) {}
368 }
369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370}