Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Module.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/StreamString.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/CompileUnit.h" |
| 21 | #include "lldb/Symbol/Function.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/lldb-private-log.h" |
| 23 | |
| 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
| 27 | //---------------------------------------------------------------------- |
| 28 | // BreakpointResolverFileLine: |
| 29 | //---------------------------------------------------------------------- |
| 30 | BreakpointResolverFileLine::BreakpointResolverFileLine |
| 31 | ( |
| 32 | Breakpoint *bkpt, |
| 33 | const FileSpec &file_spec, |
| 34 | uint32_t line_no, |
Jim Ingham | 2cf5ccb | 2012-05-22 00:12:20 +0000 | [diff] [blame] | 35 | bool check_inlines, |
| 36 | bool skip_prologue |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | ) : |
Johnny Chen | a62ad7c | 2010-10-28 17:27:46 +0000 | [diff] [blame] | 38 | BreakpointResolver (bkpt, BreakpointResolver::FileLineResolver), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | m_file_spec (file_spec), |
| 40 | m_line_number (line_no), |
Jim Ingham | 2cf5ccb | 2012-05-22 00:12:20 +0000 | [diff] [blame] | 41 | m_inlines (check_inlines), |
| 42 | m_skip_prologue(skip_prologue) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | BreakpointResolverFileLine::~BreakpointResolverFileLine () |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | Searcher::CallbackReturn |
| 51 | BreakpointResolverFileLine::SearchCallback |
| 52 | ( |
| 53 | SearchFilter &filter, |
| 54 | SymbolContext &context, |
| 55 | Address *addr, |
| 56 | bool containing |
| 57 | ) |
| 58 | { |
| 59 | SymbolContextList sc_list; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | |
| 61 | assert (m_breakpoint != NULL); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 62 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 63 | |
| 64 | // There is a tricky bit here. You can have two compilation units that #include the same file, and |
| 65 | // 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 |
| 66 | // other it isn't. If we considered the CU's independently, then in the second inclusion, we'd move the breakpoint |
| 67 | // to the next function that actually generated code in the header file. That would end up being confusing. |
| 68 | // So instead, we do the CU iterations by hand here, then scan through the complete list of matches, and figure out |
| 69 | // the closest line number match, and only set breakpoints on that match. |
| 70 | |
| 71 | // Note also that if file_spec only had a file name and not a directory, there may be many different file spec's in |
| 72 | // the resultant list. The closest line match for one will not be right for some totally different file. |
| 73 | // So we go through the match list and pull out the sets that have the same file spec in their line_entry |
| 74 | // and treat each set separately. |
| 75 | |
| 76 | uint32_t num_comp_units = context.module_sp->GetNumCompileUnits(); |
| 77 | for (uint32_t i = 0; i < num_comp_units; i++) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | { |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 79 | CompUnitSP cu_sp (context.module_sp->GetCompileUnitAtIndex (i)); |
Greg Clayton | c705730 | 2012-04-23 22:00:21 +0000 | [diff] [blame] | 80 | if (cu_sp) |
| 81 | { |
| 82 | if (filter.CompUnitPasses(*cu_sp)) |
| 83 | cu_sp->ResolveSymbolContext (m_file_spec, m_line_number, m_inlines, false, eSymbolContextEverything, sc_list); |
| 84 | } |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | while (sc_list.GetSize() > 0) |
| 88 | { |
| 89 | SymbolContextList tmp_sc_list; |
| 90 | int current_idx = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | SymbolContext sc; |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 92 | bool first_entry = true; |
| 93 | |
| 94 | FileSpec match_file_spec; |
| 95 | uint32_t closest_line_number = UINT32_MAX; |
| 96 | |
| 97 | // Pull out the first entry, and all the others that match its file spec, and stuff them in the tmp list. |
| 98 | while (current_idx < sc_list.GetSize()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | { |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 100 | bool matches; |
| 101 | |
| 102 | sc_list.GetContextAtIndex (current_idx, sc); |
| 103 | if (first_entry) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | { |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 105 | match_file_spec = sc.line_entry.file; |
| 106 | matches = true; |
| 107 | first_entry = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | } |
| 109 | else |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 110 | matches = (sc.line_entry.file == match_file_spec); |
| 111 | |
| 112 | if (matches) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | { |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 114 | tmp_sc_list.Append (sc); |
| 115 | sc_list.RemoveContextAtIndex(current_idx); |
| 116 | |
| 117 | // ResolveSymbolContext will always return a number that is >= the line number you pass in. |
| 118 | // So the smaller line number is always better. |
| 119 | if (sc.line_entry.line < closest_line_number) |
| 120 | closest_line_number = sc.line_entry.line; |
| 121 | } |
| 122 | else |
| 123 | current_idx++; |
| 124 | } |
| 125 | |
| 126 | // Okay, we've found the closest line number match, now throw away all the others, |
| 127 | // and make breakpoints out of the closest line number match. |
| 128 | |
| 129 | uint32_t tmp_sc_list_size = tmp_sc_list.GetSize(); |
| 130 | |
| 131 | for (uint32_t i = 0; i < tmp_sc_list_size; i++) |
| 132 | { |
| 133 | SymbolContext sc; |
| 134 | if (tmp_sc_list.GetContextAtIndex(i, sc)) |
| 135 | { |
| 136 | if (sc.line_entry.line == closest_line_number) |
| 137 | { |
| 138 | Address line_start = sc.line_entry.range.GetBaseAddress(); |
| 139 | if (line_start.IsValid()) |
| 140 | { |
| 141 | if (filter.AddressPasses(line_start)) |
| 142 | { |
Jim Ingham | 2cf5ccb | 2012-05-22 00:12:20 +0000 | [diff] [blame] | 143 | // If the line number is before the prologue end, move it there... |
| 144 | bool skipped_prologue = false; |
| 145 | if (m_skip_prologue) |
| 146 | { |
| 147 | if (sc.function) |
| 148 | { |
| 149 | Address prologue_addr(sc.function->GetAddressRange().GetBaseAddress()); |
| 150 | if (prologue_addr.IsValid() && (line_start == prologue_addr)) |
| 151 | { |
| 152 | const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize(); |
| 153 | if (prologue_byte_size) |
| 154 | { |
| 155 | prologue_addr.Slide(prologue_byte_size); |
| 156 | |
| 157 | if (filter.AddressPasses(prologue_addr)) |
| 158 | { |
| 159 | skipped_prologue = true; |
| 160 | line_start = prologue_addr; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 167 | BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(line_start)); |
| 168 | if (log && bp_loc_sp && !m_breakpoint->IsInternal()) |
| 169 | { |
| 170 | StreamString s; |
| 171 | bp_loc_sp->GetDescription (&s, lldb::eDescriptionLevelVerbose); |
Jim Ingham | 2cf5ccb | 2012-05-22 00:12:20 +0000 | [diff] [blame] | 172 | log->Printf ("Added location (skipped prologue: %s): %s \n", skipped_prologue ? "yes" : "no", s.GetData()); |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | else if (log) |
| 176 | { |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 177 | log->Printf ("Breakpoint at file address 0x%" PRIx64 " for %s:%d didn't pass the filter.\n", |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 178 | line_start.GetFileAddress(), |
| 179 | m_file_spec.GetFilename().AsCString("<Unknown>"), |
| 180 | m_line_number); |
| 181 | } |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | if (log) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 186 | log->Printf ("error: Unable to set breakpoint at file address 0x%" PRIx64 " for %s:%d\n", |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 187 | line_start.GetFileAddress(), |
| 188 | m_file_spec.GetFilename().AsCString("<Unknown>"), |
| 189 | m_line_number); |
| 190 | } |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | #if 0 |
| 195 | s << "error: Breakpoint at '" << pos->c_str() << "' isn't resolved yet: \n"; |
| 196 | if (sc.line_entry.address.Dump(&s, Address::DumpStyleSectionNameOffset)) |
| 197 | s.EOL(); |
| 198 | if (sc.line_entry.address.Dump(&s, Address::DumpStyleSectionPointerOffset)) |
| 199 | s.EOL(); |
| 200 | if (sc.line_entry.address.Dump(&s, Address::DumpStyleFileAddress)) |
| 201 | s.EOL(); |
| 202 | if (sc.line_entry.address.Dump(&s, Address::DumpStyleLoadAddress)) |
| 203 | s.EOL(); |
| 204 | #endif |
| 205 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | } |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | return Searcher::eCallbackReturnContinue; |
| 211 | } |
| 212 | |
| 213 | Searcher::Depth |
| 214 | BreakpointResolverFileLine::GetDepth() |
| 215 | { |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 216 | return Searcher::eDepthModule; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void |
| 220 | BreakpointResolverFileLine::GetDescription (Stream *s) |
| 221 | { |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 222 | s->Printf ("file ='%s', line = %u", m_file_spec.GetFilename().AsCString(), m_line_number); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void |
| 226 | BreakpointResolverFileLine::Dump (Stream *s) const |
| 227 | { |
| 228 | |
| 229 | } |
| 230 | |