1cfca06d7SDimitry Andric //===-- BreakpointResolverFileLine.cpp ------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
10f034231aSEd Maste
11f034231aSEd Maste #include "lldb/Breakpoint/BreakpointLocation.h"
12f034231aSEd Maste #include "lldb/Core/Module.h"
13f034231aSEd Maste #include "lldb/Symbol/CompileUnit.h"
14f034231aSEd Maste #include "lldb/Symbol/Function.h"
15e3b55780SDimitry Andric #include "lldb/Target/Target.h"
16145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
1774a628f7SDimitry Andric #include "lldb/Utility/Log.h"
1874a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
19e3b55780SDimitry Andric #include <optional>
20f034231aSEd Maste
21f034231aSEd Maste using namespace lldb;
22f034231aSEd Maste using namespace lldb_private;
23f034231aSEd Maste
24f034231aSEd Maste // BreakpointResolverFileLine:
BreakpointResolverFileLine(const BreakpointSP & bkpt,lldb::addr_t offset,bool skip_prologue,const SourceLocationSpec & location_spec,std::optional<llvm::StringRef> removed_prefix_opt)2514f1b3e8SDimitry Andric BreakpointResolverFileLine::BreakpointResolverFileLine(
26344a3780SDimitry Andric const BreakpointSP &bkpt, lldb::addr_t offset, bool skip_prologue,
27e3b55780SDimitry Andric const SourceLocationSpec &location_spec,
28e3b55780SDimitry Andric std::optional<llvm::StringRef> removed_prefix_opt)
2914f1b3e8SDimitry Andric : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver, offset),
30e3b55780SDimitry Andric m_location_spec(location_spec), m_skip_prologue(skip_prologue),
31e3b55780SDimitry Andric m_removed_prefix_opt(removed_prefix_opt) {}
3214f1b3e8SDimitry Andric
CreateFromStructuredData(const StructuredData::Dictionary & options_dict,Status & error)33b1c73532SDimitry Andric BreakpointResolverSP BreakpointResolverFileLine::CreateFromStructuredData(
34312c0ed1SDimitry Andric const StructuredData::Dictionary &options_dict, Status &error) {
35b76161e4SDimitry Andric llvm::StringRef filename;
36344a3780SDimitry Andric uint32_t line;
37344a3780SDimitry Andric uint16_t column;
3814f1b3e8SDimitry Andric bool check_inlines;
3914f1b3e8SDimitry Andric bool skip_prologue;
4014f1b3e8SDimitry Andric bool exact_match;
4114f1b3e8SDimitry Andric bool success;
4214f1b3e8SDimitry Andric
4314f1b3e8SDimitry Andric lldb::addr_t offset = 0;
4414f1b3e8SDimitry Andric
4514f1b3e8SDimitry Andric success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName),
4614f1b3e8SDimitry Andric filename);
4714f1b3e8SDimitry Andric if (!success) {
4814f1b3e8SDimitry Andric error.SetErrorString("BRFL::CFSD: Couldn't find filename entry.");
4914f1b3e8SDimitry Andric return nullptr;
50f034231aSEd Maste }
51f034231aSEd Maste
5214f1b3e8SDimitry Andric success = options_dict.GetValueForKeyAsInteger(
53344a3780SDimitry Andric GetKey(OptionNames::LineNumber), line);
5414f1b3e8SDimitry Andric if (!success) {
5514f1b3e8SDimitry Andric error.SetErrorString("BRFL::CFSD: Couldn't find line number entry.");
5614f1b3e8SDimitry Andric return nullptr;
5714f1b3e8SDimitry Andric }
5814f1b3e8SDimitry Andric
5994994d37SDimitry Andric success =
6094994d37SDimitry Andric options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Column), column);
6194994d37SDimitry Andric if (!success) {
6294994d37SDimitry Andric // Backwards compatibility.
6394994d37SDimitry Andric column = 0;
6494994d37SDimitry Andric }
6594994d37SDimitry Andric
6614f1b3e8SDimitry Andric success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines),
6714f1b3e8SDimitry Andric check_inlines);
6814f1b3e8SDimitry Andric if (!success) {
6914f1b3e8SDimitry Andric error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry.");
7014f1b3e8SDimitry Andric return nullptr;
7114f1b3e8SDimitry Andric }
7214f1b3e8SDimitry Andric
7314f1b3e8SDimitry Andric success = options_dict.GetValueForKeyAsBoolean(
7414f1b3e8SDimitry Andric GetKey(OptionNames::SkipPrologue), skip_prologue);
7514f1b3e8SDimitry Andric if (!success) {
7614f1b3e8SDimitry Andric error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry.");
7714f1b3e8SDimitry Andric return nullptr;
7814f1b3e8SDimitry Andric }
7914f1b3e8SDimitry Andric
8014f1b3e8SDimitry Andric success = options_dict.GetValueForKeyAsBoolean(
8114f1b3e8SDimitry Andric GetKey(OptionNames::ExactMatch), exact_match);
8214f1b3e8SDimitry Andric if (!success) {
8314f1b3e8SDimitry Andric error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
8414f1b3e8SDimitry Andric return nullptr;
8514f1b3e8SDimitry Andric }
8614f1b3e8SDimitry Andric
87344a3780SDimitry Andric SourceLocationSpec location_spec(FileSpec(filename), line, column,
88344a3780SDimitry Andric check_inlines, exact_match);
89344a3780SDimitry Andric if (!location_spec)
90344a3780SDimitry Andric return nullptr;
9114f1b3e8SDimitry Andric
92b1c73532SDimitry Andric return std::make_shared<BreakpointResolverFileLine>(
93312c0ed1SDimitry Andric nullptr, offset, skip_prologue, location_spec);
9414f1b3e8SDimitry Andric }
9514f1b3e8SDimitry Andric
9614f1b3e8SDimitry Andric StructuredData::ObjectSP
SerializeToStructuredData()9714f1b3e8SDimitry Andric BreakpointResolverFileLine::SerializeToStructuredData() {
9814f1b3e8SDimitry Andric StructuredData::DictionarySP options_dict_sp(
9914f1b3e8SDimitry Andric new StructuredData::Dictionary());
10014f1b3e8SDimitry Andric
10114f1b3e8SDimitry Andric options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
10214f1b3e8SDimitry Andric m_skip_prologue);
103344a3780SDimitry Andric options_dict_sp->AddStringItem(GetKey(OptionNames::FileName),
104344a3780SDimitry Andric m_location_spec.GetFileSpec().GetPath());
105344a3780SDimitry Andric options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber),
106145449b1SDimitry Andric m_location_spec.GetLine().value_or(0));
107344a3780SDimitry Andric options_dict_sp->AddIntegerItem(
108344a3780SDimitry Andric GetKey(OptionNames::Column),
109145449b1SDimitry Andric m_location_spec.GetColumn().value_or(LLDB_INVALID_COLUMN_NUMBER));
110344a3780SDimitry Andric options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines),
111344a3780SDimitry Andric m_location_spec.GetCheckInlines());
11214f1b3e8SDimitry Andric options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
113344a3780SDimitry Andric m_location_spec.GetExactMatch());
11414f1b3e8SDimitry Andric
11514f1b3e8SDimitry Andric return WrapOptionsDict(options_dict_sp);
116f034231aSEd Maste }
117f034231aSEd Maste
11874a628f7SDimitry Andric // Filter the symbol context list to remove contexts where the line number was
11974a628f7SDimitry Andric // moved into a new function. We do this conservatively, so if e.g. we cannot
120f73363f1SDimitry Andric // resolve the function in the context (which can happen in case of line-table-
121f73363f1SDimitry Andric // only debug info), we leave the context as is. The trickiest part here is
122f73363f1SDimitry Andric // handling inlined functions -- in this case we need to make sure we look at
123f73363f1SDimitry Andric // the declaration line of the inlined function, NOT the function it was
12474a628f7SDimitry Andric // inlined into.
FilterContexts(SymbolContextList & sc_list)125e3b55780SDimitry Andric void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list) {
126344a3780SDimitry Andric if (m_location_spec.GetExactMatch())
12774a628f7SDimitry Andric return; // Nothing to do. Contexts are precise.
12874a628f7SDimitry Andric
129145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
13074a628f7SDimitry Andric for(uint32_t i = 0; i < sc_list.GetSize(); ++i) {
13174a628f7SDimitry Andric SymbolContext sc;
13274a628f7SDimitry Andric sc_list.GetContextAtIndex(i, sc);
13374a628f7SDimitry Andric if (!sc.block)
13474a628f7SDimitry Andric continue;
13574a628f7SDimitry Andric
13674a628f7SDimitry Andric FileSpec file;
13774a628f7SDimitry Andric uint32_t line;
13874a628f7SDimitry Andric const Block *inline_block = sc.block->GetContainingInlinedBlock();
13974a628f7SDimitry Andric if (inline_block) {
14074a628f7SDimitry Andric const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration();
14174a628f7SDimitry Andric if (!inline_declaration.IsValid())
14274a628f7SDimitry Andric continue;
14374a628f7SDimitry Andric file = inline_declaration.GetFile();
14474a628f7SDimitry Andric line = inline_declaration.GetLine();
14574a628f7SDimitry Andric } else if (sc.function)
14674a628f7SDimitry Andric sc.function->GetStartLineSourceInfo(file, line);
14774a628f7SDimitry Andric else
14874a628f7SDimitry Andric continue;
14974a628f7SDimitry Andric
150ac9a064cSDimitry Andric if (file != sc.line_entry.GetFile()) {
151ac9a064cSDimitry Andric LLDB_LOG(log, "unexpected symbol context file {0}",
152ac9a064cSDimitry Andric sc.line_entry.GetFile());
15374a628f7SDimitry Andric continue;
15474a628f7SDimitry Andric }
15574a628f7SDimitry Andric
15674a628f7SDimitry Andric // Compare the requested line number with the line of the function
15774a628f7SDimitry Andric // declaration. In case of a function declared as:
15874a628f7SDimitry Andric //
15974a628f7SDimitry Andric // int
16074a628f7SDimitry Andric // foo()
16174a628f7SDimitry Andric // {
16274a628f7SDimitry Andric // ...
16374a628f7SDimitry Andric //
16474a628f7SDimitry Andric // the compiler will set the declaration line to the "foo" line, which is
16574a628f7SDimitry Andric // the reason why we have -1 here. This can fail in case of two inline
16674a628f7SDimitry Andric // functions defined back-to-back:
16774a628f7SDimitry Andric //
16874a628f7SDimitry Andric // inline int foo1() { ... }
16974a628f7SDimitry Andric // inline int foo2() { ... }
17074a628f7SDimitry Andric //
17174a628f7SDimitry Andric // but that's the best we can do for now.
17294994d37SDimitry Andric // One complication, if the line number returned from GetStartLineSourceInfo
17394994d37SDimitry Andric // is 0, then we can't do this calculation. That can happen if
17494994d37SDimitry Andric // GetStartLineSourceInfo gets an error, or if the first line number in
17594994d37SDimitry Andric // the function really is 0 - which happens for some languages.
176b60736ecSDimitry Andric
177b60736ecSDimitry Andric // But only do this calculation if the line number we found in the SC
178b60736ecSDimitry Andric // was different from the one requested in the source file. If we actually
179b60736ecSDimitry Andric // found an exact match it must be valid.
180b60736ecSDimitry Andric
181344a3780SDimitry Andric if (m_location_spec.GetLine() == sc.line_entry.line)
182b60736ecSDimitry Andric continue;
183b60736ecSDimitry Andric
18474a628f7SDimitry Andric const int decl_line_is_too_late_fudge = 1;
185344a3780SDimitry Andric if (line &&
186344a3780SDimitry Andric m_location_spec.GetLine() < line - decl_line_is_too_late_fudge) {
18774a628f7SDimitry Andric LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line);
18874a628f7SDimitry Andric sc_list.RemoveContextAtIndex(i);
18974a628f7SDimitry Andric --i;
19074a628f7SDimitry Andric }
19174a628f7SDimitry Andric }
19274a628f7SDimitry Andric }
19374a628f7SDimitry Andric
DeduceSourceMapping(const SymbolContextList & sc_list)194e3b55780SDimitry Andric void BreakpointResolverFileLine::DeduceSourceMapping(
1957fa27ce4SDimitry Andric const SymbolContextList &sc_list) {
196e3b55780SDimitry Andric Target &target = GetBreakpoint()->GetTarget();
197e3b55780SDimitry Andric if (!target.GetAutoSourceMapRelative())
198e3b55780SDimitry Andric return;
199e3b55780SDimitry Andric
200e3b55780SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
201e3b55780SDimitry Andric // Check if "b" is a suffix of "a".
202e3b55780SDimitry Andric // And return std::nullopt if not or the new path
203e3b55780SDimitry Andric // of "a" after consuming "b" from the back.
204e3b55780SDimitry Andric auto check_suffix =
205ac9a064cSDimitry Andric [](llvm::StringRef a, llvm::StringRef b,
206e3b55780SDimitry Andric bool case_sensitive) -> std::optional<llvm::StringRef> {
207e3b55780SDimitry Andric if (case_sensitive ? a.consume_back(b) : a.consume_back_insensitive(b)) {
208ac9a064cSDimitry Andric // Note sc_file_dir and request_file_dir below are normalized
209ac9a064cSDimitry Andric // and always contain the path separator '/'.
210ac9a064cSDimitry Andric if (a.empty() || a.ends_with("/")) {
211e3b55780SDimitry Andric return a;
212e3b55780SDimitry Andric }
213e3b55780SDimitry Andric }
214e3b55780SDimitry Andric return std::nullopt;
215e3b55780SDimitry Andric };
216e3b55780SDimitry Andric
217e3b55780SDimitry Andric FileSpec request_file = m_location_spec.GetFileSpec();
218e3b55780SDimitry Andric
219e3b55780SDimitry Andric // Only auto deduce source map if breakpoint is full path.
220e3b55780SDimitry Andric // Note: an existing source map reverse mapping (m_removed_prefix_opt has
221e3b55780SDimitry Andric // value) may make request_file relative.
222e3b55780SDimitry Andric if (!m_removed_prefix_opt.has_value() && request_file.IsRelative())
223e3b55780SDimitry Andric return;
224e3b55780SDimitry Andric
225e3b55780SDimitry Andric const bool case_sensitive = request_file.IsCaseSensitive();
2267fa27ce4SDimitry Andric for (const SymbolContext &sc : sc_list) {
227ac9a064cSDimitry Andric FileSpec sc_file = sc.line_entry.GetFile();
228e3b55780SDimitry Andric
229e3b55780SDimitry Andric if (FileSpec::Equal(sc_file, request_file, /*full*/ true))
230e3b55780SDimitry Andric continue;
231e3b55780SDimitry Andric
232e3b55780SDimitry Andric llvm::StringRef sc_file_dir = sc_file.GetDirectory().GetStringRef();
233e3b55780SDimitry Andric llvm::StringRef request_file_dir =
234e3b55780SDimitry Andric request_file.GetDirectory().GetStringRef();
235e3b55780SDimitry Andric
236e3b55780SDimitry Andric llvm::StringRef new_mapping_from;
237e3b55780SDimitry Andric llvm::SmallString<256> new_mapping_to;
238e3b55780SDimitry Andric
239e3b55780SDimitry Andric // Adding back any potentially reverse mapping stripped prefix.
240e3b55780SDimitry Andric // for new_mapping_to.
241e3b55780SDimitry Andric if (m_removed_prefix_opt.has_value())
242e3b55780SDimitry Andric llvm::sys::path::append(new_mapping_to, *m_removed_prefix_opt);
243e3b55780SDimitry Andric
244e3b55780SDimitry Andric std::optional<llvm::StringRef> new_mapping_from_opt =
245e3b55780SDimitry Andric check_suffix(sc_file_dir, request_file_dir, case_sensitive);
246e3b55780SDimitry Andric if (new_mapping_from_opt) {
247e3b55780SDimitry Andric new_mapping_from = *new_mapping_from_opt;
248e3b55780SDimitry Andric if (new_mapping_to.empty())
249e3b55780SDimitry Andric new_mapping_to = ".";
250e3b55780SDimitry Andric } else {
251e3b55780SDimitry Andric std::optional<llvm::StringRef> new_mapping_to_opt =
252e3b55780SDimitry Andric check_suffix(request_file_dir, sc_file_dir, case_sensitive);
253e3b55780SDimitry Andric if (new_mapping_to_opt) {
254e3b55780SDimitry Andric new_mapping_from = ".";
255e3b55780SDimitry Andric llvm::sys::path::append(new_mapping_to, *new_mapping_to_opt);
256e3b55780SDimitry Andric }
257e3b55780SDimitry Andric }
258e3b55780SDimitry Andric
259e3b55780SDimitry Andric if (!new_mapping_from.empty() && !new_mapping_to.empty()) {
260e3b55780SDimitry Andric LLDB_LOG(log, "generating auto source map from {0} to {1}",
261e3b55780SDimitry Andric new_mapping_from, new_mapping_to);
262e3b55780SDimitry Andric if (target.GetSourcePathMap().AppendUnique(new_mapping_from,
263e3b55780SDimitry Andric new_mapping_to,
264e3b55780SDimitry Andric /*notify*/ true))
265e3b55780SDimitry Andric target.GetStatistics().IncreaseSourceMapDeduceCount();
266e3b55780SDimitry Andric }
267e3b55780SDimitry Andric }
268e3b55780SDimitry Andric }
269e3b55780SDimitry Andric
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr)270ead24645SDimitry Andric Searcher::CallbackReturn BreakpointResolverFileLine::SearchCallback(
271ead24645SDimitry Andric SearchFilter &filter, SymbolContext &context, Address *addr) {
272f034231aSEd Maste SymbolContextList sc_list;
273f034231aSEd Maste
27414f1b3e8SDimitry Andric // There is a tricky bit here. You can have two compilation units that
275f73363f1SDimitry Andric // #include the same file, and in one of them the function at m_line_number
276f73363f1SDimitry Andric // is used (and so code and a line entry for it is generated) but in the
277f73363f1SDimitry Andric // other it isn't. If we considered the CU's independently, then in the
278f73363f1SDimitry Andric // second inclusion, we'd move the breakpoint to the next function that
279f73363f1SDimitry Andric // actually generated code in the header file. That would end up being
280f73363f1SDimitry Andric // confusing. So instead, we do the CU iterations by hand here, then scan
281f73363f1SDimitry Andric // through the complete list of matches, and figure out the closest line
282f73363f1SDimitry Andric // number match, and only set breakpoints on that match.
283f034231aSEd Maste
284f73363f1SDimitry Andric // Note also that if file_spec only had a file name and not a directory,
285f73363f1SDimitry Andric // there may be many different file spec's in the resultant list. The
286f73363f1SDimitry Andric // closest line match for one will not be right for some totally different
287f73363f1SDimitry Andric // file. So we go through the match list and pull out the sets that have the
288f73363f1SDimitry Andric // same file spec in their line_entry and treat each set separately.
289f73363f1SDimitry Andric
290145449b1SDimitry Andric const uint32_t line = m_location_spec.GetLine().value_or(0);
291e3b55780SDimitry Andric const std::optional<uint16_t> column = m_location_spec.GetColumn();
292f034231aSEd Maste
293f034231aSEd Maste const size_t num_comp_units = context.module_sp->GetNumCompileUnits();
29414f1b3e8SDimitry Andric for (size_t i = 0; i < num_comp_units; i++) {
295f034231aSEd Maste CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i));
29614f1b3e8SDimitry Andric if (cu_sp) {
297f034231aSEd Maste if (filter.CompUnitPasses(*cu_sp))
298e3b55780SDimitry Andric cu_sp->ResolveSymbolContext(m_location_spec, eSymbolContextEverything,
299e3b55780SDimitry Andric sc_list);
300f034231aSEd Maste }
301f034231aSEd Maste }
30274a628f7SDimitry Andric
303e3b55780SDimitry Andric FilterContexts(sc_list);
304e3b55780SDimitry Andric
305e3b55780SDimitry Andric DeduceSourceMapping(sc_list);
30674a628f7SDimitry Andric
307f034231aSEd Maste StreamString s;
308344a3780SDimitry Andric s.Printf("for %s:%d ",
309344a3780SDimitry Andric m_location_spec.GetFileSpec().GetFilename().AsCString("<Unknown>"),
310344a3780SDimitry Andric line);
311f21a844fSEd Maste
312344a3780SDimitry Andric SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString(), line,
313344a3780SDimitry Andric column);
314f034231aSEd Maste
315f034231aSEd Maste return Searcher::eCallbackReturnContinue;
316f034231aSEd Maste }
317f034231aSEd Maste
GetDepth()31894994d37SDimitry Andric lldb::SearchDepth BreakpointResolverFileLine::GetDepth() {
31994994d37SDimitry Andric return lldb::eSearchDepthModule;
320f034231aSEd Maste }
321f034231aSEd Maste
GetDescription(Stream * s)32214f1b3e8SDimitry Andric void BreakpointResolverFileLine::GetDescription(Stream *s) {
323344a3780SDimitry Andric s->Printf("file = '%s', line = %u, ",
324344a3780SDimitry Andric m_location_spec.GetFileSpec().GetPath().c_str(),
325145449b1SDimitry Andric m_location_spec.GetLine().value_or(0));
326344a3780SDimitry Andric auto column = m_location_spec.GetColumn();
327344a3780SDimitry Andric if (column)
328344a3780SDimitry Andric s->Printf("column = %u, ", *column);
329344a3780SDimitry Andric s->Printf("exact_match = %d", m_location_spec.GetExactMatch());
330f034231aSEd Maste }
331f034231aSEd Maste
Dump(Stream * s) const33214f1b3e8SDimitry Andric void BreakpointResolverFileLine::Dump(Stream *s) const {}
333f034231aSEd Maste
334205afe67SEd Maste lldb::BreakpointResolverSP
CopyForBreakpoint(BreakpointSP & breakpoint)335cfca06d7SDimitry Andric BreakpointResolverFileLine::CopyForBreakpoint(BreakpointSP &breakpoint) {
33614f1b3e8SDimitry Andric lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine(
337344a3780SDimitry Andric breakpoint, GetOffset(), m_skip_prologue, m_location_spec));
338205afe67SEd Maste
339205afe67SEd Maste return ret_sp;
340205afe67SEd Maste }
341