summaryrefslogtreecommitdiff
path: root/src/com/android/loganalysis/parser/KernelLogParser.java
blob: e240432f4a78ccbfcac26f443b06c6eda9dfbbe2 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (C) 2013 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.
 */
package com.android.loganalysis.parser;

import com.android.loganalysis.item.KernelLogItem;
import com.android.loganalysis.item.MiscKernelLogItem;
import com.android.loganalysis.item.SELinuxItem;
import com.android.loganalysis.util.LogPatternUtil;
import com.android.loganalysis.util.LogTailUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A {@link IParser} to parse {@code /proc/last_kmsg} and the output from {@code dmsg}.
*/
public class KernelLogParser implements IParser {
    public static final String KERNEL_RESET = "KERNEL_RESET";
    public static final String KERNEL_ERROR = "KERNEL_ERROR";
    public static final String SELINUX_DENIAL = "SELINUX_DENIAL";

    /**
     * Matches: [     0.000000] Message<br />
     * Matches: &lt;3&gt;[     0.000000] Message
     */
    private static final Pattern LOG_LINE = Pattern.compile(
            "^(<\\d+>)?\\[\\s*(\\d+\\.\\d{6})\\] (.*)$");
    private static final Pattern SELINUX_DENIAL_PATTERN = Pattern.compile(
            ".*avc:\\s.*scontext=\\w*:\\w*:([\\w\\s]*):\\w*\\s.*");

    private KernelLogItem mKernelLog = null;
    private Double mStartTime = null;
    private Double mStopTime = null;

    private LogPatternUtil mPatternUtil = new LogPatternUtil();
    private LogTailUtil mPreambleUtil = new LogTailUtil(500, 50, 50);

    public KernelLogParser() {
        initPatterns();
    }

    /**
     * Parse a kernel log from a {@link BufferedReader} into an {@link KernelLogItem} object.
     *
     * @return The {@link KernelLogItem}.
     * @see #parse(List)
     */
    public KernelLogItem parse(BufferedReader input) throws IOException {
        String line;
        while ((line = input.readLine()) != null) {
            parseLine(line);
        }
        commit();

        return mKernelLog;
    }

    /**
     * {@inheritDoc}
     *
     * @return The {@link KernelLogItem}.
     */
    @Override
    public KernelLogItem parse(List<String> lines) {
        for (String line : lines) {
            parseLine(line);
        }
        commit();

        return mKernelLog;
    }

    /**
     * Parse a line of input.
     *
     * @param line The line to parse
     */
    private void parseLine(String line) {
        if ("".equals(line.trim())) {
            return;
        }
        if (mKernelLog == null) {
            mKernelLog = new KernelLogItem();
        }
        Matcher m = LOG_LINE.matcher(line);
        if (m.matches()) {
            Double time = Double.parseDouble(m.group(2));
            String msg = m.group(3);

            if (mStartTime == null) {
                mStartTime = time;
            }
            mStopTime = time;

            checkAndAddKernelEvent(msg);

            mPreambleUtil.addLine(null, line);
        } else {
            checkAndAddKernelEvent(line);
        }
    }

    /**
     * Checks if a kernel log message matches a pattern and add a kernel event if it does.
     */
    private void checkAndAddKernelEvent(String message) {
        String category = mPatternUtil.checkMessage(message);
        if (category == null) {
            return;
        }

        MiscKernelLogItem kernelLogItem;
        if (category.equals(SELINUX_DENIAL)) {
            SELinuxItem selinuxItem = new SELinuxItem();
            Matcher m = SELINUX_DENIAL_PATTERN.matcher(message);
            if (m.matches()) {
                selinuxItem.setSContext(m.group(1));
            }
            kernelLogItem = selinuxItem;
        } else {
            kernelLogItem = new MiscKernelLogItem();
        }
        kernelLogItem.setEventTime(mStopTime);
        kernelLogItem.setPreamble(mPreambleUtil.getLastTail());
        kernelLogItem.setStack(message);
        kernelLogItem.setCategory(category);
        mKernelLog.addEvent(kernelLogItem);
    }

    /**
     * Signal that the input has finished.
     */
    private void commit() {
        if (mKernelLog == null) {
            return;
        }
        mKernelLog.setStartTime(mStartTime);
        mKernelLog.setStopTime(mStopTime);
    }

    private void initPatterns() {
        // Kernel resets
        // TODO: Separate out device specific patterns
        final String[] kernelResets = {
            "smem: DIAG.*",
            "smsm: AMSS FATAL ERROR.*",
            "kernel BUG at .*",
            "PVR_K:\\(Fatal\\): Debug assertion failed! \\[.*\\]",
            "Kernel panic.*",
            "BP panicked",
            "WROTE DSP RAMDUMP",
            "tegra_wdt: last reset due to watchdog timeout.*",
            "tegra_wdt tegra_wdt.0: last reset is due to watchdog timeout.*",
            "Last reset was MPU Watchdog Timer reset.*",
            "\\[MODEM_IF\\].*CRASH.*",
            "Last boot reason: (?:kernel_panic|rpm_err|hw_reset(?:$|\n)|wdog_.*|" +
            "tz_err|adsp_err|modem_err|mba_err|watchdogr?|Watchdog|Panic)",
            "Last reset was system watchdog timer reset.*",
        };
        for (String pattern : kernelResets) {
            mPatternUtil.addPattern(Pattern.compile(pattern), KERNEL_RESET);
        }

        mPatternUtil.addPattern(Pattern.compile("Internal error:.*"), KERNEL_ERROR);

        // SELINUX denials
        mPatternUtil.addPattern(SELINUX_DENIAL_PATTERN, SELINUX_DENIAL);
    }

    /**
     * Get the internal {@link LogPatternUtil}. Exposed for unit testing.
     */
    LogPatternUtil getLogPatternUtil() {
        return mPatternUtil;
    }
}