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