1 /*
   2  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
   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 #include "precompiled.hpp"
  25 
  26 #ifdef ASSERT
  27 
  28 #include "memory/allocation.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahHeap.hpp"
  30 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp"
  31 #include "gc_implementation/shenandoah/shenandoahMarkingContext.inline.hpp"
  32 #include "gc_implementation/shenandoah/shenandoahPrinter.hpp"
  33 
  34 class ShenandoahPrintAllRefsOopClosure: public ExtendedOopClosure {
  35 private:
  36   int _index;
  37   const char* _prefix;
  38 
  39 public:
  40   ShenandoahPrintAllRefsOopClosure(const char* prefix) : _index(0), _prefix(prefix) {}
  41 
  42 private:
  43   template <class T>
  44   inline void do_oop_work(T* p) {
  45     oop o = oopDesc::load_decode_heap_oop(p);
  46     if (o != NULL) {
  47       if (ShenandoahHeap::heap()->is_in(o) && o->is_oop()) {
  48         tty->print_cr("%s " INT32_FORMAT " (" PTR_FORMAT ")-> " PTR_FORMAT " (marked: %s) (%s " PTR_FORMAT ")",
  49                       _prefix, _index,
  50                       p2i(p), p2i(o),
  51                       BOOL_TO_STR(ShenandoahHeap::heap()->complete_marking_context()->is_marked(o)),
  52                       o->klass()->internal_name(), p2i(o->klass()));
  53       } else {
  54         tty->print_cr("%s " INT32_FORMAT " (" PTR_FORMAT " dirty -> " PTR_FORMAT " (not in heap, possibly corrupted or dirty)",
  55                       _prefix, _index,
  56                       p2i(p), p2i(o));
  57       }
  58     } else {
  59       tty->print_cr("%s " INT32_FORMAT " (" PTR_FORMAT ") -> " PTR_FORMAT, _prefix, _index, p2i(p), p2i((HeapWord*) o));
  60     }
  61     _index++;
  62   }
  63 
  64 public:
  65   void do_oop(oop* p) {
  66     do_oop_work(p);
  67   }
  68 
  69   void do_oop(narrowOop* p) {
  70     do_oop_work(p);
  71   }
  72 };
  73 
  74 class ShenandoahPrintAllRefsObjectClosure : public ObjectClosure {
  75   const char* _prefix;
  76 
  77 public:
  78   ShenandoahPrintAllRefsObjectClosure(const char* prefix) : _prefix(prefix) {}
  79 
  80   void do_object(oop p) {
  81     if (ShenandoahHeap::heap()->is_in(p)) {
  82       tty->print_cr("%s object " PTR_FORMAT " (marked: %s) (%s " PTR_FORMAT ") refers to:",
  83                     _prefix, p2i(p),
  84                     BOOL_TO_STR(ShenandoahHeap::heap()->complete_marking_context()->is_marked(p)),
  85                     p->klass()->internal_name(), p2i(p->klass()));
  86       ShenandoahPrintAllRefsOopClosure cl(_prefix);
  87       p->oop_iterate(&cl);
  88     }
  89   }
  90 };
  91 
  92 void ShenandoahPrinter::print_all_refs(const char* prefix) {
  93   tty->print_cr("printing all references in the heap");
  94   tty->print_cr("root references:");
  95 
  96   _heap->make_parsable(false);
  97 
  98   ShenandoahPrintAllRefsOopClosure cl(prefix);
  99   _heap->roots_iterate(&cl);
 100 
 101   tty->print_cr("heap references:");
 102   ShenandoahPrintAllRefsObjectClosure cl2(prefix);
 103   _heap->object_iterate(&cl2);
 104 }
 105 
 106 #endif