1*b9c1b51eSKate Stone //===-- BreakpointResolverFileRegex.cpp --------------------------*- C++
2*b9c1b51eSKate Stone //-*-===//
3969795f1SJim Ingham //
4969795f1SJim Ingham //                     The LLVM Compiler Infrastructure
5969795f1SJim Ingham //
6969795f1SJim Ingham // This file is distributed under the University of Illinois Open Source
7969795f1SJim Ingham // License. See LICENSE.TXT for details.
8969795f1SJim Ingham //
9969795f1SJim Ingham //===----------------------------------------------------------------------===//
10969795f1SJim Ingham 
11969795f1SJim Ingham #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
12969795f1SJim Ingham 
13969795f1SJim Ingham // C Includes
14969795f1SJim Ingham // C++ Includes
15969795f1SJim Ingham // Other libraries and framework includes
16969795f1SJim Ingham // Project includes
17969795f1SJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
18969795f1SJim Ingham #include "lldb/Core/Log.h"
19*b9c1b51eSKate Stone #include "lldb/Core/SourceManager.h"
20969795f1SJim Ingham #include "lldb/Core/StreamString.h"
211f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
22969795f1SJim Ingham #include "lldb/Target/Target.h"
23969795f1SJim Ingham 
24969795f1SJim Ingham using namespace lldb;
25969795f1SJim Ingham using namespace lldb_private;
26969795f1SJim Ingham 
27969795f1SJim Ingham //----------------------------------------------------------------------
28969795f1SJim Ingham // BreakpointResolverFileRegex:
29969795f1SJim Ingham //----------------------------------------------------------------------
30*b9c1b51eSKate Stone BreakpointResolverFileRegex::BreakpointResolverFileRegex(
31*b9c1b51eSKate Stone     Breakpoint *bkpt, RegularExpression &regex,
32*b9c1b51eSKate Stone     const std::unordered_set<std::string> &func_names, bool exact_match)
33*b9c1b51eSKate Stone     : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver),
34*b9c1b51eSKate Stone       m_regex(regex), m_exact_match(exact_match), m_function_names(func_names) {
35969795f1SJim Ingham }
36969795f1SJim Ingham 
37*b9c1b51eSKate Stone BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {}
38969795f1SJim Ingham 
39969795f1SJim Ingham Searcher::CallbackReturn
40*b9c1b51eSKate Stone BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter,
41969795f1SJim Ingham                                             SymbolContext &context,
42*b9c1b51eSKate Stone                                             Address *addr, bool containing) {
43969795f1SJim Ingham 
44969795f1SJim Ingham   assert(m_breakpoint != NULL);
45969795f1SJim Ingham   if (!context.target_sp)
46969795f1SJim Ingham     return eCallbackReturnContinue;
47969795f1SJim Ingham 
48969795f1SJim Ingham   CompileUnit *cu = context.comp_unit;
49969795f1SJim Ingham   FileSpec cu_file_spec = *(static_cast<FileSpec *>(cu));
50969795f1SJim Ingham   std::vector<uint32_t> line_matches;
51*b9c1b51eSKate Stone   context.target_sp->GetSourceManager().FindLinesMatchingRegex(
52*b9c1b51eSKate Stone       cu_file_spec, m_regex, 1, UINT32_MAX, line_matches);
53f642373cSJim Ingham 
54969795f1SJim Ingham   uint32_t num_matches = line_matches.size();
55*b9c1b51eSKate Stone   for (uint32_t i = 0; i < num_matches; i++) {
56f642373cSJim Ingham     SymbolContextList sc_list;
57f642373cSJim Ingham     const bool search_inlines = false;
58969795f1SJim Ingham 
59*b9c1b51eSKate Stone     cu->ResolveSymbolContext(cu_file_spec, line_matches[i], search_inlines,
60*b9c1b51eSKate Stone                              m_exact_match, eSymbolContextEverything, sc_list);
6176bb8d67SJim Ingham     // Find all the function names:
62*b9c1b51eSKate Stone     if (!m_function_names.empty()) {
6376bb8d67SJim Ingham       std::vector<size_t> sc_to_remove;
64*b9c1b51eSKate Stone       for (size_t i = 0; i < sc_list.GetSize(); i++) {
6576bb8d67SJim Ingham         SymbolContext sc_ctx;
6676bb8d67SJim Ingham         sc_list.GetContextAtIndex(i, sc_ctx);
67*b9c1b51eSKate Stone         std::string name(
68*b9c1b51eSKate Stone             sc_ctx
69*b9c1b51eSKate Stone                 .GetFunctionName(
70*b9c1b51eSKate Stone                     Mangled::NamePreference::ePreferDemangledWithoutArguments)
71*b9c1b51eSKate Stone                 .AsCString());
72*b9c1b51eSKate Stone         if (!m_function_names.count(name)) {
7376bb8d67SJim Ingham           sc_to_remove.push_back(i);
7476bb8d67SJim Ingham         }
7576bb8d67SJim Ingham       }
7676bb8d67SJim Ingham 
77*b9c1b51eSKate Stone       if (!sc_to_remove.empty()) {
7876bb8d67SJim Ingham         std::vector<size_t>::reverse_iterator iter;
7976bb8d67SJim Ingham         std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend();
80*b9c1b51eSKate Stone         for (iter = sc_to_remove.rbegin(); iter != rend; iter++) {
8176bb8d67SJim Ingham           sc_list.RemoveContextAtIndex(*iter);
8276bb8d67SJim Ingham         }
8376bb8d67SJim Ingham       }
8476bb8d67SJim Ingham     }
8576bb8d67SJim Ingham 
86f642373cSJim Ingham     const bool skip_prologue = true;
87969795f1SJim Ingham 
88*b9c1b51eSKate Stone     BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
89*b9c1b51eSKate Stone                                            m_regex.GetText());
90969795f1SJim Ingham   }
91969795f1SJim Ingham   assert(m_breakpoint != NULL);
92969795f1SJim Ingham 
93969795f1SJim Ingham   return Searcher::eCallbackReturnContinue;
94969795f1SJim Ingham }
95969795f1SJim Ingham 
96*b9c1b51eSKate Stone Searcher::Depth BreakpointResolverFileRegex::GetDepth() {
97969795f1SJim Ingham   return Searcher::eDepthCompUnit;
98969795f1SJim Ingham }
99969795f1SJim Ingham 
100*b9c1b51eSKate Stone void BreakpointResolverFileRegex::GetDescription(Stream *s) {
101*b9c1b51eSKate Stone   s->Printf("source regex = \"%s\", exact_match = %d", m_regex.GetText(),
102*b9c1b51eSKate Stone             m_exact_match);
103969795f1SJim Ingham }
104969795f1SJim Ingham 
105*b9c1b51eSKate Stone void BreakpointResolverFileRegex::Dump(Stream *s) const {}
106969795f1SJim Ingham 
10733df7cd3SJim Ingham lldb::BreakpointResolverSP
108*b9c1b51eSKate Stone BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) {
109*b9c1b51eSKate Stone   lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
110*b9c1b51eSKate Stone       &breakpoint, m_regex, m_function_names, m_exact_match));
11133df7cd3SJim Ingham   return ret_sp;
11233df7cd3SJim Ingham }
11333df7cd3SJim Ingham 
114*b9c1b51eSKate Stone void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
11576bb8d67SJim Ingham   m_function_names.insert(func_name);
11676bb8d67SJim Ingham }
117