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