1 /*
   2  * Copyright (c) 1997, 2012, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "code/codeBlob.hpp"
  28 #include "code/nativeInst.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "prims/jvm.h"
  31 #include "runtime/dtraceJSDT.hpp"
  32 #include "runtime/jniHandles.hpp"
  33 #include "runtime/os.hpp"
  34 #include "runtime/sharedRuntime.hpp"
  35 #include "utilities/exceptions.hpp"
  36 #include "utilities/globalDefinitions.hpp"
  37 #include "utilities/utf8.hpp"
  38 
  39 #ifdef HAVE_DTRACE_H
  40 
  41 jlong DTraceJSDT::activate(
  42     jint version, jstring module_name, jint providers_count,
  43     JVM_DTraceProvider* providers, TRAPS) {
  44 
  45   size_t count = 0;
  46   RegisteredProbes* probes = NULL;
  47 
  48   if (!is_supported()) {
  49     return 0;
  50   }
  51 
  52   assert(module_name != NULL, "valid module name");
  53   assert(providers != NULL, "valid provider array");
  54 
  55   for (int i = 0; i < providers_count; ++i) {
  56     count += providers[i].probe_count;
  57   }
  58   probes = new RegisteredProbes(count);
  59   count = 0;
  60 
  61   for (int i = 0; i < providers_count; ++i) {
  62     assert(providers[i].name != NULL, "valid provider name");
  63     assert(providers[i].probe_count == 0 || providers[i].probes != NULL,
  64            "valid probe count");
  65     for (int j = 0; j < providers[i].probe_count; ++j) {
  66       JVM_DTraceProbe* probe = &(providers[i].probes[j]);
  67       assert(probe != NULL, "valid probe");
  68       assert(probe->method != NULL, "valid method");
  69       assert(probe->name != NULL, "valid probe name");
  70       assert(probe->function != NULL, "valid probe function spec");
  71       methodHandle h_method =
  72         methodHandle(THREAD, Method::resolve_jmethod_id(probe->method));
  73       nmethod* nm = AdapterHandlerLibrary::create_dtrace_nmethod(h_method);
  74       if (nm == NULL) {
  75         delete probes;
  76         THROW_MSG_0(vmSymbols::java_lang_RuntimeException(),
  77           "Unable to register DTrace probes (CodeCache: no room for DTrace nmethods).");
  78       }
  79       h_method()->set_not_compilable();
  80       h_method()->set_code(h_method, nm);
  81       probes->nmethod_at_put(count++, nm);
  82     }
  83   }
  84 
  85   int handle = pd_activate((void*)probes,
  86     module_name, providers_count, providers);
  87   if (handle < 0) {
  88     delete probes;
  89     THROW_MSG_0(vmSymbols::java_lang_RuntimeException(),
  90       "Unable to register DTrace probes (internal error).");
  91   }
  92   probes->set_helper_handle(handle);
  93   return RegisteredProbes::toOpaqueProbes(probes);
  94 }
  95 
  96 jboolean DTraceJSDT::is_probe_enabled(jmethodID method) {
  97   Method* m = Method::resolve_jmethod_id(method);
  98   return nativeInstruction_at(m->code()->trap_address())->is_dtrace_trap();
  99 }
 100 
 101 void DTraceJSDT::dispose(OpaqueProbes probes) {
 102   RegisteredProbes* p = RegisteredProbes::toRegisteredProbes(probes);
 103   if (probes != -1 && p != NULL) {
 104     pd_dispose(p->helper_handle());
 105     delete p;
 106   }
 107 }
 108 
 109 jboolean DTraceJSDT::is_supported() {
 110   return pd_is_supported();
 111 }
 112 
 113 #else // HAVE_DTRACE_H
 114 
 115 jlong DTraceJSDT::activate(
 116     jint version, jstring module_name, jint providers_count,
 117     JVM_DTraceProvider* providers, TRAPS) {
 118   return 0;
 119 }
 120 
 121 jboolean DTraceJSDT::is_probe_enabled(jmethodID method) {
 122   return false;
 123 }
 124 
 125 void DTraceJSDT::dispose(OpaqueProbes probes) {
 126   return;
 127 }
 128 
 129 jboolean DTraceJSDT::is_supported() {
 130   return false;
 131 }
 132 
 133 #endif // ndef HAVE_DTRACE_H