1 /*
   2  * Copyright (c) 2015, 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/backtrace.hpp"
  27 #include "classfile/javaClasses.hpp"
  28 #include "runtime/atomic.hpp"
  29 #include "runtime/jniHandles.hpp"
  30 
  31 void Backtrace::oops_do(OopClosure* f) {
  32   f->do_oop(&_throwable);
  33 }
  34 
  35 // List of backtraces to mark Method pointers for RedefineClasses.
  36 Backtrace* BacktraceList::_backtraces_head = NULL;
  37 volatile int BacktraceList::_bt_count = 0;
  38 
  39 void BacktraceList::add(oop throwable) {
  40   Backtrace* bt = new Backtrace(throwable);
  41   Backtrace* next = _backtraces_head;
  42   do {
  43     bt->set_next(next);
  44     Backtrace* exchanged = (Backtrace*)Atomic::cmpxchg_ptr(bt, &_backtraces_head, next);
  45     if (exchanged == next) {
  46       // added the backtrace to the list.
  47       Atomic::inc(&_bt_count);
  48       return;
  49     }
  50     next = exchanged;
  51   } while (true);
  52 }
  53 
  54 void BacktraceList::oops_do(OopClosure* f) {
  55   Backtrace* bt = _backtraces_head;
  56   while (bt != NULL) {
  57     bt->oops_do(f);
  58     bt = bt->next();
  59   }
  60 } 
  61 
  62 void BacktraceList::mark_on_stack() {
  63   Backtrace* bt = _backtraces_head;
  64   while (bt != NULL) {
  65     assert(bt->throwable() != NULL, "shouldn't have null throwable");
  66     java_lang_Throwable::mark_on_stack(bt->throwable());
  67     bt = bt->next();
  68   }
  69 }
  70 
  71 void BacktraceList::unlink(BoolObjectClosure* is_alive) {
  72   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
  73   Backtrace* bt = _backtraces_head;
  74   Backtrace* prev = NULL;
  75   while (bt != NULL) {
  76     if (!is_alive->do_object_b(bt->throwable())) {
  77       // delete this backtrace object
  78       Backtrace* next = bt->next();
  79       // Delete bt object
  80       delete bt;
  81       // Remove from throwable list.
  82       bt = next;
  83       if (prev != NULL) {
  84         prev->set_next(bt);
  85       } else {
  86         _backtraces_head = bt;
  87       }
  88       Atomic::dec(&_bt_count);
  89     } else {
  90       prev = bt;
  91       bt = bt->next();
  92     }
  93   }
  94 }