1 //===-- BreakpointResolverFileLine.cpp --------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Breakpoint/BreakpointLocation.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/StreamString.h"
20 #include "lldb/Symbol/CompileUnit.h"
21 #include "lldb/Symbol/Function.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 //----------------------------------------------------------------------
27 // BreakpointResolverFileLine:
28 //----------------------------------------------------------------------
29 BreakpointResolverFileLine::BreakpointResolverFileLine(
30     Breakpoint *bkpt, const FileSpec &file_spec, uint32_t line_no,
31     lldb::addr_t offset, bool check_inlines, bool skip_prologue,
32     bool exact_match)
33     : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver, offset),
34       m_file_spec(file_spec), m_line_number(line_no), m_inlines(check_inlines),
35       m_skip_prologue(skip_prologue), m_exact_match(exact_match) {}
36 
37 BreakpointResolverFileLine::~BreakpointResolverFileLine() {}
38 
39 Searcher::CallbackReturn
40 BreakpointResolverFileLine::SearchCallback(SearchFilter &filter,
41                                            SymbolContext &context,
42                                            Address *addr, bool containing) {
43   SymbolContextList sc_list;
44 
45   assert(m_breakpoint != NULL);
46 
47   // There is a tricky bit here.  You can have two compilation units that
48   // #include the same file, and
49   // in one of them the function at m_line_number is used (and so code and a
50   // line entry for it is generated) but in the
51   // other it isn't.  If we considered the CU's independently, then in the
52   // second inclusion, we'd move the breakpoint
53   // to the next function that actually generated code in the header file.  That
54   // would end up being confusing.
55   // So instead, we do the CU iterations by hand here, then scan through the
56   // complete list of matches, and figure out
57   // the closest line number match, and only set breakpoints on that match.
58 
59   // Note also that if file_spec only had a file name and not a directory, there
60   // may be many different file spec's in
61   // the resultant list.  The closest line match for one will not be right for
62   // some totally different file.
63   // So we go through the match list and pull out the sets that have the same
64   // file spec in their line_entry
65   // and treat each set separately.
66 
67   const size_t num_comp_units = context.module_sp->GetNumCompileUnits();
68   for (size_t i = 0; i < num_comp_units; i++) {
69     CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i));
70     if (cu_sp) {
71       if (filter.CompUnitPasses(*cu_sp))
72         cu_sp->ResolveSymbolContext(m_file_spec, m_line_number, m_inlines,
73                                     m_exact_match, eSymbolContextEverything,
74                                     sc_list);
75     }
76   }
77   StreamString s;
78   s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"),
79            m_line_number);
80 
81   SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetData());
82 
83   return Searcher::eCallbackReturnContinue;
84 }
85 
86 Searcher::Depth BreakpointResolverFileLine::GetDepth() {
87   return Searcher::eDepthModule;
88 }
89 
90 void BreakpointResolverFileLine::GetDescription(Stream *s) {
91   s->Printf("file = '%s', line = %u, exact_match = %d",
92             m_file_spec.GetPath().c_str(), m_line_number, m_exact_match);
93 }
94 
95 void BreakpointResolverFileLine::Dump(Stream *s) const {}
96 
97 lldb::BreakpointResolverSP
98 BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) {
99   lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine(
100       &breakpoint, m_file_spec, m_line_number, m_offset, m_inlines,
101       m_skip_prologue, m_exact_match));
102 
103   return ret_sp;
104 }
105