blob: 794878a08e7c8550b3d18c43727a4a55630660ec [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
Brian Carlstromea46f952013-07-30 01:26:50 -070020#include "mirror/art_method.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080021#include "locks.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 {
Ian Rogers62d6c772013-02-27 08:32:07 -080029 NthCallerVisitor(Thread* thread, size_t n, bool include_runtime_and_upcalls = false)
30 : StackVisitor(thread, NULL), n(n), include_runtime_and_upcalls_(include_runtime_and_upcalls),
31 count(0), caller(NULL) {}
Elliott Hughes08fc03a2012-06-26 17:34:00 -070032
Ian Rogers62d6c772013-02-27 08:32:07 -080033 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -070034 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080035 bool do_count = false;
36 if (m == NULL || m->IsRuntimeMethod()) {
37 // Upcall.
38 do_count = include_runtime_and_upcalls_;
39 } else {
40 do_count = true;
41 }
42 if (do_count) {
43 DCHECK(caller == NULL);
44 if (count == n) {
45 caller = m;
46 return false;
47 }
48 count++;
Elliott Hughes6a144332012-04-03 13:07:11 -070049 }
50 return true;
51 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -070052
Ian Rogers62d6c772013-02-27 08:32:07 -080053 const size_t n;
54 const bool include_runtime_and_upcalls_;
Elliott Hughes6a144332012-04-03 13:07:11 -070055 size_t count;
Brian Carlstromea46f952013-07-30 01:26:50 -070056 mirror::ArtMethod* caller;
Elliott Hughes6a144332012-04-03 13:07:11 -070057};
58
59} // namespace art
60
Brian Carlstromfc0e3212013-07-17 14:40:12 -070061#endif // ART_RUNTIME_NTH_CALLER_VISITOR_H_