Home | History | Annotate | Download | only in stacks
      1 /*
      2  * Copyright (C) 2016 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 package com.android.bugreport.stacks;
     18 
     19 import com.android.bugreport.cpuinfo.CpuUsageParser;
     20 import com.android.bugreport.cpuinfo.CpuUsageSnapshot;
     21 import com.android.bugreport.util.Utils;
     22 import com.android.bugreport.util.Line;
     23 import com.android.bugreport.util.Lines;
     24 import com.android.bugreport.stacks.ProcessSnapshot;
     25 import com.android.bugreport.stacks.ProcessSnapshotParser;
     26 import com.android.bugreport.stacks.VmTraces;
     27 
     28 import java.io.BufferedReader;
     29 import java.io.IOException;
     30 import java.util.ArrayList;
     31 import java.util.regex.Pattern;
     32 import java.util.regex.Matcher;
     33 
     34 /**
     35  * Parse an anr block from a monkey log.
     36  *
     37  * The parser can be reused, but is not thread safe.
     38  */
     39 public class VmTracesParser {
     40 
     41     private final Matcher mBeginProcessRe = ProcessSnapshotParser.BEGIN_PROCESS_RE.matcher("");
     42 
     43     /**
     44      * Construct a new parser.
     45      */
     46     public VmTracesParser() {
     47     }
     48 
     49     /**
     50      * Do the parsing.
     51      */
     52     public VmTraces parse(Lines<? extends Line> lines) {
     53         final VmTraces result = new VmTraces();
     54 
     55         // Drop any preamble
     56         while (lines.hasNext()) {
     57             final Line line = lines.next();
     58             final String text = line.text;
     59             if (Utils.matches(mBeginProcessRe, text)) {
     60                 lines.rewind();
     61                 break;
     62             }
     63         }
     64 
     65         while (lines.hasNext()) {
     66             final Line line = lines.next();
     67             final String text = line.text;
     68 
     69             if (Utils.matches(mBeginProcessRe, text)) {
     70                 lines.rewind();
     71                 ProcessSnapshotParser parser = new ProcessSnapshotParser();
     72                 final ProcessSnapshot snapshot = parser.parse(lines);
     73                 if (snapshot != null) {
     74                     result.processes.add(snapshot);
     75                 } else {
     76                     // TODO: Try to backtrack and correct the parsing.
     77                 }
     78             } else {
     79                 if (false) {
     80                     System.out.println("VmTracesParser Dropping: " + text);
     81                 }
     82             }
     83         }
     84 
     85         return result;
     86     }
     87 
     88 }
     89 
     90 
     91