1 /*
   2  * Copyright (c) 2005, 2006, 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 // The AttachListener thread services a queue of operations that are enqueued
  26 // by client tools. Each operation is identified by a name and has up to 3
  27 // arguments. The operation name is mapped to a function which performs the
  28 // operation. The function is called with an outputStream which is can use to
  29 // write any result data (for examples the properties command serializes
  30 // properties names and values to the output stream). When the function
  31 // complets the result value and any result data is returned to the client
  32 // tool.
  33 
  34 #ifndef SERVICES_KERNEL
  35 
  36 class AttachOperation;
  37 
  38 typedef jint (*AttachOperationFunction)(AttachOperation* op, outputStream* out);
  39 
  40 struct AttachOperationFunctionInfo {
  41   const char* name;
  42   AttachOperationFunction func;
  43 };
  44 #endif // SERVICES_KERNEL
  45 
  46 class AttachListener: AllStatic {
  47  public:
  48   static void init()  KERNEL_RETURN;
  49   static void abort() KERNEL_RETURN;
  50 
  51   // invoke to perform clean-up tasks when all clients detach
  52   static void detachall() KERNEL_RETURN;
  53 
  54   // indicates if the Attach Listener needs to be created at startup
  55   static bool init_at_startup() KERNEL_RETURN_(return false;);
  56 
  57   // indicates if we have a trigger to start the Attach Listener
  58   static bool is_init_trigger() KERNEL_RETURN_(return false;);
  59 
  60 #ifdef SERVICES_KERNEL
  61   static bool is_attach_supported()             { return false; }
  62 #else // SERVICES_KERNEL
  63  private:
  64   static volatile bool _initialized;
  65 
  66  public:
  67   static bool is_initialized()                  { return _initialized; }
  68   static void set_initialized()                 { _initialized = true; }
  69 
  70   // indicates if this VM supports attach-on-demand
  71   static bool is_attach_supported()             { return !DisableAttachMechanism; }
  72 
  73   // platform specific initialization
  74   static int pd_init();
  75 
  76   // platform specific operation
  77   static AttachOperationFunctionInfo* pd_find_operation(const char* name);
  78 
  79   // platform specific flag change
  80   static jint pd_set_flag(AttachOperation* op, outputStream* out);
  81 
  82   // platform specific detachall
  83   static void pd_detachall();
  84 
  85   // platform specific data dump
  86   static void pd_data_dump();
  87 
  88   // dequeue the next operation
  89   static AttachOperation* dequeue();
  90 #endif // SERVICES_KERNEL
  91 };
  92 
  93 #ifndef SERVICES_KERNEL
  94 class AttachOperation: public CHeapObj {
  95  public:
  96   enum {
  97     name_length_max = 16,       // maximum length of  name
  98     arg_length_max = 1024,      // maximum length of argument
  99     arg_count_max = 3           // maximum number of arguments
 100   };
 101 
 102   // name of special operation that can be enqueued when all
 103   // clients detach
 104   static char* detachall_operation_name() { return (char*)"detachall"; }
 105 
 106  private:
 107   char _name[name_length_max+1];
 108   char _arg[arg_count_max][arg_length_max+1];
 109 
 110  public:
 111   const char* name() const                      { return _name; }
 112 
 113   // set the operation name
 114   void set_name(char* name) {
 115     assert(strlen(name) <= name_length_max, "exceeds maximum name length");
 116     strcpy(_name, name);
 117   }
 118 
 119   // get an argument value
 120   const char* arg(int i) const {
 121     assert(i>=0 && i<arg_count_max, "invalid argument index");
 122     return _arg[i];
 123   }
 124 
 125   // set an argument value
 126   void set_arg(int i, char* arg) {
 127     assert(i>=0 && i<arg_count_max, "invalid argument index");
 128     if (arg == NULL) {
 129       _arg[i][0] = '\0';
 130     } else {
 131       assert(strlen(arg) <= arg_length_max, "exceeds maximum argument length");
 132       strcpy(_arg[i], arg);
 133     }
 134   }
 135 
 136   // create an operation of a given name
 137   AttachOperation(char* name) {
 138     set_name(name);
 139     for (int i=0; i<arg_count_max; i++) {
 140       set_arg(i, NULL);
 141     }
 142   }
 143 
 144   // complete operation by sending result code and any result data to the client
 145   virtual void complete(jint result, bufferedStream* result_stream) = 0;
 146 };
 147 #endif // SERVICES_KERNEL