1 /**
2 * Copyright (c) 2021-2024 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 ark::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
64 #ifndef PANDA_TARGET_MOBILE
operator <<(std::ostream & out,std::nullptr_t)65 std::ostream &operator<<(std::ostream &out, std::nullptr_t)
66 {
67 return out << "nullptr";
68 }
69 #endif // PANDA_TARGET_MOBILE
70
ApiTest()71 ApiTest::ApiTest()
72 {
73 scenario = []() {
74 ASSERT_EXITED();
75 return true;
76 };
77 }
78
SetExtractorFactoryForTest(TestExtractorFactory * factory)79 void SetExtractorFactoryForTest(TestExtractorFactory *factory)
80 {
81 TestUtil::SetExtractorFactory(factory);
82 }
83 } // namespace ark::tooling::test
84