1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "stack_trace_element.h"
18 
19 #include "class.h"
20 #include "class-inl.h"
21 #include "gc/accounting/card_table-inl.h"
22 #include "object-inl.h"
23 #include "handle_scope-inl.h"
24 #include "string.h"
25 
26 namespace art {
27 namespace mirror {
28 
29 GcRoot<Class> StackTraceElement::java_lang_StackTraceElement_;
30 
SetClass(Class * java_lang_StackTraceElement)31 void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
32   CHECK(java_lang_StackTraceElement_.IsNull());
33   CHECK(java_lang_StackTraceElement != nullptr);
34   java_lang_StackTraceElement_ = GcRoot<Class>(java_lang_StackTraceElement);
35 }
36 
ResetClass()37 void StackTraceElement::ResetClass() {
38   CHECK(!java_lang_StackTraceElement_.IsNull());
39   java_lang_StackTraceElement_ = GcRoot<Class>(nullptr);
40 }
41 
Alloc(Thread * self,Handle<String> declaring_class,Handle<String> method_name,Handle<String> file_name,int32_t line_number)42 StackTraceElement* StackTraceElement::Alloc(Thread* self, Handle<String> declaring_class,
43                                             Handle<String> method_name, Handle<String> file_name,
44                                             int32_t line_number) {
45   StackTraceElement* trace =
46       down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject(self));
47   if (LIKELY(trace != nullptr)) {
48     if (Runtime::Current()->IsActiveTransaction()) {
49       trace->Init<true>(declaring_class, method_name, file_name, line_number);
50     } else {
51       trace->Init<false>(declaring_class, method_name, file_name, line_number);
52     }
53   }
54   return trace;
55 }
56 
57 template<bool kTransactionActive>
Init(Handle<String> declaring_class,Handle<String> method_name,Handle<String> file_name,int32_t line_number)58 void StackTraceElement::Init(Handle<String> declaring_class, Handle<String> method_name,
59                              Handle<String> file_name, int32_t line_number) {
60   SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
61                                      declaring_class.Get());
62   SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
63                                      method_name.Get());
64   SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
65                                      file_name.Get());
66   SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
67                                  line_number);
68 }
69 
VisitRoots(RootVisitor * visitor)70 void StackTraceElement::VisitRoots(RootVisitor* visitor) {
71   java_lang_StackTraceElement_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
72 }
73 
74 
75 }  // namespace mirror
76 }  // namespace art
77