• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef PANDA_VERIFIER_DEBUG_CONTEXT_H_
17 #define PANDA_VERIFIER_DEBUG_CONTEXT_H_
18 
19 #include "verification/config/config.h"
20 #include "verification/config/debug_breakpoint/breakpoint.h"
21 #include "verification/config/whitelist/whitelist.h"
22 #include "verification/util/callable.h"
23 #include "verification/util/synchronized.h"
24 
25 #include "runtime/include/mem/panda_containers.h"
26 #include "runtime/include/mem/panda_string.h"
27 
28 #include <array>
29 #include <cstdint>
30 #include <unordered_map>
31 #include <unordered_set>
32 #include <vector>
33 
34 namespace panda::verifier::debug {
35 struct DebugContext {
36     struct {
37         PandaUnorderedMap<PandaString, panda::verifier::callable<bool(const config::Section &)>> SectionHandlers;
38     } Config;
39 
40 #ifndef NDEBUG
41     struct {
42         // note: this is assumed to be small so stored as a vector (not even sorted, so we use a linear search)
43         // if changed, a sorted vector or a PandaMap would likely be better than PandaUnorderedMap for faster comparison
44         // accessed for writing only when parsing config, so doesn't need to be synchronized
45         PandaVector<std::pair<PandaString, Offsets>> Config;
46         Synchronized<PandaUnorderedMap<Method::UniqId, PandaUnorderedSet<uint32_t>>> Breakpoint;
47     } ManagedBreakpoints;
48 #endif
49 
50     struct {
51         // similar to ManagedBreakpoints.Config
52         std::array<PandaVector<PandaString>, static_cast<size_t>(WhitelistKind::__LAST__)> Names;
53         std::array<Synchronized<PandaUnorderedSet<Method::UniqId>>, static_cast<size_t>(WhitelistKind::__LAST__)> Id;
54         bool isNotEmpty = false;
55     } Whitelist;
56 
57     static DebugContext &GetCurrent();
58     static void Destroy();
59 
60     void AddMethod(const Method &method, bool isDebug);
61 
62 private:
63     static DebugContext *instance;
64 
65 #ifndef NDEBUG
66     void InsertBreakpoints(const PandaString &method_name, Method::UniqId id);
67 #else
InsertBreakpointsDebugContext68     void InsertBreakpoints([[maybe_unused]] const PandaString &method_name, [[maybe_unused]] Method::UniqId id) {}
69 #endif
70 
71     void InsertIntoWhitelist(const PandaString &name, bool isClassName, Method::UniqId id);
72 };
73 }  // namespace panda::verifier::debug
74 
75 #endif  // !PANDA_VERIFIER_DEBUG_CONTEXT_H_
76