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 static jdk.test.lib.Asserts.assertEquals;
  28 import static jdk.test.lib.Asserts.assertFalse;
  29 import static jdk.test.lib.Asserts.assertNotNull;
  30 import static jdk.test.lib.Asserts.assertTrue;
  31 
  32 import java.util.List;
  33 
  34 import jdk.jfr.Event;
  35 import jdk.jfr.Recording;
  36 import jdk.jfr.consumer.RecordedEvent;
  37 import jdk.jfr.consumer.RecordedMethod;
  38 import jdk.jfr.consumer.RecordedFrame;
  39 import jdk.jfr.consumer.RecordedStackTrace;
  40 import jdk.test.lib.jfr.Events;
  41 
  42 
  43 /*
  44  * @test
  45  * @key jfr
  46  * @library /test/lib
  47  * @run main/othervm jdk.jfr.api.consumer.TestRecordedMethodDescriptor
  48  */
  49 public final class TestRecordedMethodDescriptor {
  50 
  51     private static boolean isMainMethodDescriptorRecorded;
  52     private static final String MAIN_METHOD_DESCRIPTOR = "([Ljava/lang/String;)V";
  53     private static final String MAIN_METHOD_NAME = "main";
  54 
  55     public static void main(String[] args) throws Throwable {
  56         Recording recording = new Recording();
  57         recording.enable(MyEvent.class).withStackTrace();
  58         recording.start();
  59 
  60         MyEvent event = new MyEvent();
  61         event.begin();
  62         event.end();
  63         event.commit();
  64         recording.stop();
  65 
  66         List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
  67         assertEquals(1, recordedEvents.size(), "Expected one event");
  68         RecordedEvent recordedEvent = recordedEvents.get(0);
  69 
  70         RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
  71         List<RecordedFrame> frames = stacktrace.getFrames();
  72         assertFalse(frames.isEmpty(), "Stacktrace frames was empty");
  73         for (RecordedFrame frame : frames) {
  74             analyzeRecordedMethodDescriptor(frame.getMethod());
  75         }
  76 
  77         assertTrue(isMainMethodDescriptorRecorded, "main() method descriptor has never been recorded");
  78     }
  79 
  80     private static void analyzeRecordedMethodDescriptor(RecordedMethod method) {
  81 
  82         String descr = method.getDescriptor();
  83         assertNotNull(descr, "Method descriptor is null");
  84         String name = method.getName();
  85         assertNotNull(name, "Method name is null");
  86 
  87         if (name.equals(MAIN_METHOD_NAME) && descr.equals(MAIN_METHOD_DESCRIPTOR)) {
  88             assertFalse(isMainMethodDescriptorRecorded, "main() method descriptor already recorded");
  89             isMainMethodDescriptorRecorded = true;
  90         }
  91     }
  92 
  93     public static class MyEvent extends Event {
  94     }
  95 
  96 }