• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 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 "conditional_breakpoint.h"
17 
18 #include "debug_info_extractor.h"
19 #include "error.h"
20 #include "evaluation/evaluation_engine.h"
21 
22 namespace ark::tooling::inspector {
23 
ShouldStopAt(const PtLocation & location,EvaluationEngine & engine)24 bool ConditionalBreakpoint::ShouldStopAt(const PtLocation &location, EvaluationEngine &engine)
25 {
26     if (!location_.has_value() || !(*location_ == location)) {
27         return false;
28     }
29     // Condition is evaluated in the context of top frame
30     auto evalRes = engine.EvaluateExpression(0, bytecode_, &method_);
31     if (!evalRes) {
32         HandleError(evalRes.Error());
33         return false;
34     }
35     if (evalRes.Value().second != nullptr) {
36         LOG(WARNING, DEBUGGER) << "Breakpoint #" << GetId() << " condition evaluated with an exception";
37         return false;
38     }
39     return evalRes.Value().first.GetAsU1();
40 }
41 
SetLocations(std::set<std::string_view> & sourceFiles,const DebugInfoCache & debugCache,std::unordered_multimap<PtLocation,BreakpointId,HashLocation> & breakpointLocations)42 bool ConditionalBreakpoint::SetLocations(
43     std::set<std::string_view> &sourceFiles, const DebugInfoCache &debugCache,
44     std::unordered_multimap<PtLocation, BreakpointId, HashLocation> &breakpointLocations)
45 {
46     auto locations = debugCache.GetBreakpointLocations(sourceFileFilter_, lineNumber_, sourceFiles);
47     if (locations.size() > 1) {
48         LOG(WARNING, DEBUGGER) << "Will not set conditional breakpoint for more than one location";
49         return false;
50     }
51 
52     if (locations.empty()) {
53         LOG(WARNING, DEBUGGER) << "Pending breakpoint, 0 locations resolved currently, id = " << GetId();
54         return true;
55     }
56 
57     location_ = *locations.begin();
58     breakpointLocations.emplace(location_.value(), GetId());
59     Resolve();
60     return true;
61 }
62 
TryResolveImpl(const panda_file::File & file,const panda_file::DebugInfoExtractor * debugInfo,std::unordered_multimap<PtLocation,BreakpointId,HashLocation> & breakpointLocations)63 void ConditionalBreakpoint::TryResolveImpl(
64     const panda_file::File &file, const panda_file::DebugInfoExtractor *debugInfo,
65     std::unordered_multimap<PtLocation, BreakpointId, HashLocation> &breakpointLocations)
66 {
67     if (IsResolved()) {
68         return;
69     }
70 
71     auto lineHandler = [this, &breakpointLocations](const auto &pandaFile, auto methodId, auto &entry) {
72         if (entry.line == lineNumber_) {
73             location_.emplace(pandaFile.GetFilename().data(), methodId, entry.offset);
74             breakpointLocations.emplace(location_.value(), GetId());
75             Resolve();
76             // Must choose the first found bytecode location in each method
77             return false;
78         }
79         // Continue search
80         return true;
81     };
82 
83     for (const auto &methodId : debugInfo->GetMethodIdList()) {
84         if (!sourceFileFilter_(debugInfo->GetSourceFile(methodId))) {
85             continue;
86         }
87 
88         auto &table = debugInfo->GetLineNumberTable(methodId);
89         for (auto &entry : table) {
90             if (!lineHandler(file, methodId, entry)) {
91                 break;
92             }
93         }
94     }
95 }
96 
97 }  // namespace ark::tooling::inspector
98