blob: 94f3a33ada85954f4af0e97ac023d35ae10c9699 [file] [log] [blame]
Andreas Gampe966de9e2017-01-12 20:51:02 -08001/*
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
Andreas Gampe46651672017-04-07 09:00:04 -070017package art;
18
Andreas Gampe966de9e2017-01-12 20:51:02 -080019import java.util.ArrayList;
20import java.util.Collections;
21import java.util.List;
Andreas Gampe70ddf5e2017-04-21 16:04:27 -070022import java.util.regex.Matcher;
23import java.util.regex.Pattern;
Andreas Gampe966de9e2017-01-12 20:51:02 -080024
25public class PrintThread {
26 public static void print(String[][] stack) {
27 System.out.println("---------");
28 for (String[] stackElement : stack) {
29 for (String part : stackElement) {
30 System.out.print(' ');
31 System.out.print(part);
32 }
33 System.out.println();
34 }
35 }
36
37 public static void print(Thread t, int start, int max) {
38 print(getStackTrace(t, start, max));
39 }
40
Andreas Gampe70ddf5e2017-04-21 16:04:27 -070041 // We have to ignore some threads when printing all stack traces. These are threads that may or
42 // may not exist depending on the environment.
43 public final static String IGNORE_THREAD_NAME_REGEX =
Alex Light3cf6a702017-04-25 14:23:04 -070044 "Binder:|RenderThread|hwuiTask|Jit thread pool worker|Instr:|JDWP|Profile Saver|main|" +
Nicolas Geoffray3f649402021-03-12 16:40:34 +000045 "queued-work-looper|InstrumentationConnectionThread|intel_svc_streamer_thread|ForkJoinPool";
Andreas Gampe70ddf5e2017-04-21 16:04:27 -070046 public final static Matcher IGNORE_THREADS =
47 Pattern.compile(IGNORE_THREAD_NAME_REGEX).matcher("");
48
49 // We have to skip the stack of some threads when printing all stack traces. These are threads
50 // that may have a different call stack (e.g., when run as an app), or may be in a
51 // non-deterministic state.
52 public final static String CUT_STACK_THREAD_NAME_REGEX = "Daemon|main";
53 public final static Matcher CUT_STACK_THREADS =
54 Pattern.compile(CUT_STACK_THREAD_NAME_REGEX).matcher("");
55
Andreas Gampe966de9e2017-01-12 20:51:02 -080056 public static void printAll(Object[][] stacks) {
57 List<String> stringified = new ArrayList<String>(stacks.length);
58
59 for (Object[] stackInfo : stacks) {
60 Thread t = (Thread)stackInfo[0];
61 String name = (t != null) ? t.getName() : "null";
62 String stackSerialization;
Andreas Gampe70ddf5e2017-04-21 16:04:27 -070063 if (CUT_STACK_THREADS.reset(name).find()) {
Andreas Gampe966de9e2017-01-12 20:51:02 -080064 // Do not print daemon stacks, as they're non-deterministic.
65 stackSerialization = "<not printed>";
Andreas Gampe70ddf5e2017-04-21 16:04:27 -070066 } else if (IGNORE_THREADS.reset(name).find()) {
67 // Skip IGNORE_THREADS.
Andreas Gampe4471e4f2017-01-30 16:40:49 +000068 continue;
Andreas Gampe966de9e2017-01-12 20:51:02 -080069 } else {
70 StringBuilder sb = new StringBuilder();
71 for (String[] stackElement : (String[][])stackInfo[1]) {
72 for (String part : stackElement) {
73 sb.append(' ');
74 sb.append(part);
75 }
76 sb.append('\n');
77 }
78 stackSerialization = sb.toString();
79 }
80 stringified.add(name + "\n" + stackSerialization);
81 }
82
83 Collections.sort(stringified);
84
85 for (String s : stringified) {
86 System.out.println("---------");
87 System.out.println(s);
88 }
89 }
90
91 public static native String[][] getStackTrace(Thread thread, int start, int max);
Alex Light3cf6a702017-04-25 14:23:04 -070092}