1 /* 2 * Copyright (c) 2018, 2019, 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.testlibrary.Asserts.assertEquals; 29 import static jdk.testlibrary.Asserts.assertNotNull; 30 import static jdk.testlibrary.Asserts.assertNull; 31 import static jdk.testlibrary.Asserts.assertTrue; 32 33 import java.util.List; 34 import java.util.function.Consumer; 35 36 import jdk.jfr.Recording; 37 import jdk.jfr.consumer.RecordedEvent; 38 import jdk.jfr.consumer.RecordedFrame; 39 import jdk.jfr.consumer.RecordedMethod; 40 import jdk.jfr.consumer.RecordedStackTrace; 41 import jdk.testlibrary.Asserts; 42 import jdk.testlibrary.jfr.Events; 43 import jdk.testlibrary.jfr.SimpleEvent; 44 45 /* 46 * @test 47 * @summary Verifies that a recorded JFR event has the correct stack trace info 48 * @key jfr 49 * @library /lib/testlibrary 50 * @run main/othervm jdk.jfr.api.consumer.TestGetStackTrace 51 */ 52 public class TestGetStackTrace { 53 54 public static void main(String[] args) throws Throwable { 55 testStackTrace(r -> r.enable(SimpleEvent.class), TestGetStackTrace::assertNoStackTrace); 56 testStackTrace(r -> r.enable(SimpleEvent.class).withoutStackTrace(), TestGetStackTrace::assertStackTrace); 57 } 58 59 private static void testStackTrace(Consumer<Recording> recordingConfigurer, Consumer<RecordedEvent> asserter) throws Throwable { 60 Recording r = new Recording(); 61 recordingConfigurer.accept(r); 62 r.start(); 63 SimpleEvent event = new SimpleEvent(); 64 event.commit(); 65 r.stop(); 66 List<RecordedEvent> events = Events.fromRecording(r); 67 r.close(); 68 Events.hasEvents(events); 69 } 70 71 private static void assertNoStackTrace(RecordedEvent re) { 72 assertNull(re.getStackTrace()); 73 } 74 75 private static void assertStackTrace(RecordedEvent re) { 76 assertNotNull(re.getStackTrace()); 77 RecordedStackTrace strace = re.getStackTrace(); 78 assertEquals(strace.isTruncated(), false); 79 List<RecordedFrame> frames = strace.getFrames(); 80 assertTrue(frames.size() > 0); 81 for (RecordedFrame frame : frames) { 82 assertFrame(frame); 83 } 84 } 85 86 private static void assertFrame(RecordedFrame frame) { 87 int bci = frame.getBytecodeIndex(); 88 int line = frame.getLineNumber(); 89 boolean javaFrame = frame.isJavaFrame(); 90 RecordedMethod method = frame.getMethod(); 91 String type = frame.getType(); 92 System.out.println("*** Frame Info ***"); 93 System.out.println("bci=" + bci); 94 System.out.println("line=" + line); 95 System.out.println("type=" + type); 96 System.out.println("method=" + method); 97 System.out.println("***"); 98 Asserts.assertTrue(javaFrame, "Only Java frame are currently supported"); 99 Asserts.assertGreaterThanOrEqual(bci, -1); 100 Asserts.assertNotNull(method, "Method should not be null"); 101 } 102 }