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" |
| 18 | #include "lldb/Core/StreamString.h" |
| 19 | #include "lldb/lldb-private-log.h" |
| 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | //---------------------------------------------------------------------- |
| 25 | // BreakpointResolverFileLine: |
| 26 | //---------------------------------------------------------------------- |
| 27 | BreakpointResolverFileLine::BreakpointResolverFileLine |
| 28 | ( |
| 29 | Breakpoint *bkpt, |
| 30 | const FileSpec &file_spec, |
| 31 | uint32_t line_no, |
| 32 | bool check_inlines |
| 33 | ) : |
Johnny Chen | a62ad7c | 2010-10-28 17:27:46 +0000 | [diff] [blame] | 34 | BreakpointResolver (bkpt, BreakpointResolver::FileLineResolver), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | m_file_spec (file_spec), |
| 36 | m_line_number (line_no), |
| 37 | m_inlines (check_inlines) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | BreakpointResolverFileLine::~BreakpointResolverFileLine () |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | Searcher::CallbackReturn |
| 46 | BreakpointResolverFileLine::SearchCallback |
| 47 | ( |
| 48 | SearchFilter &filter, |
| 49 | SymbolContext &context, |
| 50 | Address *addr, |
| 51 | bool containing |
| 52 | ) |
| 53 | { |
| 54 | SymbolContextList sc_list; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | |
| 56 | assert (m_breakpoint != NULL); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 57 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 58 | |
| 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | { |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 74 | CompUnitSP cu_sp (context.module_sp->GetCompileUnitAtIndex (i)); |
Greg Clayton | c705730 | 2012-04-23 22:00:21 +0000 | [diff] [blame] | 75 | 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 Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | while (sc_list.GetSize() > 0) |
| 83 | { |
| 84 | SymbolContextList tmp_sc_list; |
| 85 | int current_idx = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | SymbolContext sc; |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 87 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | { |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 95 | bool matches; |
| 96 | |
| 97 | sc_list.GetContextAtIndex (current_idx, sc); |
| 98 | if (first_entry) |
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 | match_file_spec = sc.line_entry.file; |
| 101 | matches = true; |
| 102 | first_entry = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | } |
| 104 | else |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 105 | matches = (sc.line_entry.file == match_file_spec); |
| 106 | |
| 107 | if (matches) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | { |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 109 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | } |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | return Searcher::eCallbackReturnContinue; |
| 182 | } |
| 183 | |
| 184 | Searcher::Depth |
| 185 | BreakpointResolverFileLine::GetDepth() |
| 186 | { |
Jim Ingham | a44297a | 2012-01-13 02:04:05 +0000 | [diff] [blame] | 187 | return Searcher::eDepthModule; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void |
| 191 | BreakpointResolverFileLine::GetDescription (Stream *s) |
| 192 | { |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 193 | 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] | 194 | } |
| 195 | |
| 196 | void |
| 197 | BreakpointResolverFileLine::Dump (Stream *s) const |
| 198 | { |
| 199 | |
| 200 | } |
| 201 | |