1 /*
2  * Copyright (C) 2012 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 #ifndef ART_RUNTIME_NTH_CALLER_VISITOR_H_
18 #define ART_RUNTIME_NTH_CALLER_VISITOR_H_
19 
20 #include "art_method.h"
21 #include "base/mutex.h"
22 #include "stack.h"
23 
24 namespace art {
25 class Thread;
26 
27 // Walks up the stack 'n' callers, when used with Thread::WalkStack.
28 struct NthCallerVisitor : public StackVisitor {
29   NthCallerVisitor(Thread* thread, size_t n_in, bool include_runtime_and_upcalls = false)
StackVisitorNthCallerVisitor30       : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
31         n(n_in),
32         include_runtime_and_upcalls_(include_runtime_and_upcalls),
33         count(0),
34         caller(nullptr) {}
35 
VisitFrameNthCallerVisitor36   bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
37     ArtMethod* m = GetMethod();
38     bool do_count = false;
39     if (m == nullptr || m->IsRuntimeMethod()) {
40       // Upcall.
41       do_count = include_runtime_and_upcalls_;
42     } else {
43       do_count = true;
44     }
45     if (do_count) {
46       DCHECK(caller == nullptr);
47       if (count == n) {
48         caller = m;
49         return false;
50       }
51       count++;
52     }
53     return true;
54   }
55 
56   const size_t n;
57   const bool include_runtime_and_upcalls_;
58   size_t count;
59   ArtMethod* caller;
60 };
61 
62 }  // namespace art
63 
64 #endif  // ART_RUNTIME_NTH_CALLER_VISITOR_H_
65