blob: 97815ccad9716520e66ff5210ee0e970915e7ba6 [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
17import java.util.ArrayList;
18import java.util.Collections;
19import java.util.List;
20
21public class PrintThread {
22 public static void print(String[][] stack) {
23 System.out.println("---------");
24 for (String[] stackElement : stack) {
25 for (String part : stackElement) {
26 System.out.print(' ');
27 System.out.print(part);
28 }
29 System.out.println();
30 }
31 }
32
33 public static void print(Thread t, int start, int max) {
34 print(getStackTrace(t, start, max));
35 }
36
37 public static void printAll(Object[][] stacks) {
38 List<String> stringified = new ArrayList<String>(stacks.length);
39
40 for (Object[] stackInfo : stacks) {
41 Thread t = (Thread)stackInfo[0];
42 String name = (t != null) ? t.getName() : "null";
43 String stackSerialization;
44 if (name.contains("Daemon")) {
45 // Do not print daemon stacks, as they're non-deterministic.
46 stackSerialization = "<not printed>";
47 } else {
48 StringBuilder sb = new StringBuilder();
49 for (String[] stackElement : (String[][])stackInfo[1]) {
50 for (String part : stackElement) {
51 sb.append(' ');
52 sb.append(part);
53 }
54 sb.append('\n');
55 }
56 stackSerialization = sb.toString();
57 }
58 stringified.add(name + "\n" + stackSerialization);
59 }
60
61 Collections.sort(stringified);
62
63 for (String s : stringified) {
64 System.out.println("---------");
65 System.out.println(s);
66 }
67 }
68
69 public static native String[][] getStackTrace(Thread thread, int start, int max);
70}