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" 1830fdc8d8SChris Lattner #include "lldb/Core/StreamString.h" 1930fdc8d8SChris Lattner #include "lldb/lldb-private-log.h" 2030fdc8d8SChris Lattner 2130fdc8d8SChris Lattner using namespace lldb; 2230fdc8d8SChris Lattner using namespace lldb_private; 2330fdc8d8SChris Lattner 2430fdc8d8SChris Lattner //---------------------------------------------------------------------- 2530fdc8d8SChris Lattner // BreakpointResolverFileLine: 2630fdc8d8SChris Lattner //---------------------------------------------------------------------- 2730fdc8d8SChris Lattner BreakpointResolverFileLine::BreakpointResolverFileLine 2830fdc8d8SChris Lattner ( 2930fdc8d8SChris Lattner Breakpoint *bkpt, 3030fdc8d8SChris Lattner const FileSpec &file_spec, 3130fdc8d8SChris Lattner uint32_t line_no, 3230fdc8d8SChris Lattner bool check_inlines 3330fdc8d8SChris Lattner ) : 34b7234e40SJohnny Chen BreakpointResolver (bkpt, BreakpointResolver::FileLineResolver), 3530fdc8d8SChris Lattner m_file_spec (file_spec), 3630fdc8d8SChris Lattner m_line_number (line_no), 3730fdc8d8SChris Lattner m_inlines (check_inlines) 3830fdc8d8SChris Lattner { 3930fdc8d8SChris Lattner } 4030fdc8d8SChris Lattner 4130fdc8d8SChris Lattner BreakpointResolverFileLine::~BreakpointResolverFileLine () 4230fdc8d8SChris Lattner { 4330fdc8d8SChris Lattner } 4430fdc8d8SChris Lattner 4530fdc8d8SChris Lattner Searcher::CallbackReturn 4630fdc8d8SChris Lattner BreakpointResolverFileLine::SearchCallback 4730fdc8d8SChris Lattner ( 4830fdc8d8SChris Lattner SearchFilter &filter, 4930fdc8d8SChris Lattner SymbolContext &context, 5030fdc8d8SChris Lattner Address *addr, 5130fdc8d8SChris Lattner bool containing 5230fdc8d8SChris Lattner ) 5330fdc8d8SChris Lattner { 5430fdc8d8SChris Lattner SymbolContextList sc_list; 5530fdc8d8SChris Lattner 5630fdc8d8SChris Lattner assert (m_breakpoint != NULL); 572d4edfbcSGreg Clayton LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 5830fdc8d8SChris Lattner 59bc2f9182SJim Ingham // There is a tricky bit here. You can have two compilation units that #include the same file, and 60bc2f9182SJim Ingham // in one of them the function at m_line_number is used (and so code and a line entry for it is generated) but in the 61bc2f9182SJim Ingham // other it isn't. If we considered the CU's independently, then in the second inclusion, we'd move the breakpoint 62bc2f9182SJim Ingham // to the next function that actually generated code in the header file. That would end up being confusing. 63bc2f9182SJim Ingham // So instead, we do the CU iterations by hand here, then scan through the complete list of matches, and figure out 64bc2f9182SJim Ingham // the closest line number match, and only set breakpoints on that match. 65bc2f9182SJim Ingham 66bc2f9182SJim Ingham // Note also that if file_spec only had a file name and not a directory, there may be many different file spec's in 67bc2f9182SJim Ingham // the resultant list. The closest line match for one will not be right for some totally different file. 68bc2f9182SJim Ingham // So we go through the match list and pull out the sets that have the same file spec in their line_entry 69bc2f9182SJim Ingham // and treat each set separately. 70bc2f9182SJim Ingham 71bc2f9182SJim Ingham uint32_t num_comp_units = context.module_sp->GetNumCompileUnits(); 72bc2f9182SJim Ingham for (uint32_t i = 0; i < num_comp_units; i++) 73bc2f9182SJim Ingham { 74bc2f9182SJim Ingham CompUnitSP cu_sp (context.module_sp->GetCompileUnitAtIndex (i)); 75*2dafd8edSGreg Clayton if (cu_sp) 76*2dafd8edSGreg Clayton { 77*2dafd8edSGreg Clayton if (filter.CompUnitPasses(*cu_sp)) 78bc2f9182SJim Ingham cu_sp->ResolveSymbolContext (m_file_spec, m_line_number, m_inlines, false, eSymbolContextEverything, sc_list); 79bc2f9182SJim Ingham } 80*2dafd8edSGreg Clayton } 81bc2f9182SJim Ingham 82bc2f9182SJim Ingham while (sc_list.GetSize() > 0) 83bc2f9182SJim Ingham { 84bc2f9182SJim Ingham SymbolContextList tmp_sc_list; 85bc2f9182SJim Ingham int current_idx = 0; 86bc2f9182SJim Ingham SymbolContext sc; 87bc2f9182SJim Ingham bool first_entry = true; 88bc2f9182SJim Ingham 89bc2f9182SJim Ingham FileSpec match_file_spec; 90bc2f9182SJim Ingham uint32_t closest_line_number = UINT32_MAX; 91bc2f9182SJim Ingham 92bc2f9182SJim Ingham // Pull out the first entry, and all the others that match its file spec, and stuff them in the tmp list. 93bc2f9182SJim Ingham while (current_idx < sc_list.GetSize()) 94bc2f9182SJim Ingham { 95bc2f9182SJim Ingham bool matches; 96bc2f9182SJim Ingham 97bc2f9182SJim Ingham sc_list.GetContextAtIndex (current_idx, sc); 98bc2f9182SJim Ingham if (first_entry) 99bc2f9182SJim Ingham { 100bc2f9182SJim Ingham match_file_spec = sc.line_entry.file; 101bc2f9182SJim Ingham matches = true; 102bc2f9182SJim Ingham first_entry = false; 103bc2f9182SJim Ingham } 104bc2f9182SJim Ingham else 105bc2f9182SJim Ingham matches = (sc.line_entry.file == match_file_spec); 106bc2f9182SJim Ingham 107bc2f9182SJim Ingham if (matches) 108bc2f9182SJim Ingham { 109bc2f9182SJim Ingham tmp_sc_list.Append (sc); 110bc2f9182SJim Ingham sc_list.RemoveContextAtIndex(current_idx); 111bc2f9182SJim Ingham 112bc2f9182SJim Ingham // ResolveSymbolContext will always return a number that is >= the line number you pass in. 113bc2f9182SJim Ingham // So the smaller line number is always better. 114bc2f9182SJim Ingham if (sc.line_entry.line < closest_line_number) 115bc2f9182SJim Ingham closest_line_number = sc.line_entry.line; 116bc2f9182SJim Ingham } 117bc2f9182SJim Ingham else 118bc2f9182SJim Ingham current_idx++; 119bc2f9182SJim Ingham } 120bc2f9182SJim Ingham 121bc2f9182SJim Ingham // Okay, we've found the closest line number match, now throw away all the others, 122bc2f9182SJim Ingham // and make breakpoints out of the closest line number match. 123bc2f9182SJim Ingham 124bc2f9182SJim Ingham uint32_t tmp_sc_list_size = tmp_sc_list.GetSize(); 125bc2f9182SJim Ingham 126bc2f9182SJim Ingham for (uint32_t i = 0; i < tmp_sc_list_size; i++) 12730fdc8d8SChris Lattner { 12830fdc8d8SChris Lattner SymbolContext sc; 129bc2f9182SJim Ingham if (tmp_sc_list.GetContextAtIndex(i, sc)) 130bc2f9182SJim Ingham { 131bc2f9182SJim Ingham if (sc.line_entry.line == closest_line_number) 13230fdc8d8SChris Lattner { 13330fdc8d8SChris Lattner Address line_start = sc.line_entry.range.GetBaseAddress(); 13430fdc8d8SChris Lattner if (line_start.IsValid()) 13530fdc8d8SChris Lattner { 136969795f1SJim Ingham if (filter.AddressPasses(line_start)) 137969795f1SJim Ingham { 13830fdc8d8SChris Lattner BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(line_start)); 13930fdc8d8SChris Lattner if (log && bp_loc_sp && !m_breakpoint->IsInternal()) 14030fdc8d8SChris Lattner { 14130fdc8d8SChris Lattner StreamString s; 14230fdc8d8SChris Lattner bp_loc_sp->GetDescription (&s, lldb::eDescriptionLevelVerbose); 14330fdc8d8SChris Lattner log->Printf ("Added location: %s\n", s.GetData()); 14430fdc8d8SChris Lattner } 14530fdc8d8SChris Lattner } 146969795f1SJim Ingham else if (log) 147969795f1SJim Ingham { 148969795f1SJim Ingham log->Printf ("Breakpoint at file address 0x%llx for %s:%d didn't pass the filter.\n", 149969795f1SJim Ingham line_start.GetFileAddress(), 150969795f1SJim Ingham m_file_spec.GetFilename().AsCString("<Unknown>"), 151969795f1SJim Ingham m_line_number); 152969795f1SJim Ingham } 153969795f1SJim Ingham } 15430fdc8d8SChris Lattner else 15530fdc8d8SChris Lattner { 15630fdc8d8SChris Lattner if (log) 15730fdc8d8SChris Lattner log->Printf ("error: Unable to set breakpoint at file address 0x%llx for %s:%d\n", 15830fdc8d8SChris Lattner line_start.GetFileAddress(), 15930fdc8d8SChris Lattner m_file_spec.GetFilename().AsCString("<Unknown>"), 16030fdc8d8SChris Lattner m_line_number); 16130fdc8d8SChris Lattner } 16230fdc8d8SChris Lattner } 16330fdc8d8SChris Lattner else 16430fdc8d8SChris Lattner { 16530fdc8d8SChris Lattner #if 0 16630fdc8d8SChris Lattner s << "error: Breakpoint at '" << pos->c_str() << "' isn't resolved yet: \n"; 16730fdc8d8SChris Lattner if (sc.line_entry.address.Dump(&s, Address::DumpStyleSectionNameOffset)) 16830fdc8d8SChris Lattner s.EOL(); 16930fdc8d8SChris Lattner if (sc.line_entry.address.Dump(&s, Address::DumpStyleSectionPointerOffset)) 17030fdc8d8SChris Lattner s.EOL(); 17130fdc8d8SChris Lattner if (sc.line_entry.address.Dump(&s, Address::DumpStyleFileAddress)) 17230fdc8d8SChris Lattner s.EOL(); 17330fdc8d8SChris Lattner if (sc.line_entry.address.Dump(&s, Address::DumpStyleLoadAddress)) 17430fdc8d8SChris Lattner s.EOL(); 17530fdc8d8SChris Lattner #endif 17630fdc8d8SChris Lattner } 17730fdc8d8SChris Lattner } 178bc2f9182SJim Ingham } 179bc2f9182SJim Ingham } 180bc2f9182SJim Ingham 18130fdc8d8SChris Lattner return Searcher::eCallbackReturnContinue; 18230fdc8d8SChris Lattner } 18330fdc8d8SChris Lattner 18430fdc8d8SChris Lattner Searcher::Depth 18530fdc8d8SChris Lattner BreakpointResolverFileLine::GetDepth() 18630fdc8d8SChris Lattner { 187bc2f9182SJim Ingham return Searcher::eDepthModule; 18830fdc8d8SChris Lattner } 18930fdc8d8SChris Lattner 19030fdc8d8SChris Lattner void 19130fdc8d8SChris Lattner BreakpointResolverFileLine::GetDescription (Stream *s) 19230fdc8d8SChris Lattner { 1930c5cd90dSGreg Clayton s->Printf ("file ='%s', line = %u", m_file_spec.GetFilename().AsCString(), m_line_number); 19430fdc8d8SChris Lattner } 19530fdc8d8SChris Lattner 19630fdc8d8SChris Lattner void 19730fdc8d8SChris Lattner BreakpointResolverFileLine::Dump (Stream *s) const 19830fdc8d8SChris Lattner { 19930fdc8d8SChris Lattner 20030fdc8d8SChris Lattner } 20130fdc8d8SChris Lattner 202