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