1 /* 2 * Copyright (C) 2011 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 package com.android.loganalysis.parser; 17 18 import com.android.loganalysis.item.TracesItem; 19 import com.android.loganalysis.util.ArrayUtil; 20 21 import junit.framework.TestCase; 22 23 import java.util.Arrays; 24 import java.util.List; 25 26 /** 27 * Unit tests for {@link TracesParser} 28 */ 29 public class TracesParserTest extends TestCase { 30 31 /** 32 * Test that the parser parses the correct stack. 33 */ testTracesParser()34 public void testTracesParser() { 35 List<String> lines = Arrays.asList( 36 "", 37 "", 38 "----- pid 2887 at 2012-05-02 16:43:41 -----", 39 "Cmd line: com.android.package", 40 "", 41 "DALVIK THREADS:", 42 "(mutexes: tll=0 tsl=0 tscl=0 ghl=0)", 43 "", 44 "\"main\" prio=5 tid=1 SUSPENDED", 45 " | group=\"main\" sCount=1 dsCount=0 obj=0x00000001 self=0x00000001", 46 " | sysTid=2887 nice=0 sched=0/0 cgrp=foreground handle=0000000001", 47 " | schedstat=( 0 0 0 ) utm=5954 stm=1017 core=0", 48 " at class.method1(Class.java:1)", 49 " at class.method2(Class.java:2)", 50 " at class.method2(Class.java:2)", 51 "", 52 "\"Task_1\" prio=5 tid=27 WAIT", 53 " | group=\"main\" sCount=1 dsCount=0 obj=0x00000001 self=0x00000001", 54 " | sysTid=4789 nice=10 sched=0/0 cgrp=bg_non_interactive handle=0000000001", 55 " | schedstat=( 0 0 0 ) utm=0 stm=0 core=0", 56 " at class.method1(Class.java:1)", 57 " - waiting on <0x00000001> (a java.lang.Thread) held by tid=27 (Task_1)", 58 " at class.method2(Class.java:2)", 59 " at class.method2(Class.java:2)", 60 "", 61 "\"Task_2\" prio=5 tid=26 NATIVE", 62 " | group=\"main\" sCount=1 dsCount=0 obj=0x00000001 self=0x00000001", 63 " | sysTid=4343 nice=0 sched=0/0 cgrp=foreground handle=0000000001", 64 " | schedstat=( 0 0 0 ) utm=6 stm=3 core=0", 65 " #00 pc 00001234 /system/lib/lib.so (addr+8)", 66 " #01 pc 00001235 /system/lib/lib.so (addr+16)", 67 " #02 pc 00001236 /system/lib/lib.so (addr+24)", 68 " at class.method1(Class.java:1)", 69 "", 70 "----- end 2887 -----", 71 "", 72 "", 73 "----- pid 256 at 2012-05-02 16:43:41 -----", 74 "Cmd line: system", 75 "", 76 "DALVIK THREADS:", 77 "(mutexes: tll=0 tsl=0 tscl=0 ghl=0)", 78 "", 79 "\"main\" prio=5 tid=1 NATIVE", 80 " | group=\"main\" sCount=1 dsCount=0 obj=0x00000001 self=0x00000001", 81 " | sysTid=256 nice=0 sched=0/0 cgrp=foreground handle=0000000001", 82 " | schedstat=( 0 0 0 ) utm=175 stm=41 core=0", 83 " #00 pc 00001234 /system/lib/lib.so (addr+8)", 84 " #01 pc 00001235 /system/lib/lib.so (addr+16)", 85 " #02 pc 00001236 /system/lib/lib.so (addr+24)", 86 " at class.method1(Class.java:1)", 87 " at class.method2(Class.java:2)", 88 " at class.method2(Class.java:2)", 89 "", 90 "----- end 256 -----", 91 ""); 92 93 List<String> expectedStack = Arrays.asList( 94 "\"main\" prio=5 tid=1 SUSPENDED", 95 " | group=\"main\" sCount=1 dsCount=0 obj=0x00000001 self=0x00000001", 96 " | sysTid=2887 nice=0 sched=0/0 cgrp=foreground handle=0000000001", 97 " | schedstat=( 0 0 0 ) utm=5954 stm=1017 core=0", 98 " at class.method1(Class.java:1)", 99 " at class.method2(Class.java:2)", 100 " at class.method2(Class.java:2)"); 101 102 TracesItem traces = new TracesParser().parse(lines); 103 assertEquals(2887, traces.getPid().intValue()); 104 assertEquals("com.android.package", traces.getApp()); 105 assertEquals(ArrayUtil.join("\n", expectedStack), traces.getStack()); 106 } 107 108 /** 109 * Test that both forms of cmd line match for the trace. 110 */ testTracesParser_cmdline()111 public void testTracesParser_cmdline() { 112 List<String> expectedStack = Arrays.asList( 113 "\"main\" prio=5 tid=1 SUSPENDED", 114 " | group=\"main\" sCount=1 dsCount=0 obj=0x00000001 self=0x00000001", 115 " | sysTid=2887 nice=0 sched=0/0 cgrp=foreground handle=0000000001", 116 " | schedstat=( 0 0 0 ) utm=5954 stm=1017 core=0", 117 " at class.method1(Class.java:1)", 118 " at class.method2(Class.java:2)", 119 " at class.method2(Class.java:2)"); 120 121 List<String> lines = Arrays.asList( 122 "", 123 "", 124 "----- pid 2887 at 2012-05-02 16:43:41 -----", 125 "Cmd line: com.android.package", 126 "", 127 "DALVIK THREADS:", 128 "(mutexes: tll=0 tsl=0 tscl=0 ghl=0)", 129 "", 130 "\"main\" prio=5 tid=1 SUSPENDED", 131 " | group=\"main\" sCount=1 dsCount=0 obj=0x00000001 self=0x00000001", 132 " | sysTid=2887 nice=0 sched=0/0 cgrp=foreground handle=0000000001", 133 " | schedstat=( 0 0 0 ) utm=5954 stm=1017 core=0", 134 " at class.method1(Class.java:1)", 135 " at class.method2(Class.java:2)", 136 " at class.method2(Class.java:2)", 137 ""); 138 139 TracesItem traces = new TracesParser().parse(lines); 140 assertEquals(2887, traces.getPid().intValue()); 141 assertEquals("com.android.package", traces.getApp()); 142 assertEquals(ArrayUtil.join("\n", expectedStack), traces.getStack()); 143 144 lines = Arrays.asList( 145 "", 146 "", 147 "----- pid 2887 at 2012-05-02 16:43:41 -----", 148 "Cmdline: com.android.package Original command line: <unset>", 149 "", 150 "DALVIK THREADS:", 151 "(mutexes: tll=0 tsl=0 tscl=0 ghl=0)", 152 "", 153 "\"main\" prio=5 tid=1 SUSPENDED", 154 " | group=\"main\" sCount=1 dsCount=0 obj=0x00000001 self=0x00000001", 155 " | sysTid=2887 nice=0 sched=0/0 cgrp=foreground handle=0000000001", 156 " | schedstat=( 0 0 0 ) utm=5954 stm=1017 core=0", 157 " at class.method1(Class.java:1)", 158 " at class.method2(Class.java:2)", 159 " at class.method2(Class.java:2)", 160 ""); 161 162 traces = new TracesParser().parse(lines); 163 assertEquals(2887, traces.getPid().intValue()); 164 assertEquals("com.android.package", traces.getApp()); 165 assertEquals(ArrayUtil.join("\n", expectedStack), traces.getStack()); 166 } 167 } 168