blob: bb17cac476c303c578be06bca6e5862dd06980db [file] [log] [blame]
Andreas Gampecc13b222016-10-10 19:09:09 -07001/* Copyright (C) 2016 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
32#include "object_tagging.h"
33
34#include "art_jvmti.h"
35#include "base/logging.h"
36#include "events-inl.h"
37#include "gc/allocation_listener.h"
38#include "instrumentation.h"
39#include "jni_env_ext-inl.h"
40#include "mirror/class.h"
41#include "mirror/object.h"
42#include "runtime.h"
43#include "ScopedLocalRef.h"
44
45namespace openjdkjvmti {
46
47void ObjectTagTable::Add(art::mirror::Object* obj, jlong tag) {
48 art::Thread* self = art::Thread::Current();
49 art::MutexLock mu(self, allow_disallow_lock_);
50 Wait(self);
51
52 if (first_free_ == tagged_objects_.size()) {
53 tagged_objects_.push_back(Entry(art::GcRoot<art::mirror::Object>(obj), tag));
54 first_free_++;
55 } else {
56 DCHECK_LT(first_free_, tagged_objects_.size());
57 DCHECK(tagged_objects_[first_free_].first.IsNull());
58 tagged_objects_[first_free_] = Entry(art::GcRoot<art::mirror::Object>(obj), tag);
59 // TODO: scan for free elements.
60 first_free_ = tagged_objects_.size();
61 }
62}
63
64bool ObjectTagTable::Remove(art::mirror::Object* obj, jlong* tag) {
65 art::Thread* self = art::Thread::Current();
66 art::MutexLock mu(self, allow_disallow_lock_);
67 Wait(self);
68
69 for (auto it = tagged_objects_.begin(); it != tagged_objects_.end(); ++it) {
70 if (it->first.Read(nullptr) == obj) {
71 if (tag != nullptr) {
72 *tag = it->second;
73 }
74 it->first = art::GcRoot<art::mirror::Object>(nullptr);
75
76 size_t index = it - tagged_objects_.begin();
77 if (index < first_free_) {
78 first_free_ = index;
79 }
80
81 // TODO: compaction.
82
83 return true;
84 }
85 }
86
87 return false;
88}
89
90void ObjectTagTable::Sweep(art::IsMarkedVisitor* visitor) {
91 if (event_handler_->IsEventEnabledAnywhere(JVMTI_EVENT_OBJECT_FREE)) {
92 SweepImpl<true>(visitor);
93 } else {
94 SweepImpl<false>(visitor);
95 }
96}
97
98template <bool kHandleNull>
99void ObjectTagTable::SweepImpl(art::IsMarkedVisitor* visitor) {
100 art::Thread* self = art::Thread::Current();
101 art::MutexLock mu(self, allow_disallow_lock_);
102
103 for (auto it = tagged_objects_.begin(); it != tagged_objects_.end(); ++it) {
104 if (!it->first.IsNull()) {
105 art::mirror::Object* original_obj = it->first.Read<art::kWithoutReadBarrier>();
106 art::mirror::Object* target_obj = visitor->IsMarked(original_obj);
107 if (original_obj != target_obj) {
108 it->first = art::GcRoot<art::mirror::Object>(target_obj);
109
110 if (kHandleNull && target_obj == nullptr) {
111 HandleNullSweep(it->second);
112 }
113 }
114 } else {
115 size_t index = it - tagged_objects_.begin();
116 if (index < first_free_) {
117 first_free_ = index;
118 }
119 }
120 }
121}
122
123void ObjectTagTable::HandleNullSweep(jlong tag) {
124 event_handler_->DispatchEvent(nullptr, JVMTI_EVENT_OBJECT_FREE, tag);
125}
126
127} // namespace openjdkjvmti