1 //===-- AddressResolverFileLine.cpp ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Core/AddressResolverFileLine.h"
10 
11 #include "lldb/Core/Address.h"
12 #include "lldb/Core/AddressRange.h"
13 #include "lldb/Symbol/CompileUnit.h"
14 #include "lldb/Symbol/LineEntry.h"
15 #include "lldb/Symbol/SymbolContext.h"
16 #include "lldb/Utility/ConstString.h"
17 #include "lldb/Utility/LLDBLog.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/Utility/Stream.h"
20 #include "lldb/Utility/StreamString.h"
21 #include "lldb/lldb-enumerations.h"
22 #include "lldb/lldb-types.h"
23 
24 #include <cinttypes>
25 #include <vector>
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 
30 // AddressResolverFileLine:
AddressResolverFileLine(SourceLocationSpec location_spec)31 AddressResolverFileLine::AddressResolverFileLine(
32     SourceLocationSpec location_spec)
33     : AddressResolver(), m_src_location_spec(location_spec) {}
34 
35 AddressResolverFileLine::~AddressResolverFileLine() = default;
36 
37 Searcher::CallbackReturn
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr)38 AddressResolverFileLine::SearchCallback(SearchFilter &filter,
39                                         SymbolContext &context, Address *addr) {
40   SymbolContextList sc_list;
41   CompileUnit *cu = context.comp_unit;
42 
43   Log *log = GetLog(LLDBLog::Breakpoints);
44 
45   // TODO: Handle SourceLocationSpec column information
46   cu->ResolveSymbolContext(m_src_location_spec, eSymbolContextEverything,
47                            sc_list);
48   for (const SymbolContext &sc : sc_list) {
49     Address line_start = sc.line_entry.range.GetBaseAddress();
50     addr_t byte_size = sc.line_entry.range.GetByteSize();
51     if (line_start.IsValid()) {
52       AddressRange new_range(line_start, byte_size);
53       m_address_ranges.push_back(new_range);
54     } else {
55       LLDB_LOGF(log,
56                 "error: Unable to resolve address at file address 0x%" PRIx64
57                 " for %s:%d\n",
58                 line_start.GetFileAddress(),
59                 m_src_location_spec.GetFileSpec().GetFilename().AsCString(
60                     "<Unknown>"),
61                 m_src_location_spec.GetLine().value_or(0));
62     }
63   }
64   return Searcher::eCallbackReturnContinue;
65 }
66 
GetDepth()67 lldb::SearchDepth AddressResolverFileLine::GetDepth() {
68   return lldb::eSearchDepthCompUnit;
69 }
70 
GetDescription(Stream * s)71 void AddressResolverFileLine::GetDescription(Stream *s) {
72   s->Printf(
73       "File and line address - file: \"%s\" line: %u",
74       m_src_location_spec.GetFileSpec().GetFilename().AsCString("<Unknown>"),
75       m_src_location_spec.GetLine().value_or(0));
76 }
77