blob: ca24377077dcadd3124d70301617d451dd1d027e [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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/StreamString.h"
19#include "lldb/lldb-private-log.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24//----------------------------------------------------------------------
25// BreakpointResolverFileLine:
26//----------------------------------------------------------------------
27BreakpointResolverFileLine::BreakpointResolverFileLine
28(
29 Breakpoint *bkpt,
30 const FileSpec &file_spec,
31 uint32_t line_no,
32 bool check_inlines
33) :
Johnny Chena62ad7c2010-10-28 17:27:46 +000034 BreakpointResolver (bkpt, BreakpointResolver::FileLineResolver),
Chris Lattner24943d22010-06-08 16:52:24 +000035 m_file_spec (file_spec),
36 m_line_number (line_no),
37 m_inlines (check_inlines)
38{
39}
40
41BreakpointResolverFileLine::~BreakpointResolverFileLine ()
42{
43}
44
45Searcher::CallbackReturn
46BreakpointResolverFileLine::SearchCallback
47(
48 SearchFilter &filter,
49 SymbolContext &context,
50 Address *addr,
51 bool containing
52)
53{
54 SymbolContextList sc_list;
Chris Lattner24943d22010-06-08 16:52:24 +000055
56 assert (m_breakpoint != NULL);
Greg Claytone005f2c2010-11-06 01:53:30 +000057 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Jim Inghama44297a2012-01-13 02:04:05 +000058
59 // There is a tricky bit here. You can have two compilation units that #include the same file, and
60 // 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
61 // other it isn't. If we considered the CU's independently, then in the second inclusion, we'd move the breakpoint
62 // to the next function that actually generated code in the header file. That would end up being confusing.
63 // So instead, we do the CU iterations by hand here, then scan through the complete list of matches, and figure out
64 // the closest line number match, and only set breakpoints on that match.
65
66 // Note also that if file_spec only had a file name and not a directory, there may be many different file spec's in
67 // the resultant list. The closest line match for one will not be right for some totally different file.
68 // So we go through the match list and pull out the sets that have the same file spec in their line_entry
69 // and treat each set separately.
70
71 uint32_t num_comp_units = context.module_sp->GetNumCompileUnits();
72 for (uint32_t i = 0; i < num_comp_units; i++)
Chris Lattner24943d22010-06-08 16:52:24 +000073 {
Jim Inghama44297a2012-01-13 02:04:05 +000074 CompUnitSP cu_sp (context.module_sp->GetCompileUnitAtIndex (i));
Greg Claytonc7057302012-04-23 22:00:21 +000075 if (cu_sp)
76 {
77 if (filter.CompUnitPasses(*cu_sp))
78 cu_sp->ResolveSymbolContext (m_file_spec, m_line_number, m_inlines, false, eSymbolContextEverything, sc_list);
79 }
Jim Inghama44297a2012-01-13 02:04:05 +000080 }
81
82 while (sc_list.GetSize() > 0)
83 {
84 SymbolContextList tmp_sc_list;
85 int current_idx = 0;
Chris Lattner24943d22010-06-08 16:52:24 +000086 SymbolContext sc;
Jim Inghama44297a2012-01-13 02:04:05 +000087 bool first_entry = true;
88
89 FileSpec match_file_spec;
90 uint32_t closest_line_number = UINT32_MAX;
91
92 // Pull out the first entry, and all the others that match its file spec, and stuff them in the tmp list.
93 while (current_idx < sc_list.GetSize())
Chris Lattner24943d22010-06-08 16:52:24 +000094 {
Jim Inghama44297a2012-01-13 02:04:05 +000095 bool matches;
96
97 sc_list.GetContextAtIndex (current_idx, sc);
98 if (first_entry)
Chris Lattner24943d22010-06-08 16:52:24 +000099 {
Jim Inghama44297a2012-01-13 02:04:05 +0000100 match_file_spec = sc.line_entry.file;
101 matches = true;
102 first_entry = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000103 }
104 else
Jim Inghama44297a2012-01-13 02:04:05 +0000105 matches = (sc.line_entry.file == match_file_spec);
106
107 if (matches)
Chris Lattner24943d22010-06-08 16:52:24 +0000108 {
Jim Inghama44297a2012-01-13 02:04:05 +0000109 tmp_sc_list.Append (sc);
110 sc_list.RemoveContextAtIndex(current_idx);
111
112 // ResolveSymbolContext will always return a number that is >= the line number you pass in.
113 // So the smaller line number is always better.
114 if (sc.line_entry.line < closest_line_number)
115 closest_line_number = sc.line_entry.line;
116 }
117 else
118 current_idx++;
119 }
120
121 // Okay, we've found the closest line number match, now throw away all the others,
122 // and make breakpoints out of the closest line number match.
123
124 uint32_t tmp_sc_list_size = tmp_sc_list.GetSize();
125
126 for (uint32_t i = 0; i < tmp_sc_list_size; i++)
127 {
128 SymbolContext sc;
129 if (tmp_sc_list.GetContextAtIndex(i, sc))
130 {
131 if (sc.line_entry.line == closest_line_number)
132 {
133 Address line_start = sc.line_entry.range.GetBaseAddress();
134 if (line_start.IsValid())
135 {
136 if (filter.AddressPasses(line_start))
137 {
138 BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(line_start));
139 if (log && bp_loc_sp && !m_breakpoint->IsInternal())
140 {
141 StreamString s;
142 bp_loc_sp->GetDescription (&s, lldb::eDescriptionLevelVerbose);
143 log->Printf ("Added location: %s\n", s.GetData());
144 }
145 }
146 else if (log)
147 {
148 log->Printf ("Breakpoint at file address 0x%llx for %s:%d didn't pass the filter.\n",
149 line_start.GetFileAddress(),
150 m_file_spec.GetFilename().AsCString("<Unknown>"),
151 m_line_number);
152 }
153 }
154 else
155 {
156 if (log)
157 log->Printf ("error: Unable to set breakpoint at file address 0x%llx for %s:%d\n",
158 line_start.GetFileAddress(),
159 m_file_spec.GetFilename().AsCString("<Unknown>"),
160 m_line_number);
161 }
162 }
163 else
164 {
165 #if 0
166 s << "error: Breakpoint at '" << pos->c_str() << "' isn't resolved yet: \n";
167 if (sc.line_entry.address.Dump(&s, Address::DumpStyleSectionNameOffset))
168 s.EOL();
169 if (sc.line_entry.address.Dump(&s, Address::DumpStyleSectionPointerOffset))
170 s.EOL();
171 if (sc.line_entry.address.Dump(&s, Address::DumpStyleFileAddress))
172 s.EOL();
173 if (sc.line_entry.address.Dump(&s, Address::DumpStyleLoadAddress))
174 s.EOL();
175 #endif
176 }
Chris Lattner24943d22010-06-08 16:52:24 +0000177 }
178 }
Chris Lattner24943d22010-06-08 16:52:24 +0000179 }
Jim Inghama44297a2012-01-13 02:04:05 +0000180
Chris Lattner24943d22010-06-08 16:52:24 +0000181 return Searcher::eCallbackReturnContinue;
182}
183
184Searcher::Depth
185BreakpointResolverFileLine::GetDepth()
186{
Jim Inghama44297a2012-01-13 02:04:05 +0000187 return Searcher::eDepthModule;
Chris Lattner24943d22010-06-08 16:52:24 +0000188}
189
190void
191BreakpointResolverFileLine::GetDescription (Stream *s)
192{
Greg Clayton12bec712010-06-28 21:30:43 +0000193 s->Printf ("file ='%s', line = %u", m_file_spec.GetFilename().AsCString(), m_line_number);
Chris Lattner24943d22010-06-08 16:52:24 +0000194}
195
196void
197BreakpointResolverFileLine::Dump (Stream *s) const
198{
199
200}
201