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