1969795f1SJim Ingham //===-- BreakpointResolverFileRegex.cpp --------------------------*- C++ -*-===//
2969795f1SJim Ingham //
3969795f1SJim Ingham //                     The LLVM Compiler Infrastructure
4969795f1SJim Ingham //
5969795f1SJim Ingham // This file is distributed under the University of Illinois Open Source
6969795f1SJim Ingham // License. See LICENSE.TXT for details.
7969795f1SJim Ingham //
8969795f1SJim Ingham //===----------------------------------------------------------------------===//
9969795f1SJim Ingham 
10969795f1SJim Ingham #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
11969795f1SJim Ingham 
12969795f1SJim Ingham // C Includes
13969795f1SJim Ingham // C++ Includes
14969795f1SJim Ingham // Other libraries and framework includes
15969795f1SJim Ingham // Project includes
16969795f1SJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
17969795f1SJim Ingham #include "lldb/Core/SourceManager.h"
18969795f1SJim Ingham #include "lldb/Core/Log.h"
19969795f1SJim Ingham #include "lldb/Core/StreamString.h"
201f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
21969795f1SJim Ingham #include "lldb/Target/Target.h"
22969795f1SJim Ingham #include "lldb/lldb-private-log.h"
23969795f1SJim Ingham 
24969795f1SJim Ingham using namespace lldb;
25969795f1SJim Ingham using namespace lldb_private;
26969795f1SJim Ingham 
27969795f1SJim Ingham //----------------------------------------------------------------------
28969795f1SJim Ingham // BreakpointResolverFileRegex:
29969795f1SJim Ingham //----------------------------------------------------------------------
30969795f1SJim Ingham BreakpointResolverFileRegex::BreakpointResolverFileRegex
31969795f1SJim Ingham (
32969795f1SJim Ingham     Breakpoint *bkpt,
33969795f1SJim Ingham     RegularExpression &regex
34969795f1SJim Ingham ) :
35969795f1SJim Ingham     BreakpointResolver (bkpt, BreakpointResolver::FileLineResolver),
36969795f1SJim Ingham     m_regex (regex)
37969795f1SJim Ingham {
38969795f1SJim Ingham }
39969795f1SJim Ingham 
40969795f1SJim Ingham BreakpointResolverFileRegex::~BreakpointResolverFileRegex ()
41969795f1SJim Ingham {
42969795f1SJim Ingham }
43969795f1SJim Ingham 
44969795f1SJim Ingham Searcher::CallbackReturn
45969795f1SJim Ingham BreakpointResolverFileRegex::SearchCallback
46969795f1SJim Ingham (
47969795f1SJim Ingham     SearchFilter &filter,
48969795f1SJim Ingham     SymbolContext &context,
49969795f1SJim Ingham     Address *addr,
50969795f1SJim Ingham     bool containing
51969795f1SJim Ingham )
52969795f1SJim Ingham {
53969795f1SJim Ingham 
54969795f1SJim Ingham     assert (m_breakpoint != NULL);
55969795f1SJim Ingham     if (!context.target_sp)
56969795f1SJim Ingham         return eCallbackReturnContinue;
57969795f1SJim Ingham 
585160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
59969795f1SJim Ingham 
60969795f1SJim Ingham     CompileUnit *cu = context.comp_unit;
61969795f1SJim Ingham     FileSpec cu_file_spec = *(static_cast<FileSpec *>(cu));
62969795f1SJim Ingham     std::vector<uint32_t> line_matches;
63969795f1SJim Ingham     context.target_sp->GetSourceManager().FindLinesMatchingRegex(cu_file_spec, m_regex, 1, UINT32_MAX, line_matches);
64969795f1SJim Ingham     uint32_t num_matches = line_matches.size();
65*a297a97eSAndy Gibbs     for (uint32_t i = 0; i < num_matches; i++)
66969795f1SJim Ingham     {
67969795f1SJim Ingham         uint32_t start_idx = 0;
6887df91b8SJim Ingham         bool exact = false;
69969795f1SJim Ingham         while (1)
70969795f1SJim Ingham         {
71969795f1SJim Ingham             LineEntry line_entry;
72969795f1SJim Ingham 
73969795f1SJim Ingham             // Cycle through all the line entries that might match this one:
7487df91b8SJim Ingham             start_idx = cu->FindLineEntry (start_idx, line_matches[i], NULL, exact, &line_entry);
75969795f1SJim Ingham             if (start_idx == UINT32_MAX)
76969795f1SJim Ingham                 break;
7787df91b8SJim Ingham             exact = true;
78969795f1SJim Ingham             start_idx++;
79969795f1SJim Ingham 
80969795f1SJim Ingham             Address line_start = line_entry.range.GetBaseAddress();
81969795f1SJim Ingham             if (line_start.IsValid())
82969795f1SJim Ingham             {
83969795f1SJim Ingham                 if (filter.AddressPasses(line_start))
84969795f1SJim Ingham                 {
85969795f1SJim Ingham                     BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(line_start));
86969795f1SJim Ingham                     if (log && bp_loc_sp && !m_breakpoint->IsInternal())
87969795f1SJim Ingham                     {
88969795f1SJim Ingham                         StreamString s;
89969795f1SJim Ingham                         bp_loc_sp->GetDescription (&s, lldb::eDescriptionLevelVerbose);
90969795f1SJim Ingham                         log->Printf ("Added location: %s\n", s.GetData());
91969795f1SJim Ingham                     }
92969795f1SJim Ingham                 }
93969795f1SJim Ingham                 else if (log)
94969795f1SJim Ingham                 {
95d01b2953SDaniel Malea                     log->Printf ("Breakpoint at file address 0x%" PRIx64 " for %s:%d didn't pass filter.\n",
96969795f1SJim Ingham                                  line_start.GetFileAddress(),
9787df91b8SJim Ingham                                  cu_file_spec.GetFilename().AsCString("<Unknown>"),
98969795f1SJim Ingham                                  line_matches[i]);
99969795f1SJim Ingham                 }
100969795f1SJim Ingham             }
101969795f1SJim Ingham             else
102969795f1SJim Ingham             {
103969795f1SJim Ingham                 if (log)
104d01b2953SDaniel Malea                     log->Printf ("error: Unable to set breakpoint at file address 0x%" PRIx64 " for %s:%d\n",
105969795f1SJim Ingham                                  line_start.GetFileAddress(),
10687df91b8SJim Ingham                                  cu_file_spec.GetFilename().AsCString("<Unknown>"),
107969795f1SJim Ingham                                  line_matches[i]);
108969795f1SJim Ingham             }
109969795f1SJim Ingham 
110969795f1SJim Ingham         }
111969795f1SJim Ingham     }
112969795f1SJim Ingham     assert (m_breakpoint != NULL);
113969795f1SJim Ingham 
114969795f1SJim Ingham     return Searcher::eCallbackReturnContinue;
115969795f1SJim Ingham }
116969795f1SJim Ingham 
117969795f1SJim Ingham Searcher::Depth
118969795f1SJim Ingham BreakpointResolverFileRegex::GetDepth()
119969795f1SJim Ingham {
120969795f1SJim Ingham     return Searcher::eDepthCompUnit;
121969795f1SJim Ingham }
122969795f1SJim Ingham 
123969795f1SJim Ingham void
124969795f1SJim Ingham BreakpointResolverFileRegex::GetDescription (Stream *s)
125969795f1SJim Ingham {
12687df91b8SJim Ingham     s->Printf ("source regex = \"%s\"", m_regex.GetText());
127969795f1SJim Ingham }
128969795f1SJim Ingham 
129969795f1SJim Ingham void
130969795f1SJim Ingham BreakpointResolverFileRegex::Dump (Stream *s) const
131969795f1SJim Ingham {
132969795f1SJim Ingham 
133969795f1SJim Ingham }
134969795f1SJim Ingham 
135