1 /**
2 * Copyright (c) 2021-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_util.h"
17
18 #include "include/method.h"
19 #include "include/tooling/pt_location.h"
20
21 namespace panda::tooling::test {
22
23 // NOLINTBEGIN(fuchsia-statically-constructed-objects)
24 TestMap TestUtil::testMap_;
25 os::memory::Mutex TestUtil::eventMutex_;
26 os::memory::ConditionVariable TestUtil::eventCv_;
27 DebugEvent TestUtil::lastEvent_ = DebugEvent::UNINITIALIZED;
28 bool TestUtil::initialized_ = false;
29 os::memory::Mutex TestUtil::suspendMutex_;
30 os::memory::ConditionVariable TestUtil::suspendCv_;
31 bool TestUtil::suspended_;
32 PtThread TestUtil::lastEventThread_ = PtThread(nullptr);
33 PtLocation TestUtil::lastEventLocation_("", EntityId(0), 0);
34 TestExtractorFactory *TestUtil::extractorFactory_;
35 // NOLINTEND(fuchsia-statically-constructed-objects)
36
GetValueRegister(Method * method,const char * varName,uint32_t offset)37 int32_t TestUtil::GetValueRegister(Method *method, const char *varName, uint32_t offset)
38 {
39 auto methodId = method->GetFileId();
40 auto pf = method->GetPandaFile();
41 PtLocation location(pf->GetFilename().c_str(), methodId, offset);
42 auto extractor = extractorFactory_->MakeTestExtractor(pf);
43
44 auto variables = extractor->GetLocalVariableInfo(location.GetMethodId(), location.GetBytecodeOffset());
45 for (const auto &var : variables) {
46 if (var.name == varName) {
47 return var.regNumber;
48 }
49 }
50
51 auto params = extractor->GetParameterInfo(location.GetMethodId());
52 auto paramReg = method->GetNumVregs();
53
54 for (const auto ¶m : params) {
55 if (param.name == varName) {
56 return paramReg;
57 }
58 paramReg++;
59 }
60
61 return -2; // NOTE(maksenov): Replace with invalid register constant;
62 }
63
operator <<(std::ostream & out,DebugEvent value)64 std::ostream &operator<<(std::ostream &out, DebugEvent value)
65 {
66 const char *s = nullptr;
67
68 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
69 #define ADD_CASE(entry) \
70 case (entry): \
71 s = #entry; \
72 break
73
74 switch (value) {
75 ADD_CASE(DebugEvent::BREAKPOINT);
76 ADD_CASE(DebugEvent::LOAD_MODULE);
77 ADD_CASE(DebugEvent::PAUSED);
78 ADD_CASE(DebugEvent::EXCEPTION);
79 ADD_CASE(DebugEvent::EXCEPTION_CATCH);
80 ADD_CASE(DebugEvent::FIELD_ACCESS);
81 ADD_CASE(DebugEvent::FIELD_MODIFICATION);
82 ADD_CASE(DebugEvent::FRAME_POP);
83 ADD_CASE(DebugEvent::GARBAGE_COLLECTIION_START);
84 ADD_CASE(DebugEvent::GARBAGE_COLLECTIION_FINISH);
85 ADD_CASE(DebugEvent::METHOD_ENTRY);
86 ADD_CASE(DebugEvent::METHOD_EXIT);
87 ADD_CASE(DebugEvent::SINGLE_STEP);
88 ADD_CASE(DebugEvent::THREAD_START);
89 ADD_CASE(DebugEvent::THREAD_END);
90 ADD_CASE(DebugEvent::VM_START);
91 ADD_CASE(DebugEvent::VM_INITIALIZATION);
92 ADD_CASE(DebugEvent::VM_DEATH);
93 ADD_CASE(DebugEvent::EXCEPTION_REVOKED);
94 ADD_CASE(DebugEvent::EXECUTION_CONTEXT_CREATED);
95 ADD_CASE(DebugEvent::EXECUTION_CONTEXT_DESTROYED);
96 ADD_CASE(DebugEvent::EXECUTION_CONTEXT_CLEARED);
97 ADD_CASE(DebugEvent::INSPECT_REQUESTED);
98 ADD_CASE(DebugEvent::CLASS_LOAD);
99 ADD_CASE(DebugEvent::CLASS_PREPARE);
100 ADD_CASE(DebugEvent::MONITOR_WAIT);
101 ADD_CASE(DebugEvent::MONITOR_WAITED);
102 ADD_CASE(DebugEvent::MONITOR_CONTENDED_ENTER);
103 ADD_CASE(DebugEvent::MONITOR_CONTENDED_ENTERED);
104 ADD_CASE(DebugEvent::UNINITIALIZED);
105 default: {
106 ASSERT(false && "Unknown DebugEvent"); // NOLINT
107 }
108 }
109
110 #undef ADD_CASE
111
112 return out << s;
113 }
114
115 #ifndef PANDA_TARGET_MOBILE
operator <<(std::ostream & out,std::nullptr_t)116 std::ostream &operator<<(std::ostream &out, std::nullptr_t)
117 {
118 return out << "nullptr";
119 }
120 #endif // PANDA_TARGET_MOBILE
121
ApiTest()122 ApiTest::ApiTest()
123 {
124 scenario = []() {
125 ASSERT_EXITED();
126 return true;
127 };
128 }
129
SetExtractorFactoryForTest(TestExtractorFactory * factory)130 void SetExtractorFactoryForTest(TestExtractorFactory *factory)
131 {
132 TestUtil::SetExtractorFactory(factory);
133 }
134 } // namespace panda::tooling::test
135