1 //===-- BreakpointResolver.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/BreakpointResolver.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Address.h"
17 #include "lldb/Breakpoint/Breakpoint.h"
18 #include "lldb/Breakpoint/BreakpointLocation.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/ModuleList.h"
21 #include "lldb/Core/SearchFilter.h"
22 #include "lldb/Core/Stream.h"
23 #include "lldb/Core/StreamString.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Symbol/CompileUnit.h"
27 #include "lldb/Symbol/Function.h"
28 
29 using namespace lldb_private;
30 using namespace lldb;
31 
32 //----------------------------------------------------------------------
33 // BreakpointResolver:
34 //----------------------------------------------------------------------
35 BreakpointResolver::BreakpointResolver (Breakpoint *bkpt, const unsigned char resolverTy) :
36     m_breakpoint (bkpt),
37     SubclassID (resolverTy)
38 {
39 }
40 
41 BreakpointResolver::~BreakpointResolver ()
42 {
43 
44 }
45 
46 void
47 BreakpointResolver::SetBreakpoint (Breakpoint *bkpt)
48 {
49     m_breakpoint = bkpt;
50 }
51 
52 void
53 BreakpointResolver::ResolveBreakpointInModules (SearchFilter &filter, ModuleList &modules)
54 {
55     filter.SearchInModuleList(*this, modules);
56 }
57 
58 void
59 BreakpointResolver::ResolveBreakpoint (SearchFilter &filter)
60 {
61     filter.Search (*this);
62 }
63 
64 void
65 BreakpointResolver::SetSCMatchesByLine (SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue, const char *log_ident)
66 {
67     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
68 
69     while (sc_list.GetSize() > 0)
70     {
71         SymbolContextList tmp_sc_list;
72         unsigned current_idx = 0;
73         SymbolContext sc;
74         bool first_entry = true;
75 
76         FileSpec match_file_spec;
77         uint32_t closest_line_number = UINT32_MAX;
78 
79         // Pull out the first entry, and all the others that match its file spec, and stuff them in the tmp list.
80         while (current_idx < sc_list.GetSize())
81         {
82             bool matches;
83 
84             sc_list.GetContextAtIndex (current_idx, sc);
85             if (first_entry)
86             {
87                 match_file_spec = sc.line_entry.file;
88                 matches = true;
89                 first_entry = false;
90             }
91             else
92                 matches = (sc.line_entry.file == match_file_spec);
93 
94             if (matches)
95             {
96                 tmp_sc_list.Append (sc);
97                 sc_list.RemoveContextAtIndex(current_idx);
98 
99                 // ResolveSymbolContext will always return a number that is >= the line number you pass in.
100                 // So the smaller line number is always better.
101                 if (sc.line_entry.line < closest_line_number)
102                     closest_line_number = sc.line_entry.line;
103             }
104             else
105                 current_idx++;
106         }
107 
108         // Okay, we've found the closest line number match, now throw away all the others:
109 
110         current_idx = 0;
111         while (current_idx < tmp_sc_list.GetSize())
112         {
113             if (tmp_sc_list.GetContextAtIndex(current_idx, sc))
114             {
115                 if (sc.line_entry.line != closest_line_number)
116                     tmp_sc_list.RemoveContextAtIndex(current_idx);
117                 else
118                     current_idx++;
119             }
120         }
121 
122         // Next go through and see if there are line table entries that are contiguous, and if so keep only the
123         // first of the contiguous range:
124 
125         current_idx = 0;
126         std::map<Block *, lldb::addr_t> blocks_with_breakpoints;
127 
128         while (current_idx < tmp_sc_list.GetSize())
129         {
130             if (tmp_sc_list.GetContextAtIndex(current_idx, sc))
131             {
132                 if (blocks_with_breakpoints.find (sc.block) != blocks_with_breakpoints.end())
133                     tmp_sc_list.RemoveContextAtIndex(current_idx);
134                 else
135                 {
136                     blocks_with_breakpoints.insert (std::pair<Block *, lldb::addr_t>(sc.block, sc.line_entry.range.GetBaseAddress().GetFileAddress()));
137                     current_idx++;
138                 }
139             }
140         }
141 
142         // and make breakpoints out of the closest line number match.
143 
144         uint32_t tmp_sc_list_size = tmp_sc_list.GetSize();
145 
146         for (uint32_t i = 0; i < tmp_sc_list_size; i++)
147         {
148             if (tmp_sc_list.GetContextAtIndex(i, sc))
149             {
150                 Address line_start = sc.line_entry.range.GetBaseAddress();
151                 if (line_start.IsValid())
152                 {
153                     if (filter.AddressPasses(line_start))
154                     {
155                         // If the line number is before the prologue end, move it there...
156                         bool skipped_prologue = false;
157                         if (skip_prologue)
158                         {
159                             if (sc.function)
160                             {
161                                 Address prologue_addr(sc.function->GetAddressRange().GetBaseAddress());
162                                 if (prologue_addr.IsValid() && (line_start == prologue_addr))
163                                 {
164                                     const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
165                                     if (prologue_byte_size)
166                                     {
167                                         prologue_addr.Slide(prologue_byte_size);
168 
169                                         if (filter.AddressPasses(prologue_addr))
170                                         {
171                                             skipped_prologue = true;
172                                             line_start = prologue_addr;
173                                         }
174                                     }
175                                 }
176                             }
177                         }
178 
179                         BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(line_start));
180                         if (log && bp_loc_sp && !m_breakpoint->IsInternal())
181                         {
182                             StreamString s;
183                             bp_loc_sp->GetDescription (&s, lldb::eDescriptionLevelVerbose);
184                             log->Printf ("Added location (skipped prologue: %s): %s \n", skipped_prologue ? "yes" : "no", s.GetData());
185                         }
186                     }
187                     else if (log)
188                     {
189                         log->Printf ("Breakpoint %s at file address 0x%" PRIx64 " didn't pass the filter.\n",
190                                      log_ident ? log_ident : "",
191                                      line_start.GetFileAddress());
192                     }
193                 }
194                 else
195                 {
196                     if (log)
197                         log->Printf ("error: Unable to set breakpoint %s at file address 0x%" PRIx64 "\n",
198                                      log_ident ? log_ident : "",
199                                      line_start.GetFileAddress());
200                 }
201             }
202         }
203     }
204 }
205