1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHASSERTS_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHASSERTS_HPP
  26 
  27 #include "memory/iterator.hpp"
  28 #include "runtime/mutex.hpp"
  29 #include "utilities/formatBuffer.hpp"
  30 
  31 typedef FormatBuffer<8192> ShenandoahMessageBuffer;
  32 
  33 class ShenandoahAsserts {
  34 public:
  35   enum SafeLevel {
  36     _safe_unknown,
  37     _safe_oop,
  38     _safe_oop_fwd,
  39     _safe_all
  40   };
  41 
  42   static void print_obj(ShenandoahMessageBuffer &msg, oop obj);
  43 
  44   static void print_non_obj(ShenandoahMessageBuffer &msg, void *loc);
  45 
  46   static void print_obj_safe(ShenandoahMessageBuffer &msg, void *loc);
  47 
  48   static void print_failure(SafeLevel level, oop obj, void *interior_loc, oop loc,
  49                             const char *phase, const char *label,
  50                             const char *file, int line);
  51 
  52   static void print_rp_failure(const char *label, BoolObjectClosure* actual,
  53                                const char *file, int line);
  54 
  55   static void assert_in_heap(void* interior_loc, oop obj, const char* file, int line);
  56   static void assert_in_correct_region(void* interior_loc, oop obj, const char* file, int line);
  57 
  58   static void assert_correct(void* interior_loc, oop obj, const char* file, int line);
  59   static void assert_forwarded(void* interior_loc, oop obj, const char* file, int line);
  60   static void assert_not_forwarded(void* interior_loc, oop obj, const char* file, int line);
  61   static void assert_marked(void* interior_loc, oop obj, const char* file, int line);
  62   static void assert_in_cset(void* interior_loc, oop obj, const char* file, int line);
  63   static void assert_not_in_cset(void* interior_loc, oop obj, const char* file, int line);
  64   static void assert_not_in_cset_loc(void* interior_loc, const char* file, int line);
  65 
  66   static void assert_rp_isalive_not_installed(const char *file, int line);
  67   static void assert_rp_isalive_installed(const char *file, int line);
  68 
  69   static void assert_locked_or_shenandoah_safepoint(const Monitor* lock, const char*file, int line);
  70 
  71   static void assert_heaplocked(const char* file, int line);
  72   static void assert_not_heaplocked(const char* file, int line);
  73   static void assert_heaplocked_or_safepoint(const char* file, int line);
  74 
  75 #ifdef ASSERT
  76 #define shenandoah_assert_in_heap(interior_loc, obj) \
  77                     ShenandoahAsserts::assert_in_heap(interior_loc, obj, __FILE__, __LINE__)
  78 #define shenandoah_assert_in_correct_region(interior_loc, obj) \
  79                     ShenandoahAsserts::assert_in_correct_region(interior_loc, obj, __FILE__, __LINE__)
  80 
  81 #define shenandoah_assert_correct_if(interior_loc, obj, condition) \
  82   if (condition)    ShenandoahAsserts::assert_correct(interior_loc, obj, __FILE__, __LINE__)
  83 #define shenandoah_assert_correct_except(interior_loc, obj, exception) \
  84   if (!(exception)) ShenandoahAsserts::assert_correct(interior_loc, obj, __FILE__, __LINE__)
  85 #define shenandoah_assert_correct(interior_loc, obj) \
  86                     ShenandoahAsserts::assert_correct(interior_loc, obj, __FILE__, __LINE__)
  87 
  88 #define shenandoah_assert_forwarded_if(interior_loc, obj, condition) \
  89   if (condition)    ShenandoahAsserts::assert_forwarded(interior_loc, obj, __FILE__, __LINE__)
  90 #define shenandoah_assert_forwarded_except(interior_loc, obj, exception) \
  91   if (!(exception)) ShenandoahAsserts::assert_forwarded(interior_loc, obj, __FILE__, __LINE__)
  92 #define shenandoah_assert_forwarded(interior_loc, obj) \
  93                     ShenandoahAsserts::assert_forwarded(interior_loc, obj, __FILE__, __LINE__)
  94 
  95 #define shenandoah_assert_not_forwarded_if(interior_loc, obj, condition) \
  96   if (condition)    ShenandoahAsserts::assert_not_forwarded(interior_loc, obj, __FILE__, __LINE__)
  97 #define shenandoah_assert_not_forwarded_except(interior_loc, obj, exception) \
  98   if (!(exception)) ShenandoahAsserts::assert_not_forwarded(interior_loc, obj, __FILE__, __LINE__)
  99 #define shenandoah_assert_not_forwarded(interior_loc, obj) \
 100                     ShenandoahAsserts::assert_not_forwarded(interior_loc, obj, __FILE__, __LINE__)
 101 
 102 #define shenandoah_assert_marked_if(interior_loc, obj, condition) \
 103   if (condition)    ShenandoahAsserts::assert_marked(interior_loc, obj, __FILE__, __LINE__)
 104 #define shenandoah_assert_marked_except(interior_loc, obj, exception) \
 105   if (!(exception)) ShenandoahAsserts::assert_marked(interior_loc, obj, __FILE__, __LINE__)
 106 #define shenandoah_assert_marked(interior_loc, obj) \
 107                     ShenandoahAsserts::assert_marked(interior_loc, obj, __FILE__, __LINE__)
 108 
 109 #define shenandoah_assert_in_cset_if(interior_loc, obj, condition) \
 110   if (condition)    ShenandoahAsserts::assert_in_cset(interior_loc, obj, __FILE__, __LINE__)
 111 #define shenandoah_assert_in_cset_except(interior_loc, obj, exception) \
 112   if (!(exception)) ShenandoahAsserts::assert_in_cset(interior_loc, obj, __FILE__, __LINE__)
 113 #define shenandoah_assert_in_cset(interior_loc, obj) \
 114                     ShenandoahAsserts::assert_in_cset(interior_loc, obj, __FILE__, __LINE__)
 115 
 116 #define shenandoah_assert_not_in_cset_if(interior_loc, obj, condition) \
 117   if (condition)    ShenandoahAsserts::assert_not_in_cset(interior_loc, obj, __FILE__, __LINE__)
 118 #define shenandoah_assert_not_in_cset_except(interior_loc, obj, exception) \
 119   if (!(exception)) ShenandoahAsserts::assert_not_in_cset(interior_loc, obj, __FILE__, __LINE__)
 120 #define shenandoah_assert_not_in_cset(interior_loc, obj) \
 121                     ShenandoahAsserts::assert_not_in_cset(interior_loc, obj, __FILE__, __LINE__)
 122 
 123 #define shenandoah_assert_not_in_cset_loc_if(interior_loc, condition) \
 124   if (condition)    ShenandoahAsserts::assert_not_in_cset_loc(interior_loc, __FILE__, __LINE__)
 125 #define shenandoah_assert_not_in_cset_loc_except(interior_loc, exception) \
 126   if (!(exception)) ShenandoahAsserts::assert_not_in_cset_loc(interior_loc, __FILE__, __LINE__)
 127 #define shenandoah_assert_not_in_cset_loc(interior_loc) \
 128                     ShenandoahAsserts::assert_not_in_cset_loc(interior_loc, __FILE__, __LINE__)
 129 
 130 #define shenandoah_assert_rp_isalive_installed() \
 131                     ShenandoahAsserts::assert_rp_isalive_installed(__FILE__, __LINE__)
 132 #define shenandoah_assert_rp_isalive_not_installed() \
 133                     ShenandoahAsserts::assert_rp_isalive_not_installed(__FILE__, __LINE__)
 134 
 135 #define shenandoah_assert_safepoint() \
 136                     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Should be at Shenandoah Safepoints")
 137 
 138 #define shenandoah_assert_locked_or_safepoint(lock) \
 139                     ShenandoahAsserts::assert_locked_or_shenandoah_safepoint(lock, __FILE__, __LINE__)
 140 
 141 #define shenandoah_assert_heaplocked() \
 142                     ShenandoahAsserts::assert_heaplocked(__FILE__, __LINE__)
 143 
 144 #define shenandoah_assert_not_heaplocked() \
 145                     ShenandoahAsserts::assert_not_heaplocked(__FILE__, __LINE__)
 146 
 147 #define shenandoah_assert_heaplocked_or_safepoint() \
 148                     ShenandoahAsserts::assert_heaplocked_or_safepoint(__FILE__, __LINE__)
 149 #else
 150 #define shenandoah_assert_in_heap(interior_loc, obj)
 151 #define shenandoah_assert_in_correct_region(interior_loc, obj)
 152 
 153 #define shenandoah_assert_correct_if(interior_loc, obj, condition)
 154 #define shenandoah_assert_correct_except(interior_loc, obj, exception)
 155 #define shenandoah_assert_correct(interior_loc, obj)
 156 
 157 #define shenandoah_assert_forwarded_if(interior_loc, obj, condition)
 158 #define shenandoah_assert_forwarded_except(interior_loc, obj, exception)
 159 #define shenandoah_assert_forwarded(interior_loc, obj)
 160 
 161 #define shenandoah_assert_not_forwarded_if(interior_loc, obj, condition)
 162 #define shenandoah_assert_not_forwarded_except(interior_loc, obj, exception)
 163 #define shenandoah_assert_not_forwarded(interior_loc, obj)
 164 
 165 #define shenandoah_assert_marked_if(interior_loc, obj, condition)
 166 #define shenandoah_assert_marked_except(interior_loc, obj, exception)
 167 #define shenandoah_assert_marked(interior_loc, obj)
 168 
 169 #define shenandoah_assert_in_cset_if(interior_loc, obj, condition)
 170 #define shenandoah_assert_in_cset_except(interior_loc, obj, exception)
 171 #define shenandoah_assert_in_cset(interior_loc, obj)
 172 
 173 #define shenandoah_assert_not_in_cset_if(interior_loc, obj, condition)
 174 #define shenandoah_assert_not_in_cset_except(interior_loc, obj, exception)
 175 #define shenandoah_assert_not_in_cset(interior_loc, obj)
 176 
 177 #define shenandoah_assert_not_in_cset_loc_if(interior_loc, condition)
 178 #define shenandoah_assert_not_in_cset_loc_except(interior_loc, exception)
 179 #define shenandoah_assert_not_in_cset_loc(interior_loc)
 180 
 181 #define shenandoah_assert_rp_isalive_installed()
 182 #define shenandoah_assert_rp_isalive_not_installed()
 183 
 184 #define shenandoah_assert_safepoint()
 185 #define shenandoah_assert_locked_or_safepoint(lock)
 186 
 187 #define shenandoah_assert_heaplocked()
 188 #define shenandoah_assert_not_heaplocked()
 189 #define shenandoah_assert_heaplocked_or_safepoint()
 190 
 191 #endif
 192 
 193 #define shenandoah_not_implemented \
 194                     { fatal("Deliberately not implemented."); }
 195 #define shenandoah_not_implemented_return(v) \
 196                     { fatal("Deliberately not implemented."); return v; }
 197 
 198 };
 199 
 200 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHASSERTS_HPP