blob: 14e9201ff1c1d27851efb3c7bec0f5c5cd81d037 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 */
20class Record {
21
Jesse Wilsone9fcaa02010-02-23 17:06:58 -080022 /**
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",
Kenny Rootdbf30ed2012-09-19 13:08:12 -070031 "com.google.android.apps.maps:GoogleLocationService",
32 "com.google.android.apps.maps\\u003AGoogleLocationService",
Jesse Wilsone9fcaa02010-02-23 17:06:58 -080033 "com.google.android.apps.maps:LocationFriendService",
34 "com.google.android.apps.maps\\u003ALocationFriendService",
Elliott Hughes98e00ad2012-05-17 17:03:51 -070035 "com.google.android.apps.maps:MapsBackgroundService",
36 "com.google.android.apps.maps\\u003AMapsBackgroundService",
Jesse Wilson80686182011-01-21 17:12:43 -080037 "com.google.android.apps.maps:NetworkLocationService",
38 "com.google.android.apps.maps\\u003ANetworkLocationService",
Kenny Rootdbf30ed2012-09-19 13:08:12 -070039 "com.android.chrome:sandboxed_process",
40 "com.android.chrome\\u003Asandboxed_process",
Elliott Hughes98e00ad2012-05-17 17:03:51 -070041 "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",
Jesse Wilsone9fcaa02010-02-23 17:06:58 -080053 };
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 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;
Jesse Wilson80686182011-01-21 17:12:43 -080092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 /** 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;
Jesse Wilsone9fcaa02010-02-23 17:06:58 -0800110
111 for (int i = 0; i < REPLACE_CLASSES.length; i+= 2) {
112 line = line.replace(REPLACE_CLASSES[i], REPLACE_CLASSES[i+1]);
113 }
Jesse Wilson80686182011-01-21 17:12:43 -0800114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 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 }
Jesse Wilson80686182011-01-21 17:12:43 -0800129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 /**
131 * Decode any escaping that may have been written to the log line.
Jesse Wilson80686182011-01-21 17:12:43 -0800132 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * Supports unicode-style escaping: \\uXXXX = character in hex
Jesse Wilson80686182011-01-21 17:12:43 -0800134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * @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);
Jesse Wilson80686182011-01-21 17:12:43 -0800145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 result = String.format("%s%c%s", before, Integer.parseInt(escaped, 16), after);
147
Jesse Wilson80686182011-01-21 17:12:43 -0800148 // find another but don't recurse
149 offset = result.indexOf("\\u", offset + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 }
151 return result;
152 }
153
154 /**
155 * Converts a VM-style name to a language-style name.
156 */
157 String vmTypeToLanguage(String typeName) {
Jesse Wilson80686182011-01-21 17:12:43 -0800158 // if the typename is (null), just return it as-is. This is probably in dexopt and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 // 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 }
Jesse Wilson80686182011-01-21 17:12:43 -0800164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 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}