Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Breakpoint/Breakpoint.h" |
| 17 | #include "lldb/Breakpoint/BreakpointLocation.h" |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 18 | // Have to include the other breakpoint resolver types here so the static create |
| 19 | // from StructuredData can call them. |
| 20 | #include "lldb/Breakpoint/BreakpointResolverAddress.h" |
| 21 | #include "lldb/Breakpoint/BreakpointResolverFileLine.h" |
| 22 | #include "lldb/Breakpoint/BreakpointResolverFileRegex.h" |
| 23 | #include "lldb/Breakpoint/BreakpointResolverName.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | #include "lldb/Core/Address.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | #include "lldb/Core/Log.h" |
| 26 | #include "lldb/Core/ModuleList.h" |
| 27 | #include "lldb/Core/SearchFilter.h" |
| 28 | #include "lldb/Core/Stream.h" |
| 29 | #include "lldb/Core/StreamString.h" |
Jim Ingham | f642373 | 2013-09-27 01:16:58 +0000 | [diff] [blame] | 30 | #include "lldb/Symbol/CompileUnit.h" |
| 31 | #include "lldb/Symbol/Function.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | #include "lldb/Symbol/SymbolContext.h" |
| 33 | #include "lldb/Target/Target.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace lldb_private; |
Jim Ingham | f642373 | 2013-09-27 01:16:58 +0000 | [diff] [blame] | 36 | using namespace lldb; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | |
| 38 | //---------------------------------------------------------------------- |
| 39 | // BreakpointResolver: |
| 40 | //---------------------------------------------------------------------- |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 41 | const char *BreakpointResolver::g_ty_to_name[] = {"FileAndLine", "Address", |
| 42 | "SymbolName", "SourceRegex", |
| 43 | "Exception", "Unknown"}; |
| 44 | |
| 45 | const char *BreakpointResolver::g_option_names |
| 46 | [BreakpointResolver::OptionNames::LastOptionName] = { |
| 47 | "AddressOffset", "Exact", "FileName", "Inlines", "Language", |
| 48 | "LineNumber", "ModuleName", "NameMask", "Offset", "Regex", |
| 49 | "SectionName", "SkipPrologue", "SymbolNames"}; |
| 50 | |
| 51 | const char *BreakpointResolver::ResolverTyToName(enum ResolverTy type) { |
| 52 | if (type > LastKnownResolverType) |
| 53 | return g_ty_to_name[UnknownResolver]; |
| 54 | |
| 55 | return g_ty_to_name[type]; |
| 56 | } |
| 57 | |
| 58 | BreakpointResolver::ResolverTy |
| 59 | BreakpointResolver::NameToResolverTy(const char *name) { |
| 60 | for (size_t i = 0; i < LastKnownResolverType; i++) { |
| 61 | if (strcmp(name, g_ty_to_name[i]) == 0) |
| 62 | return (ResolverTy)i; |
| 63 | } |
| 64 | return UnknownResolver; |
| 65 | } |
| 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | BreakpointResolver::BreakpointResolver(Breakpoint *bkpt, |
| 68 | const unsigned char resolverTy, |
| 69 | lldb::addr_t offset) |
| 70 | : m_breakpoint(bkpt), m_offset(offset), SubclassID(resolverTy) {} |
| 71 | |
| 72 | BreakpointResolver::~BreakpointResolver() {} |
| 73 | |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 74 | BreakpointResolverSP BreakpointResolver::CreateFromStructuredData( |
Jim Ingham | 1a81b27 | 2016-09-13 01:58:08 +0000 | [diff] [blame] | 75 | const StructuredData::Dictionary &resolver_dict, Error &error) { |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 76 | BreakpointResolverSP result_sp; |
| 77 | if (!resolver_dict.IsValid()) { |
| 78 | error.SetErrorString("Can't deserialize from an invalid data object."); |
| 79 | return result_sp; |
| 80 | } |
| 81 | |
| 82 | std::string subclass_name; |
| 83 | |
| 84 | bool success = resolver_dict.GetValueForKeyAsString( |
| 85 | GetSerializationSubclassKey(), subclass_name); |
| 86 | |
| 87 | if (!success) { |
| 88 | error.SetErrorStringWithFormat( |
| 89 | "Resolver data missing subclass resolver key"); |
| 90 | return result_sp; |
| 91 | } |
| 92 | |
| 93 | ResolverTy resolver_type = NameToResolverTy(subclass_name.c_str()); |
| 94 | if (resolver_type == UnknownResolver) { |
| 95 | error.SetErrorStringWithFormat("Unknown resolver type: %s.", |
| 96 | subclass_name.c_str()); |
| 97 | return result_sp; |
| 98 | } |
| 99 | |
| 100 | StructuredData::Dictionary *subclass_options = nullptr; |
| 101 | success = resolver_dict.GetValueForKeyAsDictionary( |
| 102 | GetSerializationSubclassOptionsKey(), subclass_options); |
| 103 | if (!success || !subclass_options || !subclass_options->IsValid()) { |
| 104 | error.SetErrorString("Resolver data missing subclass options key."); |
| 105 | return result_sp; |
| 106 | } |
| 107 | |
| 108 | lldb::addr_t offset; |
| 109 | success = subclass_options->GetValueForKeyAsInteger( |
| 110 | GetKey(OptionNames::Offset), offset); |
| 111 | if (!success) { |
| 112 | error.SetErrorString("Resolver data missing offset options key."); |
| 113 | return result_sp; |
| 114 | } |
| 115 | |
| 116 | BreakpointResolver *resolver; |
| 117 | |
| 118 | switch (resolver_type) { |
| 119 | case FileLineResolver: |
| 120 | resolver = BreakpointResolverFileLine::CreateFromStructuredData( |
| 121 | nullptr, *subclass_options, error); |
| 122 | break; |
| 123 | case AddressResolver: |
| 124 | resolver = BreakpointResolverAddress::CreateFromStructuredData( |
| 125 | nullptr, *subclass_options, error); |
| 126 | break; |
| 127 | case NameResolver: |
| 128 | resolver = BreakpointResolverName::CreateFromStructuredData( |
| 129 | nullptr, *subclass_options, error); |
| 130 | break; |
| 131 | case FileRegexResolver: |
| 132 | resolver = BreakpointResolverFileRegex::CreateFromStructuredData( |
| 133 | nullptr, *subclass_options, error); |
| 134 | break; |
| 135 | case ExceptionResolver: |
| 136 | error.SetErrorString("Exception resolvers are hard."); |
| 137 | break; |
| 138 | default: |
| 139 | llvm_unreachable("Should never get an unresolvable resolver type."); |
| 140 | } |
| 141 | |
| 142 | if (!error.Success()) { |
| 143 | return result_sp; |
| 144 | } else { |
| 145 | // Add on the global offset option: |
| 146 | resolver->SetOffset(offset); |
| 147 | return BreakpointResolverSP(resolver); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | StructuredData::DictionarySP BreakpointResolver::WrapOptionsDict( |
| 152 | StructuredData::DictionarySP options_dict_sp) { |
| 153 | if (!options_dict_sp || !options_dict_sp->IsValid()) |
| 154 | return StructuredData::DictionarySP(); |
| 155 | |
| 156 | StructuredData::DictionarySP type_dict_sp(new StructuredData::Dictionary()); |
| 157 | type_dict_sp->AddStringItem(GetSerializationSubclassKey(), GetResolverName()); |
| 158 | type_dict_sp->AddItem(GetSerializationSubclassOptionsKey(), options_dict_sp); |
| 159 | |
| 160 | // Add the m_offset to the dictionary: |
| 161 | options_dict_sp->AddIntegerItem(GetKey(OptionNames::Offset), m_offset); |
| 162 | |
| 163 | return type_dict_sp; |
| 164 | } |
| 165 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 166 | void BreakpointResolver::SetBreakpoint(Breakpoint *bkpt) { |
| 167 | m_breakpoint = bkpt; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | void BreakpointResolver::ResolveBreakpointInModules(SearchFilter &filter, |
| 171 | ModuleList &modules) { |
| 172 | filter.SearchInModuleList(*this, modules); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | void BreakpointResolver::ResolveBreakpoint(SearchFilter &filter) { |
| 176 | filter.Search(*this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | void BreakpointResolver::SetSCMatchesByLine(SearchFilter &filter, |
| 180 | SymbolContextList &sc_list, |
| 181 | bool skip_prologue, |
| 182 | const char *log_ident) { |
| 183 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 184 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | while (sc_list.GetSize() > 0) { |
| 186 | SymbolContextList tmp_sc_list; |
| 187 | unsigned current_idx = 0; |
| 188 | SymbolContext sc; |
| 189 | bool first_entry = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | FileSpec match_file_spec; |
| 192 | FileSpec match_original_file_spec; |
| 193 | uint32_t closest_line_number = UINT32_MAX; |
Jim Ingham | f642373 | 2013-09-27 01:16:58 +0000 | [diff] [blame] | 194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 195 | // Pull out the first entry, and all the others that match its file spec, |
| 196 | // and stuff them in the tmp list. |
| 197 | while (current_idx < sc_list.GetSize()) { |
| 198 | bool matches; |
Jim Ingham | f642373 | 2013-09-27 01:16:58 +0000 | [diff] [blame] | 199 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | sc_list.GetContextAtIndex(current_idx, sc); |
| 201 | if (first_entry) { |
| 202 | match_file_spec = sc.line_entry.file; |
| 203 | match_original_file_spec = sc.line_entry.original_file; |
| 204 | matches = true; |
| 205 | first_entry = false; |
| 206 | } else |
| 207 | matches = ((sc.line_entry.file == match_file_spec) || |
| 208 | (sc.line_entry.original_file == match_original_file_spec)); |
| 209 | |
| 210 | if (matches) { |
| 211 | tmp_sc_list.Append(sc); |
| 212 | sc_list.RemoveContextAtIndex(current_idx); |
| 213 | |
| 214 | // ResolveSymbolContext will always return a number that is >= the line |
| 215 | // number you pass in. |
| 216 | // So the smaller line number is always better. |
| 217 | if (sc.line_entry.line < closest_line_number) |
| 218 | closest_line_number = sc.line_entry.line; |
| 219 | } else |
| 220 | current_idx++; |
Jim Ingham | f642373 | 2013-09-27 01:16:58 +0000 | [diff] [blame] | 221 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 222 | |
| 223 | // Okay, we've found the closest line number match, now throw away all the |
| 224 | // others: |
| 225 | |
| 226 | current_idx = 0; |
| 227 | while (current_idx < tmp_sc_list.GetSize()) { |
| 228 | if (tmp_sc_list.GetContextAtIndex(current_idx, sc)) { |
| 229 | if (sc.line_entry.line != closest_line_number) |
| 230 | tmp_sc_list.RemoveContextAtIndex(current_idx); |
| 231 | else |
| 232 | current_idx++; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // Next go through and see if there are line table entries that are |
| 237 | // contiguous, and if so keep only the |
| 238 | // first of the contiguous range: |
| 239 | |
| 240 | current_idx = 0; |
| 241 | std::map<Block *, lldb::addr_t> blocks_with_breakpoints; |
| 242 | |
| 243 | while (current_idx < tmp_sc_list.GetSize()) { |
| 244 | if (tmp_sc_list.GetContextAtIndex(current_idx, sc)) { |
| 245 | if (blocks_with_breakpoints.find(sc.block) != |
| 246 | blocks_with_breakpoints.end()) |
| 247 | tmp_sc_list.RemoveContextAtIndex(current_idx); |
| 248 | else { |
| 249 | blocks_with_breakpoints.insert(std::pair<Block *, lldb::addr_t>( |
| 250 | sc.block, sc.line_entry.range.GetBaseAddress().GetFileAddress())); |
| 251 | current_idx++; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // and make breakpoints out of the closest line number match. |
| 257 | |
| 258 | uint32_t tmp_sc_list_size = tmp_sc_list.GetSize(); |
| 259 | |
| 260 | for (uint32_t i = 0; i < tmp_sc_list_size; i++) { |
| 261 | if (tmp_sc_list.GetContextAtIndex(i, sc)) { |
| 262 | Address line_start = sc.line_entry.range.GetBaseAddress(); |
| 263 | if (line_start.IsValid()) { |
| 264 | if (filter.AddressPasses(line_start)) { |
| 265 | // If the line number is before the prologue end, move it there... |
| 266 | bool skipped_prologue = false; |
| 267 | if (skip_prologue) { |
| 268 | if (sc.function) { |
| 269 | Address prologue_addr( |
| 270 | sc.function->GetAddressRange().GetBaseAddress()); |
| 271 | if (prologue_addr.IsValid() && (line_start == prologue_addr)) { |
| 272 | const uint32_t prologue_byte_size = |
| 273 | sc.function->GetPrologueByteSize(); |
| 274 | if (prologue_byte_size) { |
| 275 | prologue_addr.Slide(prologue_byte_size); |
| 276 | |
| 277 | if (filter.AddressPasses(prologue_addr)) { |
| 278 | skipped_prologue = true; |
| 279 | line_start = prologue_addr; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | BreakpointLocationSP bp_loc_sp(AddLocation(line_start)); |
| 287 | if (log && bp_loc_sp && !m_breakpoint->IsInternal()) { |
| 288 | StreamString s; |
| 289 | bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
| 290 | log->Printf("Added location (skipped prologue: %s): %s \n", |
| 291 | skipped_prologue ? "yes" : "no", s.GetData()); |
| 292 | } |
| 293 | } else if (log) { |
| 294 | log->Printf("Breakpoint %s at file address 0x%" PRIx64 |
| 295 | " didn't pass the filter.\n", |
| 296 | log_ident ? log_ident : "", |
| 297 | line_start.GetFileAddress()); |
| 298 | } |
| 299 | } else { |
| 300 | if (log) |
| 301 | log->Printf( |
| 302 | "error: Unable to set breakpoint %s at file address 0x%" PRIx64 |
| 303 | "\n", |
| 304 | log_ident ? log_ident : "", line_start.GetFileAddress()); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
Jim Ingham | f642373 | 2013-09-27 01:16:58 +0000 | [diff] [blame] | 309 | } |
Jim Ingham | 2411167 | 2016-03-09 18:59:13 +0000 | [diff] [blame] | 310 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 311 | BreakpointLocationSP BreakpointResolver::AddLocation(Address loc_addr, |
| 312 | bool *new_location) { |
| 313 | loc_addr.Slide(m_offset); |
| 314 | return m_breakpoint->AddLocation(loc_addr, new_location); |
Jim Ingham | 2411167 | 2016-03-09 18:59:13 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 317 | void BreakpointResolver::SetOffset(lldb::addr_t offset) { |
| 318 | // There may already be an offset, so we are actually adjusting location |
| 319 | // addresses by the difference. |
| 320 | // lldb::addr_t slide = offset - m_offset; |
| 321 | // FIXME: We should go fix up all the already set locations for the new slide. |
| 322 | |
| 323 | m_offset = offset; |
| 324 | } |