1 /*
2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package vm.share.stack;
24 
25 import java.util.List;
26 import java.util.Map;
27 
28 import nsk.share.TestFailure;
29 import nsk.share.log.Log;
30 
31 public final class StackUtils {
StackUtils()32         private StackUtils() {
33         }
34 
replace(String s)35         private static String replace(String s) {
36                 return (s == null || s.length() == 0) ? "?" : s;
37         }
38 
39         /**
40          * String representation of stack trace element.
41          *
42          * Note that null and empty values are replaced with '?'.
43          */
strStackTraceElement(StackTraceElement element)44         public static String strStackTraceElement(StackTraceElement element) {
45                 return "at " + replace(element.getClassName()) + "." + replace(element.getMethodName()) + "(" + replace(element.getFileName()) + ":" + element.getLineNumber() + ")";
46         }
47 
printStackTraceElement(Log log, StackTraceElement element)48         public static void printStackTraceElement(Log log, StackTraceElement element) {
49                 log.info("       " + strStackTraceElement(element));
50         }
51 
printStackTrace(Log log, StackTraceElement[] elements)52         public static void printStackTrace(Log log, StackTraceElement[] elements) {
53                 for (StackTraceElement element : elements)
54                         printStackTraceElement(log, element);
55         }
56 
printStackTrace(Log log, Iterable<StackTraceElement> elements)57         public static void printStackTrace(Log log, Iterable<StackTraceElement> elements) {
58                 for (StackTraceElement element : elements)
59                         printStackTraceElement(log, element);
60         }
61 
62         /**
63          * Check that element matches expected element.
64          *
65          * Expected element is used as pattern for matching. A null or empty
66          * field value means that no comparison is done.
67          */
matches(StackTraceElement element, StackTraceElement expected)68         public static boolean matches(StackTraceElement element, StackTraceElement expected) {
69                 return
70                         (expected.getClassName() == null || expected.getClassName().length() == 0 || expected.getClassName().equals(element.getClassName())) &&
71                         (expected.getMethodName() == null || expected.getMethodName().length() == 0 || expected.getMethodName().equals(element.getMethodName())) &&
72                         (expected.isNativeMethod() == element.isNativeMethod());
73         }
74 
expectedTraceElement(String className, String methodName, boolean nativeMethod)75         public static StackTraceElement expectedTraceElement(String className, String methodName, boolean nativeMethod) {
76                 // Replace null className with empty because StackTraceElement constructor does not allow null className.
77                 return new StackTraceElement(className == null ? "" : className, methodName, null, (nativeMethod ? -2 : 0));
78         }
79 
addExpectedTraceElement(List<StackTraceElement> expectedTrace, String className, String methodName, boolean nativeMethod)80         public static void addExpectedTraceElement(List<StackTraceElement> expectedTrace, String className, String methodName, boolean nativeMethod) {
81                 expectedTrace.add(0, expectedTraceElement(className, methodName, nativeMethod));
82         }
83 
84         /**
85          * Check that trace elements starting from given index match expected elements.
86          */
checkMatches(StackTraceElement[] elements, List<StackTraceElement> expectedTrace, int i)87         public static void checkMatches(StackTraceElement[] elements, List<StackTraceElement> expectedTrace, int i) {
88                 if (elements.length - i < expectedTrace.size())
89                         throw new TestFailure("Expected at least " + expectedTrace.size() + " trace elements, got only " + (i + 1));
90                 for (int j = 0; j < expectedTrace.size(); ++j) {
91                         StackTraceElement expected = expectedTrace.get((expectedTrace.size() - 1) - j);
92                         int index = (elements.length - 1) - i - j;
93                         StackTraceElement actual = elements[index];
94                         if (!matches(actual, expected))
95                                 throw new TestFailure("Expected element at index " + index + " to match: " + strStackTraceElement(expected));
96                 }
97         }
98 
99         /**
100          * Find matching stack trace element starting from top of the stack.
101          */
findMatch(StackTraceElement[] elements, StackTraceElement expected)102         public static int findMatch(StackTraceElement[] elements, StackTraceElement expected) {
103                 for (int i = 0; i < elements.length; ++i)
104                         if (StackUtils.matches(elements[elements.length - 1 - i], expected))
105                                 return i;
106                 return -1;
107         }
108 
109         /**
110          * Find the stack trace element that contains the "main(String[])" method
111          *
112          * @return StackTraceElement containing "main" function, null if there are more than one
113          */
findMain()114         public static StackTraceElement findMain() {
115                 Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
116                 StackTraceElement mainMethodFrame = null;
117                 for(StackTraceElement[] current : stackTraces.values()) {
118                         if (current.length > 0) {
119                                 StackTraceElement last = current[current.length - 1];
120                                 if ("main".equals(last.getMethodName())) {
121                                         if (mainMethodFrame == null) {
122                                                 mainMethodFrame = last;
123                                         } else if (!mainMethodFrame.getClassName().equals(last.getClassName())) {
124                                                 // more than one class has a "main"
125                                                 return null;
126                                         }
127                                 }
128                         }
129                 }
130                 return mainMethodFrame;
131         }
132 }
133