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 TestMap TestUtil::test_map_;
24 os::memory::Mutex TestUtil::event_mutex_;
25 os::memory::ConditionVariable TestUtil::event_cv_;
26 DebugEvent TestUtil::last_event_ = DebugEvent::UNINITIALIZED;
27 bool TestUtil::initialized_ = false;
28 os::memory::Mutex TestUtil::suspend_mutex_;
29 os::memory::ConditionVariable TestUtil::suspend_cv_;
30 bool TestUtil::suspended_;
31 PtThread TestUtil::last_event_thread_ = PtThread::NONE;
32 PtLocation TestUtil::last_event_location_("", EntityId(0), 0);
33 TestExtractorFactory *TestUtil::extractor_factory_;
34
GetVariables(Method * method,uint32_t offset)35 std::vector<panda_file::LocalVariableInfo> TestUtil::GetVariables(Method *method, uint32_t offset)
36 {
37 auto methodId = method->GetFileId();
38 auto pf = method->GetPandaFile();
39 PtLocation location(pf->GetFilename().c_str(), methodId, offset);
40 return GetVariables(pf, location);
41 }
42
GetVariables(const panda_file::File * pf,PtLocation location)43 std::vector<panda_file::LocalVariableInfo> TestUtil::GetVariables(const panda_file::File *pf, PtLocation location)
44 {
45 auto extractor = extractor_factory_->MakeTestExtractor(pf);
46 return extractor->GetLocalVariableInfo(location.GetMethodId(), location.GetBytecodeOffset());
47 }
48
GetValueRegister(Method * method,const char * varName,uint32_t offset)49 int32_t TestUtil::GetValueRegister(Method *method, const char *varName, uint32_t offset)
50 {
51 auto variables = TestUtil::GetVariables(method, offset);
52 for (const auto &var : variables) {
53 if (var.name == varName) {
54 return var.reg_number;
55 }
56 }
57
58 return -2; // TODO(maksenov): Replace with invalid register constant;
59 }
60
operator <<(std::ostream & out,DebugEvent value)61 std::ostream &operator<<(std::ostream &out, DebugEvent value)
62 {
63 const char *s = nullptr;
64
65 #define ADD_CASE(entry) \
66 case (entry): \
67 s = #entry; \
68 break;
69
70 switch (value) {
71 ADD_CASE(DebugEvent::BREAKPOINT);
72 ADD_CASE(DebugEvent::LOAD_MODULE);
73 ADD_CASE(DebugEvent::PAUSED);
74 ADD_CASE(DebugEvent::EXCEPTION);
75 ADD_CASE(DebugEvent::EXCEPTION_CATCH);
76 ADD_CASE(DebugEvent::FIELD_ACCESS);
77 ADD_CASE(DebugEvent::FIELD_MODIFICATION);
78 ADD_CASE(DebugEvent::FRAME_POP);
79 ADD_CASE(DebugEvent::GARBAGE_COLLECTIION_START);
80 ADD_CASE(DebugEvent::GARBAGE_COLLECTIION_FINISH);
81 ADD_CASE(DebugEvent::METHOD_ENTRY);
82 ADD_CASE(DebugEvent::METHOD_EXIT);
83 ADD_CASE(DebugEvent::SINGLE_STEP);
84 ADD_CASE(DebugEvent::THREAD_START);
85 ADD_CASE(DebugEvent::THREAD_END);
86 ADD_CASE(DebugEvent::VM_START);
87 ADD_CASE(DebugEvent::VM_INITIALIZATION);
88 ADD_CASE(DebugEvent::VM_DEATH);
89 ADD_CASE(DebugEvent::EXCEPTION_REVOKED);
90 ADD_CASE(DebugEvent::EXECUTION_CONTEXT_CREATED);
91 ADD_CASE(DebugEvent::EXECUTION_CONTEXT_DESTROYED);
92 ADD_CASE(DebugEvent::EXECUTION_CONTEXT_CLEARED);
93 ADD_CASE(DebugEvent::INSPECT_REQUESTED);
94 ADD_CASE(DebugEvent::CLASS_LOAD);
95 ADD_CASE(DebugEvent::CLASS_PREPARE);
96 ADD_CASE(DebugEvent::MONITOR_WAIT);
97 ADD_CASE(DebugEvent::MONITOR_WAITED);
98 ADD_CASE(DebugEvent::MONITOR_CONTENDED_ENTER);
99 ADD_CASE(DebugEvent::MONITOR_CONTENDED_ENTERED);
100 ADD_CASE(DebugEvent::UNINITIALIZED);
101 default: {
102 ASSERT(false && "Unknown DebugEvent"); // NOLINT
103 }
104 }
105
106 #undef ADD_CASE
107
108 return out << s;
109 }
110
111 #ifndef PANDA_TARGET_MOBILE
operator <<(std::ostream & out,std::nullptr_t)112 std::ostream &operator<<(std::ostream &out, std::nullptr_t)
113 {
114 return out << "nullptr";
115 }
116 #endif // PANDA_TARGET_MOBILE
117
ApiTest()118 ApiTest::ApiTest()
119 {
120 scenario = []() {
121 ASSERT_EXITED();
122 return true;
123 };
124 }
125
SetExtractorFactoryForTest(TestExtractorFactory * factory)126 void SetExtractorFactoryForTest(TestExtractorFactory *factory)
127 {
128 TestUtil::SetExtractorFactory(factory);
129 }
130 } // namespace panda::tooling::test
131