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