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         caller_pc(0) {}
36 
VisitFrameNthCallerVisitor37   bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
38     ArtMethod* m = GetMethod();
39     bool do_count = false;
40     if (m == nullptr || m->IsRuntimeMethod()) {
41       // Upcall.
42       do_count = include_runtime_and_upcalls_;
43     } else {
44       do_count = true;
45     }
46     if (do_count) {
47       DCHECK(caller == nullptr);
48       if (count == n) {
49         caller = m;
50         caller_pc = GetCurrentQuickFramePc();
51         return false;
52       }
53       count++;
54     }
55     return true;
56   }
57 
58   const size_t n;
59   const bool include_runtime_and_upcalls_;
60   size_t count;
61   ArtMethod* caller;
62   uintptr_t caller_pc;
63 };
64 
65 }  // namespace art
66 
67 #endif  // ART_RUNTIME_NTH_CALLER_VISITOR_H_
68