summaryrefslogtreecommitdiffstats
path: root/tools/preload/Record.java
blob: b2be4d436dbe8132a2a72207df8214dc4f4f1380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * One line from the loaded-classes file.
 */
class Record {

    enum Type {
        /** Start of initialization. */
        START_LOAD,

        /** End of initialization. */
        END_LOAD,

        /** Start of initialization. */
        START_INIT,

        /** End of initialization. */
        END_INIT
    }

    /** Parent process ID. */
    final int ppid;

    /** Process ID. */
    final int pid;

    /** Thread ID. */
    final int tid;

    /** Process name. */
    final String processName;

    /** Class loader pointer. */
    final int classLoader;

    /** Type of record. */
    final Type type;

    /** Name of loaded class. */
    final String className;

    /** Record time (ns). */
    final long time;
    
    /** Source file line# */
    int sourceLineNumber;

    /**
     * Parses a line from the loaded-classes file.
     */
    Record(String line, int lineNum) {
        char typeChar = line.charAt(0);
        switch (typeChar) {
            case '>': type = Type.START_LOAD; break;
            case '<': type = Type.END_LOAD; break;
            case '+': type = Type.START_INIT; break;
            case '-': type = Type.END_INIT; break;
            default: throw new AssertionError("Bad line: " + line);
        }

        sourceLineNumber = lineNum;
        
        line = line.substring(1);
        String[] parts = line.split(":");

        ppid = Integer.parseInt(parts[0]);
        pid = Integer.parseInt(parts[1]);
        tid = Integer.parseInt(parts[2]);

        processName = decode(parts[3]).intern();

        classLoader = Integer.parseInt(parts[4]);
        className = vmTypeToLanguage(decode(parts[5])).intern();

        time = Long.parseLong(parts[6]);
    }
    
    /**
     * Decode any escaping that may have been written to the log line.
     * 
     * Supports unicode-style escaping:  \\uXXXX = character in hex
     * 
     * @param rawField the field as it was written into the log
     * @result the same field with any escaped characters replaced
     */
    String decode(String rawField) {
        String result = rawField;
        int offset = result.indexOf("\\u");
        while (offset >= 0) {
            String before = result.substring(0, offset);
            String escaped = result.substring(offset+2, offset+6);
            String after = result.substring(offset+6);
            
            result = String.format("%s%c%s", before, Integer.parseInt(escaped, 16), after);

            // find another but don't recurse  
            offset = result.indexOf("\\u", offset + 1);          
        }
        return result;
    }

    /**
     * Converts a VM-style name to a language-style name.
     */
    String vmTypeToLanguage(String typeName) {
        // if the typename is (null), just return it as-is.  This is probably in dexopt and 
        // will be discarded anyway.  NOTE: This corresponds to the case in dalvik/vm/oo/Class.c
        // where dvmLinkClass() returns false and we clean up and exit.
        if ("(null)".equals(typeName)) {
            return typeName;
        }
        
        if (!typeName.startsWith("L") || !typeName.endsWith(";") ) {
            throw new AssertionError("Bad name: " + typeName + " in line " + sourceLineNumber);
        }

        typeName = typeName.substring(1, typeName.length() - 1);
        return typeName.replace("/", ".");
    }
}