1 /* 2 * Copyright (C) 2008 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 /** 18 * One line from the loaded-classes file. 19 */ 20 class Record { 21 22 /** 23 * The delimiter character we use, {@code :}, conflicts with some other 24 * names. In that case, manually replace the delimiter with something else. 25 */ 26 private static final String[] REPLACE_CLASSES = { 27 "com.google.android.apps.maps:FriendService", 28 "com.google.android.apps.maps\\u003AFriendService", 29 "com.google.android.apps.maps:driveabout", 30 "com.google.android.apps.maps\\u003Adriveabout", 31 "com.google.android.apps.maps:GoogleLocationService", 32 "com.google.android.apps.maps\\u003AGoogleLocationService", 33 "com.google.android.apps.maps:LocationFriendService", 34 "com.google.android.apps.maps\\u003ALocationFriendService", 35 "com.google.android.apps.maps:MapsBackgroundService", 36 "com.google.android.apps.maps\\u003AMapsBackgroundService", 37 "com.google.android.apps.maps:NetworkLocationService", 38 "com.google.android.apps.maps\\u003ANetworkLocationService", 39 "com.android.chrome:sandboxed_process", 40 "com.android.chrome\\u003Asandboxed_process", 41 "com.android.fakeoemfeatures:background", 42 "com.android.fakeoemfeatures\\u003Abackground", 43 "com.android.fakeoemfeatures:core", 44 "com.android.fakeoemfeatures\\u003Acore", 45 "com.google.android.music:main", 46 "com.google.android.music\\u003Amain", 47 "com.google.android.music:ui", 48 "com.google.android.music\\u003Aui", 49 "com.google.android.setupwarlock:broker", 50 "com.google.android.setupwarlock\\u003Abroker", 51 "android:ui", 52 "android\\u003Aui", 53 }; 54 55 enum Type { 56 /** Start of initialization. */ 57 START_LOAD, 58 59 /** End of initialization. */ 60 END_LOAD, 61 62 /** Start of initialization. */ 63 START_INIT, 64 65 /** End of initialization. */ 66 END_INIT 67 } 68 69 /** Parent process ID. */ 70 final int ppid; 71 72 /** Process ID. */ 73 final int pid; 74 75 /** Thread ID. */ 76 final int tid; 77 78 /** Process name. */ 79 final String processName; 80 81 /** Class loader pointer. */ 82 final int classLoader; 83 84 /** Type of record. */ 85 final Type type; 86 87 /** Name of loaded class. */ 88 final String className; 89 90 /** Record time (ns). */ 91 final long time; 92 93 /** Source file line# */ 94 int sourceLineNumber; 95 96 /** 97 * Parses a line from the loaded-classes file. 98 */ 99 Record(String line, int lineNum) { 100 char typeChar = line.charAt(0); 101 switch (typeChar) { 102 case '>': type = Type.START_LOAD; break; 103 case '<': type = Type.END_LOAD; break; 104 case '+': type = Type.START_INIT; break; 105 case '-': type = Type.END_INIT; break; 106 default: throw new AssertionError("Bad line: " + line); 107 } 108 109 sourceLineNumber = lineNum; 110 111 for (int i = 0; i < REPLACE_CLASSES.length; i+= 2) { 112 line = line.replace(REPLACE_CLASSES[i], REPLACE_CLASSES[i+1]); 113 } 114 115 line = line.substring(1); 116 String[] parts = line.split(":"); 117 118 ppid = Integer.parseInt(parts[0]); 119 pid = Integer.parseInt(parts[1]); 120 tid = Integer.parseInt(parts[2]); 121 122 processName = decode(parts[3]).intern(); 123 124 classLoader = Integer.parseInt(parts[4]); 125 className = vmTypeToLanguage(decode(parts[5])).intern(); 126 127 time = Long.parseLong(parts[6]); 128 } 129 130 /** 131 * Decode any escaping that may have been written to the log line. 132 * 133 * Supports unicode-style escaping: \\uXXXX = character in hex 134 * 135 * @param rawField the field as it was written into the log 136 * @result the same field with any escaped characters replaced 137 */ 138 String decode(String rawField) { 139 String result = rawField; 140 int offset = result.indexOf("\\u"); 141 while (offset >= 0) { 142 String before = result.substring(0, offset); 143 String escaped = result.substring(offset+2, offset+6); 144 String after = result.substring(offset+6); 145 146 result = String.format("%s%c%s", before, Integer.parseInt(escaped, 16), after); 147 148 // find another but don't recurse 149 offset = result.indexOf("\\u", offset + 1); 150 } 151 return result; 152 } 153 154 /** 155 * Converts a VM-style name to a language-style name. 156 */ 157 String vmTypeToLanguage(String typeName) { 158 // if the typename is (null), just return it as-is. This is probably in dexopt and 159 // will be discarded anyway. NOTE: This corresponds to the case in dalvik/vm/oo/Class.c 160 // where dvmLinkClass() returns false and we clean up and exit. 161 if ("(null)".equals(typeName)) { 162 return typeName; 163 } 164 165 if (!typeName.startsWith("L") || !typeName.endsWith(";") ) { 166 throw new AssertionError("Bad name: " + typeName + " in line " + sourceLineNumber); 167 } 168 169 typeName = typeName.substring(1, typeName.length() - 1); 170 return typeName.replace("/", "."); 171 } 172 } 173