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 (
31     Breakpoint *bkpt,
32     const FileSpec &file_spec,
33     uint32_t line_no,
34     bool check_inlines,
35     bool skip_prologue
36 ) :
37     BreakpointResolver (bkpt, BreakpointResolver::FileLineResolver),
38     m_file_spec (file_spec),
39     m_line_number (line_no),
40     m_inlines (check_inlines),
41     m_skip_prologue(skip_prologue)
42 {
43 }
44 
45 BreakpointResolverFileLine::~BreakpointResolverFileLine ()
46 {
47 }
48 
49 Searcher::CallbackReturn
50 BreakpointResolverFileLine::SearchCallback
51 (
52     SearchFilter &filter,
53     SymbolContext &context,
54     Address *addr,
55     bool containing
56 )
57 {
58     SymbolContextList sc_list;
59 
60     assert (m_breakpoint != NULL);
61 
62     // There is a tricky bit here.  You can have two compilation units that #include the same file, and
63     // in one of them the function at m_line_number is used (and so code and a line entry for it is generated) but in the
64     // other it isn't.  If we considered the CU's independently, then in the second inclusion, we'd move the breakpoint
65     // to the next function that actually generated code in the header file.  That would end up being confusing.
66     // So instead, we do the CU iterations by hand here, then scan through the complete list of matches, and figure out
67     // the closest line number match, and only set breakpoints on that match.
68 
69     // Note also that if file_spec only had a file name and not a directory, there may be many different file spec's in
70     // the resultant list.  The closest line match for one will not be right for some totally different file.
71     // So we go through the match list and pull out the sets that have the same file spec in their line_entry
72     // and treat each set separately.
73 
74     const size_t num_comp_units = context.module_sp->GetNumCompileUnits();
75     for (size_t i = 0; i < num_comp_units; i++)
76     {
77         CompUnitSP cu_sp (context.module_sp->GetCompileUnitAtIndex (i));
78         if (cu_sp)
79         {
80             if (filter.CompUnitPasses(*cu_sp))
81                 cu_sp->ResolveSymbolContext (m_file_spec, m_line_number, m_inlines, false, eSymbolContextEverything, sc_list);
82         }
83     }
84     StreamString s;
85     s.Printf ("for %s:%d ",
86                         m_file_spec.GetFilename().AsCString("<Unknown>"),
87                         m_line_number);
88 
89     SetSCMatchesByLine (filter, sc_list, m_skip_prologue, s.GetData());
90 
91     return Searcher::eCallbackReturnContinue;
92 }
93 
94 Searcher::Depth
95 BreakpointResolverFileLine::GetDepth()
96 {
97     return Searcher::eDepthModule;
98 }
99 
100 void
101 BreakpointResolverFileLine::GetDescription (Stream *s)
102 {
103     s->Printf ("file = '%s', line = %u", m_file_spec.GetPath().c_str(), m_line_number);
104 }
105 
106 void
107 BreakpointResolverFileLine::Dump (Stream *s) const
108 {
109 
110 }
111 
112 lldb::BreakpointResolverSP
113 BreakpointResolverFileLine::CopyForBreakpoint (Breakpoint &breakpoint)
114 {
115     lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine(&breakpoint,
116                                                                      m_file_spec,
117                                                                      m_line_number,
118                                                                      m_inlines,
119                                                                      m_skip_prologue));
120 
121     return ret_sp;
122 }
123