1 /* 2 * Copyright (c) 2016, 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 package jdk.jfr.api.consumer; 26 27 import java.io.IOException; 28 import java.util.List; 29 30 import jdk.jfr.Recording; 31 import jdk.jfr.consumer.RecordedEvent; 32 import jdk.jfr.consumer.RecordedFrame; 33 import jdk.jfr.consumer.RecordedMethod; 34 import jdk.jfr.consumer.RecordedStackTrace; 35 import jdk.test.lib.Asserts; 36 import jdk.test.lib.jfr.Events; 37 import jdk.test.lib.jfr.SimpleEvent; 38 39 40 /** 41 * @test 42 * @summary Simple test for RecordedFrame APIs 43 * @key jfr 44 * 45 * @library /lib / 46 * @run main/othervm -Xint -XX:+UseInterpreter -Dinterpreted=true jdk.jfr.api.consumer.TestRecordedFrame 47 * @run main/othervm -Xcomp -XX:-UseInterpreter -Dinterpreted=false jdk.jfr.api.consumer.TestRecordedFrame 48 */ 49 public final class TestRecordedFrame { 50 51 public static void main(String[] args) throws IOException { 52 StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); 53 doTest(getLineNumber("main", stackTrace) + 1); 54 } 55 56 /** 57 * Returns line number of the passed method for the passed stacktrace 58 */ 59 private static int getLineNumber(String methodName, StackTraceElement[] stackTrace) { 60 for (StackTraceElement ste : stackTrace) { 61 if (methodName.equals(ste.getMethodName())) { 62 return ste.getLineNumber(); 63 } 64 } 65 throw new RuntimeException("Unexpected error: could not analyze stacktrace"); 66 } 67 68 public static void doTest(int lineNumber) throws IOException { 69 70 System.out.println("Enetring method"); 71 72 Recording recording = new Recording(); 73 recording.start(); 74 75 SimpleEvent ev = new SimpleEvent(); 76 commitEvent(ev); 77 recording.stop(); 78 79 List<RecordedEvent> recordedEvents = Events.fromRecording(recording); 80 Events.hasEvents(recordedEvents); 81 RecordedEvent recordedEvent = recordedEvents.get(0); 82 83 RecordedStackTrace stacktrace = recordedEvent.getStackTrace(); 84 List<RecordedFrame> frames = stacktrace.getFrames(); 85 for (RecordedFrame frame : frames) { 86 87 // All frames are java frames 88 Asserts.assertTrue(frame.isJavaFrame()); 89 // Verify the main() method frame 90 RecordedMethod method = frame.getMethod(); 91 if (method.getName().equals("main")) { 92 93 // Frame type 94 String type = frame.getType(); 95 System.out.println("type: " + type); 96 Asserts.assertTrue( 97 type.equals("Interpreted") 98 || type.equals("JIT compiled") 99 || type.equals("Inlined")); 100 101 Asserts.assertEquals(lineNumber, frame.getLineNumber()); 102 103 boolean isInterpreted = "Interpreted".equals(type); 104 boolean expectedInterpreted = "true".equals(System.getProperty("interpreted")); 105 Asserts.assertEquals(isInterpreted, expectedInterpreted); 106 107 int bci = frame.getBytecodeIndex(); 108 109 System.out.println("bci: " + bci); 110 Asserts.assertTrue(bci > 0); 111 } 112 113 } 114 115 } 116 117 private static void commitEvent(SimpleEvent ev) { 118 System.out.println("commit"); 119 ev.commit(); 120 } 121 122 }