Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- BreakpointResolverName.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/BreakpointResolverName.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 | BreakpointResolverName::BreakpointResolverName |
| 25 | ( |
| 26 | Breakpoint *bkpt, |
| 27 | const char *func_name, |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 28 | uint32_t func_name_type_mask, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | Breakpoint::MatchType type |
| 30 | ) : |
Johnny Chen | a62ad7c | 2010-10-28 17:27:46 +0000 | [diff] [blame^] | 31 | BreakpointResolver (bkpt, BreakpointResolver::NameResolver), |
Greg Clayton | 48fbdf7 | 2010-10-12 04:29:14 +0000 | [diff] [blame] | 32 | m_func_name (), |
| 33 | m_basename_filter (), |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 34 | m_func_name_type_mask (func_name_type_mask), |
| 35 | m_class_name (), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | m_regex (), |
| 37 | m_match_type (type) |
| 38 | { |
Greg Clayton | 48fbdf7 | 2010-10-12 04:29:14 +0000 | [diff] [blame] | 39 | if (func_name_type_mask == eFunctionNameTypeAuto) |
| 40 | { |
| 41 | if ((::strchr (func_name, '(' ) != NULL) || |
| 42 | (::strstr (func_name, "-[") == func_name) || |
| 43 | (::strstr (func_name, "+[") == func_name)) |
| 44 | { |
| 45 | // We have a name that contains an open parens, or starts with |
| 46 | // "+[" or "-[", so this looks like a complete function prototype |
| 47 | m_func_name_type_mask = eFunctionNameTypeFull; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | // We don't have a full function name, but we might have a partial |
| 52 | // function basename with namespaces or classes |
| 53 | if (::strstr (func_name, "::") != NULL) |
| 54 | { |
| 55 | // Keep the full name in "m_basename_filter" |
| 56 | m_basename_filter = func_name; |
| 57 | // Now set "m_func_name" to just the function basename |
| 58 | m_func_name.SetCString(m_basename_filter.c_str() + m_basename_filter.rfind("::") + 2); |
| 59 | // We have a name with a double colon which means we have a |
| 60 | // function name that is a C++ method or a function in a C++ |
| 61 | // namespace |
| 62 | m_func_name_type_mask = eFunctionNameTypeBase | eFunctionNameTypeMethod; |
| 63 | } |
| 64 | else if (::strstr (func_name, ":") != NULL) |
| 65 | { |
| 66 | // Single colon => selector |
| 67 | m_func_name_type_mask = eFunctionNameTypeSelector; |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | // just a basename by default |
| 72 | m_func_name_type_mask = eFunctionNameTypeBase; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (!m_func_name) |
| 78 | m_func_name.SetCString(func_name); |
| 79 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | if (m_match_type == Breakpoint::Regexp) |
| 81 | { |
| 82 | if (!m_regex.Compile (m_func_name.AsCString())) |
| 83 | { |
| 84 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); |
| 85 | |
| 86 | if (log) |
| 87 | log->Warning ("function name regexp: \"%s\" did not compile.", m_func_name.AsCString()); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | BreakpointResolverName::BreakpointResolverName |
| 93 | ( |
| 94 | Breakpoint *bkpt, |
| 95 | RegularExpression &func_regex |
| 96 | ) : |
Johnny Chen | a62ad7c | 2010-10-28 17:27:46 +0000 | [diff] [blame^] | 97 | BreakpointResolver (bkpt, BreakpointResolver::NameResolver), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | m_func_name (NULL), |
| 99 | m_class_name (NULL), |
| 100 | m_regex (func_regex), |
| 101 | m_match_type (Breakpoint::Regexp) |
| 102 | { |
| 103 | |
| 104 | } |
| 105 | |
| 106 | BreakpointResolverName::BreakpointResolverName |
| 107 | ( |
| 108 | Breakpoint *bkpt, |
| 109 | const char *class_name, |
| 110 | const char *method, |
| 111 | Breakpoint::MatchType type |
| 112 | ) : |
Johnny Chen | a62ad7c | 2010-10-28 17:27:46 +0000 | [diff] [blame^] | 113 | BreakpointResolver (bkpt, BreakpointResolver::NameResolver), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 114 | m_func_name (method), |
| 115 | m_class_name (class_name), |
| 116 | m_regex (), |
| 117 | m_match_type (type) |
| 118 | { |
| 119 | |
| 120 | } |
| 121 | |
| 122 | BreakpointResolverName::~BreakpointResolverName () |
| 123 | { |
| 124 | } |
| 125 | |
| 126 | // FIXME: Right now we look at the module level, and call the module's "FindFunctions". |
| 127 | // Greg says he will add function tables, maybe at the CompileUnit level to accelerate function |
| 128 | // lookup. At that point, we should switch the depth to CompileUnit, and look in these tables. |
| 129 | |
| 130 | Searcher::CallbackReturn |
| 131 | BreakpointResolverName::SearchCallback |
| 132 | ( |
| 133 | SearchFilter &filter, |
| 134 | SymbolContext &context, |
| 135 | Address *addr, |
| 136 | bool containing |
| 137 | ) |
| 138 | { |
| 139 | SymbolContextList func_list; |
| 140 | SymbolContextList sym_list; |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | bool skip_prologue = true; |
| 143 | uint32_t i; |
| 144 | bool new_location; |
| 145 | SymbolContext sc; |
| 146 | Address break_addr; |
| 147 | assert (m_breakpoint != NULL); |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | if (m_class_name) |
| 152 | { |
| 153 | if (log) |
| 154 | log->Warning ("Class/method function specification not supported yet.\n"); |
| 155 | return Searcher::eCallbackReturnStop; |
| 156 | } |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 158 | switch (m_match_type) |
| 159 | { |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 160 | case Breakpoint::Exact: |
| 161 | if (context.module_sp) |
| 162 | { |
| 163 | if (m_func_name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull)) |
| 164 | context.module_sp->FindSymbolsWithNameAndType (m_func_name, eSymbolTypeCode, sym_list); |
| 165 | context.module_sp->FindFunctions (m_func_name, m_func_name_type_mask, false, func_list); |
| 166 | } |
| 167 | break; |
| 168 | case Breakpoint::Regexp: |
| 169 | if (context.module_sp) |
| 170 | { |
Caroline Tice | d8ac2a1 | 2010-10-26 18:33:57 +0000 | [diff] [blame] | 171 | context.module_sp->FindSymbolsMatchingRegExAndType (m_regex, eSymbolTypeCode, sym_list); |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 172 | context.module_sp->FindFunctions (m_regex, true, func_list); |
| 173 | } |
| 174 | break; |
| 175 | case Breakpoint::Glob: |
| 176 | if (log) |
| 177 | log->Warning ("glob is not supported yet."); |
| 178 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | } |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 180 | |
Greg Clayton | 48fbdf7 | 2010-10-12 04:29:14 +0000 | [diff] [blame] | 181 | if (!m_basename_filter.empty()) |
| 182 | { |
| 183 | // Filter out any matches whose names don't contain the basename filter |
| 184 | const char *basename_filter = m_basename_filter.c_str(); |
| 185 | if (func_list.GetSize()) |
| 186 | { |
| 187 | bool remove = false; |
| 188 | for (i = 0; i < func_list.GetSize(); remove = false) |
| 189 | { |
| 190 | if (func_list.GetContextAtIndex(i, sc) == false) |
| 191 | remove = true; |
| 192 | else if (sc.function == NULL) |
| 193 | remove = true; |
| 194 | else if (::strstr (sc.function->GetName().AsCString(), basename_filter) == NULL) |
| 195 | remove = true; |
| 196 | |
| 197 | if (remove) |
| 198 | { |
| 199 | func_list.RemoveContextAtIndex(i); |
| 200 | continue; |
| 201 | } |
| 202 | i++; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (sym_list.GetSize()) |
| 207 | { |
| 208 | bool remove = false; |
| 209 | for (i = 0; i < sym_list.GetSize(); remove = false) |
| 210 | { |
| 211 | if (sym_list.GetContextAtIndex(i, sc) == false) |
| 212 | remove = true; |
| 213 | else if (sc.symbol == NULL) |
| 214 | remove = true; |
| 215 | else if (::strstr (sc.symbol->GetName().AsCString(), basename_filter) == NULL) |
| 216 | remove = true; |
| 217 | |
| 218 | if (remove) |
| 219 | { |
| 220 | sym_list.RemoveContextAtIndex(i); |
| 221 | continue; |
| 222 | } |
| 223 | i++; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | // Remove any duplicates between the funcion list and the symbol list |
| 229 | if (func_list.GetSize()) |
| 230 | { |
| 231 | for (i = 0; i < func_list.GetSize(); i++) |
| 232 | { |
| 233 | if (func_list.GetContextAtIndex(i, sc) == false) |
| 234 | continue; |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | if (sc.function == NULL) |
| 237 | continue; |
| 238 | uint32_t j = 0; |
| 239 | while (j < sym_list.GetSize()) |
| 240 | { |
| 241 | SymbolContext symbol_sc; |
| 242 | if (sym_list.GetContextAtIndex(j, symbol_sc)) |
| 243 | { |
| 244 | if (symbol_sc.symbol && symbol_sc.symbol->GetAddressRangePtr()) |
| 245 | { |
| 246 | if (sc.function->GetAddressRange().GetBaseAddress() == symbol_sc.symbol->GetAddressRangePtr()->GetBaseAddress()) |
| 247 | { |
| 248 | sym_list.RemoveContextAtIndex(j); |
| 249 | continue; // Don't increment j |
| 250 | } |
| 251 | } |
| 252 | } |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 254 | j++; |
| 255 | } |
| 256 | } |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 258 | for (i = 0; i < func_list.GetSize(); i++) |
| 259 | { |
| 260 | if (func_list.GetContextAtIndex(i, sc)) |
| 261 | { |
| 262 | if (sc.function) |
| 263 | { |
| 264 | break_addr = sc.function->GetAddressRange().GetBaseAddress(); |
| 265 | if (skip_prologue) |
| 266 | { |
| 267 | const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize(); |
| 268 | if (prologue_byte_size) |
| 269 | break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size); |
| 270 | } |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 272 | if (filter.AddressPasses(break_addr)) |
| 273 | { |
| 274 | BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(break_addr, &new_location)); |
| 275 | if (bp_loc_sp && new_location && !m_breakpoint->IsInternal()) |
| 276 | { |
| 277 | if (log) |
| 278 | { |
| 279 | StreamString s; |
| 280 | bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
| 281 | log->Printf ("Added location: %s\n", s.GetData()); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 290 | for (i = 0; i < sym_list.GetSize(); i++) |
| 291 | { |
| 292 | if (sym_list.GetContextAtIndex(i, sc)) |
| 293 | { |
| 294 | if (sc.symbol && sc.symbol->GetAddressRangePtr()) |
| 295 | { |
| 296 | break_addr = sc.symbol->GetAddressRangePtr()->GetBaseAddress(); |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 298 | if (skip_prologue) |
| 299 | { |
| 300 | const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize(); |
| 301 | if (prologue_byte_size) |
| 302 | break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size); |
| 303 | } |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 305 | if (filter.AddressPasses(break_addr)) |
| 306 | { |
| 307 | BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(break_addr, &new_location)); |
| 308 | if (bp_loc_sp && new_location && !m_breakpoint->IsInternal()) |
| 309 | { |
| 310 | StreamString s; |
| 311 | bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
| 312 | if (log) |
| 313 | log->Printf ("Added location: %s\n", s.GetData()); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | return Searcher::eCallbackReturnContinue; |
| 320 | } |
| 321 | |
| 322 | Searcher::Depth |
| 323 | BreakpointResolverName::GetDepth() |
| 324 | { |
| 325 | return Searcher::eDepthModule; |
| 326 | } |
| 327 | |
| 328 | void |
| 329 | BreakpointResolverName::GetDescription (Stream *s) |
| 330 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 331 | if (m_match_type == Breakpoint::Regexp) |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 332 | s->Printf("regex = '%s'", m_regex.GetText()); |
Greg Clayton | 48fbdf7 | 2010-10-12 04:29:14 +0000 | [diff] [blame] | 333 | else if (m_basename_filter.empty()) |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 334 | s->Printf("name = '%s'", m_func_name.AsCString()); |
Greg Clayton | 48fbdf7 | 2010-10-12 04:29:14 +0000 | [diff] [blame] | 335 | else |
| 336 | s->Printf("name = '%s'", m_basename_filter.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void |
| 340 | BreakpointResolverName::Dump (Stream *s) const |
| 341 | { |
| 342 | |
| 343 | } |
| 344 | |