1 /*
2  * Copyright (c) 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package jdk.jfr.api.consumer;
27 
28 import static jdk.test.lib.Asserts.assertEquals;
29 import static jdk.test.lib.Asserts.assertNotNull;
30 import static jdk.test.lib.Asserts.assertNull;
31 import static jdk.test.lib.Asserts.assertTrue;
32 
33 import java.util.List;
34 
35 import jdk.jfr.Recording;
36 import jdk.jfr.consumer.RecordedEvent;
37 import jdk.jfr.consumer.RecordedFrame;
38 import jdk.jfr.consumer.RecordedMethod;
39 import jdk.jfr.consumer.RecordedStackTrace;
40 import jdk.test.lib.Asserts;
41 import jdk.test.lib.jfr.Events;
42 import jdk.test.lib.jfr.SimpleEvent;
43 
44 /**
45  * @test
46  * @summary Verifies that a recorded JFR event has the correct stack trace info
47  * @key jfr
48  * @requires vm.hasJFR
49  * @library /test/lib
50  * @run main/othervm jdk.jfr.api.consumer.TestGetStackTrace
51  */
52 public class TestGetStackTrace {
53 
main(String[] args)54     public static void main(String[] args) throws Throwable {
55         testWithoutStackTrace(recordEvent("stackTrace", "false"));
56         testWithStackTrace(recordEvent("stackTrace", "true"));
57     }
58 
recordEvent(String key, String value)59     private static RecordedEvent recordEvent(String key, String value) throws Throwable {
60         try (Recording r = new Recording()) {
61             r.enable(SimpleEvent.class).with(key, value);
62             r.start();
63             SimpleEvent event = new SimpleEvent();
64             event.commit();
65             r.stop();
66             return Events.fromRecording(r).get(0);
67         }
68     }
69 
testWithoutStackTrace(RecordedEvent re)70     private static void testWithoutStackTrace(RecordedEvent re) {
71         assertNull(re.getStackTrace());
72     }
73 
testWithStackTrace(RecordedEvent re)74     private static void testWithStackTrace(RecordedEvent re) {
75         assertNotNull(re.getStackTrace());
76         RecordedStackTrace strace = re.getStackTrace();
77         assertEquals(strace.isTruncated(), false);
78         List<RecordedFrame> frames = strace.getFrames();
79         assertTrue(frames.size() > 0);
80         for (RecordedFrame frame : frames) {
81             assertFrame(frame);
82         }
83     }
84 
assertFrame(RecordedFrame frame)85     private static void assertFrame(RecordedFrame frame) {
86         int bci = frame.getBytecodeIndex();
87         int line = frame.getLineNumber();
88         boolean javaFrame = frame.isJavaFrame();
89         RecordedMethod method = frame.getMethod();
90         String type = frame.getType();
91         System.out.println("*** Frame Info ***");
92         System.out.println("bci=" + bci);
93         System.out.println("line=" + line);
94         System.out.println("type=" + type);
95         System.out.println("method=" + method);
96         System.out.println("***");
97         Asserts.assertTrue(javaFrame, "Only Java frame are currently supported");
98         Asserts.assertGreaterThanOrEqual(bci, -1);
99         Asserts.assertNotNull(method, "Method should not be null");
100     }
101 }
102