1 //===-- CommandObjectSource.cpp ---------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "CommandObjectSource.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Interpreter/Args.h" 17 #include "lldb/Core/Debugger.h" 18 #include "lldb/Core/FileLineResolver.h" 19 #include "lldb/Core/SourceManager.h" 20 #include "lldb/Interpreter/CommandInterpreter.h" 21 #include "lldb/Interpreter/CommandReturnObject.h" 22 #include "lldb/Host/FileSpec.h" 23 #include "lldb/Target/Process.h" 24 #include "lldb/Target/TargetList.h" 25 #include "lldb/Interpreter/CommandCompletions.h" 26 #include "lldb/Interpreter/Options.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 //------------------------------------------------------------------------- 32 // CommandObjectSourceInfo 33 //------------------------------------------------------------------------- 34 35 class CommandObjectSourceInfo : public CommandObject 36 { 37 38 class CommandOptions : public Options 39 { 40 public: 41 CommandOptions (CommandInterpreter &interpreter) : 42 Options(interpreter) 43 { 44 } 45 46 ~CommandOptions () 47 { 48 } 49 50 Error 51 SetOptionValue (uint32_t option_idx, const char *option_arg) 52 { 53 Error error; 54 const char short_option = g_option_table[option_idx].short_option; 55 switch (short_option) 56 { 57 case 'l': 58 start_line = Args::StringToUInt32 (option_arg, 0); 59 if (start_line == 0) 60 error.SetErrorStringWithFormat("Invalid line number: '%s'.\n", option_arg); 61 break; 62 63 case 'f': 64 file_name = option_arg; 65 break; 66 67 default: 68 error.SetErrorStringWithFormat("Unrecognized short option '%c'.\n", short_option); 69 break; 70 } 71 72 return error; 73 } 74 75 void 76 OptionParsingStarting () 77 { 78 file_spec.Clear(); 79 file_name.clear(); 80 start_line = 0; 81 } 82 83 const OptionDefinition* 84 GetDefinitions () 85 { 86 return g_option_table; 87 } 88 static OptionDefinition g_option_table[]; 89 90 // Instance variables to hold the values for command options. 91 FileSpec file_spec; 92 std::string file_name; 93 uint32_t start_line; 94 95 }; 96 97 public: 98 CommandObjectSourceInfo(CommandInterpreter &interpreter) : 99 CommandObject (interpreter, 100 "source info", 101 "Display information about the source lines from the current executable's debug info.", 102 "source info [<cmd-options>]"), 103 m_options (interpreter) 104 { 105 } 106 107 ~CommandObjectSourceInfo () 108 { 109 } 110 111 112 Options * 113 GetOptions () 114 { 115 return &m_options; 116 } 117 118 119 bool 120 Execute 121 ( 122 Args& args, 123 CommandReturnObject &result 124 ) 125 { 126 result.AppendError ("Not yet implemented"); 127 result.SetStatus (eReturnStatusFailed); 128 return false; 129 } 130 protected: 131 CommandOptions m_options; 132 }; 133 134 OptionDefinition 135 CommandObjectSourceInfo::CommandOptions::g_option_table[] = 136 { 137 { LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."}, 138 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."}, 139 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } 140 }; 141 142 #pragma mark CommandObjectSourceList 143 //------------------------------------------------------------------------- 144 // CommandObjectSourceList 145 //------------------------------------------------------------------------- 146 147 class CommandObjectSourceList : public CommandObject 148 { 149 150 class CommandOptions : public Options 151 { 152 public: 153 CommandOptions (CommandInterpreter &interpreter) : 154 Options(interpreter) 155 { 156 } 157 158 ~CommandOptions () 159 { 160 } 161 162 Error 163 SetOptionValue (uint32_t option_idx, const char *option_arg) 164 { 165 Error error; 166 const char short_option = g_option_table[option_idx].short_option; 167 switch (short_option) 168 { 169 case 'l': 170 start_line = Args::StringToUInt32 (option_arg, 0); 171 if (start_line == 0) 172 error.SetErrorStringWithFormat("Invalid line number: '%s'.\n", option_arg); 173 break; 174 175 case 'c': 176 num_lines = Args::StringToUInt32 (option_arg, 0); 177 if (num_lines == 0) 178 error.SetErrorStringWithFormat("Invalid line count: '%s'.\n", option_arg); 179 break; 180 181 case 'f': 182 file_name = option_arg; 183 break; 184 185 case 'n': 186 symbol_name = option_arg; 187 break; 188 189 case 's': 190 modules.push_back (std::string (option_arg)); 191 break; 192 193 case 'b': 194 show_bp_locs = true; 195 break; 196 default: 197 error.SetErrorStringWithFormat("Unrecognized short option '%c'.\n", short_option); 198 break; 199 } 200 201 return error; 202 } 203 204 void 205 OptionParsingStarting () 206 { 207 file_spec.Clear(); 208 file_name.clear(); 209 symbol_name.clear(); 210 start_line = 0; 211 num_lines = 10; 212 show_bp_locs = false; 213 modules.clear(); 214 } 215 216 const OptionDefinition* 217 GetDefinitions () 218 { 219 return g_option_table; 220 } 221 static OptionDefinition g_option_table[]; 222 223 // Instance variables to hold the values for command options. 224 FileSpec file_spec; 225 std::string file_name; 226 std::string symbol_name; 227 uint32_t start_line; 228 uint32_t num_lines; 229 STLStringArray modules; 230 bool show_bp_locs; 231 }; 232 233 public: 234 CommandObjectSourceList(CommandInterpreter &interpreter) : 235 CommandObject (interpreter, 236 "source list", 237 "Display source code (as specified) based on the current executable's debug info.", 238 NULL), 239 m_options (interpreter) 240 { 241 CommandArgumentEntry arg; 242 CommandArgumentData file_arg; 243 244 // Define the first (and only) variant of this arg. 245 file_arg.arg_type = eArgTypeFilename; 246 file_arg.arg_repetition = eArgRepeatOptional; 247 248 // There is only one variant this argument could be; put it into the argument entry. 249 arg.push_back (file_arg); 250 251 // Push the data for the first argument into the m_arguments vector. 252 m_arguments.push_back (arg); 253 } 254 255 ~CommandObjectSourceList () 256 { 257 } 258 259 260 Options * 261 GetOptions () 262 { 263 return &m_options; 264 } 265 266 267 bool 268 Execute 269 ( 270 Args& args, 271 CommandReturnObject &result 272 ) 273 { 274 const int argc = args.GetArgumentCount(); 275 276 if (argc != 0) 277 { 278 result.AppendErrorWithFormat("'%s' takes no arguments, only flags.\n", GetCommandName()); 279 result.SetStatus (eReturnStatusFailed); 280 } 281 282 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 283 284 if (!m_options.symbol_name.empty()) 285 { 286 // Displaying the source for a symbol: 287 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 288 if (target == NULL) 289 { 290 result.AppendError ("invalid target, set executable file using 'file' command"); 291 result.SetStatus (eReturnStatusFailed); 292 return false; 293 } 294 295 SymbolContextList sc_list; 296 ConstString name(m_options.symbol_name.c_str()); 297 bool include_symbols = false; 298 bool append = true; 299 size_t num_matches = 0; 300 301 if (m_options.modules.size() > 0) 302 { 303 ModuleList matching_modules; 304 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++) 305 { 306 FileSpec module_spec(m_options.modules[i].c_str(), false); 307 if (module_spec) 308 { 309 matching_modules.Clear(); 310 target->GetImages().FindModules (&module_spec, NULL, NULL, NULL, matching_modules); 311 num_matches += matching_modules.FindFunctions (name, eFunctionNameTypeBase, include_symbols, append, sc_list); 312 } 313 } 314 } 315 else 316 { 317 num_matches = target->GetImages().FindFunctions (name, eFunctionNameTypeBase, include_symbols, append, sc_list); 318 } 319 320 SymbolContext sc; 321 322 if (num_matches == 0) 323 { 324 result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", m_options.symbol_name.c_str()); 325 result.SetStatus (eReturnStatusFailed); 326 return false; 327 } 328 329 sc_list.GetContextAtIndex (0, sc); 330 FileSpec start_file; 331 uint32_t start_line; 332 uint32_t end_line; 333 FileSpec end_file; 334 if (sc.function != NULL) 335 { 336 sc.function->GetStartLineSourceInfo (start_file, start_line); 337 if (start_line == 0) 338 { 339 result.AppendErrorWithFormat("Could not find line information for start of function: \"%s\".\n", m_options.symbol_name.c_str()); 340 result.SetStatus (eReturnStatusFailed); 341 return false; 342 } 343 sc.function->GetEndLineSourceInfo (end_file, end_line); 344 } 345 else 346 { 347 result.AppendErrorWithFormat("Could not find function info for: \"%s\".\n", m_options.symbol_name.c_str()); 348 result.SetStatus (eReturnStatusFailed); 349 return false; 350 } 351 352 if (num_matches > 1) 353 { 354 // This could either be because there are multiple functions of this name, in which case 355 // we'll have to specify this further... Or it could be because there are multiple inlined instances 356 // of one function. So run through the matches and if they all have the same file & line then we can just 357 // list one. 358 359 bool found_multiple = false; 360 361 for (size_t i = 1; i < num_matches; i++) 362 { 363 SymbolContext scratch_sc; 364 sc_list.GetContextAtIndex (i, scratch_sc); 365 if (scratch_sc.function != NULL) 366 { 367 FileSpec scratch_file; 368 uint32_t scratch_line; 369 scratch_sc.function->GetStartLineSourceInfo (scratch_file, scratch_line); 370 if (scratch_file != start_file 371 || scratch_line != start_line) 372 { 373 found_multiple = true; 374 break; 375 } 376 } 377 } 378 if (found_multiple) 379 { 380 StreamString s; 381 for (size_t i = 0; i < num_matches; i++) 382 { 383 SymbolContext scratch_sc; 384 sc_list.GetContextAtIndex (i, scratch_sc); 385 if (scratch_sc.function != NULL) 386 { 387 s.Printf("\n%d: ", i); 388 scratch_sc.function->Dump (&s, true); 389 } 390 } 391 result.AppendErrorWithFormat("Multiple functions found matching: %s: \n%s\n", 392 m_options.symbol_name.c_str(), 393 s.GetData()); 394 result.SetStatus (eReturnStatusFailed); 395 return false; 396 } 397 } 398 399 400 // This is a little hacky, but the first line table entry for a function points to the "{" that 401 // starts the function block. It would be nice to actually get the function 402 // declaration in there too. So back up a bit, but not further than what you're going to display. 403 size_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2; 404 uint32_t line_no; 405 if (start_line <= lines_to_back_up) 406 line_no = 1; 407 else 408 line_no = start_line - lines_to_back_up; 409 410 // For fun, if the function is shorter than the number of lines we're supposed to display, 411 // only display the function... 412 if (end_line != 0) 413 { 414 if (m_options.num_lines > end_line - line_no) 415 m_options.num_lines = end_line - line_no; 416 } 417 418 char path_buf[PATH_MAX]; 419 start_file.GetPath(path_buf, sizeof(path_buf)); 420 421 if (m_options.show_bp_locs && exe_ctx.target) 422 { 423 const bool show_inlines = true; 424 m_breakpoint_locations.Reset (start_file, 0, show_inlines); 425 SearchFilter target_search_filter (exe_ctx.target->GetSP()); 426 target_search_filter.Search (m_breakpoint_locations); 427 } 428 else 429 m_breakpoint_locations.Clear(); 430 431 result.AppendMessageWithFormat("File: %s.\n", path_buf); 432 m_interpreter.GetDebugger().GetSourceManager().DisplaySourceLinesWithLineNumbers (target, 433 start_file, 434 line_no, 435 0, 436 m_options.num_lines, 437 "", 438 &result.GetOutputStream(), 439 GetBreakpointLocations ()); 440 441 result.SetStatus (eReturnStatusSuccessFinishResult); 442 return true; 443 444 } 445 else if (m_options.file_name.empty()) 446 { 447 // Last valid source manager context, or the current frame if no 448 // valid last context in source manager. 449 // One little trick here, if you type the exact same list command twice in a row, it is 450 // more likely because you typed it once, then typed it again 451 if (m_options.start_line == 0) 452 { 453 if (m_interpreter.GetDebugger().GetSourceManager().DisplayMoreWithLineNumbers (&result.GetOutputStream(), 454 GetBreakpointLocations ())) 455 { 456 result.SetStatus (eReturnStatusSuccessFinishResult); 457 } 458 } 459 else 460 { 461 if (m_options.show_bp_locs && exe_ctx.target) 462 { 463 SourceManager::FileSP last_file_sp (m_interpreter.GetDebugger().GetSourceManager().GetLastFile ()); 464 if (last_file_sp) 465 { 466 const bool show_inlines = true; 467 m_breakpoint_locations.Reset (last_file_sp->GetFileSpec(), 0, show_inlines); 468 SearchFilter target_search_filter (exe_ctx.target->GetSP()); 469 target_search_filter.Search (m_breakpoint_locations); 470 } 471 } 472 else 473 m_breakpoint_locations.Clear(); 474 475 if (m_interpreter.GetDebugger().GetSourceManager().DisplaySourceLinesWithLineNumbersUsingLastFile( 476 m_options.start_line, // Line to display 477 0, // Lines before line to display 478 m_options.num_lines, // Lines after line to display 479 "", // Don't mark "line" 480 &result.GetOutputStream(), 481 GetBreakpointLocations ())) 482 { 483 result.SetStatus (eReturnStatusSuccessFinishResult); 484 } 485 486 } 487 } 488 else 489 { 490 const char *filename = m_options.file_name.c_str(); 491 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 492 if (target == NULL) 493 { 494 result.AppendError ("invalid target, set executable file using 'file' command"); 495 result.SetStatus (eReturnStatusFailed); 496 return false; 497 } 498 499 500 bool check_inlines = false; 501 SymbolContextList sc_list; 502 size_t num_matches = 0; 503 504 if (m_options.modules.size() > 0) 505 { 506 ModuleList matching_modules; 507 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++) 508 { 509 FileSpec module_spec(m_options.modules[i].c_str(), false); 510 if (module_spec) 511 { 512 matching_modules.Clear(); 513 target->GetImages().FindModules (&module_spec, NULL, NULL, NULL, matching_modules); 514 num_matches += matching_modules.ResolveSymbolContextForFilePath (filename, 515 0, 516 check_inlines, 517 eSymbolContextModule | eSymbolContextCompUnit, 518 sc_list); 519 } 520 } 521 } 522 else 523 { 524 num_matches = target->GetImages().ResolveSymbolContextForFilePath (filename, 525 0, 526 check_inlines, 527 eSymbolContextModule | eSymbolContextCompUnit, 528 sc_list); 529 } 530 531 if (num_matches == 0) 532 { 533 result.AppendErrorWithFormat("Could not find source file \"%s\".\n", 534 m_options.file_name.c_str()); 535 result.SetStatus (eReturnStatusFailed); 536 return false; 537 } 538 539 if (num_matches > 1) 540 { 541 SymbolContext sc; 542 bool got_multiple = false; 543 FileSpec *test_cu_spec = NULL; 544 545 for (unsigned i = 0; i < num_matches; i++) 546 { 547 sc_list.GetContextAtIndex(i, sc); 548 if (sc.comp_unit) 549 { 550 if (test_cu_spec) 551 { 552 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit)) 553 got_multiple = true; 554 break; 555 } 556 else 557 test_cu_spec = sc.comp_unit; 558 } 559 } 560 if (got_multiple) 561 { 562 result.AppendErrorWithFormat("Multiple source files found matching: \"%s.\"\n", 563 m_options.file_name.c_str()); 564 result.SetStatus (eReturnStatusFailed); 565 return false; 566 } 567 } 568 569 SymbolContext sc; 570 if (sc_list.GetContextAtIndex(0, sc)) 571 { 572 if (sc.comp_unit) 573 { 574 if (m_options.show_bp_locs && exe_ctx.target) 575 { 576 const bool show_inlines = true; 577 m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines); 578 SearchFilter target_search_filter (exe_ctx.target->GetSP()); 579 target_search_filter.Search (m_breakpoint_locations); 580 } 581 else 582 m_breakpoint_locations.Clear(); 583 584 m_interpreter.GetDebugger().GetSourceManager().DisplaySourceLinesWithLineNumbers (target, 585 sc.comp_unit, 586 m_options.start_line, 587 0, 588 m_options.num_lines, 589 "", 590 &result.GetOutputStream(), 591 GetBreakpointLocations ()); 592 593 result.SetStatus (eReturnStatusSuccessFinishResult); 594 } 595 else 596 { 597 result.AppendErrorWithFormat("No comp unit found for: \"%s.\"\n", 598 m_options.file_name.c_str()); 599 result.SetStatus (eReturnStatusFailed); 600 return false; 601 } 602 } 603 } 604 return result.Succeeded(); 605 } 606 607 virtual const char *GetRepeatCommand (Args ¤t_command_args, uint32_t index) 608 { 609 return m_cmd_name.c_str(); 610 } 611 612 protected: 613 const SymbolContextList * 614 GetBreakpointLocations () 615 { 616 if (m_breakpoint_locations.GetFileLineMatches().GetSize() > 0) 617 return &m_breakpoint_locations.GetFileLineMatches(); 618 return NULL; 619 } 620 CommandOptions m_options; 621 FileLineResolver m_breakpoint_locations; 622 623 }; 624 625 OptionDefinition 626 CommandObjectSourceList::CommandOptions::g_option_table[] = 627 { 628 { LLDB_OPT_SET_ALL, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "The number of source lines to display."}, 629 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Look up the source file in the given shared library."}, 630 { LLDB_OPT_SET_ALL, false, "show-breakpoints", 'b', no_argument, NULL, 0, eArgTypeNone, "Show the line table locations from the debug information that indicate valid places to set source level breakpoints."}, 631 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."}, 632 { LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."}, 633 { LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeSymbol, "The name of a function whose source to display."}, 634 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } 635 }; 636 637 #pragma mark CommandObjectMultiwordSource 638 639 //------------------------------------------------------------------------- 640 // CommandObjectMultiwordSource 641 //------------------------------------------------------------------------- 642 643 CommandObjectMultiwordSource::CommandObjectMultiwordSource (CommandInterpreter &interpreter) : 644 CommandObjectMultiword (interpreter, 645 "source", 646 "A set of commands for accessing source file information", 647 "source <subcommand> [<subcommand-options>]") 648 { 649 LoadSubCommand ("info", CommandObjectSP (new CommandObjectSourceInfo (interpreter))); 650 LoadSubCommand ("list", CommandObjectSP (new CommandObjectSourceList (interpreter))); 651 } 652 653 CommandObjectMultiwordSource::~CommandObjectMultiwordSource () 654 { 655 } 656 657