The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.util; |
| 18 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 19 | import java.io.BufferedReader; |
| 20 | import java.io.FileReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import java.io.IOException; |
| 22 | import java.io.UnsupportedEncodingException; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 23 | import java.nio.BufferUnderflowException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import java.nio.ByteBuffer; |
| 25 | import java.nio.ByteOrder; |
Rubin Xu | 75431fb | 2016-01-07 21:12:14 +0000 | [diff] [blame] | 26 | import java.util.Arrays; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import java.util.Collection; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 28 | import java.util.HashMap; |
| 29 | import java.util.regex.Matcher; |
| 30 | import java.util.regex.Pattern; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 33 | * Access to the system diagnostic event record. System diagnostic events are |
| 34 | * used to record certain system-level events (such as garbage collection, |
| 35 | * activity manager state, system watchdogs, and other low level activity), |
| 36 | * which may be automatically collected and analyzed during system development. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | * |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 38 | * <p>This is <b>not</b> the main "logcat" debugging log ({@link android.util.Log})! |
| 39 | * These diagnostic events are for system integrators, not application authors. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | * |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 41 | * <p>Events use integer tag codes corresponding to /system/etc/event-log-tags. |
| 42 | * They carry a payload of one or more int, long, or String values. The |
| 43 | * event-log-tags file defines the payload contents for each type code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | public class EventLog { |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 46 | /** @hide */ public EventLog() {} |
| 47 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 48 | private static final String TAG = "EventLog"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 50 | private static final String TAGS_FILE = "/system/etc/event-log-tags"; |
| 51 | private static final String COMMENT_PATTERN = "^\\s*(#.*)?$"; |
| 52 | private static final String TAG_PATTERN = "^\\s*(\\d+)\\s+(\\w+)\\s*(\\(.*\\))?\\s*$"; |
| 53 | private static HashMap<String, Integer> sTagCodes = null; |
| 54 | private static HashMap<Integer, String> sTagNames = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
Neil Fuller | bf0dc0f | 2015-11-24 18:19:06 +0000 | [diff] [blame] | 56 | /** A previously logged event read from the logs. Instances are thread safe. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | public static final class Event { |
| 58 | private final ByteBuffer mBuffer; |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 59 | private Exception mLastWtf; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 61 | // Layout of event log entry received from Android logger. |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 62 | // see system/core/include/log/log.h |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | private static final int LENGTH_OFFSET = 0; |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 64 | private static final int HEADER_SIZE_OFFSET = 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | private static final int PROCESS_OFFSET = 4; |
| 66 | private static final int THREAD_OFFSET = 8; |
| 67 | private static final int SECONDS_OFFSET = 12; |
| 68 | private static final int NANOSECONDS_OFFSET = 16; |
| 69 | |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 70 | // Layout for event log v1 format, v2 and v3 use HEADER_SIZE_OFFSET |
| 71 | private static final int V1_PAYLOAD_START = 20; |
| 72 | private static final int DATA_OFFSET = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 74 | // Value types |
| 75 | private static final byte INT_TYPE = 0; |
| 76 | private static final byte LONG_TYPE = 1; |
| 77 | private static final byte STRING_TYPE = 2; |
| 78 | private static final byte LIST_TYPE = 3; |
Jeff Brown | e7e9cce | 2015-04-28 13:26:48 -0700 | [diff] [blame] | 79 | private static final byte FLOAT_TYPE = 4; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 80 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | /** @param data containing event, read from the system */ |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 82 | /*package*/ Event(byte[] data) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | mBuffer = ByteBuffer.wrap(data); |
| 84 | mBuffer.order(ByteOrder.nativeOrder()); |
| 85 | } |
| 86 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 87 | /** @return the process ID which wrote the log entry */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | public int getProcessId() { |
| 89 | return mBuffer.getInt(PROCESS_OFFSET); |
| 90 | } |
| 91 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 92 | /** @return the thread ID which wrote the log entry */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | public int getThreadId() { |
| 94 | return mBuffer.getInt(THREAD_OFFSET); |
| 95 | } |
| 96 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 97 | /** @return the wall clock time when the entry was written */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | public long getTimeNanos() { |
| 99 | return mBuffer.getInt(SECONDS_OFFSET) * 1000000000l |
| 100 | + mBuffer.getInt(NANOSECONDS_OFFSET); |
| 101 | } |
| 102 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 103 | /** @return the type tag code of the entry */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | public int getTag() { |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 105 | int offset = mBuffer.getShort(HEADER_SIZE_OFFSET); |
| 106 | if (offset == 0) { |
| 107 | offset = V1_PAYLOAD_START; |
| 108 | } |
| 109 | return mBuffer.getInt(offset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Jeff Brown | e7e9cce | 2015-04-28 13:26:48 -0700 | [diff] [blame] | 112 | /** @return one of Integer, Long, Float, String, null, or Object[] of same. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | public synchronized Object getData() { |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 114 | try { |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 115 | int offset = mBuffer.getShort(HEADER_SIZE_OFFSET); |
| 116 | if (offset == 0) { |
| 117 | offset = V1_PAYLOAD_START; |
| 118 | } |
| 119 | mBuffer.limit(offset + mBuffer.getShort(LENGTH_OFFSET)); |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 120 | if ((offset + DATA_OFFSET) >= mBuffer.limit()) { |
| 121 | // no payload |
| 122 | return null; |
| 123 | } |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 124 | mBuffer.position(offset + DATA_OFFSET); // Just after the tag. |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 125 | return decodeObject(); |
| 126 | } catch (IllegalArgumentException e) { |
| 127 | Log.wtf(TAG, "Illegal entry payload: tag=" + getTag(), e); |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 128 | mLastWtf = e; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 129 | return null; |
| 130 | } catch (BufferUnderflowException e) { |
| 131 | Log.wtf(TAG, "Truncated entry payload: tag=" + getTag(), e); |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 132 | mLastWtf = e; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 133 | return null; |
| 134 | } |
Jim Miller | 3063687 | 2009-06-08 18:59:49 -0700 | [diff] [blame] | 135 | } |
| 136 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | /** @return the loggable item at the current position in mBuffer. */ |
| 138 | private Object decodeObject() { |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 139 | byte type = mBuffer.get(); |
| 140 | switch (type) { |
| 141 | case INT_TYPE: |
Jeff Brown | e7e9cce | 2015-04-28 13:26:48 -0700 | [diff] [blame] | 142 | return mBuffer.getInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 144 | case LONG_TYPE: |
Jeff Brown | e7e9cce | 2015-04-28 13:26:48 -0700 | [diff] [blame] | 145 | return mBuffer.getLong(); |
| 146 | |
| 147 | case FLOAT_TYPE: |
| 148 | return mBuffer.getFloat(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 150 | case STRING_TYPE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | int length = mBuffer.getInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | int start = mBuffer.position(); |
| 154 | mBuffer.position(start + length); |
| 155 | return new String(mBuffer.array(), start, length, "UTF-8"); |
| 156 | } catch (UnsupportedEncodingException e) { |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 157 | Log.wtf(TAG, "UTF-8 is not supported", e); |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 158 | mLastWtf = e; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 159 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 162 | case LIST_TYPE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | int length = mBuffer.get(); |
Dan Egnor | 6916089 | 2010-01-06 23:12:57 -0800 | [diff] [blame] | 164 | if (length < 0) length += 256; // treat as signed byte |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | Object[] array = new Object[length]; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 166 | for (int i = 0; i < length; ++i) array[i] = decodeObject(); |
| 167 | return array; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
| 169 | default: |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 170 | throw new IllegalArgumentException("Unknown entry type: " + type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
| 172 | } |
Rubin Xu | 75431fb | 2016-01-07 21:12:14 +0000 | [diff] [blame] | 173 | |
| 174 | /** @hide */ |
| 175 | public static Event fromBytes(byte[] data) { |
| 176 | return new Event(data); |
| 177 | } |
| 178 | |
| 179 | /** @hide */ |
| 180 | public byte[] getBytes() { |
| 181 | byte[] bytes = mBuffer.array(); |
| 182 | return Arrays.copyOf(bytes, bytes.length); |
| 183 | } |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * Retreive the last WTF error generated by this object. |
| 187 | * @hide |
| 188 | */ |
| 189 | //VisibleForTesting |
| 190 | public Exception getLastError() { |
| 191 | return mLastWtf; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Clear the error state for this object. |
| 196 | * @hide |
| 197 | */ |
| 198 | //VisibleForTesting |
| 199 | public void clearError() { |
| 200 | mLastWtf = null; |
| 201 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // We assume that the native methods deal with any concurrency issues. |
| 205 | |
| 206 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 207 | * Record an event log message. |
| 208 | * @param tag The event type tag code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | * @param value A value to log |
| 210 | * @return The number of bytes written |
| 211 | */ |
| 212 | public static native int writeEvent(int tag, int value); |
| 213 | |
| 214 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 215 | * Record an event log message. |
| 216 | * @param tag The event type tag code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | * @param value A value to log |
| 218 | * @return The number of bytes written |
| 219 | */ |
| 220 | public static native int writeEvent(int tag, long value); |
| 221 | |
| 222 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 223 | * Record an event log message. |
| 224 | * @param tag The event type tag code |
Jeff Brown | e7e9cce | 2015-04-28 13:26:48 -0700 | [diff] [blame] | 225 | * @param value A value to log |
| 226 | * @return The number of bytes written |
| 227 | */ |
| 228 | public static native int writeEvent(int tag, float value); |
| 229 | |
| 230 | /** |
| 231 | * Record an event log message. |
| 232 | * @param tag The event type tag code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | * @param str A value to log |
| 234 | * @return The number of bytes written |
| 235 | */ |
| 236 | public static native int writeEvent(int tag, String str); |
| 237 | |
| 238 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 239 | * Record an event log message. |
| 240 | * @param tag The event type tag code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | * @param list A list of values to log |
| 242 | * @return The number of bytes written |
| 243 | */ |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 244 | public static native int writeEvent(int tag, Object... list); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | |
| 246 | /** |
| 247 | * Read events from the log, filtered by type. |
| 248 | * @param tags to search for |
| 249 | * @param output container to add events into |
| 250 | * @throws IOException if something goes wrong reading events |
| 251 | */ |
| 252 | public static native void readEvents(int[] tags, Collection<Event> output) |
| 253 | throws IOException; |
Jim Miller | 95ff240 | 2009-07-01 18:03:41 -0700 | [diff] [blame] | 254 | |
| 255 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 256 | * Get the name associated with an event type tag code. |
| 257 | * @param tag code to look up |
| 258 | * @return the name of the tag, or null if no tag has that number |
Jim Miller | 95ff240 | 2009-07-01 18:03:41 -0700 | [diff] [blame] | 259 | */ |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 260 | public static String getTagName(int tag) { |
| 261 | readTagsFile(); |
| 262 | return sTagNames.get(tag); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Get the event type tag code associated with an event name. |
| 267 | * @param name of event to look up |
| 268 | * @return the tag code, or -1 if no tag has that name |
| 269 | */ |
| 270 | public static int getTagCode(String name) { |
| 271 | readTagsFile(); |
| 272 | Integer code = sTagCodes.get(name); |
| 273 | return code != null ? code : -1; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Read TAGS_FILE, populating sTagCodes and sTagNames, if not already done. |
| 278 | */ |
| 279 | private static synchronized void readTagsFile() { |
| 280 | if (sTagCodes != null && sTagNames != null) return; |
| 281 | |
| 282 | sTagCodes = new HashMap<String, Integer>(); |
| 283 | sTagNames = new HashMap<Integer, String>(); |
| 284 | |
| 285 | Pattern comment = Pattern.compile(COMMENT_PATTERN); |
| 286 | Pattern tag = Pattern.compile(TAG_PATTERN); |
| 287 | BufferedReader reader = null; |
| 288 | String line; |
| 289 | |
| 290 | try { |
| 291 | reader = new BufferedReader(new FileReader(TAGS_FILE), 256); |
| 292 | while ((line = reader.readLine()) != null) { |
| 293 | if (comment.matcher(line).matches()) continue; |
| 294 | |
| 295 | Matcher m = tag.matcher(line); |
| 296 | if (!m.matches()) { |
| 297 | Log.wtf(TAG, "Bad entry in " + TAGS_FILE + ": " + line); |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | try { |
| 302 | int num = Integer.parseInt(m.group(1)); |
| 303 | String name = m.group(2); |
| 304 | sTagCodes.put(name, num); |
| 305 | sTagNames.put(num, name); |
| 306 | } catch (NumberFormatException e) { |
| 307 | Log.wtf(TAG, "Error in " + TAGS_FILE + ": " + line, e); |
| 308 | } |
| 309 | } |
| 310 | } catch (IOException e) { |
| 311 | Log.wtf(TAG, "Error reading " + TAGS_FILE, e); |
| 312 | // Leave the maps existing but unpopulated |
| 313 | } finally { |
| 314 | try { if (reader != null) reader.close(); } catch (IOException e) {} |
| 315 | } |
| 316 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |