blob: 71c6a82dc5ced804f6e4239e2c7fd9cdfab32991 [file] [log] [blame]
Elliott Hughes6a144332012-04-03 13:07:11 -07001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_NTH_CALLER_VISITOR_H_
18#define ART_RUNTIME_NTH_CALLER_VISITOR_H_
Elliott Hughes6a144332012-04-03 13:07:11 -070019
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080021#include "base/mutex.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "stack.h"
Elliott Hughes6a144332012-04-03 13:07:11 -070023
24namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025class Thread;
Elliott Hughes6a144332012-04-03 13:07:11 -070026
27// Walks up the stack 'n' callers, when used with Thread::WalkStack.
Ian Rogers0399dde2012-06-06 17:09:28 -070028struct NthCallerVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -080029 NthCallerVisitor(Thread* thread, size_t n_in, bool include_runtime_and_upcalls = false)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010030 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
31 n(n_in),
32 include_runtime_and_upcalls_(include_runtime_and_upcalls),
33 count(0),
Andreas Gamped9911ee2017-03-27 13:27:24 -070034 caller(nullptr),
35 caller_pc(0) {}
Elliott Hughes08fc03a2012-06-26 17:34:00 -070036
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070037 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070038 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080039 bool do_count = false;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070040 if (m == nullptr || m->IsRuntimeMethod()) {
Ian Rogers62d6c772013-02-27 08:32:07 -080041 // Upcall.
42 do_count = include_runtime_and_upcalls_;
43 } else {
44 do_count = true;
45 }
46 if (do_count) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070047 DCHECK(caller == nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -080048 if (count == n) {
49 caller = m;
Mingyao Yangf711f2c2016-05-23 12:29:39 -070050 caller_pc = GetCurrentQuickFramePc();
Ian Rogers62d6c772013-02-27 08:32:07 -080051 return false;
52 }
53 count++;
Elliott Hughes6a144332012-04-03 13:07:11 -070054 }
55 return true;
56 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -070057
Ian Rogers62d6c772013-02-27 08:32:07 -080058 const size_t n;
59 const bool include_runtime_and_upcalls_;
Elliott Hughes6a144332012-04-03 13:07:11 -070060 size_t count;
Mathieu Chartiere401d142015-04-22 13:56:20 -070061 ArtMethod* caller;
Mingyao Yangf711f2c2016-05-23 12:29:39 -070062 uintptr_t caller_pc;
Elliott Hughes6a144332012-04-03 13:07:11 -070063};
64
65} // namespace art
66
Brian Carlstromfc0e3212013-07-17 14:40:12 -070067#endif // ART_RUNTIME_NTH_CALLER_VISITOR_H_