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" 19*a8558b62SJim Ingham #include "lldb/Target/Target.h" 2030fdc8d8SChris Lattner #include "lldb/lldb-private-log.h" 2130fdc8d8SChris Lattner 2230fdc8d8SChris Lattner using namespace lldb; 2330fdc8d8SChris Lattner using namespace lldb_private; 2430fdc8d8SChris Lattner 2530fdc8d8SChris Lattner //---------------------------------------------------------------------- 2630fdc8d8SChris Lattner // BreakpointResolverFileLine: 2730fdc8d8SChris Lattner //---------------------------------------------------------------------- 2830fdc8d8SChris Lattner BreakpointResolverFileLine::BreakpointResolverFileLine 2930fdc8d8SChris Lattner ( 3030fdc8d8SChris Lattner Breakpoint *bkpt, 3130fdc8d8SChris Lattner const FileSpec &file_spec, 3230fdc8d8SChris Lattner uint32_t line_no, 33*a8558b62SJim Ingham bool check_inlines, 34*a8558b62SJim Ingham bool skip_prologue 3530fdc8d8SChris Lattner ) : 36b7234e40SJohnny Chen BreakpointResolver (bkpt, BreakpointResolver::FileLineResolver), 3730fdc8d8SChris Lattner m_file_spec (file_spec), 3830fdc8d8SChris Lattner m_line_number (line_no), 39*a8558b62SJim Ingham m_inlines (check_inlines), 40*a8558b62SJim Ingham m_skip_prologue(skip_prologue) 4130fdc8d8SChris Lattner { 4230fdc8d8SChris Lattner } 4330fdc8d8SChris Lattner 4430fdc8d8SChris Lattner BreakpointResolverFileLine::~BreakpointResolverFileLine () 4530fdc8d8SChris Lattner { 4630fdc8d8SChris Lattner } 4730fdc8d8SChris Lattner 4830fdc8d8SChris Lattner Searcher::CallbackReturn 4930fdc8d8SChris Lattner BreakpointResolverFileLine::SearchCallback 5030fdc8d8SChris Lattner ( 5130fdc8d8SChris Lattner SearchFilter &filter, 5230fdc8d8SChris Lattner SymbolContext &context, 5330fdc8d8SChris Lattner Address *addr, 5430fdc8d8SChris Lattner bool containing 5530fdc8d8SChris Lattner ) 5630fdc8d8SChris Lattner { 5730fdc8d8SChris Lattner SymbolContextList sc_list; 5830fdc8d8SChris Lattner 5930fdc8d8SChris Lattner assert (m_breakpoint != NULL); 602d4edfbcSGreg Clayton LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 6130fdc8d8SChris Lattner 62bc2f9182SJim Ingham // There is a tricky bit here. You can have two compilation units that #include the same file, and 63bc2f9182SJim 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 64bc2f9182SJim Ingham // other it isn't. If we considered the CU's independently, then in the second inclusion, we'd move the breakpoint 65bc2f9182SJim Ingham // to the next function that actually generated code in the header file. That would end up being confusing. 66bc2f9182SJim Ingham // So instead, we do the CU iterations by hand here, then scan through the complete list of matches, and figure out 67bc2f9182SJim Ingham // the closest line number match, and only set breakpoints on that match. 68bc2f9182SJim Ingham 69bc2f9182SJim 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 70bc2f9182SJim Ingham // the resultant list. The closest line match for one will not be right for some totally different file. 71bc2f9182SJim Ingham // So we go through the match list and pull out the sets that have the same file spec in their line_entry 72bc2f9182SJim Ingham // and treat each set separately. 73bc2f9182SJim Ingham 74bc2f9182SJim Ingham uint32_t num_comp_units = context.module_sp->GetNumCompileUnits(); 75bc2f9182SJim Ingham for (uint32_t i = 0; i < num_comp_units; i++) 76bc2f9182SJim Ingham { 77bc2f9182SJim Ingham CompUnitSP cu_sp (context.module_sp->GetCompileUnitAtIndex (i)); 782dafd8edSGreg Clayton if (cu_sp) 792dafd8edSGreg Clayton { 802dafd8edSGreg Clayton if (filter.CompUnitPasses(*cu_sp)) 81bc2f9182SJim Ingham cu_sp->ResolveSymbolContext (m_file_spec, m_line_number, m_inlines, false, eSymbolContextEverything, sc_list); 82bc2f9182SJim Ingham } 832dafd8edSGreg Clayton } 84bc2f9182SJim Ingham 85bc2f9182SJim Ingham while (sc_list.GetSize() > 0) 86bc2f9182SJim Ingham { 87bc2f9182SJim Ingham SymbolContextList tmp_sc_list; 88bc2f9182SJim Ingham int current_idx = 0; 89bc2f9182SJim Ingham SymbolContext sc; 90bc2f9182SJim Ingham bool first_entry = true; 91bc2f9182SJim Ingham 92bc2f9182SJim Ingham FileSpec match_file_spec; 93bc2f9182SJim Ingham uint32_t closest_line_number = UINT32_MAX; 94bc2f9182SJim Ingham 95bc2f9182SJim Ingham // Pull out the first entry, and all the others that match its file spec, and stuff them in the tmp list. 96bc2f9182SJim Ingham while (current_idx < sc_list.GetSize()) 97bc2f9182SJim Ingham { 98bc2f9182SJim Ingham bool matches; 99bc2f9182SJim Ingham 100bc2f9182SJim Ingham sc_list.GetContextAtIndex (current_idx, sc); 101bc2f9182SJim Ingham if (first_entry) 102bc2f9182SJim Ingham { 103bc2f9182SJim Ingham match_file_spec = sc.line_entry.file; 104bc2f9182SJim Ingham matches = true; 105bc2f9182SJim Ingham first_entry = false; 106bc2f9182SJim Ingham } 107bc2f9182SJim Ingham else 108bc2f9182SJim Ingham matches = (sc.line_entry.file == match_file_spec); 109bc2f9182SJim Ingham 110bc2f9182SJim Ingham if (matches) 111bc2f9182SJim Ingham { 112bc2f9182SJim Ingham tmp_sc_list.Append (sc); 113bc2f9182SJim Ingham sc_list.RemoveContextAtIndex(current_idx); 114bc2f9182SJim Ingham 115bc2f9182SJim Ingham // ResolveSymbolContext will always return a number that is >= the line number you pass in. 116bc2f9182SJim Ingham // So the smaller line number is always better. 117bc2f9182SJim Ingham if (sc.line_entry.line < closest_line_number) 118bc2f9182SJim Ingham closest_line_number = sc.line_entry.line; 119bc2f9182SJim Ingham } 120bc2f9182SJim Ingham else 121bc2f9182SJim Ingham current_idx++; 122bc2f9182SJim Ingham } 123bc2f9182SJim Ingham 124bc2f9182SJim Ingham // Okay, we've found the closest line number match, now throw away all the others, 125bc2f9182SJim Ingham // and make breakpoints out of the closest line number match. 126bc2f9182SJim Ingham 127bc2f9182SJim Ingham uint32_t tmp_sc_list_size = tmp_sc_list.GetSize(); 128bc2f9182SJim Ingham 129bc2f9182SJim Ingham for (uint32_t i = 0; i < tmp_sc_list_size; i++) 13030fdc8d8SChris Lattner { 13130fdc8d8SChris Lattner SymbolContext sc; 132bc2f9182SJim Ingham if (tmp_sc_list.GetContextAtIndex(i, sc)) 133bc2f9182SJim Ingham { 134bc2f9182SJim Ingham if (sc.line_entry.line == closest_line_number) 13530fdc8d8SChris Lattner { 13630fdc8d8SChris Lattner Address line_start = sc.line_entry.range.GetBaseAddress(); 13730fdc8d8SChris Lattner if (line_start.IsValid()) 13830fdc8d8SChris Lattner { 139969795f1SJim Ingham if (filter.AddressPasses(line_start)) 140969795f1SJim Ingham { 141*a8558b62SJim Ingham // If the line number is before the prologue end, move it there... 142*a8558b62SJim Ingham bool skipped_prologue = false; 143*a8558b62SJim Ingham if (m_skip_prologue) 144*a8558b62SJim Ingham { 145*a8558b62SJim Ingham if (sc.function) 146*a8558b62SJim Ingham { 147*a8558b62SJim Ingham Address prologue_addr(sc.function->GetAddressRange().GetBaseAddress()); 148*a8558b62SJim Ingham if (prologue_addr.IsValid() && (line_start == prologue_addr)) 149*a8558b62SJim Ingham { 150*a8558b62SJim Ingham const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize(); 151*a8558b62SJim Ingham if (prologue_byte_size) 152*a8558b62SJim Ingham { 153*a8558b62SJim Ingham prologue_addr.Slide(prologue_byte_size); 154*a8558b62SJim Ingham 155*a8558b62SJim Ingham if (filter.AddressPasses(prologue_addr)) 156*a8558b62SJim Ingham { 157*a8558b62SJim Ingham skipped_prologue = true; 158*a8558b62SJim Ingham line_start = prologue_addr; 159*a8558b62SJim Ingham } 160*a8558b62SJim Ingham } 161*a8558b62SJim Ingham } 162*a8558b62SJim Ingham } 163*a8558b62SJim Ingham } 164*a8558b62SJim Ingham 16530fdc8d8SChris Lattner BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(line_start)); 16630fdc8d8SChris Lattner if (log && bp_loc_sp && !m_breakpoint->IsInternal()) 16730fdc8d8SChris Lattner { 16830fdc8d8SChris Lattner StreamString s; 16930fdc8d8SChris Lattner bp_loc_sp->GetDescription (&s, lldb::eDescriptionLevelVerbose); 170*a8558b62SJim Ingham log->Printf ("Added location (skipped prologue: %s): %s \n", skipped_prologue ? "yes" : "no", s.GetData()); 17130fdc8d8SChris Lattner } 17230fdc8d8SChris Lattner } 173969795f1SJim Ingham else if (log) 174969795f1SJim Ingham { 175969795f1SJim Ingham log->Printf ("Breakpoint at file address 0x%llx for %s:%d didn't pass the filter.\n", 176969795f1SJim Ingham line_start.GetFileAddress(), 177969795f1SJim Ingham m_file_spec.GetFilename().AsCString("<Unknown>"), 178969795f1SJim Ingham m_line_number); 179969795f1SJim Ingham } 180969795f1SJim Ingham } 18130fdc8d8SChris Lattner else 18230fdc8d8SChris Lattner { 18330fdc8d8SChris Lattner if (log) 18430fdc8d8SChris Lattner log->Printf ("error: Unable to set breakpoint at file address 0x%llx for %s:%d\n", 18530fdc8d8SChris Lattner line_start.GetFileAddress(), 18630fdc8d8SChris Lattner m_file_spec.GetFilename().AsCString("<Unknown>"), 18730fdc8d8SChris Lattner m_line_number); 18830fdc8d8SChris Lattner } 18930fdc8d8SChris Lattner } 19030fdc8d8SChris Lattner else 19130fdc8d8SChris Lattner { 19230fdc8d8SChris Lattner #if 0 19330fdc8d8SChris Lattner s << "error: Breakpoint at '" << pos->c_str() << "' isn't resolved yet: \n"; 19430fdc8d8SChris Lattner if (sc.line_entry.address.Dump(&s, Address::DumpStyleSectionNameOffset)) 19530fdc8d8SChris Lattner s.EOL(); 19630fdc8d8SChris Lattner if (sc.line_entry.address.Dump(&s, Address::DumpStyleSectionPointerOffset)) 19730fdc8d8SChris Lattner s.EOL(); 19830fdc8d8SChris Lattner if (sc.line_entry.address.Dump(&s, Address::DumpStyleFileAddress)) 19930fdc8d8SChris Lattner s.EOL(); 20030fdc8d8SChris Lattner if (sc.line_entry.address.Dump(&s, Address::DumpStyleLoadAddress)) 20130fdc8d8SChris Lattner s.EOL(); 20230fdc8d8SChris Lattner #endif 20330fdc8d8SChris Lattner } 20430fdc8d8SChris Lattner } 205bc2f9182SJim Ingham } 206bc2f9182SJim Ingham } 207bc2f9182SJim Ingham 20830fdc8d8SChris Lattner return Searcher::eCallbackReturnContinue; 20930fdc8d8SChris Lattner } 21030fdc8d8SChris Lattner 21130fdc8d8SChris Lattner Searcher::Depth 21230fdc8d8SChris Lattner BreakpointResolverFileLine::GetDepth() 21330fdc8d8SChris Lattner { 214bc2f9182SJim Ingham return Searcher::eDepthModule; 21530fdc8d8SChris Lattner } 21630fdc8d8SChris Lattner 21730fdc8d8SChris Lattner void 21830fdc8d8SChris Lattner BreakpointResolverFileLine::GetDescription (Stream *s) 21930fdc8d8SChris Lattner { 2200c5cd90dSGreg Clayton s->Printf ("file ='%s', line = %u", m_file_spec.GetFilename().AsCString(), m_line_number); 22130fdc8d8SChris Lattner } 22230fdc8d8SChris Lattner 22330fdc8d8SChris Lattner void 22430fdc8d8SChris Lattner BreakpointResolverFileLine::Dump (Stream *s) const 22530fdc8d8SChris Lattner { 22630fdc8d8SChris Lattner 22730fdc8d8SChris Lattner } 22830fdc8d8SChris Lattner 229