• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "instruction_pointer.h"
17 
18 #include "../inspector.h"
19 #include "client.h"
20 #include "json_object_matcher.h"
21 
22 #include "bytecode_instruction.h"
23 #include "macros.h"
24 #include "tooling/pt_location.h"
25 #include "tooling/pt_thread.h"
26 #include "utils/json_builder.h"
27 #include "utils/json_parser.h"
28 
29 #include "gtest/gtest.h"
30 #include "gmock/gmock.h"
31 
32 namespace panda::tooling::inspector::test {
~InstructionPointer()33 InstructionPointer::~InstructionPointer()
34 {
35     EXPECT_EQ(instruction_.GetAddress(), instruction_.GetFrom()) << "InstructionPointer not finished stepping";
36 }
37 
ContinueTo(std::string_view scriptId,size_t lineNumber)38 void InstructionPointer::ContinueTo(std::string_view scriptId, size_t lineNumber)
39 {
40     client_.Call(
41         "Debugger.continueToLocation",
42         [&](auto &params) {
43             params.AddProperty("location", [&](JsonObjectBuilder &location) {
44                 location.AddProperty("scriptId", scriptId);
45                 location.AddProperty("lineNumber", lineNumber);
46             });
47         },
48         // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) due to bug in clang-tidy #3553 (gtest repo)
49         [](auto &result) { EXPECT_THAT(result, JsonProperties()); });
50 
51     Step();
52 }
53 
Finish()54 void InstructionPointer::Finish()
55 {
56     if (unset_) {
57         Reset();
58     }
59 
60     while (instruction_.IsValid()) {
61         Step();
62     }
63 }
64 
Resume()65 void InstructionPointer::Resume()
66 {
67     // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) due to bug in clang-tidy #3553 (gtest repo)
68     client_.Call("Debugger.resume", [](const JsonObject &result) { EXPECT_THAT(result, JsonProperties()); });
69     Step();
70 }
71 
Step()72 void InstructionPointer::Step()
73 {
74     if (unset_) {
75         Reset();
76     }
77 
78     ASSERT(instruction_.IsValid());
79 
80     PtLocation location(GetMethod()->GetPandaFile()->GetFilename().c_str(), GetMethod()->GetFileId(),
81                         instruction_.GetOffset());
82 
83     frame_.SetBytecodeOffset(instruction_.GetOffset());
84 
85     debugger_.GetHooks().SingleStep(PtThread::NONE, GetMethod(), location);
86     debugger_.HandleBreakpoint(location);
87 
88     instruction_ = instruction_.GetNext();
89 }
90 
StepInto()91 void InstructionPointer::StepInto()
92 {
93     // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) due to bug in clang-tidy #3553 (gtest repo)
94     client_.Call("Debugger.stepInto", [](const JsonObject &result) { EXPECT_THAT(result, JsonProperties()); });
95     Step();
96 }
97 
StepOut()98 void InstructionPointer::StepOut()
99 {
100     // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) due to bug in clang-tidy #3553 (gtest repo)
101     client_.Call("Debugger.stepOut", [](const JsonObject &result) { EXPECT_THAT(result, JsonProperties()); });
102     Step();
103 }
104 
StepOver()105 void InstructionPointer::StepOver()
106 {
107     // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) due to bug in clang-tidy #3553 (gtest repo)
108     client_.Call("Debugger.stepOver", [](const JsonObject &result) { EXPECT_THAT(result, JsonProperties()); });
109     Step();
110 }
111 
Reset()112 void InstructionPointer::Reset()
113 {
114     ASSERT(GetMethod());
115     instruction_ = BytecodeInstructionSafe(GetMethod()->GetInstructions(), GetMethod()->GetInstructions(),
116                                            GetMethod()->GetInstructions() + GetMethod()->GetCodeSize() - 1);
117     frame_.SetBytecodeOffset(0);
118     unset_ = false;
119 }
120 }  // namespace panda::tooling::inspector::test
121