1 //===-- CommandObjectSource.cpp ---------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "CommandObjectSource.h" 10 11 #include "lldb/Core/Debugger.h" 12 #include "lldb/Core/FileLineResolver.h" 13 #include "lldb/Core/Module.h" 14 #include "lldb/Core/ModuleSpec.h" 15 #include "lldb/Core/SourceManager.h" 16 #include "lldb/Host/OptionParser.h" 17 #include "lldb/Interpreter/CommandCompletions.h" 18 #include "lldb/Interpreter/CommandInterpreter.h" 19 #include "lldb/Interpreter/CommandReturnObject.h" 20 #include "lldb/Interpreter/OptionArgParser.h" 21 #include "lldb/Interpreter/Options.h" 22 #include "lldb/Symbol/CompileUnit.h" 23 #include "lldb/Symbol/Function.h" 24 #include "lldb/Symbol/Symbol.h" 25 #include "lldb/Target/Process.h" 26 #include "lldb/Target/SectionLoadList.h" 27 #include "lldb/Target/StackFrame.h" 28 #include "lldb/Target/TargetList.h" 29 #include "lldb/Utility/FileSpec.h" 30 31 using namespace lldb; 32 using namespace lldb_private; 33 34 #pragma mark CommandObjectSourceInfo 35 //---------------------------------------------------------------------- 36 // CommandObjectSourceInfo - debug line entries dumping command 37 //---------------------------------------------------------------------- 38 39 static constexpr OptionDefinition g_source_info_options[] = { 40 // clang-format off 41 { LLDB_OPT_SET_ALL, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCount, "The number of line entries to display." }, 42 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "shlib", 's', OptionParser::eRequiredArgument, nullptr, {}, CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Look up the source in the given module or shared library (can be specified more than once)." }, 43 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, {}, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source." }, 44 { LLDB_OPT_SET_1, false, "line", 'l', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeLineNum, "The line number at which to start the displaying lines." }, 45 { LLDB_OPT_SET_1, false, "end-line", 'e', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeLineNum, "The line number at which to stop displaying lines." }, 46 { LLDB_OPT_SET_2, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, {}, CommandCompletions::eSymbolCompletion, eArgTypeSymbol, "The name of a function whose source to display." }, 47 { LLDB_OPT_SET_3, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeAddressOrExpression, "Lookup the address and display the source information for the corresponding file and line." }, 48 // clang-format on 49 }; 50 51 class CommandObjectSourceInfo : public CommandObjectParsed { 52 class CommandOptions : public Options { 53 public: 54 CommandOptions() : Options() {} 55 56 ~CommandOptions() override = default; 57 58 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 59 ExecutionContext *execution_context) override { 60 Status error; 61 const int short_option = GetDefinitions()[option_idx].short_option; 62 switch (short_option) { 63 case 'l': 64 if (option_arg.getAsInteger(0, start_line)) 65 error.SetErrorStringWithFormat("invalid line number: '%s'", 66 option_arg.str().c_str()); 67 break; 68 69 case 'e': 70 if (option_arg.getAsInteger(0, end_line)) 71 error.SetErrorStringWithFormat("invalid line number: '%s'", 72 option_arg.str().c_str()); 73 break; 74 75 case 'c': 76 if (option_arg.getAsInteger(0, num_lines)) 77 error.SetErrorStringWithFormat("invalid line count: '%s'", 78 option_arg.str().c_str()); 79 break; 80 81 case 'f': 82 file_name = option_arg; 83 break; 84 85 case 'n': 86 symbol_name = option_arg; 87 break; 88 89 case 'a': { 90 address = OptionArgParser::ToAddress(execution_context, option_arg, 91 LLDB_INVALID_ADDRESS, &error); 92 } break; 93 case 's': 94 modules.push_back(std::string(option_arg)); 95 break; 96 default: 97 error.SetErrorStringWithFormat("unrecognized short option '%c'", 98 short_option); 99 break; 100 } 101 102 return error; 103 } 104 105 void OptionParsingStarting(ExecutionContext *execution_context) override { 106 file_spec.Clear(); 107 file_name.clear(); 108 symbol_name.clear(); 109 address = LLDB_INVALID_ADDRESS; 110 start_line = 0; 111 end_line = 0; 112 num_lines = 0; 113 modules.clear(); 114 } 115 116 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 117 return llvm::makeArrayRef(g_source_info_options); 118 } 119 120 // Instance variables to hold the values for command options. 121 FileSpec file_spec; 122 std::string file_name; 123 std::string symbol_name; 124 lldb::addr_t address; 125 uint32_t start_line; 126 uint32_t end_line; 127 uint32_t num_lines; 128 STLStringArray modules; 129 }; 130 131 public: 132 CommandObjectSourceInfo(CommandInterpreter &interpreter) 133 : CommandObjectParsed( 134 interpreter, "source info", 135 "Display source line information for the current target " 136 "process. Defaults to instruction pointer in current stack " 137 "frame.", 138 nullptr, eCommandRequiresTarget), 139 m_options() {} 140 141 ~CommandObjectSourceInfo() override = default; 142 143 Options *GetOptions() override { return &m_options; } 144 145 protected: 146 // Dump the line entries in each symbol context. Return the number of entries 147 // found. If module_list is set, only dump lines contained in one of the 148 // modules. If file_spec is set, only dump lines in the file. If the 149 // start_line option was specified, don't print lines less than start_line. 150 // If the end_line option was specified, don't print lines greater than 151 // end_line. If the num_lines option was specified, dont print more than 152 // num_lines entries. 153 uint32_t DumpLinesInSymbolContexts(Stream &strm, 154 const SymbolContextList &sc_list, 155 const ModuleList &module_list, 156 const FileSpec &file_spec) { 157 uint32_t start_line = m_options.start_line; 158 uint32_t end_line = m_options.end_line; 159 uint32_t num_lines = m_options.num_lines; 160 Target *target = m_exe_ctx.GetTargetPtr(); 161 162 uint32_t num_matches = 0; 163 bool has_path = false; 164 if (file_spec) { 165 assert(file_spec.GetFilename().AsCString()); 166 has_path = (file_spec.GetDirectory().AsCString() != nullptr); 167 } 168 169 // Dump all the line entries for the file in the list. 170 ConstString last_module_file_name; 171 uint32_t num_scs = sc_list.GetSize(); 172 for (uint32_t i = 0; i < num_scs; ++i) { 173 SymbolContext sc; 174 sc_list.GetContextAtIndex(i, sc); 175 if (sc.comp_unit) { 176 Module *module = sc.module_sp.get(); 177 CompileUnit *cu = sc.comp_unit; 178 const LineEntry &line_entry = sc.line_entry; 179 assert(module && cu); 180 181 // Are we looking for specific modules, files or lines? 182 if (module_list.GetSize() && 183 module_list.GetIndexForModule(module) == LLDB_INVALID_INDEX32) 184 continue; 185 if (file_spec && 186 !lldb_private::FileSpec::Equal(file_spec, line_entry.file, 187 has_path)) 188 continue; 189 if (start_line > 0 && line_entry.line < start_line) 190 continue; 191 if (end_line > 0 && line_entry.line > end_line) 192 continue; 193 if (num_lines > 0 && num_matches > num_lines) 194 continue; 195 196 // Print a new header if the module changed. 197 const ConstString &module_file_name = 198 module->GetFileSpec().GetFilename(); 199 assert(module_file_name); 200 if (module_file_name != last_module_file_name) { 201 if (num_matches > 0) 202 strm << "\n\n"; 203 strm << "Lines found in module `" << module_file_name << "\n"; 204 } 205 // Dump the line entry. 206 line_entry.GetDescription(&strm, lldb::eDescriptionLevelBrief, cu, 207 target, /*show_address_only=*/false); 208 strm << "\n"; 209 last_module_file_name = module_file_name; 210 num_matches++; 211 } 212 } 213 return num_matches; 214 } 215 216 // Dump the requested line entries for the file in the compilation unit. 217 // Return the number of entries found. If module_list is set, only dump lines 218 // contained in one of the modules. If the start_line option was specified, 219 // don't print lines less than start_line. If the end_line option was 220 // specified, don't print lines greater than end_line. If the num_lines 221 // option was specified, dont print more than num_lines entries. 222 uint32_t DumpFileLinesInCompUnit(Stream &strm, Module *module, 223 CompileUnit *cu, const FileSpec &file_spec) { 224 uint32_t start_line = m_options.start_line; 225 uint32_t end_line = m_options.end_line; 226 uint32_t num_lines = m_options.num_lines; 227 Target *target = m_exe_ctx.GetTargetPtr(); 228 229 uint32_t num_matches = 0; 230 assert(module); 231 if (cu) { 232 assert(file_spec.GetFilename().AsCString()); 233 bool has_path = (file_spec.GetDirectory().AsCString() != nullptr); 234 const FileSpecList &cu_file_list = cu->GetSupportFiles(); 235 size_t file_idx = cu_file_list.FindFileIndex(0, file_spec, has_path); 236 if (file_idx != UINT32_MAX) { 237 // Update the file to how it appears in the CU. 238 const FileSpec &cu_file_spec = 239 cu_file_list.GetFileSpecAtIndex(file_idx); 240 241 // Dump all matching lines at or above start_line for the file in the 242 // CU. 243 const ConstString &file_spec_name = file_spec.GetFilename(); 244 const ConstString &module_file_name = 245 module->GetFileSpec().GetFilename(); 246 bool cu_header_printed = false; 247 uint32_t line = start_line; 248 while (true) { 249 LineEntry line_entry; 250 251 // Find the lowest index of a line entry with a line equal to or 252 // higher than 'line'. 253 uint32_t start_idx = 0; 254 start_idx = cu->FindLineEntry(start_idx, line, &cu_file_spec, 255 /*exact=*/false, &line_entry); 256 if (start_idx == UINT32_MAX) 257 // No more line entries for our file in this CU. 258 break; 259 260 if (end_line > 0 && line_entry.line > end_line) 261 break; 262 263 // Loop through to find any other entries for this line, dumping 264 // each. 265 line = line_entry.line; 266 do { 267 num_matches++; 268 if (num_lines > 0 && num_matches > num_lines) 269 break; 270 assert(lldb_private::FileSpec::Equal(cu_file_spec, line_entry.file, 271 has_path)); 272 if (!cu_header_printed) { 273 if (num_matches > 0) 274 strm << "\n\n"; 275 strm << "Lines found for file " << file_spec_name 276 << " in compilation unit " << cu->GetFilename() << " in `" 277 << module_file_name << "\n"; 278 cu_header_printed = true; 279 } 280 line_entry.GetDescription(&strm, lldb::eDescriptionLevelBrief, cu, 281 target, /*show_address_only=*/false); 282 strm << "\n"; 283 284 // Anymore after this one? 285 start_idx++; 286 start_idx = cu->FindLineEntry(start_idx, line, &cu_file_spec, 287 /*exact=*/true, &line_entry); 288 } while (start_idx != UINT32_MAX); 289 290 // Try the next higher line, starting over at start_idx 0. 291 line++; 292 } 293 } 294 } 295 return num_matches; 296 } 297 298 // Dump the requested line entries for the file in the module. Return the 299 // number of entries found. If module_list is set, only dump lines contained 300 // in one of the modules. If the start_line option was specified, don't print 301 // lines less than start_line. If the end_line option was specified, don't 302 // print lines greater than end_line. If the num_lines option was specified, 303 // dont print more than num_lines entries. 304 uint32_t DumpFileLinesInModule(Stream &strm, Module *module, 305 const FileSpec &file_spec) { 306 uint32_t num_matches = 0; 307 if (module) { 308 // Look through all the compilation units (CUs) in this module for ones 309 // that contain lines of code from this source file. 310 for (size_t i = 0; i < module->GetNumCompileUnits(); i++) { 311 // Look for a matching source file in this CU. 312 CompUnitSP cu_sp(module->GetCompileUnitAtIndex(i)); 313 if (cu_sp) { 314 num_matches += 315 DumpFileLinesInCompUnit(strm, module, cu_sp.get(), file_spec); 316 } 317 } 318 } 319 return num_matches; 320 } 321 322 // Given an address and a list of modules, append the symbol contexts of all 323 // line entries containing the address found in the modules and return the 324 // count of matches. If none is found, return an error in 'error_strm'. 325 size_t GetSymbolContextsForAddress(const ModuleList &module_list, 326 lldb::addr_t addr, 327 SymbolContextList &sc_list, 328 StreamString &error_strm) { 329 Address so_addr; 330 size_t num_matches = 0; 331 assert(module_list.GetSize() > 0); 332 Target *target = m_exe_ctx.GetTargetPtr(); 333 if (target->GetSectionLoadList().IsEmpty()) { 334 // The target isn't loaded yet, we need to lookup the file address in all 335 // modules. Note: the module list option does not apply to addresses. 336 const size_t num_modules = module_list.GetSize(); 337 for (size_t i = 0; i < num_modules; ++i) { 338 ModuleSP module_sp(module_list.GetModuleAtIndex(i)); 339 if (!module_sp) 340 continue; 341 if (module_sp->ResolveFileAddress(addr, so_addr)) { 342 SymbolContext sc; 343 sc.Clear(true); 344 if (module_sp->ResolveSymbolContextForAddress( 345 so_addr, eSymbolContextEverything, sc) & 346 eSymbolContextLineEntry) { 347 sc_list.AppendIfUnique(sc, /*merge_symbol_into_function=*/false); 348 ++num_matches; 349 } 350 } 351 } 352 if (num_matches == 0) 353 error_strm.Printf("Source information for file address 0x%" PRIx64 354 " not found in any modules.\n", 355 addr); 356 } else { 357 // The target has some things loaded, resolve this address to a compile 358 // unit + file + line and display 359 if (target->GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) { 360 ModuleSP module_sp(so_addr.GetModule()); 361 // Check to make sure this module is in our list. 362 if (module_sp && 363 module_list.GetIndexForModule(module_sp.get()) != 364 LLDB_INVALID_INDEX32) { 365 SymbolContext sc; 366 sc.Clear(true); 367 if (module_sp->ResolveSymbolContextForAddress( 368 so_addr, eSymbolContextEverything, sc) & 369 eSymbolContextLineEntry) { 370 sc_list.AppendIfUnique(sc, /*merge_symbol_into_function=*/false); 371 ++num_matches; 372 } else { 373 StreamString addr_strm; 374 so_addr.Dump(&addr_strm, nullptr, 375 Address::DumpStyleModuleWithFileAddress); 376 error_strm.Printf( 377 "Address 0x%" PRIx64 " resolves to %s, but there is" 378 " no source information available for this address.\n", 379 addr, addr_strm.GetData()); 380 } 381 } else { 382 StreamString addr_strm; 383 so_addr.Dump(&addr_strm, nullptr, 384 Address::DumpStyleModuleWithFileAddress); 385 error_strm.Printf("Address 0x%" PRIx64 386 " resolves to %s, but it cannot" 387 " be found in any modules.\n", 388 addr, addr_strm.GetData()); 389 } 390 } else 391 error_strm.Printf("Unable to resolve address 0x%" PRIx64 ".\n", addr); 392 } 393 return num_matches; 394 } 395 396 // Dump the line entries found in functions matching the name specified in 397 // the option. 398 bool DumpLinesInFunctions(CommandReturnObject &result) { 399 SymbolContextList sc_list_funcs; 400 ConstString name(m_options.symbol_name.c_str()); 401 SymbolContextList sc_list_lines; 402 Target *target = m_exe_ctx.GetTargetPtr(); 403 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 404 405 // Note: module_list can't be const& because FindFunctionSymbols isn't 406 // const. 407 ModuleList module_list = 408 (m_module_list.GetSize() > 0) ? m_module_list : target->GetImages(); 409 size_t num_matches = 410 module_list.FindFunctions(name, eFunctionNameTypeAuto, 411 /*include_symbols=*/false, 412 /*include_inlines=*/true, 413 /*append=*/true, sc_list_funcs); 414 if (!num_matches) { 415 // If we didn't find any functions with that name, try searching for 416 // symbols that line up exactly with function addresses. 417 SymbolContextList sc_list_symbols; 418 size_t num_symbol_matches = module_list.FindFunctionSymbols( 419 name, eFunctionNameTypeAuto, sc_list_symbols); 420 for (size_t i = 0; i < num_symbol_matches; i++) { 421 SymbolContext sc; 422 sc_list_symbols.GetContextAtIndex(i, sc); 423 if (sc.symbol && sc.symbol->ValueIsAddress()) { 424 const Address &base_address = sc.symbol->GetAddressRef(); 425 Function *function = base_address.CalculateSymbolContextFunction(); 426 if (function) { 427 sc_list_funcs.Append(SymbolContext(function)); 428 num_matches++; 429 } 430 } 431 } 432 } 433 if (num_matches == 0) { 434 result.AppendErrorWithFormat("Could not find function named \'%s\'.\n", 435 m_options.symbol_name.c_str()); 436 return false; 437 } 438 for (size_t i = 0; i < num_matches; i++) { 439 SymbolContext sc; 440 sc_list_funcs.GetContextAtIndex(i, sc); 441 bool context_found_for_symbol = false; 442 // Loop through all the ranges in the function. 443 AddressRange range; 444 for (uint32_t r = 0; 445 sc.GetAddressRange(eSymbolContextEverything, r, 446 /*use_inline_block_range=*/true, range); 447 ++r) { 448 // Append the symbol contexts for each address in the range to 449 // sc_list_lines. 450 const Address &base_address = range.GetBaseAddress(); 451 const addr_t size = range.GetByteSize(); 452 lldb::addr_t start_addr = base_address.GetLoadAddress(target); 453 if (start_addr == LLDB_INVALID_ADDRESS) 454 start_addr = base_address.GetFileAddress(); 455 lldb::addr_t end_addr = start_addr + size; 456 for (lldb::addr_t addr = start_addr; addr < end_addr; 457 addr += addr_byte_size) { 458 StreamString error_strm; 459 if (!GetSymbolContextsForAddress(module_list, addr, sc_list_lines, 460 error_strm)) 461 result.AppendWarningWithFormat("in symbol '%s': %s", 462 sc.GetFunctionName().AsCString(), 463 error_strm.GetData()); 464 else 465 context_found_for_symbol = true; 466 } 467 } 468 if (!context_found_for_symbol) 469 result.AppendWarningWithFormat("Unable to find line information" 470 " for matching symbol '%s'.\n", 471 sc.GetFunctionName().AsCString()); 472 } 473 if (sc_list_lines.GetSize() == 0) { 474 result.AppendErrorWithFormat("No line information could be found" 475 " for any symbols matching '%s'.\n", 476 name.AsCString()); 477 return false; 478 } 479 FileSpec file_spec; 480 if (!DumpLinesInSymbolContexts(result.GetOutputStream(), sc_list_lines, 481 module_list, file_spec)) { 482 result.AppendErrorWithFormat( 483 "Unable to dump line information for symbol '%s'.\n", 484 name.AsCString()); 485 return false; 486 } 487 return true; 488 } 489 490 // Dump the line entries found for the address specified in the option. 491 bool DumpLinesForAddress(CommandReturnObject &result) { 492 Target *target = m_exe_ctx.GetTargetPtr(); 493 SymbolContextList sc_list; 494 495 StreamString error_strm; 496 if (!GetSymbolContextsForAddress(target->GetImages(), m_options.address, 497 sc_list, error_strm)) { 498 result.AppendErrorWithFormat("%s.\n", error_strm.GetData()); 499 return false; 500 } 501 ModuleList module_list; 502 FileSpec file_spec; 503 if (!DumpLinesInSymbolContexts(result.GetOutputStream(), sc_list, 504 module_list, file_spec)) { 505 result.AppendErrorWithFormat("No modules contain load address 0x%" PRIx64 506 ".\n", 507 m_options.address); 508 return false; 509 } 510 return true; 511 } 512 513 // Dump the line entries found in the file specified in the option. 514 bool DumpLinesForFile(CommandReturnObject &result) { 515 FileSpec file_spec(m_options.file_name); 516 const char *filename = m_options.file_name.c_str(); 517 Target *target = m_exe_ctx.GetTargetPtr(); 518 const ModuleList &module_list = 519 (m_module_list.GetSize() > 0) ? m_module_list : target->GetImages(); 520 521 bool displayed_something = false; 522 const size_t num_modules = module_list.GetSize(); 523 for (uint32_t i = 0; i < num_modules; ++i) { 524 // Dump lines for this module. 525 Module *module = module_list.GetModulePointerAtIndex(i); 526 assert(module); 527 if (DumpFileLinesInModule(result.GetOutputStream(), module, file_spec)) 528 displayed_something = true; 529 } 530 if (!displayed_something) { 531 result.AppendErrorWithFormat("No source filenames matched '%s'.\n", 532 filename); 533 return false; 534 } 535 return true; 536 } 537 538 // Dump the line entries for the current frame. 539 bool DumpLinesForFrame(CommandReturnObject &result) { 540 StackFrame *cur_frame = m_exe_ctx.GetFramePtr(); 541 if (cur_frame == nullptr) { 542 result.AppendError( 543 "No selected frame to use to find the default source."); 544 return false; 545 } else if (!cur_frame->HasDebugInformation()) { 546 result.AppendError("No debug info for the selected frame."); 547 return false; 548 } else { 549 const SymbolContext &sc = 550 cur_frame->GetSymbolContext(eSymbolContextLineEntry); 551 SymbolContextList sc_list; 552 sc_list.Append(sc); 553 ModuleList module_list; 554 FileSpec file_spec; 555 if (!DumpLinesInSymbolContexts(result.GetOutputStream(), sc_list, 556 module_list, file_spec)) { 557 result.AppendError( 558 "No source line info available for the selected frame."); 559 return false; 560 } 561 } 562 return true; 563 } 564 565 bool DoExecute(Args &command, CommandReturnObject &result) override { 566 const size_t argc = command.GetArgumentCount(); 567 568 if (argc != 0) { 569 result.AppendErrorWithFormat("'%s' takes no arguments, only flags.\n", 570 GetCommandName().str().c_str()); 571 result.SetStatus(eReturnStatusFailed); 572 return false; 573 } 574 575 Target *target = m_exe_ctx.GetTargetPtr(); 576 if (target == nullptr) { 577 target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 578 if (target == nullptr) { 579 result.AppendError("invalid target, create a debug target using the " 580 "'target create' command."); 581 result.SetStatus(eReturnStatusFailed); 582 return false; 583 } 584 } 585 586 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 587 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 588 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 589 590 // Collect the list of modules to search. 591 m_module_list.Clear(); 592 if (!m_options.modules.empty()) { 593 for (size_t i = 0, e = m_options.modules.size(); i < e; ++i) { 594 FileSpec module_file_spec(m_options.modules[i]); 595 if (module_file_spec) { 596 ModuleSpec module_spec(module_file_spec); 597 if (target->GetImages().FindModules(module_spec, m_module_list) == 0) 598 result.AppendWarningWithFormat("No module found for '%s'.\n", 599 m_options.modules[i].c_str()); 600 } 601 } 602 if (!m_module_list.GetSize()) { 603 result.AppendError("No modules match the input."); 604 result.SetStatus(eReturnStatusFailed); 605 return false; 606 } 607 } else if (target->GetImages().GetSize() == 0) { 608 result.AppendError("The target has no associated executable images."); 609 result.SetStatus(eReturnStatusFailed); 610 return false; 611 } 612 613 // Check the arguments to see what lines we should dump. 614 if (!m_options.symbol_name.empty()) { 615 // Print lines for symbol. 616 if (DumpLinesInFunctions(result)) 617 result.SetStatus(eReturnStatusSuccessFinishResult); 618 else 619 result.SetStatus(eReturnStatusFailed); 620 } else if (m_options.address != LLDB_INVALID_ADDRESS) { 621 // Print lines for an address. 622 if (DumpLinesForAddress(result)) 623 result.SetStatus(eReturnStatusSuccessFinishResult); 624 else 625 result.SetStatus(eReturnStatusFailed); 626 } else if (!m_options.file_name.empty()) { 627 // Dump lines for a file. 628 if (DumpLinesForFile(result)) 629 result.SetStatus(eReturnStatusSuccessFinishResult); 630 else 631 result.SetStatus(eReturnStatusFailed); 632 } else { 633 // Dump the line for the current frame. 634 if (DumpLinesForFrame(result)) 635 result.SetStatus(eReturnStatusSuccessFinishResult); 636 else 637 result.SetStatus(eReturnStatusFailed); 638 } 639 return result.Succeeded(); 640 } 641 642 CommandOptions m_options; 643 ModuleList m_module_list; 644 }; 645 646 #pragma mark CommandObjectSourceList 647 //------------------------------------------------------------------------- 648 // CommandObjectSourceList 649 //------------------------------------------------------------------------- 650 651 static constexpr OptionDefinition g_source_list_options[] = { 652 // clang-format off 653 { LLDB_OPT_SET_ALL, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCount, "The number of source lines to display." }, 654 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "shlib", 's', OptionParser::eRequiredArgument, nullptr, {}, CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Look up the source file in the given shared library." }, 655 { LLDB_OPT_SET_ALL, false, "show-breakpoints", 'b', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Show the line table locations from the debug information that indicate valid places to set source level breakpoints." }, 656 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, {}, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source." }, 657 { LLDB_OPT_SET_1, false, "line", 'l', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeLineNum, "The line number at which to start the display source." }, 658 { LLDB_OPT_SET_2, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, {}, CommandCompletions::eSymbolCompletion, eArgTypeSymbol, "The name of a function whose source to display." }, 659 { LLDB_OPT_SET_3, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeAddressOrExpression, "Lookup the address and display the source information for the corresponding file and line." }, 660 { LLDB_OPT_SET_4, false, "reverse", 'r', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Reverse the listing to look backwards from the last displayed block of source." }, 661 // clang-format on 662 }; 663 664 class CommandObjectSourceList : public CommandObjectParsed { 665 class CommandOptions : public Options { 666 public: 667 CommandOptions() : Options() {} 668 669 ~CommandOptions() override = default; 670 671 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 672 ExecutionContext *execution_context) override { 673 Status error; 674 const int short_option = GetDefinitions()[option_idx].short_option; 675 switch (short_option) { 676 case 'l': 677 if (option_arg.getAsInteger(0, start_line)) 678 error.SetErrorStringWithFormat("invalid line number: '%s'", 679 option_arg.str().c_str()); 680 break; 681 682 case 'c': 683 if (option_arg.getAsInteger(0, num_lines)) 684 error.SetErrorStringWithFormat("invalid line count: '%s'", 685 option_arg.str().c_str()); 686 break; 687 688 case 'f': 689 file_name = option_arg; 690 break; 691 692 case 'n': 693 symbol_name = option_arg; 694 break; 695 696 case 'a': { 697 address = OptionArgParser::ToAddress(execution_context, option_arg, 698 LLDB_INVALID_ADDRESS, &error); 699 } break; 700 case 's': 701 modules.push_back(std::string(option_arg)); 702 break; 703 704 case 'b': 705 show_bp_locs = true; 706 break; 707 case 'r': 708 reverse = true; 709 break; 710 default: 711 error.SetErrorStringWithFormat("unrecognized short option '%c'", 712 short_option); 713 break; 714 } 715 716 return error; 717 } 718 719 void OptionParsingStarting(ExecutionContext *execution_context) override { 720 file_spec.Clear(); 721 file_name.clear(); 722 symbol_name.clear(); 723 address = LLDB_INVALID_ADDRESS; 724 start_line = 0; 725 num_lines = 0; 726 show_bp_locs = false; 727 reverse = false; 728 modules.clear(); 729 } 730 731 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 732 return llvm::makeArrayRef(g_source_list_options); 733 } 734 735 // Instance variables to hold the values for command options. 736 FileSpec file_spec; 737 std::string file_name; 738 std::string symbol_name; 739 lldb::addr_t address; 740 uint32_t start_line; 741 uint32_t num_lines; 742 STLStringArray modules; 743 bool show_bp_locs; 744 bool reverse; 745 }; 746 747 public: 748 CommandObjectSourceList(CommandInterpreter &interpreter) 749 : CommandObjectParsed(interpreter, "source list", 750 "Display source code for the current target " 751 "process as specified by options.", 752 nullptr, eCommandRequiresTarget), 753 m_options() {} 754 755 ~CommandObjectSourceList() override = default; 756 757 Options *GetOptions() override { return &m_options; } 758 759 const char *GetRepeatCommand(Args ¤t_command_args, 760 uint32_t index) override { 761 // This is kind of gross, but the command hasn't been parsed yet so we 762 // can't look at the option values for this invocation... I have to scan 763 // the arguments directly. 764 auto iter = 765 llvm::find_if(current_command_args, [](const Args::ArgEntry &e) { 766 return e.ref == "-r" || e.ref == "--reverse"; 767 }); 768 if (iter == current_command_args.end()) 769 return m_cmd_name.c_str(); 770 771 if (m_reverse_name.empty()) { 772 m_reverse_name = m_cmd_name; 773 m_reverse_name.append(" -r"); 774 } 775 return m_reverse_name.c_str(); 776 } 777 778 protected: 779 struct SourceInfo { 780 ConstString function; 781 LineEntry line_entry; 782 783 SourceInfo(const ConstString &name, const LineEntry &line_entry) 784 : function(name), line_entry(line_entry) {} 785 786 SourceInfo() : function(), line_entry() {} 787 788 bool IsValid() const { return (bool)function && line_entry.IsValid(); } 789 790 bool operator==(const SourceInfo &rhs) const { 791 return function == rhs.function && 792 line_entry.original_file == rhs.line_entry.original_file && 793 line_entry.line == rhs.line_entry.line; 794 } 795 796 bool operator!=(const SourceInfo &rhs) const { 797 return function != rhs.function || 798 line_entry.original_file != rhs.line_entry.original_file || 799 line_entry.line != rhs.line_entry.line; 800 } 801 802 bool operator<(const SourceInfo &rhs) const { 803 if (function.GetCString() < rhs.function.GetCString()) 804 return true; 805 if (line_entry.file.GetDirectory().GetCString() < 806 rhs.line_entry.file.GetDirectory().GetCString()) 807 return true; 808 if (line_entry.file.GetFilename().GetCString() < 809 rhs.line_entry.file.GetFilename().GetCString()) 810 return true; 811 if (line_entry.line < rhs.line_entry.line) 812 return true; 813 return false; 814 } 815 }; 816 817 size_t DisplayFunctionSource(const SymbolContext &sc, SourceInfo &source_info, 818 CommandReturnObject &result) { 819 if (!source_info.IsValid()) { 820 source_info.function = sc.GetFunctionName(); 821 source_info.line_entry = sc.GetFunctionStartLineEntry(); 822 } 823 824 if (sc.function) { 825 Target *target = m_exe_ctx.GetTargetPtr(); 826 827 FileSpec start_file; 828 uint32_t start_line; 829 uint32_t end_line; 830 FileSpec end_file; 831 832 if (sc.block == nullptr) { 833 // Not an inlined function 834 sc.function->GetStartLineSourceInfo(start_file, start_line); 835 if (start_line == 0) { 836 result.AppendErrorWithFormat("Could not find line information for " 837 "start of function: \"%s\".\n", 838 source_info.function.GetCString()); 839 result.SetStatus(eReturnStatusFailed); 840 return 0; 841 } 842 sc.function->GetEndLineSourceInfo(end_file, end_line); 843 } else { 844 // We have an inlined function 845 start_file = source_info.line_entry.file; 846 start_line = source_info.line_entry.line; 847 end_line = start_line + m_options.num_lines; 848 } 849 850 // This is a little hacky, but the first line table entry for a function 851 // points to the "{" that starts the function block. It would be nice to 852 // actually get the function declaration in there too. So back up a bit, 853 // but not further than what you're going to display. 854 uint32_t extra_lines; 855 if (m_options.num_lines >= 10) 856 extra_lines = 5; 857 else 858 extra_lines = m_options.num_lines / 2; 859 uint32_t line_no; 860 if (start_line <= extra_lines) 861 line_no = 1; 862 else 863 line_no = start_line - extra_lines; 864 865 // For fun, if the function is shorter than the number of lines we're 866 // supposed to display, only display the function... 867 if (end_line != 0) { 868 if (m_options.num_lines > end_line - line_no) 869 m_options.num_lines = end_line - line_no + extra_lines; 870 } 871 872 m_breakpoint_locations.Clear(); 873 874 if (m_options.show_bp_locs) { 875 const bool show_inlines = true; 876 m_breakpoint_locations.Reset(start_file, 0, show_inlines); 877 SearchFilterForUnconstrainedSearches target_search_filter( 878 m_exe_ctx.GetTargetSP()); 879 target_search_filter.Search(m_breakpoint_locations); 880 } 881 882 result.AppendMessageWithFormat("File: %s\n", 883 start_file.GetPath().c_str()); 884 // We don't care about the column here. 885 const uint32_t column = 0; 886 return target->GetSourceManager().DisplaySourceLinesWithLineNumbers( 887 start_file, line_no, column, 0, m_options.num_lines, "", 888 &result.GetOutputStream(), GetBreakpointLocations()); 889 } else { 890 result.AppendErrorWithFormat( 891 "Could not find function info for: \"%s\".\n", 892 m_options.symbol_name.c_str()); 893 } 894 return 0; 895 } 896 897 // From Jim: The FindMatchingFunctions / FindMatchingFunctionSymbols 898 // functions "take a possibly empty vector of strings which are names of 899 // modules, and run the two search functions on the subset of the full module 900 // list that matches the strings in the input vector". If we wanted to put 901 // these somewhere, there should probably be a module-filter-list that can be 902 // passed to the various ModuleList::Find* calls, which would either be a 903 // vector of string names or a ModuleSpecList. 904 size_t FindMatchingFunctions(Target *target, const ConstString &name, 905 SymbolContextList &sc_list) { 906 // Displaying the source for a symbol: 907 bool include_inlines = true; 908 bool append = true; 909 bool include_symbols = false; 910 size_t num_matches = 0; 911 912 if (m_options.num_lines == 0) 913 m_options.num_lines = 10; 914 915 const size_t num_modules = m_options.modules.size(); 916 if (num_modules > 0) { 917 ModuleList matching_modules; 918 for (size_t i = 0; i < num_modules; ++i) { 919 FileSpec module_file_spec(m_options.modules[i]); 920 if (module_file_spec) { 921 ModuleSpec module_spec(module_file_spec); 922 matching_modules.Clear(); 923 target->GetImages().FindModules(module_spec, matching_modules); 924 num_matches += matching_modules.FindFunctions( 925 name, eFunctionNameTypeAuto, include_symbols, include_inlines, 926 append, sc_list); 927 } 928 } 929 } else { 930 num_matches = target->GetImages().FindFunctions( 931 name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, 932 sc_list); 933 } 934 return num_matches; 935 } 936 937 size_t FindMatchingFunctionSymbols(Target *target, const ConstString &name, 938 SymbolContextList &sc_list) { 939 size_t num_matches = 0; 940 const size_t num_modules = m_options.modules.size(); 941 if (num_modules > 0) { 942 ModuleList matching_modules; 943 for (size_t i = 0; i < num_modules; ++i) { 944 FileSpec module_file_spec(m_options.modules[i]); 945 if (module_file_spec) { 946 ModuleSpec module_spec(module_file_spec); 947 matching_modules.Clear(); 948 target->GetImages().FindModules(module_spec, matching_modules); 949 num_matches += matching_modules.FindFunctionSymbols( 950 name, eFunctionNameTypeAuto, sc_list); 951 } 952 } 953 } else { 954 num_matches = target->GetImages().FindFunctionSymbols( 955 name, eFunctionNameTypeAuto, sc_list); 956 } 957 return num_matches; 958 } 959 960 bool DoExecute(Args &command, CommandReturnObject &result) override { 961 const size_t argc = command.GetArgumentCount(); 962 963 if (argc != 0) { 964 result.AppendErrorWithFormat("'%s' takes no arguments, only flags.\n", 965 GetCommandName().str().c_str()); 966 result.SetStatus(eReturnStatusFailed); 967 return false; 968 } 969 970 Target *target = m_exe_ctx.GetTargetPtr(); 971 972 if (!m_options.symbol_name.empty()) { 973 SymbolContextList sc_list; 974 ConstString name(m_options.symbol_name.c_str()); 975 976 // Displaying the source for a symbol. Search for function named name. 977 size_t num_matches = FindMatchingFunctions(target, name, sc_list); 978 if (!num_matches) { 979 // If we didn't find any functions with that name, try searching for 980 // symbols that line up exactly with function addresses. 981 SymbolContextList sc_list_symbols; 982 size_t num_symbol_matches = 983 FindMatchingFunctionSymbols(target, name, sc_list_symbols); 984 for (size_t i = 0; i < num_symbol_matches; i++) { 985 SymbolContext sc; 986 sc_list_symbols.GetContextAtIndex(i, sc); 987 if (sc.symbol && sc.symbol->ValueIsAddress()) { 988 const Address &base_address = sc.symbol->GetAddressRef(); 989 Function *function = base_address.CalculateSymbolContextFunction(); 990 if (function) { 991 sc_list.Append(SymbolContext(function)); 992 num_matches++; 993 break; 994 } 995 } 996 } 997 } 998 999 if (num_matches == 0) { 1000 result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", 1001 m_options.symbol_name.c_str()); 1002 result.SetStatus(eReturnStatusFailed); 1003 return false; 1004 } 1005 1006 if (num_matches > 1) { 1007 std::set<SourceInfo> source_match_set; 1008 1009 bool displayed_something = false; 1010 for (size_t i = 0; i < num_matches; i++) { 1011 SymbolContext sc; 1012 sc_list.GetContextAtIndex(i, sc); 1013 SourceInfo source_info(sc.GetFunctionName(), 1014 sc.GetFunctionStartLineEntry()); 1015 1016 if (source_info.IsValid()) { 1017 if (source_match_set.find(source_info) == source_match_set.end()) { 1018 source_match_set.insert(source_info); 1019 if (DisplayFunctionSource(sc, source_info, result)) 1020 displayed_something = true; 1021 } 1022 } 1023 } 1024 1025 if (displayed_something) 1026 result.SetStatus(eReturnStatusSuccessFinishResult); 1027 else 1028 result.SetStatus(eReturnStatusFailed); 1029 } else { 1030 SymbolContext sc; 1031 sc_list.GetContextAtIndex(0, sc); 1032 SourceInfo source_info; 1033 1034 if (DisplayFunctionSource(sc, source_info, result)) { 1035 result.SetStatus(eReturnStatusSuccessFinishResult); 1036 } else { 1037 result.SetStatus(eReturnStatusFailed); 1038 } 1039 } 1040 return result.Succeeded(); 1041 } else if (m_options.address != LLDB_INVALID_ADDRESS) { 1042 Address so_addr; 1043 StreamString error_strm; 1044 SymbolContextList sc_list; 1045 1046 if (target->GetSectionLoadList().IsEmpty()) { 1047 // The target isn't loaded yet, we need to lookup the file address in 1048 // all modules 1049 const ModuleList &module_list = target->GetImages(); 1050 const size_t num_modules = module_list.GetSize(); 1051 for (size_t i = 0; i < num_modules; ++i) { 1052 ModuleSP module_sp(module_list.GetModuleAtIndex(i)); 1053 if (module_sp && 1054 module_sp->ResolveFileAddress(m_options.address, so_addr)) { 1055 SymbolContext sc; 1056 sc.Clear(true); 1057 if (module_sp->ResolveSymbolContextForAddress( 1058 so_addr, eSymbolContextEverything, sc) & 1059 eSymbolContextLineEntry) 1060 sc_list.Append(sc); 1061 } 1062 } 1063 1064 if (sc_list.GetSize() == 0) { 1065 result.AppendErrorWithFormat( 1066 "no modules have source information for file address 0x%" PRIx64 1067 ".\n", 1068 m_options.address); 1069 result.SetStatus(eReturnStatusFailed); 1070 return false; 1071 } 1072 } else { 1073 // The target has some things loaded, resolve this address to a compile 1074 // unit + file + line and display 1075 if (target->GetSectionLoadList().ResolveLoadAddress(m_options.address, 1076 so_addr)) { 1077 ModuleSP module_sp(so_addr.GetModule()); 1078 if (module_sp) { 1079 SymbolContext sc; 1080 sc.Clear(true); 1081 if (module_sp->ResolveSymbolContextForAddress( 1082 so_addr, eSymbolContextEverything, sc) & 1083 eSymbolContextLineEntry) { 1084 sc_list.Append(sc); 1085 } else { 1086 so_addr.Dump(&error_strm, nullptr, 1087 Address::DumpStyleModuleWithFileAddress); 1088 result.AppendErrorWithFormat("address resolves to %s, but there " 1089 "is no line table information " 1090 "available for this address.\n", 1091 error_strm.GetData()); 1092 result.SetStatus(eReturnStatusFailed); 1093 return false; 1094 } 1095 } 1096 } 1097 1098 if (sc_list.GetSize() == 0) { 1099 result.AppendErrorWithFormat( 1100 "no modules contain load address 0x%" PRIx64 ".\n", 1101 m_options.address); 1102 result.SetStatus(eReturnStatusFailed); 1103 return false; 1104 } 1105 } 1106 uint32_t num_matches = sc_list.GetSize(); 1107 for (uint32_t i = 0; i < num_matches; ++i) { 1108 SymbolContext sc; 1109 sc_list.GetContextAtIndex(i, sc); 1110 if (sc.comp_unit) { 1111 if (m_options.show_bp_locs) { 1112 m_breakpoint_locations.Clear(); 1113 const bool show_inlines = true; 1114 m_breakpoint_locations.Reset(*sc.comp_unit, 0, show_inlines); 1115 SearchFilterForUnconstrainedSearches target_search_filter( 1116 target->shared_from_this()); 1117 target_search_filter.Search(m_breakpoint_locations); 1118 } 1119 1120 bool show_fullpaths = true; 1121 bool show_module = true; 1122 bool show_inlined_frames = true; 1123 const bool show_function_arguments = true; 1124 const bool show_function_name = true; 1125 sc.DumpStopContext(&result.GetOutputStream(), 1126 m_exe_ctx.GetBestExecutionContextScope(), 1127 sc.line_entry.range.GetBaseAddress(), 1128 show_fullpaths, show_module, show_inlined_frames, 1129 show_function_arguments, show_function_name); 1130 result.GetOutputStream().EOL(); 1131 1132 if (m_options.num_lines == 0) 1133 m_options.num_lines = 10; 1134 1135 size_t lines_to_back_up = 1136 m_options.num_lines >= 10 ? 5 : m_options.num_lines / 2; 1137 1138 const uint32_t column = 1139 (m_interpreter.GetDebugger().GetStopShowColumn() != 1140 eStopShowColumnNone) 1141 ? sc.line_entry.column 1142 : 0; 1143 target->GetSourceManager().DisplaySourceLinesWithLineNumbers( 1144 sc.comp_unit, sc.line_entry.line, column, lines_to_back_up, 1145 m_options.num_lines - lines_to_back_up, "->", 1146 &result.GetOutputStream(), GetBreakpointLocations()); 1147 result.SetStatus(eReturnStatusSuccessFinishResult); 1148 } 1149 } 1150 } else if (m_options.file_name.empty()) { 1151 // Last valid source manager context, or the current frame if no valid 1152 // last context in source manager. One little trick here, if you type the 1153 // exact same list command twice in a row, it is more likely because you 1154 // typed it once, then typed it again 1155 if (m_options.start_line == 0) { 1156 if (target->GetSourceManager().DisplayMoreWithLineNumbers( 1157 &result.GetOutputStream(), m_options.num_lines, 1158 m_options.reverse, GetBreakpointLocations())) { 1159 result.SetStatus(eReturnStatusSuccessFinishResult); 1160 } 1161 } else { 1162 if (m_options.num_lines == 0) 1163 m_options.num_lines = 10; 1164 1165 if (m_options.show_bp_locs) { 1166 SourceManager::FileSP last_file_sp( 1167 target->GetSourceManager().GetLastFile()); 1168 if (last_file_sp) { 1169 const bool show_inlines = true; 1170 m_breakpoint_locations.Reset(last_file_sp->GetFileSpec(), 0, 1171 show_inlines); 1172 SearchFilterForUnconstrainedSearches target_search_filter( 1173 target->shared_from_this()); 1174 target_search_filter.Search(m_breakpoint_locations); 1175 } 1176 } else 1177 m_breakpoint_locations.Clear(); 1178 1179 const uint32_t column = 0; 1180 if (target->GetSourceManager() 1181 .DisplaySourceLinesWithLineNumbersUsingLastFile( 1182 m_options.start_line, // Line to display 1183 m_options.num_lines, // Lines after line to 1184 UINT32_MAX, // Don't mark "line" 1185 column, 1186 "", // Don't mark "line" 1187 &result.GetOutputStream(), GetBreakpointLocations())) { 1188 result.SetStatus(eReturnStatusSuccessFinishResult); 1189 } 1190 } 1191 } else { 1192 const char *filename = m_options.file_name.c_str(); 1193 1194 bool check_inlines = false; 1195 SymbolContextList sc_list; 1196 size_t num_matches = 0; 1197 1198 if (!m_options.modules.empty()) { 1199 ModuleList matching_modules; 1200 for (size_t i = 0, e = m_options.modules.size(); i < e; ++i) { 1201 FileSpec module_file_spec(m_options.modules[i]); 1202 if (module_file_spec) { 1203 ModuleSpec module_spec(module_file_spec); 1204 matching_modules.Clear(); 1205 target->GetImages().FindModules(module_spec, matching_modules); 1206 num_matches += matching_modules.ResolveSymbolContextForFilePath( 1207 filename, 0, check_inlines, 1208 SymbolContextItem(eSymbolContextModule | 1209 eSymbolContextCompUnit), 1210 sc_list); 1211 } 1212 } 1213 } else { 1214 num_matches = target->GetImages().ResolveSymbolContextForFilePath( 1215 filename, 0, check_inlines, 1216 eSymbolContextModule | eSymbolContextCompUnit, sc_list); 1217 } 1218 1219 if (num_matches == 0) { 1220 result.AppendErrorWithFormat("Could not find source file \"%s\".\n", 1221 m_options.file_name.c_str()); 1222 result.SetStatus(eReturnStatusFailed); 1223 return false; 1224 } 1225 1226 if (num_matches > 1) { 1227 bool got_multiple = false; 1228 FileSpec *test_cu_spec = nullptr; 1229 1230 for (unsigned i = 0; i < num_matches; i++) { 1231 SymbolContext sc; 1232 sc_list.GetContextAtIndex(i, sc); 1233 if (sc.comp_unit) { 1234 if (test_cu_spec) { 1235 if (test_cu_spec != static_cast<FileSpec *>(sc.comp_unit)) 1236 got_multiple = true; 1237 break; 1238 } else 1239 test_cu_spec = sc.comp_unit; 1240 } 1241 } 1242 if (got_multiple) { 1243 result.AppendErrorWithFormat( 1244 "Multiple source files found matching: \"%s.\"\n", 1245 m_options.file_name.c_str()); 1246 result.SetStatus(eReturnStatusFailed); 1247 return false; 1248 } 1249 } 1250 1251 SymbolContext sc; 1252 if (sc_list.GetContextAtIndex(0, sc)) { 1253 if (sc.comp_unit) { 1254 if (m_options.show_bp_locs) { 1255 const bool show_inlines = true; 1256 m_breakpoint_locations.Reset(*sc.comp_unit, 0, show_inlines); 1257 SearchFilterForUnconstrainedSearches target_search_filter( 1258 target->shared_from_this()); 1259 target_search_filter.Search(m_breakpoint_locations); 1260 } else 1261 m_breakpoint_locations.Clear(); 1262 1263 if (m_options.num_lines == 0) 1264 m_options.num_lines = 10; 1265 const uint32_t column = 0; 1266 target->GetSourceManager().DisplaySourceLinesWithLineNumbers( 1267 sc.comp_unit, m_options.start_line, column, 1268 0, m_options.num_lines, 1269 "", &result.GetOutputStream(), GetBreakpointLocations()); 1270 1271 result.SetStatus(eReturnStatusSuccessFinishResult); 1272 } else { 1273 result.AppendErrorWithFormat("No comp unit found for: \"%s.\"\n", 1274 m_options.file_name.c_str()); 1275 result.SetStatus(eReturnStatusFailed); 1276 return false; 1277 } 1278 } 1279 } 1280 return result.Succeeded(); 1281 } 1282 1283 const SymbolContextList *GetBreakpointLocations() { 1284 if (m_breakpoint_locations.GetFileLineMatches().GetSize() > 0) 1285 return &m_breakpoint_locations.GetFileLineMatches(); 1286 return nullptr; 1287 } 1288 1289 CommandOptions m_options; 1290 FileLineResolver m_breakpoint_locations; 1291 std::string m_reverse_name; 1292 }; 1293 1294 #pragma mark CommandObjectMultiwordSource 1295 //------------------------------------------------------------------------- 1296 // CommandObjectMultiwordSource 1297 //------------------------------------------------------------------------- 1298 1299 CommandObjectMultiwordSource::CommandObjectMultiwordSource( 1300 CommandInterpreter &interpreter) 1301 : CommandObjectMultiword(interpreter, "source", "Commands for examining " 1302 "source code described by " 1303 "debug information for the " 1304 "current target process.", 1305 "source <subcommand> [<subcommand-options>]") { 1306 LoadSubCommand("info", 1307 CommandObjectSP(new CommandObjectSourceInfo(interpreter))); 1308 LoadSubCommand("list", 1309 CommandObjectSP(new CommandObjectSourceList(interpreter))); 1310 } 1311 1312 CommandObjectMultiwordSource::~CommandObjectMultiwordSource() = default; 1313