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