BreakpointResolverFileRegex.cpp revision 314564
1//===-- BreakpointResolverFileRegex.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/BreakpointResolverFileRegex.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/SourceManager.h"
19#include "lldb/Core/StreamString.h"
20#include "lldb/Symbol/CompileUnit.h"
21#include "lldb/Target/Target.h"
22
23using namespace lldb;
24using namespace lldb_private;
25
26//----------------------------------------------------------------------
27// BreakpointResolverFileRegex:
28//----------------------------------------------------------------------
29BreakpointResolverFileRegex::BreakpointResolverFileRegex(
30    Breakpoint *bkpt, RegularExpression &regex,
31    const std::unordered_set<std::string> &func_names, bool exact_match)
32    : BreakpointResolver(bkpt, BreakpointResolver::FileRegexResolver),
33      m_regex(regex), m_exact_match(exact_match), m_function_names(func_names) {
34}
35
36BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {}
37
38BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData(
39    Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
40    Error &error) {
41  bool success;
42
43  std::string regex_string;
44  success = options_dict.GetValueForKeyAsString(
45      GetKey(OptionNames::RegexString), regex_string);
46  if (!success) {
47    error.SetErrorString("BRFR::CFSD: Couldn't find regex entry.");
48    return nullptr;
49  }
50  RegularExpression regex(regex_string);
51
52  bool exact_match;
53  success = options_dict.GetValueForKeyAsBoolean(
54      GetKey(OptionNames::ExactMatch), exact_match);
55  if (!success) {
56    error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
57    return nullptr;
58  }
59
60  // The names array is optional:
61  std::unordered_set<std::string> names_set;
62  StructuredData::Array *names_array;
63  success = options_dict.GetValueForKeyAsArray(
64      GetKey(OptionNames::SymbolNameArray), names_array);
65  if (success && names_array) {
66    size_t num_names = names_array->GetSize();
67    for (size_t i = 0; i < num_names; i++) {
68      std::string name;
69      success = names_array->GetItemAtIndexAsString(i, name);
70      if (!success) {
71        error.SetErrorStringWithFormat(
72            "BRFR::CFSD: Malformed element %zu in the names array.", i);
73        return nullptr;
74      }
75      names_set.insert(name);
76    }
77  }
78
79  return new BreakpointResolverFileRegex(bkpt, regex, names_set, exact_match);
80}
81
82StructuredData::ObjectSP
83BreakpointResolverFileRegex::SerializeToStructuredData() {
84  StructuredData::DictionarySP options_dict_sp(
85      new StructuredData::Dictionary());
86
87  options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
88                                 m_regex.GetText());
89  options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
90                                  m_exact_match);
91  if (!m_function_names.empty()) {
92    StructuredData::ArraySP names_array_sp(new StructuredData::Array());
93    for (std::string name : m_function_names) {
94      StructuredData::StringSP item(new StructuredData::String(name));
95      names_array_sp->AddItem(item);
96    }
97    options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp);
98  }
99
100  return WrapOptionsDict(options_dict_sp);
101}
102
103Searcher::CallbackReturn
104BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter,
105                                            SymbolContext &context,
106                                            Address *addr, bool containing) {
107
108  assert(m_breakpoint != NULL);
109  if (!context.target_sp)
110    return eCallbackReturnContinue;
111
112  CompileUnit *cu = context.comp_unit;
113  FileSpec cu_file_spec = *(static_cast<FileSpec *>(cu));
114  std::vector<uint32_t> line_matches;
115  context.target_sp->GetSourceManager().FindLinesMatchingRegex(
116      cu_file_spec, m_regex, 1, UINT32_MAX, line_matches);
117
118  uint32_t num_matches = line_matches.size();
119  for (uint32_t i = 0; i < num_matches; i++) {
120    SymbolContextList sc_list;
121    const bool search_inlines = false;
122
123    cu->ResolveSymbolContext(cu_file_spec, line_matches[i], search_inlines,
124                             m_exact_match, eSymbolContextEverything, sc_list);
125    // Find all the function names:
126    if (!m_function_names.empty()) {
127      std::vector<size_t> sc_to_remove;
128      for (size_t i = 0; i < sc_list.GetSize(); i++) {
129        SymbolContext sc_ctx;
130        sc_list.GetContextAtIndex(i, sc_ctx);
131        std::string name(
132            sc_ctx
133                .GetFunctionName(
134                    Mangled::NamePreference::ePreferDemangledWithoutArguments)
135                .AsCString());
136        if (!m_function_names.count(name)) {
137          sc_to_remove.push_back(i);
138        }
139      }
140
141      if (!sc_to_remove.empty()) {
142        std::vector<size_t>::reverse_iterator iter;
143        std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend();
144        for (iter = sc_to_remove.rbegin(); iter != rend; iter++) {
145          sc_list.RemoveContextAtIndex(*iter);
146        }
147      }
148    }
149
150    const bool skip_prologue = true;
151
152    BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
153                                           m_regex.GetText());
154  }
155  assert(m_breakpoint != NULL);
156
157  return Searcher::eCallbackReturnContinue;
158}
159
160Searcher::Depth BreakpointResolverFileRegex::GetDepth() {
161  return Searcher::eDepthCompUnit;
162}
163
164void BreakpointResolverFileRegex::GetDescription(Stream *s) {
165  s->Printf("source regex = \"%s\", exact_match = %d",
166            m_regex.GetText().str().c_str(), m_exact_match);
167}
168
169void BreakpointResolverFileRegex::Dump(Stream *s) const {}
170
171lldb::BreakpointResolverSP
172BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) {
173  lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
174      &breakpoint, m_regex, m_function_names, m_exact_match));
175  return ret_sp;
176}
177
178void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
179  m_function_names.insert(func_name);
180}
181