130fdc8d8SChris Lattner //===-- BreakpointResolverFileLine.cpp --------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1530fdc8d8SChris Lattner // Project includes
1630fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocation.h"
1730fdc8d8SChris Lattner #include "lldb/Core/Log.h"
181f746071SGreg Clayton #include "lldb/Core/Module.h"
1930fdc8d8SChris Lattner #include "lldb/Core/StreamString.h"
201f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
211f746071SGreg Clayton #include "lldb/Symbol/Function.h"
2230fdc8d8SChris Lattner 
2330fdc8d8SChris Lattner using namespace lldb;
2430fdc8d8SChris Lattner using namespace lldb_private;
2530fdc8d8SChris Lattner 
2630fdc8d8SChris Lattner //----------------------------------------------------------------------
2730fdc8d8SChris Lattner // BreakpointResolverFileLine:
2830fdc8d8SChris Lattner //----------------------------------------------------------------------
29b9c1b51eSKate Stone BreakpointResolverFileLine::BreakpointResolverFileLine(
30b9c1b51eSKate Stone     Breakpoint *bkpt, const FileSpec &file_spec, uint32_t line_no,
31b9c1b51eSKate Stone     lldb::addr_t offset, bool check_inlines, bool skip_prologue,
32b9c1b51eSKate Stone     bool exact_match)
33b9c1b51eSKate Stone     : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver, offset),
34b9c1b51eSKate Stone       m_file_spec(file_spec), m_line_number(line_no), m_inlines(check_inlines),
35b9c1b51eSKate Stone       m_skip_prologue(skip_prologue), m_exact_match(exact_match) {}
3630fdc8d8SChris Lattner 
37b9c1b51eSKate Stone BreakpointResolverFileLine::~BreakpointResolverFileLine() {}
3830fdc8d8SChris Lattner 
39*e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData(
40*e14dc268SJim Ingham     Breakpoint *bkpt, StructuredData::Dictionary &options_dict, Error &error) {
41*e14dc268SJim Ingham   std::string filename;
42*e14dc268SJim Ingham   uint32_t line_no;
43*e14dc268SJim Ingham   bool check_inlines;
44*e14dc268SJim Ingham   bool skip_prologue;
45*e14dc268SJim Ingham   bool exact_match;
46*e14dc268SJim Ingham   bool success;
47*e14dc268SJim Ingham 
48*e14dc268SJim Ingham   lldb::addr_t offset = 0;
49*e14dc268SJim Ingham 
50*e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName),
51*e14dc268SJim Ingham                                                 filename);
52*e14dc268SJim Ingham   if (!success) {
53*e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find filename entry.");
54*e14dc268SJim Ingham     return nullptr;
55*e14dc268SJim Ingham   }
56*e14dc268SJim Ingham 
57*e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsInteger(
58*e14dc268SJim Ingham       GetKey(OptionNames::LineNumber), line_no);
59*e14dc268SJim Ingham   if (!success) {
60*e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find line number entry.");
61*e14dc268SJim Ingham     return nullptr;
62*e14dc268SJim Ingham   }
63*e14dc268SJim Ingham 
64*e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines),
65*e14dc268SJim Ingham                                                  check_inlines);
66*e14dc268SJim Ingham   if (!success) {
67*e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry.");
68*e14dc268SJim Ingham     return nullptr;
69*e14dc268SJim Ingham   }
70*e14dc268SJim Ingham 
71*e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsBoolean(
72*e14dc268SJim Ingham       GetKey(OptionNames::SkipPrologue), skip_prologue);
73*e14dc268SJim Ingham   if (!success) {
74*e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry.");
75*e14dc268SJim Ingham     return nullptr;
76*e14dc268SJim Ingham   }
77*e14dc268SJim Ingham 
78*e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsBoolean(
79*e14dc268SJim Ingham       GetKey(OptionNames::ExactMatch), exact_match);
80*e14dc268SJim Ingham   if (!success) {
81*e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
82*e14dc268SJim Ingham     return nullptr;
83*e14dc268SJim Ingham   }
84*e14dc268SJim Ingham 
85*e14dc268SJim Ingham   FileSpec file_spec(filename.c_str(), false);
86*e14dc268SJim Ingham 
87*e14dc268SJim Ingham   return new BreakpointResolverFileLine(bkpt, file_spec, line_no, offset,
88*e14dc268SJim Ingham                                         check_inlines, skip_prologue,
89*e14dc268SJim Ingham                                         exact_match);
90*e14dc268SJim Ingham }
91*e14dc268SJim Ingham 
92*e14dc268SJim Ingham StructuredData::ObjectSP
93*e14dc268SJim Ingham BreakpointResolverFileLine::SerializeToStructuredData() {
94*e14dc268SJim Ingham   StructuredData::DictionarySP options_dict_sp(
95*e14dc268SJim Ingham       new StructuredData::Dictionary());
96*e14dc268SJim Ingham 
97*e14dc268SJim Ingham   options_dict_sp->AddStringItem(GetKey(OptionNames::FileName),
98*e14dc268SJim Ingham                                  m_file_spec.GetPath());
99*e14dc268SJim Ingham   options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber),
100*e14dc268SJim Ingham                                   m_line_number);
101*e14dc268SJim Ingham   options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), m_inlines);
102*e14dc268SJim Ingham   options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
103*e14dc268SJim Ingham                                   m_skip_prologue);
104*e14dc268SJim Ingham   options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
105*e14dc268SJim Ingham                                   m_exact_match);
106*e14dc268SJim Ingham 
107*e14dc268SJim Ingham   return WrapOptionsDict(options_dict_sp);
108*e14dc268SJim Ingham }
109*e14dc268SJim Ingham 
11030fdc8d8SChris Lattner Searcher::CallbackReturn
111b9c1b51eSKate Stone BreakpointResolverFileLine::SearchCallback(SearchFilter &filter,
11230fdc8d8SChris Lattner                                            SymbolContext &context,
113b9c1b51eSKate Stone                                            Address *addr, bool containing) {
11430fdc8d8SChris Lattner   SymbolContextList sc_list;
11530fdc8d8SChris Lattner 
11630fdc8d8SChris Lattner   assert(m_breakpoint != NULL);
11730fdc8d8SChris Lattner 
118b9c1b51eSKate Stone   // There is a tricky bit here.  You can have two compilation units that
119b9c1b51eSKate Stone   // #include the same file, and
120b9c1b51eSKate Stone   // in one of them the function at m_line_number is used (and so code and a
121b9c1b51eSKate Stone   // line entry for it is generated) but in the
122b9c1b51eSKate Stone   // other it isn't.  If we considered the CU's independently, then in the
123b9c1b51eSKate Stone   // second inclusion, we'd move the breakpoint
124b9c1b51eSKate Stone   // to the next function that actually generated code in the header file.  That
125b9c1b51eSKate Stone   // would end up being confusing.
126b9c1b51eSKate Stone   // So instead, we do the CU iterations by hand here, then scan through the
127b9c1b51eSKate Stone   // complete list of matches, and figure out
128bc2f9182SJim Ingham   // the closest line number match, and only set breakpoints on that match.
129bc2f9182SJim Ingham 
130b9c1b51eSKate Stone   // Note also that if file_spec only had a file name and not a directory, there
131b9c1b51eSKate Stone   // may be many different file spec's in
132b9c1b51eSKate Stone   // the resultant list.  The closest line match for one will not be right for
133b9c1b51eSKate Stone   // some totally different file.
134b9c1b51eSKate Stone   // So we go through the match list and pull out the sets that have the same
135b9c1b51eSKate Stone   // file spec in their line_entry
136bc2f9182SJim Ingham   // and treat each set separately.
137bc2f9182SJim Ingham 
138c7bece56SGreg Clayton   const size_t num_comp_units = context.module_sp->GetNumCompileUnits();
139b9c1b51eSKate Stone   for (size_t i = 0; i < num_comp_units; i++) {
140bc2f9182SJim Ingham     CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i));
141b9c1b51eSKate Stone     if (cu_sp) {
1422dafd8edSGreg Clayton       if (filter.CompUnitPasses(*cu_sp))
143b9c1b51eSKate Stone         cu_sp->ResolveSymbolContext(m_file_spec, m_line_number, m_inlines,
144b9c1b51eSKate Stone                                     m_exact_match, eSymbolContextEverything,
145b9c1b51eSKate Stone                                     sc_list);
146bc2f9182SJim Ingham     }
1472dafd8edSGreg Clayton   }
14830fdc8d8SChris Lattner   StreamString s;
149b9c1b51eSKate Stone   s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"),
150969795f1SJim Ingham            m_line_number);
151f642373cSJim Ingham 
152f642373cSJim Ingham   SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetData());
153bc2f9182SJim Ingham 
15430fdc8d8SChris Lattner   return Searcher::eCallbackReturnContinue;
15530fdc8d8SChris Lattner }
15630fdc8d8SChris Lattner 
157b9c1b51eSKate Stone Searcher::Depth BreakpointResolverFileLine::GetDepth() {
158bc2f9182SJim Ingham   return Searcher::eDepthModule;
15930fdc8d8SChris Lattner }
16030fdc8d8SChris Lattner 
161b9c1b51eSKate Stone void BreakpointResolverFileLine::GetDescription(Stream *s) {
162b9c1b51eSKate Stone   s->Printf("file = '%s', line = %u, exact_match = %d",
163b9c1b51eSKate Stone             m_file_spec.GetPath().c_str(), m_line_number, m_exact_match);
16430fdc8d8SChris Lattner }
16530fdc8d8SChris Lattner 
166b9c1b51eSKate Stone void BreakpointResolverFileLine::Dump(Stream *s) const {}
16730fdc8d8SChris Lattner 
16833df7cd3SJim Ingham lldb::BreakpointResolverSP
169b9c1b51eSKate Stone BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) {
170b9c1b51eSKate Stone   lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine(
171b9c1b51eSKate Stone       &breakpoint, m_file_spec, m_line_number, m_offset, m_inlines,
172b9c1b51eSKate Stone       m_skip_prologue, m_exact_match));
17333df7cd3SJim Ingham 
17433df7cd3SJim Ingham   return ret_sp;
17533df7cd3SJim Ingham }
176