1 /*
   2  * Copyright (c) 2013, 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.event.runtime;
  27 
  28 import java.time.Duration;
  29 import java.util.List;
  30 
  31 import jdk.jfr.Recording;
  32 import jdk.jfr.consumer.RecordedClass;
  33 import jdk.jfr.consumer.RecordedEvent;
  34 import jdk.jfr.consumer.RecordedFrame;
  35 import jdk.jfr.consumer.RecordedStackTrace;
  36 import jdk.jfr.consumer.RecordedThread;
  37 import jdk.test.lib.Asserts;
  38 import jdk.test.lib.jfr.EventNames;
  39 import jdk.test.lib.jfr.Events;
  40 
  41 /**
  42  * @test
  43  * @key jfr
  44  *
  45  * @library /lib /
  46  * @run main/othervm jdk.jfr.event.runtime.TestExceptionEvents
  47  */
  48 public class TestExceptionEvents {
  49 
  50     private static final String EXCEPTION_EVENT_PATH = EventNames.JavaExceptionThrow;
  51     private static final String ERROR_EVENT_PATH  = EventNames.JavaErrorThrow;
  52     private static final String EXCEPTION_STATISTICS_PATH = EventNames.ExceptionStatistics;
  53 
  54     private static final String EXCEPTION_MESSAGE = "exceptiontest";
  55     private static final String ERROR_MESSAGE = "errortest";
  56     private static final String THROWABLE_MESSAGE = "throwabletest";
  57 
  58     private static final int ITERATIONS = 10;
  59 
  60     private static int exceptionCount = 0;
  61 
  62     public static void main(String[] args) throws Throwable {
  63         Recording recording = createRecording();
  64 
  65         List<RecordedEvent> events = Events.fromRecording(recording);
  66         checkStatisticsEvent(events, exceptionCount);
  67         checkThrowableEvents(events, EXCEPTION_EVENT_PATH, ITERATIONS, MyException.class, EXCEPTION_MESSAGE);
  68         checkThrowableEvents(events, ERROR_EVENT_PATH, ITERATIONS, MyError.class, ERROR_MESSAGE);
  69         checkThrowableEvents(events, EXCEPTION_EVENT_PATH, ITERATIONS, MyThrowable.class, THROWABLE_MESSAGE);
  70         checkExceptionStackTrace();
  71     }
  72 
  73     private static void checkExceptionStackTrace() throws Exception {
  74         @SuppressWarnings("serial")
  75         class TestError extends Error {
  76         }
  77         @SuppressWarnings("serial")
  78         class TestException extends Exception {
  79         }
  80 
  81         try (Recording r = new Recording()) {
  82             r.enable(EventNames.JavaErrorThrow).withStackTrace();
  83             r.enable(EventNames.JavaExceptionThrow).withStackTrace();
  84             r.start();
  85             try {
  86                 throw new TestError();
  87             } catch (Error e) {
  88                 System.out.println(e.getClass() + " thrown!");
  89             }
  90             try {
  91                 throw new TestException();
  92             } catch (Exception e) {
  93                 System.out.println(e.getClass() + " thrown!");
  94             }
  95             try {
  96                 throw new Exception();
  97             } catch (Exception e) {
  98                 System.out.println(e.getClass() + " thrown!");
  99             }
 100             r.stop();
 101             List<RecordedEvent> events = Events.fromRecording(r);
 102             Events.hasEvents(events);
 103             for (RecordedEvent e : events) {
 104                 RecordedStackTrace rs = e.getStackTrace();
 105                 RecordedClass rc = e.getValue("thrownClass");
 106                 List<RecordedFrame> frames = rs.getFrames();
 107                 RecordedFrame topFrame = frames.get(0);
 108                 System.out.println(rc.getName() + " Top frame: " + topFrame.getMethod().getName());
 109                 if (!topFrame.getMethod().getName().equals("<init>")) {
 110                     throw new Exception("Expected name of top frame to be <init>");
 111                 }
 112             }
 113         }
 114     }
 115 
 116     private static Recording createRecording() throws Exception {
 117         Recording recording = new Recording();
 118         recording.enable(EXCEPTION_STATISTICS_PATH);
 119         recording.enable(EXCEPTION_EVENT_PATH).withThreshold(Duration.ofMillis(0));
 120         recording.enable(ERROR_EVENT_PATH).withThreshold(Duration.ofMillis(0));
 121         recording.start();
 122 
 123         for (int i = 0; i < ITERATIONS; i++) {
 124             try {
 125                 throw new MyException(EXCEPTION_MESSAGE);
 126             } catch (MyException e) {
 127                 exceptionCount++;
 128             }
 129             try {
 130                 throw new MyError(ERROR_MESSAGE);
 131             } catch (MyError e) {
 132                 exceptionCount++;
 133             }
 134             try {
 135                 throw new MyThrowable(THROWABLE_MESSAGE);
 136             } catch (MyThrowable t) {
 137                 exceptionCount++;
 138             }
 139         }
 140         recording.stop();
 141         return recording;
 142     }
 143 
 144 
 145     private static void checkStatisticsEvent(List<RecordedEvent> events, long minCount) throws Exception {
 146         // Events are not guaranteed to be in chronological order, take highest value.
 147         long count = -1;
 148         for(RecordedEvent event : events) {
 149             if (Events.isEventType(event, EXCEPTION_STATISTICS_PATH)) {
 150                 System.out.println("Event: " + event);
 151                 count = Math.max(count, Events.assertField(event, "throwables").getValue());
 152                 System.out.println("count=" +  count);
 153             }
 154         }
 155         Asserts.assertTrue(count != -1, "No events of type " + EXCEPTION_STATISTICS_PATH);
 156         Asserts.assertGreaterThanOrEqual(count, minCount, "Too few exception count in statistics event");
 157     }
 158 
 159     private static void checkThrowableEvents(List<RecordedEvent> events, String eventName,
 160         int excpectedEvents, Class<?> expectedClass, String expectedMessage) throws Exception {
 161         int count = 0;
 162         for(RecordedEvent event : events) {
 163             if (Events.isEventType(event, eventName)) {
 164                 String message = Events.assertField(event, "message").getValue();
 165                 if (expectedMessage.equals(message)) {
 166                     RecordedThread t = event.getThread();
 167                     String threadName = t.getJavaName();
 168                     if (threadName != null && threadName.equals(Thread.currentThread().getName())) {
 169                         RecordedClass jc = event.getValue("thrownClass");
 170                         if (jc.getName().equals(expectedClass.getName())) {
 171                             count++;
 172                         }
 173                     }
 174                 }
 175             }
 176         }
 177         Asserts.assertEquals(count, excpectedEvents, "Wrong event count for type " + eventName);
 178     }
 179 
 180     private static class MyException extends Exception {
 181         private static final long serialVersionUID = -2614309279743448910L;
 182         public MyException(String msg) {
 183             super(msg);
 184         }
 185     }
 186 
 187     private static class MyError extends Error {
 188         private static final long serialVersionUID = -8519872786387358196L;
 189         public MyError(String msg) {
 190             super(msg);
 191         }
 192     }
 193 
 194     private static class MyThrowable extends Throwable {
 195         private static final long serialVersionUID = -7929442863511070361L;
 196         public MyThrowable(String msg) {
 197             super(msg);
 198         }
 199     }
 200 }