summaryrefslogtreecommitdiff
path: root/src/com/android/loganalysis/parser/BugreportParser.java
blob: 09ba78da046a70915bb1b354e4a7518c0c2d7bfe (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (C) 2011 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.AnrItem;
import com.android.loganalysis.item.BugreportItem;
import com.android.loganalysis.item.BugreportItem.CommandLineItem;
import com.android.loganalysis.item.DumpsysItem;
import com.android.loganalysis.item.IItem;
import com.android.loganalysis.item.KernelLogItem;
import com.android.loganalysis.item.LogcatItem;
import com.android.loganalysis.item.MemInfoItem;
import com.android.loganalysis.item.MiscKernelLogItem;
import com.android.loganalysis.item.MiscLogcatItem;
import com.android.loganalysis.item.ProcrankItem;
import com.android.loganalysis.item.SystemPropsItem;
import com.android.loganalysis.item.TopItem;
import com.android.loganalysis.item.TracesItem;

import java.io.BufferedReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A {@link IParser} to parse Android bugreports.
 */
public class BugreportParser extends AbstractSectionParser {
    private static final String MEM_INFO_SECTION_REGEX = "------ MEMORY INFO .*";
    private static final String PROCRANK_SECTION_REGEX = "------ PROCRANK .*";
    private static final String KERNEL_LOG_SECTION_REGEX = "------ KERNEL LOG .*";
    private static final String LAST_KMSG_SECTION_REGEX = "------ LAST KMSG .*";
    private static final String TOP_SECTION_REGEX = "------ CPU INFO .*";
    private static final String SYSTEM_PROP_SECTION_REGEX = "------ SYSTEM PROPERTIES .*";
    private static final String SYSTEM_LOG_SECTION_REGEX =
            "------ (SYSTEM|MAIN|MAIN AND SYSTEM) LOG .*";
    private static final String ANR_TRACES_SECTION_REGEX = "------ VM TRACES AT LAST ANR .*";
    private static final String DUMPSYS_SECTION_REGEX = "------ DUMPSYS .*";
    private static final String NOOP_SECTION_REGEX = "------ .*";

    private static final String BOOTREASON = "androidboot.bootreason";

    /**
     * Matches: == dumpstate: 2012-04-26 12:13:14
     */
    private static final Pattern DATE = Pattern.compile(
            "^== dumpstate: (\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})$");

    /**
     * Matches: Command line: key=value key=value
     */
    private static final Pattern COMMAND_LINE = Pattern.compile(
            "Command line: (.*)");

    private IParser mBugreportParser = new IParser() {
        @Override
        public BugreportItem parse(List<String> lines) {
            BugreportItem bugreport = null;
            for (String line : lines) {
                if (bugreport == null && !"".equals(line.trim())) {
                    bugreport = new BugreportItem();
                }
                Matcher m = DATE.matcher(line);
                if (m.matches()) {
                    bugreport.setTime(parseTime(m.group(1)));
                }
                m = COMMAND_LINE.matcher(line);
                if (m.matches()) {
                    String argString = m.group(1).trim();
                    if (!argString.isEmpty()) {
                        String[] args = argString.split("\\s+");
                        for (String arg : args) {
                            String[] keyValue = arg.split("=", 2);
                            if (keyValue.length == 2) {
                                mCommandLine.put(keyValue[0], keyValue[1]);
                            } else {
                                mCommandLine.put(keyValue[0], null);
                            }
                        }
                    }
                }
            }
            return bugreport;
        }
    };
    private MemInfoParser mMemInfoParser = new MemInfoParser();
    private ProcrankParser mProcrankParser = new ProcrankParser();
    private TopParser mTopParser = new TopParser();
    private SystemPropsParser mSystemPropsParser = new SystemPropsParser();
    private TracesParser mTracesParser = new TracesParser();
    private KernelLogParser mKernelLogParser = new KernelLogParser();
    private KernelLogParser mLastKmsgParser = new KernelLogParser();
    private LogcatParser mLogcatParser = new LogcatParser();
    private DumpsysParser mDumpsysParser = new DumpsysParser();

    private BugreportItem mBugreport = null;
    private CommandLineItem mCommandLine = new CommandLineItem();

    private boolean mParsedInput = false;

    /**
     * Parse a bugreport from a {@link BufferedReader} into an {@link BugreportItem} object.
     *
     * @param input a {@link BufferedReader}.
     * @return The {@link BugreportItem}.
     * @see #parse(List)
     */
    public BugreportItem parse(BufferedReader input) throws IOException {
        String line;

        setup();
        while ((line = input.readLine()) != null) {
            if (!mParsedInput && !"".equals(line.trim())) {
                mParsedInput = true;
            }
            parseLine(line);
        }
        commit();

        return mBugreport;
    }

    /**
     * {@inheritDoc}
     *
     * @return The {@link BugreportItem}.
     */
    @Override
    public BugreportItem parse(List<String> lines) {
        setup();
        for (String line : lines) {
            if (!mParsedInput && !"".equals(line.trim())) {
                mParsedInput = true;
            }
            parseLine(line);
        }
        commit();

        return mBugreport;
    }

    /**
     * Sets up the parser by adding the section parsers and adding an initial {@link IParser} to
     * parse the bugreport header.
     */
    protected void setup() {
        // Set the initial parser explicitly since the header isn't part of a section.
        setParser(mBugreportParser);
        addSectionParser(mMemInfoParser, MEM_INFO_SECTION_REGEX);
        addSectionParser(mProcrankParser, PROCRANK_SECTION_REGEX);
        addSectionParser(mTopParser, TOP_SECTION_REGEX);
        addSectionParser(mSystemPropsParser, SYSTEM_PROP_SECTION_REGEX);
        addSectionParser(mTracesParser, ANR_TRACES_SECTION_REGEX);
        addSectionParser(mLogcatParser, SYSTEM_LOG_SECTION_REGEX);
        addSectionParser(mKernelLogParser, KERNEL_LOG_SECTION_REGEX);
        addSectionParser(mLastKmsgParser, LAST_KMSG_SECTION_REGEX);
        addSectionParser(mDumpsysParser, DUMPSYS_SECTION_REGEX);
        addSectionParser(new NoopParser(), NOOP_SECTION_REGEX);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void commit() {
        // signal EOF
        super.commit();

        if (mParsedInput && mBugreport == null) {
            mBugreport = new BugreportItem();
        }

        if (mBugreport != null) {
            mBugreport.setCommandLine(mCommandLine);
            mBugreport.setMemInfo((MemInfoItem) getSection(mMemInfoParser));
            mBugreport.setProcrank((ProcrankItem) getSection(mProcrankParser));
            mBugreport.setTop((TopItem) getSection(mTopParser));
            mBugreport.setSystemLog((LogcatItem) getSection(mLogcatParser));
            mBugreport.setKernelLog((KernelLogItem) getSection(mKernelLogParser));
            mBugreport.setLastKmsg((KernelLogItem) getSection(mLastKmsgParser));
            mBugreport.setSystemProps((SystemPropsItem) getSection(mSystemPropsParser));
            mBugreport.setDumpsys((DumpsysItem) getSection(mDumpsysParser));

            if (mBugreport.getSystemLog() != null && mBugreport.getProcrank() != null) {
                for (IItem item : mBugreport.getSystemLog().getEvents()) {
                    if (item instanceof MiscLogcatItem &&
                            ((MiscLogcatItem) item).getApp() == null) {
                        MiscLogcatItem logcatItem = (MiscLogcatItem) item;
                        logcatItem.setApp(mBugreport.getProcrank().getProcessName(
                                logcatItem.getPid()));
                    }
                }
            }

            TracesItem traces = (TracesItem) getSection(mTracesParser);
            if (traces != null && traces.getApp() != null && traces.getStack() != null &&
                    mBugreport.getSystemLog() != null) {
                addAnrTrace(mBugreport.getSystemLog().getAnrs(), traces.getApp(),
                        traces.getStack());
            }

            if (mCommandLine.containsKey(BOOTREASON)) {
                String bootreason = mCommandLine.get(BOOTREASON);
                Matcher m = KernelLogParser.BAD_BOOTREASONS.matcher(bootreason);
                if (m.matches()) {
                    if (mBugreport.getLastKmsg() == null) {
                        mBugreport.setLastKmsg(new KernelLogItem());
                    }
                    MiscKernelLogItem item = new MiscKernelLogItem();
                    item.setStack("Last boot reason: " + bootreason.trim());
                    item.setCategory(KernelLogParser.KERNEL_RESET);
                    mBugreport.getLastKmsg().addEvent(item);
                }
            }
        }
    }

    /**
     * Add the trace from {@link TracesItem} to the last seen {@link AnrItem} matching a given app.
     */
    private void addAnrTrace(List<AnrItem> anrs, String app, String trace) {
        ListIterator<AnrItem> li = anrs.listIterator(anrs.size());

        while (li.hasPrevious()) {
            AnrItem anr = li.previous();
            if (app.equals(anr.getApp())) {
                anr.setTrace(trace);
                return;
            }
        }
    }

    /**
     * Set the {@link BugreportItem} and the year of the {@link LogcatParser} from the bugreport
     * header.
     */
    @Override
    protected void onSwitchParser() {
        if (mBugreport == null) {
            mBugreport = (BugreportItem) getSection(mBugreportParser);
            if (mBugreport != null && mBugreport.getTime() != null) {
                mLogcatParser.setYear(new SimpleDateFormat("yyyy").format(mBugreport.getTime()));
            }
        }
    }

    /**
     * Converts a {@link String} into a {@link Date}.
     */
    private static Date parseTime(String timeStr) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return formatter.parse(timeStr);
        } catch (ParseException e) {
            // CLog.e("Could not parse time string %s", timeStr);
            return null;
        }
    }
}