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 
  26 package jdk.jfr.internal;
  27 
  28 import java.io.IOException;
  29 import java.security.AccessControlContext;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedActionException;
  32 import java.security.PrivilegedExceptionAction;
  33 
  34 import jdk.jfr.RecordingState;
  35 
  36 /**
  37  * Class responsible for dumping recordings on exit
  38  *
  39  */
  40 final class ShutdownHook implements Runnable {
  41     private final PlatformRecorder recorder;
  42     Object tlabDummyObject;
  43 
  44     ShutdownHook(PlatformRecorder recorder) {
  45         this.recorder = recorder;
  46     }
  47 
  48     @Override
  49     public void run() {
  50         // this allocation is done in order to fetch a new TLAB before
  51         // starting any "real" operations. In low memory situations,
  52         // we would like to take an OOM as early as possible.
  53         tlabDummyObject = new Object();
  54 
  55         for (PlatformRecording recording : recorder.getRecordings()) {
  56             if (recording.getDumpOnExit() && recording.getState() == RecordingState.RUNNING) {
  57                 dump(recording);
  58             }
  59         }
  60         recorder.destroy();
  61     }
  62 
  63     private void dump(PlatformRecording recording) {
  64         try {
  65             WriteableUserPath dest = recording.getDestination();
  66             if (dest == null) {
  67                 dest = makeDumpOnExitPath(recording);
  68                 recording.setDestination(dest);
  69             }
  70             if (dest != null) {
  71                 recording.stop("Dump on exit");
  72             }
  73         } catch (Exception e) {
  74             Logger.log(LogTag.JFR, LogLevel.DEBUG, () -> "Could not dump recording " + recording.getName() + " on exit.");
  75         }
  76     }
  77 
  78     private WriteableUserPath makeDumpOnExitPath(PlatformRecording recording) {
  79         try {
  80             String name = Utils.makeFilename(recording.getRecording());
  81             AccessControlContext acc = recording.getNoDestinationDumpOnExitAccessControlContext();
  82             return AccessController.doPrivileged(new PrivilegedExceptionAction<WriteableUserPath>() {
  83                 @Override
  84                 public WriteableUserPath run() throws Exception {
  85                     return new WriteableUserPath(recording.getDumpOnExitDirectory().toPath().resolve(name));
  86                 }
  87             }, acc);
  88         } catch (PrivilegedActionException e) {
  89             Throwable t = e.getCause();
  90             if (t instanceof SecurityException) {
  91                 Logger.log(LogTag.JFR, LogLevel.WARN, "Not allowed to create dump path for recording " + recording.getId() + " on exit.");
  92             }
  93             if (t instanceof IOException) {
  94                 Logger.log(LogTag.JFR, LogLevel.WARN, "Could not dump " + recording.getId() + " on exit.");
  95             }
  96             return null;
  97         }
  98     }
  99 
 100     static final class ExceptionHandler implements Thread.UncaughtExceptionHandler {
 101         public void uncaughtException(Thread t, Throwable e) {
 102             JVM.getJVM().uncaughtException(t, e);
 103         }
 104     }
 105 }