1 /*
   2  * Copyright (c) 1997, 2010, 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 "memory/allocation.hpp"
  29 #include "prims/jvm.h"
  30 #include "runtime/dtraceJSDT.hpp"
  31 #include "runtime/jniHandles.hpp"
  32 #include "runtime/os.hpp"
  33 #include "utilities/exceptions.hpp"
  34 #include "utilities/globalDefinitions.hpp"
  35 #include "utilities/utf8.hpp"
  36 
  37 #ifdef HAVE_DTRACE_H
  38 
  39 jlong DTraceJSDT::activate(
  40     jint version, jstring module_name, jint providers_count,
  41     JVM_DTraceProvider* providers, TRAPS) {
  42 
  43   size_t count = 0;
  44   RegisteredProbes* probes = NULL;
  45 
  46   if (!is_supported()) {
  47     return 0;
  48   }
  49 
  50   assert(module_name != NULL, "valid module name");
  51   assert(providers != NULL, "valid provider array");
  52 
  53   for (int i = 0; i < providers_count; ++i) {
  54     count += providers[i].probe_count;
  55   }
  56   probes = new RegisteredProbes(count);
  57   count = 0;
  58 
  59   for (int i = 0; i < providers_count; ++i) {
  60     assert(providers[i].name != NULL, "valid provider name");
  61     assert(providers[i].probe_count == 0 || providers[i].probes != NULL,
  62            "valid probe count");
  63     for (int j = 0; j < providers[i].probe_count; ++j) {
  64       JVM_DTraceProbe* probe = &(providers[i].probes[j]);
  65       assert(probe != NULL, "valid probe");
  66       assert(probe->method != NULL, "valid method");
  67       assert(probe->name != NULL, "valid probe name");
  68       assert(probe->function != NULL, "valid probe function spec");
  69       methodHandle h_method =
  70         methodHandle(THREAD, JNIHandles::resolve_jmethod_id(probe->method));
  71       nmethod* nm = AdapterHandlerLibrary::create_dtrace_nmethod(h_method);
  72       if (nm == NULL) {
  73         delete probes;
  74         THROW_MSG_0(vmSymbols::java_lang_RuntimeException(),
  75           "Unable to register DTrace probes (CodeCache: no room for DTrace nmethods).");
  76       }
  77       h_method()->set_not_compilable();
  78       h_method()->set_code(h_method, nm);
  79       probes->nmethod_at_put(count++, nm);
  80     }
  81   }
  82 
  83   int handle = pd_activate((void*)probes,
  84     module_name, providers_count, providers);
  85   if (handle <= 0) {
  86     delete probes;
  87     THROW_MSG_0(vmSymbols::java_lang_RuntimeException(),
  88       "Unable to register DTrace probes (internal error).");
  89   }
  90   probes->set_helper_handle(handle);
  91   return RegisteredProbes::toOpaqueProbes(probes);
  92 }
  93 
  94 jboolean DTraceJSDT::is_probe_enabled(jmethodID method) {
  95   methodOop m = JNIHandles::resolve_jmethod_id(method);
  96   return nativeInstruction_at(m->code()->trap_address())->is_dtrace_trap();
  97 }
  98 
  99 void DTraceJSDT::dispose(OpaqueProbes probes) {
 100   RegisteredProbes* p = RegisteredProbes::toRegisteredProbes(probes);
 101   if (probes != -1 && p != NULL) {
 102     pd_dispose(p->helper_handle());
 103     delete p;
 104   }
 105 }
 106 
 107 jboolean DTraceJSDT::is_supported() {
 108   return pd_is_supported();
 109 }
 110 
 111 #else // HAVE_DTRACE_H
 112 
 113 jlong DTraceJSDT::activate(
 114     jint version, jstring module_name, jint providers_count,
 115     JVM_DTraceProvider* providers, TRAPS) {
 116   return 0;
 117 }
 118 
 119 jboolean DTraceJSDT::is_probe_enabled(jmethodID method) {
 120   return false;
 121 }
 122 
 123 void DTraceJSDT::dispose(OpaqueProbes probes) {
 124   return;
 125 }
 126 
 127 jboolean DTraceJSDT::is_supported() {
 128   return false;
 129 }
 130 
 131 #endif // ndef HAVE_DTRACE_H