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