1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef PANDA_RUNTIME_INCLUDE_STACK_WALKER_INL_H_
17 #define PANDA_RUNTIME_INCLUDE_STACK_WALKER_INL_H_
18
19 #include "macros.h"
20 #include "stack_walker.h"
21 #include "runtime/include/cframe_iterators.h"
22
23 namespace panda {
24
25 template <bool objects, bool with_reg_info, typename Func>
26 // NOLINTNEXTLINE(google-runtime-references)
InvokeCallback(Func func,VRegInfo reg_info,Frame::VRegister & vreg)27 bool InvokeCallback(Func func, [[maybe_unused]] VRegInfo reg_info, Frame::VRegister &vreg)
28 {
29 if (objects && !vreg.HasObject()) {
30 return true;
31 }
32 if constexpr (with_reg_info) { // NOLINT
33 if (!func(reg_info, vreg)) {
34 return false;
35 }
36 } else { // NOLINT
37 if (!func(vreg)) {
38 return false;
39 }
40 }
41 return true;
42 }
43
44 template <bool objects, bool with_reg_info, typename Func>
IterateRegsForIFrame(Func func)45 bool StackWalker::IterateRegsForIFrame(Func func)
46 {
47 auto frame = GetIFrame();
48 for (size_t i = 0; i < frame->GetSize(); i++) {
49 auto &vreg = frame->GetVReg(i);
50 if (objects && !vreg.HasObject()) {
51 continue;
52 }
53 VRegInfo reg_info(0, VRegInfo::Location::SLOT,
54 vreg.HasObject() ? VRegInfo::Type::OBJECT : VRegInfo::Type::INT64, false, i);
55 if (!InvokeCallback<objects, with_reg_info>(func, reg_info, vreg)) {
56 return false;
57 }
58 }
59 auto &acc = frame->GetAcc();
60 VRegInfo reg_info(0, VRegInfo::Location::SLOT, acc.HasObject() ? VRegInfo::Type::OBJECT : VRegInfo::Type::INT64,
61 true, 0);
62 return InvokeCallback<objects, with_reg_info>(func, reg_info, acc);
63 }
64
65 template <bool objects, bool with_reg_info, typename Func>
IterateRegs(Func func)66 bool StackWalker::IterateRegs(Func func)
67 {
68 ASSERT(!IsCFrame());
69 return IterateRegsForIFrame<objects, with_reg_info>(func);
70 }
71
72 } // namespace panda
73
74 #endif // PANDA_RUNTIME_INCLUDE_STACK_WALKER_INL_H_
75