• 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 NDEBUG
17 
18 #include "breakpoint_private.h"
19 
20 #include "verification/config/context/context.h"
21 #include "verification/util/optional_ref.h"
22 #include "verification/util/str.h"
23 
24 #include "verifier_messages.h"
25 
26 #include "utils/logger.h"
27 
28 namespace panda::verifier::debug {
29 
30 template <typename BreakpointConfigT>
BreakpointsForName(const BreakpointConfigT & breakpoint_config,const PandaString & method_name)31 OptionalRef<Offsets> BreakpointsForName(const BreakpointConfigT &breakpoint_config, const PandaString &method_name)
32 {
33     auto iter = std::find_if(breakpoint_config.begin(), breakpoint_config.end(),
34                              [&](const auto &pair) { return pair.first == method_name; });
35     if (iter == breakpoint_config.end()) {
36         return {};
37     }
38     return iter->second;
39 }
40 
AddBreakpointConfig(const PandaString & method_name,Offset offset)41 void AddBreakpointConfig(const PandaString &method_name, Offset offset)
42 {
43     auto &breakpoint_config = DebugContext::GetCurrent().ManagedBreakpoints.Config;
44     auto opt_breakpoints = BreakpointsForName(breakpoint_config, method_name);
45     if (opt_breakpoints.HasRef()) {
46         opt_breakpoints->push_back(offset);
47     } else {
48         breakpoint_config.push_back({method_name, Offsets {offset}});
49     }
50     LOG_VERIFIER_DEBUG_BREAKPOINT_ADDED_INFO(method_name, offset);
51 }
52 
InsertBreakpoints(const PandaString & method_name,Method::UniqId id)53 void DebugContext::InsertBreakpoints(const PandaString &method_name, Method::UniqId id)
54 {
55     auto opt_breakpoints = BreakpointsForName(ManagedBreakpoints.Config, method_name);
56     if (opt_breakpoints.HasRef()) {
57         for (const auto offset : opt_breakpoints.Get()) {
58             LOG_VERIFIER_DEBUG_BREAKPOINT_SET_INFO(method_name, id, offset);
59             ManagedBreakpoints.Breakpoint.Apply([&](auto &breakpoint_map) { breakpoint_map[id].insert(offset); });
60         }
61     }
62 }
63 
CheckManagedBreakpoint(Method::UniqId id,Offset offset)64 bool CheckManagedBreakpoint(Method::UniqId id, Offset offset)
65 {
66     const auto &breakpoints = DebugContext::GetCurrent().ManagedBreakpoints.Breakpoint;
67     return breakpoints.Apply([&](const auto &breakpoint_map) {
68         auto iter = breakpoint_map.find(id);
69         if (iter == breakpoint_map.end()) {
70             return false;
71         }
72         return iter->second.count(offset) > 0;
73     });
74 }
75 
ManagedBreakpointPresent(Method::UniqId id)76 bool ManagedBreakpointPresent(Method::UniqId id)
77 {
78     return DebugContext::GetCurrent().ManagedBreakpoints.Breakpoint->count(id) > 0;
79 }
80 
81 }  // namespace panda::verifier::debug
82 
83 #endif
84