130fdc8d8SChris Lattner //===-- BreakpointResolverFileLine.cpp --------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1530fdc8d8SChris Lattner // Project includes
1630fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocation.h"
1730fdc8d8SChris Lattner #include "lldb/Core/Log.h"
181f746071SGreg Clayton #include "lldb/Core/Module.h"
1930fdc8d8SChris Lattner #include "lldb/Core/StreamString.h"
201f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
211f746071SGreg Clayton #include "lldb/Symbol/Function.h"
2230fdc8d8SChris Lattner #include "lldb/lldb-private-log.h"
2330fdc8d8SChris Lattner 
2430fdc8d8SChris Lattner using namespace lldb;
2530fdc8d8SChris Lattner using namespace lldb_private;
2630fdc8d8SChris Lattner 
2730fdc8d8SChris Lattner //----------------------------------------------------------------------
2830fdc8d8SChris Lattner // BreakpointResolverFileLine:
2930fdc8d8SChris Lattner //----------------------------------------------------------------------
3030fdc8d8SChris Lattner BreakpointResolverFileLine::BreakpointResolverFileLine
3130fdc8d8SChris Lattner (
3230fdc8d8SChris Lattner     Breakpoint *bkpt,
3330fdc8d8SChris Lattner     const FileSpec &file_spec,
3430fdc8d8SChris Lattner     uint32_t line_no,
35a8558b62SJim Ingham     bool check_inlines,
36a8558b62SJim Ingham     bool skip_prologue
3730fdc8d8SChris Lattner ) :
38b7234e40SJohnny Chen     BreakpointResolver (bkpt, BreakpointResolver::FileLineResolver),
3930fdc8d8SChris Lattner     m_file_spec (file_spec),
4030fdc8d8SChris Lattner     m_line_number (line_no),
41a8558b62SJim Ingham     m_inlines (check_inlines),
42a8558b62SJim Ingham     m_skip_prologue(skip_prologue)
4330fdc8d8SChris Lattner {
4430fdc8d8SChris Lattner }
4530fdc8d8SChris Lattner 
4630fdc8d8SChris Lattner BreakpointResolverFileLine::~BreakpointResolverFileLine ()
4730fdc8d8SChris Lattner {
4830fdc8d8SChris Lattner }
4930fdc8d8SChris Lattner 
5030fdc8d8SChris Lattner Searcher::CallbackReturn
5130fdc8d8SChris Lattner BreakpointResolverFileLine::SearchCallback
5230fdc8d8SChris Lattner (
5330fdc8d8SChris Lattner     SearchFilter &filter,
5430fdc8d8SChris Lattner     SymbolContext &context,
5530fdc8d8SChris Lattner     Address *addr,
5630fdc8d8SChris Lattner     bool containing
5730fdc8d8SChris Lattner )
5830fdc8d8SChris Lattner {
5930fdc8d8SChris Lattner     SymbolContextList sc_list;
6030fdc8d8SChris Lattner 
6130fdc8d8SChris Lattner     assert (m_breakpoint != NULL);
622d4edfbcSGreg Clayton     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
6330fdc8d8SChris Lattner 
64bc2f9182SJim Ingham     // There is a tricky bit here.  You can have two compilation units that #include the same file, and
65bc2f9182SJim Ingham     // 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
66bc2f9182SJim Ingham     // other it isn't.  If we considered the CU's independently, then in the second inclusion, we'd move the breakpoint
67bc2f9182SJim Ingham     // to the next function that actually generated code in the header file.  That would end up being confusing.
68bc2f9182SJim Ingham     // So instead, we do the CU iterations by hand here, then scan through the complete list of matches, and figure out
69bc2f9182SJim Ingham     // the closest line number match, and only set breakpoints on that match.
70bc2f9182SJim Ingham 
71bc2f9182SJim Ingham     // Note also that if file_spec only had a file name and not a directory, there may be many different file spec's in
72bc2f9182SJim Ingham     // the resultant list.  The closest line match for one will not be right for some totally different file.
73bc2f9182SJim Ingham     // So we go through the match list and pull out the sets that have the same file spec in their line_entry
74bc2f9182SJim Ingham     // and treat each set separately.
75bc2f9182SJim Ingham 
76bc2f9182SJim Ingham     uint32_t num_comp_units = context.module_sp->GetNumCompileUnits();
77bc2f9182SJim Ingham     for (uint32_t i = 0; i < num_comp_units; i++)
78bc2f9182SJim Ingham     {
79bc2f9182SJim Ingham         CompUnitSP cu_sp (context.module_sp->GetCompileUnitAtIndex (i));
802dafd8edSGreg Clayton         if (cu_sp)
812dafd8edSGreg Clayton         {
822dafd8edSGreg Clayton             if (filter.CompUnitPasses(*cu_sp))
83bc2f9182SJim Ingham                 cu_sp->ResolveSymbolContext (m_file_spec, m_line_number, m_inlines, false, eSymbolContextEverything, sc_list);
84bc2f9182SJim Ingham         }
852dafd8edSGreg Clayton     }
86bc2f9182SJim Ingham 
87bc2f9182SJim Ingham     while (sc_list.GetSize() > 0)
88bc2f9182SJim Ingham     {
89bc2f9182SJim Ingham         SymbolContextList tmp_sc_list;
90bc2f9182SJim Ingham         int current_idx = 0;
91bc2f9182SJim Ingham         SymbolContext sc;
92bc2f9182SJim Ingham         bool first_entry = true;
93bc2f9182SJim Ingham 
94bc2f9182SJim Ingham         FileSpec match_file_spec;
95bc2f9182SJim Ingham         uint32_t closest_line_number = UINT32_MAX;
96bc2f9182SJim Ingham 
97bc2f9182SJim Ingham         // Pull out the first entry, and all the others that match its file spec, and stuff them in the tmp list.
98bc2f9182SJim Ingham         while (current_idx < sc_list.GetSize())
99bc2f9182SJim Ingham         {
100bc2f9182SJim Ingham             bool matches;
101bc2f9182SJim Ingham 
102bc2f9182SJim Ingham             sc_list.GetContextAtIndex (current_idx, sc);
103bc2f9182SJim Ingham             if (first_entry)
104bc2f9182SJim Ingham             {
105bc2f9182SJim Ingham                 match_file_spec = sc.line_entry.file;
106bc2f9182SJim Ingham                 matches = true;
107bc2f9182SJim Ingham                 first_entry = false;
108bc2f9182SJim Ingham             }
109bc2f9182SJim Ingham             else
110bc2f9182SJim Ingham                 matches = (sc.line_entry.file == match_file_spec);
111bc2f9182SJim Ingham 
112bc2f9182SJim Ingham             if (matches)
113bc2f9182SJim Ingham             {
114bc2f9182SJim Ingham                 tmp_sc_list.Append (sc);
115bc2f9182SJim Ingham                 sc_list.RemoveContextAtIndex(current_idx);
116bc2f9182SJim Ingham 
117bc2f9182SJim Ingham                 // ResolveSymbolContext will always return a number that is >= the line number you pass in.
118bc2f9182SJim Ingham                 // So the smaller line number is always better.
119bc2f9182SJim Ingham                 if (sc.line_entry.line < closest_line_number)
120bc2f9182SJim Ingham                     closest_line_number = sc.line_entry.line;
121bc2f9182SJim Ingham             }
122bc2f9182SJim Ingham             else
123bc2f9182SJim Ingham                 current_idx++;
124bc2f9182SJim Ingham         }
125bc2f9182SJim Ingham 
126bc2f9182SJim Ingham         // Okay, we've found the closest line number match, now throw away all the others,
127bc2f9182SJim Ingham         // and make breakpoints out of the closest line number match.
128bc2f9182SJim Ingham 
129bc2f9182SJim Ingham         uint32_t tmp_sc_list_size = tmp_sc_list.GetSize();
130bc2f9182SJim Ingham 
131bc2f9182SJim Ingham         for (uint32_t i = 0; i < tmp_sc_list_size; i++)
13230fdc8d8SChris Lattner         {
13330fdc8d8SChris Lattner             SymbolContext sc;
134bc2f9182SJim Ingham             if (tmp_sc_list.GetContextAtIndex(i, sc))
135bc2f9182SJim Ingham             {
136bc2f9182SJim Ingham                 if (sc.line_entry.line == closest_line_number)
13730fdc8d8SChris Lattner                 {
13830fdc8d8SChris Lattner                     Address line_start = sc.line_entry.range.GetBaseAddress();
13930fdc8d8SChris Lattner                     if (line_start.IsValid())
14030fdc8d8SChris Lattner                     {
141969795f1SJim Ingham                         if (filter.AddressPasses(line_start))
142969795f1SJim Ingham                         {
143a8558b62SJim Ingham                             // If the line number is before the prologue end, move it there...
144a8558b62SJim Ingham                             bool skipped_prologue = false;
145a8558b62SJim Ingham                             if (m_skip_prologue)
146a8558b62SJim Ingham                             {
147a8558b62SJim Ingham                                 if (sc.function)
148a8558b62SJim Ingham                                 {
149a8558b62SJim Ingham                                     Address prologue_addr(sc.function->GetAddressRange().GetBaseAddress());
150a8558b62SJim Ingham                                     if (prologue_addr.IsValid() && (line_start == prologue_addr))
151a8558b62SJim Ingham                                     {
152a8558b62SJim Ingham                                         const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
153a8558b62SJim Ingham                                         if (prologue_byte_size)
154a8558b62SJim Ingham                                         {
155a8558b62SJim Ingham                                             prologue_addr.Slide(prologue_byte_size);
156a8558b62SJim Ingham 
157a8558b62SJim Ingham                                             if (filter.AddressPasses(prologue_addr))
158a8558b62SJim Ingham                                             {
159a8558b62SJim Ingham                                                 skipped_prologue = true;
160a8558b62SJim Ingham                                                 line_start = prologue_addr;
161a8558b62SJim Ingham                                             }
162a8558b62SJim Ingham                                         }
163a8558b62SJim Ingham                                     }
164a8558b62SJim Ingham                                 }
165a8558b62SJim Ingham                             }
166a8558b62SJim Ingham 
16730fdc8d8SChris Lattner                             BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(line_start));
16830fdc8d8SChris Lattner                             if (log && bp_loc_sp && !m_breakpoint->IsInternal())
16930fdc8d8SChris Lattner                             {
17030fdc8d8SChris Lattner                                 StreamString s;
17130fdc8d8SChris Lattner                                 bp_loc_sp->GetDescription (&s, lldb::eDescriptionLevelVerbose);
172a8558b62SJim Ingham                                 log->Printf ("Added location (skipped prologue: %s): %s \n", skipped_prologue ? "yes" : "no", s.GetData());
17330fdc8d8SChris Lattner                             }
17430fdc8d8SChris Lattner                         }
175969795f1SJim Ingham                         else if (log)
176969795f1SJim Ingham                         {
177*d01b2953SDaniel Malea                             log->Printf ("Breakpoint at file address 0x%" PRIx64 " for %s:%d didn't pass the filter.\n",
178969795f1SJim Ingham                                          line_start.GetFileAddress(),
179969795f1SJim Ingham                                          m_file_spec.GetFilename().AsCString("<Unknown>"),
180969795f1SJim Ingham                                          m_line_number);
181969795f1SJim Ingham                         }
182969795f1SJim Ingham                     }
18330fdc8d8SChris Lattner                     else
18430fdc8d8SChris Lattner                     {
18530fdc8d8SChris Lattner                         if (log)
186*d01b2953SDaniel Malea                             log->Printf ("error: Unable to set breakpoint at file address 0x%" PRIx64 " for %s:%d\n",
18730fdc8d8SChris Lattner                                          line_start.GetFileAddress(),
18830fdc8d8SChris Lattner                                          m_file_spec.GetFilename().AsCString("<Unknown>"),
18930fdc8d8SChris Lattner                                          m_line_number);
19030fdc8d8SChris Lattner                     }
19130fdc8d8SChris Lattner                 }
19230fdc8d8SChris Lattner                 else
19330fdc8d8SChris Lattner                 {
19430fdc8d8SChris Lattner         #if 0
19530fdc8d8SChris Lattner                     s << "error: Breakpoint at '" << pos->c_str() << "' isn't resolved yet: \n";
19630fdc8d8SChris Lattner                     if (sc.line_entry.address.Dump(&s, Address::DumpStyleSectionNameOffset))
19730fdc8d8SChris Lattner                         s.EOL();
19830fdc8d8SChris Lattner                     if (sc.line_entry.address.Dump(&s, Address::DumpStyleSectionPointerOffset))
19930fdc8d8SChris Lattner                         s.EOL();
20030fdc8d8SChris Lattner                     if (sc.line_entry.address.Dump(&s, Address::DumpStyleFileAddress))
20130fdc8d8SChris Lattner                         s.EOL();
20230fdc8d8SChris Lattner                     if (sc.line_entry.address.Dump(&s, Address::DumpStyleLoadAddress))
20330fdc8d8SChris Lattner                         s.EOL();
20430fdc8d8SChris Lattner         #endif
20530fdc8d8SChris Lattner                 }
20630fdc8d8SChris Lattner             }
207bc2f9182SJim Ingham         }
208bc2f9182SJim Ingham     }
209bc2f9182SJim Ingham 
21030fdc8d8SChris Lattner     return Searcher::eCallbackReturnContinue;
21130fdc8d8SChris Lattner }
21230fdc8d8SChris Lattner 
21330fdc8d8SChris Lattner Searcher::Depth
21430fdc8d8SChris Lattner BreakpointResolverFileLine::GetDepth()
21530fdc8d8SChris Lattner {
216bc2f9182SJim Ingham     return Searcher::eDepthModule;
21730fdc8d8SChris Lattner }
21830fdc8d8SChris Lattner 
21930fdc8d8SChris Lattner void
22030fdc8d8SChris Lattner BreakpointResolverFileLine::GetDescription (Stream *s)
22130fdc8d8SChris Lattner {
2220c5cd90dSGreg Clayton     s->Printf ("file ='%s', line = %u", m_file_spec.GetFilename().AsCString(), m_line_number);
22330fdc8d8SChris Lattner }
22430fdc8d8SChris Lattner 
22530fdc8d8SChris Lattner void
22630fdc8d8SChris Lattner BreakpointResolverFileLine::Dump (Stream *s) const
22730fdc8d8SChris Lattner {
22830fdc8d8SChris Lattner 
22930fdc8d8SChris Lattner }
23030fdc8d8SChris Lattner 
231