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