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 "context.h"
17
18 #include "runtime/include/mem/allocator.h"
19
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <vector>
23
24 namespace panda::verifier::debug {
25
26 DebugContext *DebugContext::instance = nullptr;
27
GetCurrent()28 DebugContext &DebugContext::GetCurrent()
29 {
30 if (instance != nullptr) {
31 return *instance;
32 }
33 instance = new (mem::AllocatorAdapter<DebugContext>().allocate(1)) DebugContext {};
34 ASSERT(instance != nullptr);
35 return *instance;
36 }
37
AddMethod(const Method & method,bool isDebug)38 void DebugContext::AddMethod(const Method &method, bool isDebug)
39 {
40 if (Whitelist.isNotEmpty || isDebug) {
41 auto id = method.GetUniqId();
42 // this is calculated repeatedly for each class, but caching was tried and didn't improve performance
43 auto name {ClassHelper::GetName<PandaString>(method.GetClassName().data)};
44 if (Whitelist.isNotEmpty) {
45 InsertIntoWhitelist(name, true, id);
46 }
47 name += "::";
48 name += utf::Mutf8AsCString(method.GetName().data);
49 if (Whitelist.isNotEmpty) {
50 InsertIntoWhitelist(name, false, id);
51 }
52 if (isDebug) {
53 InsertBreakpoints(name, id);
54 }
55 }
56 }
57
Destroy()58 void DebugContext::Destroy()
59 {
60 if (instance != nullptr) {
61 instance->~DebugContext();
62 mem::AllocatorAdapter<DebugContext>().deallocate(instance, 1);
63 instance = nullptr;
64 }
65 }
66
67 } // namespace panda::verifier::debug
68