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 "mirror/art_method.h"
21#include "locks.h"
22#include "stack.h"
23
24namespace art {
25class Thread;
26
27// Walks up the stack 'n' callers, when used with Thread::WalkStack.
28struct NthCallerVisitor : public StackVisitor {
29  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) {}
32
33  bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
34    mirror::ArtMethod* m = GetMethod();
35    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++;
49    }
50    return true;
51  }
52
53  const size_t n;
54  const bool include_runtime_and_upcalls_;
55  size_t count;
56  mirror::ArtMethod* caller;
57};
58
59}  // namespace art
60
61#endif  // ART_RUNTIME_NTH_CALLER_VISITOR_H_
62