1 /*
   2  * Copyright (c) 1997, 2020, 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 #ifndef SHARE_RUNTIME_VMOPERATIONS_HPP
  26 #define SHARE_RUNTIME_VMOPERATIONS_HPP
  27 
  28 #include "classfile/javaClasses.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "oops/oop.hpp"
  31 #include "runtime/thread.hpp"
  32 #include "runtime/threadSMR.hpp"
  33 #include "code/codeCache.hpp"
  34 
  35 // The following classes are used for operations
  36 // initiated by a Java thread but that must
  37 // take place in the VMThread.
  38 
  39 #define VM_OP_ENUM(type)   VMOp_##type,
  40 
  41 // Note: When new VM_XXX comes up, add 'XXX' to the template table.
  42 #define VM_OPS_DO(template)                       \
  43   template(None)                                  \
  44   template(Cleanup)                               \
  45   template(ThreadDump)                            \
  46   template(PrintThreads)                          \
  47   template(FindDeadlocks)                         \
  48   template(ClearICs)                              \
  49   template(ForceSafepoint)                        \
  50   template(ForceAsyncSafepoint)                   \
  51   template(DeoptimizeFrame)                       \
  52   template(DeoptimizeAll)                         \
  53   template(ZombieAll)                             \
  54   template(Verify)                                \
  55   template(PrintJNI)                              \
  56   template(HeapDumper)                            \
  57   template(DeoptimizeTheWorld)                    \
  58   template(CollectForMetadataAllocation)          \
  59   template(GC_HeapInspection)                     \
  60   template(GenCollectFull)                        \
  61   template(GenCollectFullConcurrent)              \
  62   template(GenCollectForAllocation)               \
  63   template(ParallelGCFailedAllocation)            \
  64   template(ParallelGCSystemGC)                    \
  65   template(G1CollectForAllocation)                \
  66   template(G1CollectFull)                         \
  67   template(G1Concurrent)                          \
  68   template(G1TryInitiateConcMark)                 \
  69   template(ZMarkStart)                            \
  70   template(ZMarkEnd)                              \
  71   template(ZRelocateStart)                        \
  72   template(ZVerify)                               \
  73   template(HandshakeOneThread)                    \
  74   template(HandshakeAllThreads)                   \
  75   template(HandshakeFallback)                     \
  76   template(EnableBiasedLocking)                   \
  77   template(BulkRevokeBias)                        \
  78   template(PopulateDumpSharedSpace)               \
  79   template(JNIFunctionTableCopier)                \
  80   template(RedefineClasses)                       \
  81   template(UpdateForPopTopFrame)                  \
  82   template(SetFramePop)                           \
  83   template(GetObjectMonitorUsage)                 \
  84   template(GetStackTrace)                         \
  85   template(GetMultipleStackTraces)                \
  86   template(GetAllStackTraces)                     \
  87   template(GetThreadListStackTraces)              \
  88   template(GetFrameCount)                         \
  89   template(GetFrameLocation)                      \
  90   template(ChangeBreakpoints)                     \
  91   template(GetOrSetLocal)                         \
  92   template(GetCurrentLocation)                    \
  93   template(ChangeSingleStep)                      \
  94   template(HeapWalkOperation)                     \
  95   template(HeapIterateOperation)                  \
  96   template(ReportJavaOutOfMemory)                 \
  97   template(JFRCheckpoint)                         \
  98   template(ShenandoahFullGC)                      \
  99   template(ShenandoahInitMark)                    \
 100   template(ShenandoahFinalMarkStartEvac)          \
 101   template(ShenandoahInitUpdateRefs)              \
 102   template(ShenandoahFinalUpdateRefs)             \
 103   template(ShenandoahDegeneratedGC)               \
 104   template(Exit)                                  \
 105   template(LinuxDllLoad)                          \
 106   template(RotateGCLog)                           \
 107   template(WhiteBoxOperation)                     \
 108   template(JVMCIResizeCounters)                   \
 109   template(ClassLoaderStatsOperation)             \
 110   template(ClassLoaderHierarchyOperation)         \
 111   template(DumpHashtable)                         \
 112   template(DumpTouchedMethods)                    \
 113   template(MarkActiveNMethods)                    \
 114   template(PrintCompileQueue)                     \
 115   template(PrintClassHierarchy)                   \
 116   template(ThreadSuspend)                         \
 117   template(ThreadsSuspendJVMTI)                   \
 118   template(ICBufferFull)                          \
 119   template(ScavengeMonitors)                      \
 120   template(PrintMetadata)                         \
 121   template(GTestExecuteAtSafepoint)               \
 122   template(JFROldObject)                          \
 123 
 124 class VM_Operation : public StackObj {
 125  public:
 126   enum VMOp_Type {
 127     VM_OPS_DO(VM_OP_ENUM)
 128     VMOp_Terminating
 129   };
 130 
 131  private:
 132   Thread*         _calling_thread;
 133   VM_Operation*   _next;
 134   VM_Operation*   _prev;
 135 
 136   // The VM operation name array
 137   static const char* _names[];
 138 
 139  public:
 140   VM_Operation() : _calling_thread(NULL), _next(NULL), _prev(NULL) {}
 141 
 142   // VM operation support (used by VM thread)
 143   Thread* calling_thread() const                 { return _calling_thread; }
 144   void set_calling_thread(Thread* thread);
 145 
 146   // Called by VM thread - does in turn invoke doit(). Do not override this
 147   void evaluate();
 148 
 149   // evaluate() is called by the VMThread and in turn calls doit().
 150   // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread,
 151   // doit_prologue() is called in that thread before transferring control to
 152   // the VMThread.
 153   // If doit_prologue() returns true the VM operation will proceed, and
 154   // doit_epilogue() will be called by the JavaThread once the VM operation
 155   // completes. If doit_prologue() returns false the VM operation is cancelled.
 156   virtual void doit()                            = 0;
 157   virtual bool doit_prologue()                   { return true; };
 158   virtual void doit_epilogue()                   {};
 159 
 160   // Linking
 161   VM_Operation *next() const                     { return _next; }
 162   VM_Operation *prev() const                     { return _prev; }
 163   void set_next(VM_Operation *next)              { _next = next; }
 164   void set_prev(VM_Operation *prev)              { _prev = prev; }
 165 
 166   // Configuration. Override these appropriately in subclasses.
 167   virtual VMOp_Type type() const = 0;
 168   virtual bool allow_nested_vm_operations() const { return false; }
 169 
 170   // An operation can either be done inside a safepoint
 171   // or concurrently with Java threads running.
 172   virtual bool evaluate_at_safepoint() const { return true; }
 173 
 174   // Debugging
 175   virtual void print_on_error(outputStream* st) const;
 176   virtual const char* name() const  { return _names[type()]; }
 177   static const char* name(int type) {
 178     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
 179     return _names[type];
 180   }
 181 #ifndef PRODUCT
 182   void print_on(outputStream* st) const { print_on_error(st); }
 183 #endif
 184 };
 185 
 186 class VM_None: public VM_Operation {
 187   const char* _reason;
 188  public:
 189   VM_None(const char* reason) : _reason(reason) {}
 190   const char* name() const { return _reason; }
 191   VMOp_Type type() const { return VMOp_None; }
 192   void doit() {};
 193 };
 194 
 195 class VM_Cleanup: public VM_Operation {
 196  public:
 197   VMOp_Type type() const { return VMOp_Cleanup; }
 198   void doit() {};
 199 };
 200 
 201 class VM_ClearICs: public VM_Operation {
 202  private:
 203   bool _preserve_static_stubs;
 204  public:
 205   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 206   void doit();
 207   VMOp_Type type() const { return VMOp_ClearICs; }
 208 };
 209 
 210 // empty vm op, evaluated just to force a safepoint
 211 class VM_ForceSafepoint: public VM_Operation {
 212  public:
 213   void doit()         {}
 214   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 215 };
 216 
 217 // empty vm op, when forcing a safepoint to suspend a thread
 218 class VM_ThreadSuspend: public VM_ForceSafepoint {
 219  public:
 220   VMOp_Type type() const { return VMOp_ThreadSuspend; }
 221 };
 222 
 223 // empty vm op, when forcing a safepoint to suspend threads from jvmti
 224 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint {
 225  public:
 226   VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; }
 227 };
 228 
 229 // empty vm op, when forcing a safepoint due to inline cache buffers being full
 230 class VM_ICBufferFull: public VM_ForceSafepoint {
 231  public:
 232   VMOp_Type type() const { return VMOp_ICBufferFull; }
 233 };
 234 
 235 // Base class for invoking parts of a gtest in a safepoint.
 236 // Derived classes provide the doit method.
 237 // Typically also need to transition the gtest thread from native to VM.
 238 class VM_GTestExecuteAtSafepoint: public VM_Operation {
 239  public:
 240   VMOp_Type type() const                         { return VMOp_GTestExecuteAtSafepoint; }
 241 
 242  protected:
 243   VM_GTestExecuteAtSafepoint() {}
 244 };
 245 
 246 // Deopt helper that can deoptimize frames in threads other than the
 247 // current thread.  Only used through Deoptimization::deoptimize_frame.
 248 class VM_DeoptimizeFrame: public VM_Operation {
 249   friend class Deoptimization;
 250 
 251  private:
 252   JavaThread* _thread;
 253   intptr_t*   _id;
 254   int _reason;
 255   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
 256 
 257  public:
 258   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
 259   void doit();
 260   bool allow_nested_vm_operations() const        { return true;  }
 261 };
 262 
 263 #ifndef PRODUCT
 264 class VM_DeoptimizeAll: public VM_Operation {
 265  private:
 266   Klass* _dependee;
 267  public:
 268   VM_DeoptimizeAll() {}
 269   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
 270   void doit();
 271   bool allow_nested_vm_operations() const        { return true; }
 272 };
 273 
 274 
 275 class VM_ZombieAll: public VM_Operation {
 276  public:
 277   VM_ZombieAll() {}
 278   VMOp_Type type() const                         { return VMOp_ZombieAll; }
 279   void doit();
 280   bool allow_nested_vm_operations() const        { return true; }
 281 };
 282 #endif // PRODUCT
 283 
 284 class VM_Verify: public VM_Operation {
 285  public:
 286   VMOp_Type type() const { return VMOp_Verify; }
 287   void doit();
 288 };
 289 
 290 
 291 class VM_PrintThreads: public VM_Operation {
 292  private:
 293   outputStream* _out;
 294   bool _print_concurrent_locks;
 295   bool _print_extended_info;
 296  public:
 297   VM_PrintThreads()
 298     : _out(tty), _print_concurrent_locks(PrintConcurrentLocks), _print_extended_info(false)
 299   {}
 300   VM_PrintThreads(outputStream* out, bool print_concurrent_locks, bool print_extended_info)
 301     : _out(out), _print_concurrent_locks(print_concurrent_locks), _print_extended_info(print_extended_info)
 302   {}
 303   VMOp_Type type() const {
 304     return VMOp_PrintThreads;
 305   }
 306   void doit();
 307   bool doit_prologue();
 308   void doit_epilogue();
 309 };
 310 
 311 class VM_PrintJNI: public VM_Operation {
 312  private:
 313   outputStream* _out;
 314  public:
 315   VM_PrintJNI()                         { _out = tty; }
 316   VM_PrintJNI(outputStream* out)        { _out = out; }
 317   VMOp_Type type() const                { return VMOp_PrintJNI; }
 318   void doit();
 319 };
 320 
 321 class VM_PrintMetadata : public VM_Operation {
 322  private:
 323   outputStream* const _out;
 324   const size_t        _scale;
 325   const int           _flags;
 326 
 327  public:
 328   VM_PrintMetadata(outputStream* out, size_t scale, int flags)
 329     : _out(out), _scale(scale), _flags(flags)
 330   {};
 331 
 332   VMOp_Type type() const  { return VMOp_PrintMetadata; }
 333   void doit();
 334 };
 335 
 336 class DeadlockCycle;
 337 class VM_FindDeadlocks: public VM_Operation {
 338  private:
 339   bool              _concurrent_locks;
 340   DeadlockCycle*    _deadlocks;
 341   outputStream*     _out;
 342   ThreadsListSetter _setter;  // Helper to set hazard ptr in the originating thread
 343                               // which protects the JavaThreads in _deadlocks.
 344 
 345  public:
 346   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _deadlocks(NULL), _out(NULL), _setter() {};
 347   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _deadlocks(NULL), _out(st) {};
 348   ~VM_FindDeadlocks();
 349 
 350   DeadlockCycle* result()      { return _deadlocks; };
 351   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 352   void doit();
 353 };
 354 
 355 class ThreadDumpResult;
 356 class ThreadSnapshot;
 357 class ThreadConcurrentLocks;
 358 
 359 class VM_ThreadDump : public VM_Operation {
 360  private:
 361   ThreadDumpResult*              _result;
 362   int                            _num_threads;
 363   GrowableArray<instanceHandle>* _threads;
 364   int                            _max_depth;
 365   bool                           _with_locked_monitors;
 366   bool                           _with_locked_synchronizers;
 367 
 368   void snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 369 
 370  public:
 371   VM_ThreadDump(ThreadDumpResult* result,
 372                 int max_depth,  // -1 indicates entire stack
 373                 bool with_locked_monitors,
 374                 bool with_locked_synchronizers);
 375 
 376   VM_ThreadDump(ThreadDumpResult* result,
 377                 GrowableArray<instanceHandle>* threads,
 378                 int num_threads, // -1 indicates entire stack
 379                 int max_depth,
 380                 bool with_locked_monitors,
 381                 bool with_locked_synchronizers);
 382 
 383   VMOp_Type type() const { return VMOp_ThreadDump; }
 384   void doit();
 385   bool doit_prologue();
 386   void doit_epilogue();
 387 };
 388 
 389 
 390 class VM_Exit: public VM_Operation {
 391  private:
 392   int  _exit_code;
 393   static volatile bool _vm_exited;
 394   static Thread * volatile _shutdown_thread;
 395   static void wait_if_vm_exited();
 396  public:
 397   VM_Exit(int exit_code) {
 398     _exit_code = exit_code;
 399   }
 400   static int wait_for_threads_in_native_to_block();
 401   static int set_vm_exited();
 402   static bool vm_exited()                      { return _vm_exited; }
 403   static Thread * shutdown_thread()            { return _shutdown_thread; }
 404   static void block_if_vm_exited() {
 405     if (_vm_exited) {
 406       wait_if_vm_exited();
 407     }
 408   }
 409   VMOp_Type type() const { return VMOp_Exit; }
 410   bool doit_prologue();
 411   void doit();
 412 };
 413 
 414 class VM_PrintCompileQueue: public VM_Operation {
 415  private:
 416   outputStream* _out;
 417 
 418  public:
 419   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
 420   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
 421   void doit();
 422 };
 423 
 424 #if INCLUDE_SERVICES
 425 class VM_PrintClassHierarchy: public VM_Operation {
 426  private:
 427   outputStream* _out;
 428   bool _print_interfaces;
 429   bool _print_subclasses;
 430   char* _classname;
 431 
 432  public:
 433   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 434     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 435     _classname(classname) {}
 436   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 437   void doit();
 438 };
 439 #endif // INCLUDE_SERVICES
 440 
 441 #endif // SHARE_RUNTIME_VMOPERATIONS_HPP