1 /**
2 * Copyright (c) 2022 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 #include "test_debugger.h"
17 #include "test_frame.h"
18
19 #include "macros.h"
20 #include "tooling/debug_interface.h"
21 #include "tooling/pt_thread.h"
22 #include "utils/expected.h"
23
24 #include <algorithm>
25 #include <iterator>
26 #include <memory>
27
28 namespace panda::tooling::inspector::test {
PushFrame(TestFrame & frame)29 void TestDebugger::PushFrame(TestFrame &frame)
30 {
31 return callStack_.PushFront(frame);
32 }
33
GetCurrentFrame(PtThread) const34 Expected<std::unique_ptr<PtFrame>, Error> TestDebugger::GetCurrentFrame(PtThread /* thread */) const
35 {
36 if (callStack_.Empty()) {
37 return Unexpected(Error(Error::Type::NO_MORE_FRAMES, "No current frame"));
38 }
39
40 return {std::make_unique<TestFrame>(callStack_.Front())};
41 }
42
EnumerateFrames(PtThread,std::function<bool (const PtFrame &)> function) const43 std::optional<Error> TestDebugger::EnumerateFrames(PtThread /* thread */,
44 std::function<bool(const PtFrame &)> function) const
45 {
46 std::all_of(callStack_.begin(), callStack_.end(), function);
47 return {};
48 }
49
NotifyFramePop(PtThread,uint32_t frameDepth) const50 std::optional<Error> TestDebugger::NotifyFramePop(PtThread /* thread */, uint32_t frameDepth) const
51 {
52 std::next(callStack_.begin(), frameDepth)->SetNotifyPop();
53 return {};
54 }
55
HandleBreakpoint(const PtLocation & location) const56 void TestDebugger::HandleBreakpoint(const PtLocation &location) const
57 {
58 if (breakpoints_.count(location) == 0) {
59 return;
60 }
61
62 ASSERT(hooks_ != nullptr && !callStack_.Empty());
63 hooks_->Breakpoint(PtThread::NONE, callStack_.Front().GetMethod(), location);
64 }
65
SetBreakpoint(const PtLocation & location)66 std::optional<Error> TestDebugger::SetBreakpoint(const PtLocation &location)
67 {
68 if (!breakpoints_.insert(location).second) {
69 return Error(Error::Type::BREAKPOINT_ALREADY_EXISTS, "Breakpoint already exists");
70 }
71
72 return {};
73 }
74
RemoveBreakpoint(const PtLocation & location)75 std::optional<Error> TestDebugger::RemoveBreakpoint(const PtLocation &location)
76 {
77 if (breakpoints_.erase(location) == 0) {
78 return Error(Error::Type::BREAKPOINT_NOT_FOUND, "Breakpoint not found");
79 }
80
81 return {};
82 }
83 } // namespace panda::tooling::inspector::test
84