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'", 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'", 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'", 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'", 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'", 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 return false; 281 } 282 283 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 284 Target *target = exe_ctx.GetTargetPtr(); 285 286 if (target == NULL) 287 target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 288 289 if (target == NULL) 290 { 291 result.AppendError ("invalid target, create a debug target using the 'target create' command"); 292 result.SetStatus (eReturnStatusFailed); 293 return false; 294 } 295 296 if (!m_options.symbol_name.empty()) 297 { 298 // Displaying the source for a symbol: 299 SymbolContextList sc_list; 300 ConstString name(m_options.symbol_name.c_str()); 301 bool include_symbols = false; 302 bool append = true; 303 size_t num_matches = 0; 304 305 if (m_options.modules.size() > 0) 306 { 307 ModuleList matching_modules; 308 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++) 309 { 310 FileSpec module_spec(m_options.modules[i].c_str(), false); 311 if (module_spec) 312 { 313 matching_modules.Clear(); 314 target->GetImages().FindModules (&module_spec, NULL, NULL, NULL, matching_modules); 315 num_matches += matching_modules.FindFunctions (name, eFunctionNameTypeAuto, include_symbols, append, sc_list); 316 } 317 } 318 } 319 else 320 { 321 num_matches = target->GetImages().FindFunctions (name, eFunctionNameTypeAuto, include_symbols, append, sc_list); 322 } 323 324 SymbolContext sc; 325 326 if (num_matches == 0) 327 { 328 result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", m_options.symbol_name.c_str()); 329 result.SetStatus (eReturnStatusFailed); 330 return false; 331 } 332 333 sc_list.GetContextAtIndex (0, sc); 334 FileSpec start_file; 335 uint32_t start_line; 336 uint32_t end_line; 337 FileSpec end_file; 338 if (sc.function != NULL) 339 { 340 sc.function->GetStartLineSourceInfo (start_file, start_line); 341 if (start_line == 0) 342 { 343 result.AppendErrorWithFormat("Could not find line information for start of function: \"%s\".\n", m_options.symbol_name.c_str()); 344 result.SetStatus (eReturnStatusFailed); 345 return false; 346 } 347 sc.function->GetEndLineSourceInfo (end_file, end_line); 348 } 349 else 350 { 351 result.AppendErrorWithFormat("Could not find function info for: \"%s\".\n", m_options.symbol_name.c_str()); 352 result.SetStatus (eReturnStatusFailed); 353 return false; 354 } 355 356 if (num_matches > 1) 357 { 358 // This could either be because there are multiple functions of this name, in which case 359 // we'll have to specify this further... Or it could be because there are multiple inlined instances 360 // of one function. So run through the matches and if they all have the same file & line then we can just 361 // list one. 362 363 bool found_multiple = false; 364 365 for (size_t i = 1; i < num_matches; i++) 366 { 367 SymbolContext scratch_sc; 368 sc_list.GetContextAtIndex (i, scratch_sc); 369 if (scratch_sc.function != NULL) 370 { 371 FileSpec scratch_file; 372 uint32_t scratch_line; 373 scratch_sc.function->GetStartLineSourceInfo (scratch_file, scratch_line); 374 if (scratch_file != start_file 375 || scratch_line != start_line) 376 { 377 found_multiple = true; 378 break; 379 } 380 } 381 } 382 if (found_multiple) 383 { 384 StreamString s; 385 for (size_t i = 0; i < num_matches; i++) 386 { 387 SymbolContext scratch_sc; 388 sc_list.GetContextAtIndex (i, scratch_sc); 389 if (scratch_sc.function != NULL) 390 { 391 s.Printf("\n%lu: ", i); 392 scratch_sc.function->Dump (&s, true); 393 } 394 } 395 result.AppendErrorWithFormat("Multiple functions found matching: %s: \n%s\n", 396 m_options.symbol_name.c_str(), 397 s.GetData()); 398 result.SetStatus (eReturnStatusFailed); 399 return false; 400 } 401 } 402 403 404 // This is a little hacky, but the first line table entry for a function points to the "{" that 405 // starts the function block. It would be nice to actually get the function 406 // declaration in there too. So back up a bit, but not further than what you're going to display. 407 size_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2; 408 uint32_t line_no; 409 if (start_line <= lines_to_back_up) 410 line_no = 1; 411 else 412 line_no = start_line - lines_to_back_up; 413 414 // For fun, if the function is shorter than the number of lines we're supposed to display, 415 // only display the function... 416 if (end_line != 0) 417 { 418 if (m_options.num_lines > end_line - line_no) 419 m_options.num_lines = end_line - line_no; 420 } 421 422 char path_buf[PATH_MAX]; 423 start_file.GetPath(path_buf, sizeof(path_buf)); 424 425 if (m_options.show_bp_locs) 426 { 427 const bool show_inlines = true; 428 m_breakpoint_locations.Reset (start_file, 0, show_inlines); 429 SearchFilter target_search_filter (exe_ctx.GetTargetSP()); 430 target_search_filter.Search (m_breakpoint_locations); 431 } 432 else 433 m_breakpoint_locations.Clear(); 434 435 result.AppendMessageWithFormat("File: %s.\n", path_buf); 436 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (start_file, 437 line_no, 438 0, 439 m_options.num_lines, 440 "", 441 &result.GetOutputStream(), 442 GetBreakpointLocations ()); 443 444 result.SetStatus (eReturnStatusSuccessFinishResult); 445 return true; 446 447 } 448 else if (m_options.file_name.empty()) 449 { 450 // Last valid source manager context, or the current frame if no 451 // valid last context in source manager. 452 // One little trick here, if you type the exact same list command twice in a row, it is 453 // more likely because you typed it once, then typed it again 454 if (m_options.start_line == 0) 455 { 456 if (target->GetSourceManager().DisplayMoreWithLineNumbers (&result.GetOutputStream(), 457 GetBreakpointLocations ())) 458 { 459 result.SetStatus (eReturnStatusSuccessFinishResult); 460 } 461 } 462 else 463 { 464 if (m_options.show_bp_locs) 465 { 466 SourceManager::FileSP last_file_sp (target->GetSourceManager().GetLastFile ()); 467 if (last_file_sp) 468 { 469 const bool show_inlines = true; 470 m_breakpoint_locations.Reset (last_file_sp->GetFileSpec(), 0, show_inlines); 471 SearchFilter target_search_filter (target->GetSP()); 472 target_search_filter.Search (m_breakpoint_locations); 473 } 474 } 475 else 476 m_breakpoint_locations.Clear(); 477 478 if (target->GetSourceManager().DisplaySourceLinesWithLineNumbersUsingLastFile( 479 m_options.start_line, // Line to display 480 0, // Lines before line to display 481 m_options.num_lines, // Lines after line to display 482 "", // Don't mark "line" 483 &result.GetOutputStream(), 484 GetBreakpointLocations ())) 485 { 486 result.SetStatus (eReturnStatusSuccessFinishResult); 487 } 488 489 } 490 } 491 else 492 { 493 const char *filename = m_options.file_name.c_str(); 494 495 bool check_inlines = false; 496 SymbolContextList sc_list; 497 size_t num_matches = 0; 498 499 if (m_options.modules.size() > 0) 500 { 501 ModuleList matching_modules; 502 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++) 503 { 504 FileSpec module_spec(m_options.modules[i].c_str(), false); 505 if (module_spec) 506 { 507 matching_modules.Clear(); 508 target->GetImages().FindModules (&module_spec, NULL, NULL, NULL, matching_modules); 509 num_matches += matching_modules.ResolveSymbolContextForFilePath (filename, 510 0, 511 check_inlines, 512 eSymbolContextModule | eSymbolContextCompUnit, 513 sc_list); 514 } 515 } 516 } 517 else 518 { 519 num_matches = target->GetImages().ResolveSymbolContextForFilePath (filename, 520 0, 521 check_inlines, 522 eSymbolContextModule | eSymbolContextCompUnit, 523 sc_list); 524 } 525 526 if (num_matches == 0) 527 { 528 result.AppendErrorWithFormat("Could not find source file \"%s\".\n", 529 m_options.file_name.c_str()); 530 result.SetStatus (eReturnStatusFailed); 531 return false; 532 } 533 534 if (num_matches > 1) 535 { 536 SymbolContext sc; 537 bool got_multiple = false; 538 FileSpec *test_cu_spec = NULL; 539 540 for (unsigned i = 0; i < num_matches; i++) 541 { 542 sc_list.GetContextAtIndex(i, sc); 543 if (sc.comp_unit) 544 { 545 if (test_cu_spec) 546 { 547 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit)) 548 got_multiple = true; 549 break; 550 } 551 else 552 test_cu_spec = sc.comp_unit; 553 } 554 } 555 if (got_multiple) 556 { 557 result.AppendErrorWithFormat("Multiple source files found matching: \"%s.\"\n", 558 m_options.file_name.c_str()); 559 result.SetStatus (eReturnStatusFailed); 560 return false; 561 } 562 } 563 564 SymbolContext sc; 565 if (sc_list.GetContextAtIndex(0, sc)) 566 { 567 if (sc.comp_unit) 568 { 569 if (m_options.show_bp_locs) 570 { 571 const bool show_inlines = true; 572 m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines); 573 SearchFilter target_search_filter (target->GetSP()); 574 target_search_filter.Search (m_breakpoint_locations); 575 } 576 else 577 m_breakpoint_locations.Clear(); 578 579 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit, 580 m_options.start_line, 581 0, 582 m_options.num_lines, 583 "", 584 &result.GetOutputStream(), 585 GetBreakpointLocations ()); 586 587 result.SetStatus (eReturnStatusSuccessFinishResult); 588 } 589 else 590 { 591 result.AppendErrorWithFormat("No comp unit found for: \"%s.\"\n", 592 m_options.file_name.c_str()); 593 result.SetStatus (eReturnStatusFailed); 594 return false; 595 } 596 } 597 } 598 return result.Succeeded(); 599 } 600 601 virtual const char *GetRepeatCommand (Args ¤t_command_args, uint32_t index) 602 { 603 return m_cmd_name.c_str(); 604 } 605 606 protected: 607 const SymbolContextList * 608 GetBreakpointLocations () 609 { 610 if (m_breakpoint_locations.GetFileLineMatches().GetSize() > 0) 611 return &m_breakpoint_locations.GetFileLineMatches(); 612 return NULL; 613 } 614 CommandOptions m_options; 615 FileLineResolver m_breakpoint_locations; 616 617 }; 618 619 OptionDefinition 620 CommandObjectSourceList::CommandOptions::g_option_table[] = 621 { 622 { LLDB_OPT_SET_ALL, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "The number of source lines to display."}, 623 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Look up the source file in the given shared library."}, 624 { 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."}, 625 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."}, 626 { LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."}, 627 { LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeSymbol, "The name of a function whose source to display."}, 628 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } 629 }; 630 631 #pragma mark CommandObjectMultiwordSource 632 633 //------------------------------------------------------------------------- 634 // CommandObjectMultiwordSource 635 //------------------------------------------------------------------------- 636 637 CommandObjectMultiwordSource::CommandObjectMultiwordSource (CommandInterpreter &interpreter) : 638 CommandObjectMultiword (interpreter, 639 "source", 640 "A set of commands for accessing source file information", 641 "source <subcommand> [<subcommand-options>]") 642 { 643 LoadSubCommand ("info", CommandObjectSP (new CommandObjectSourceInfo (interpreter))); 644 LoadSubCommand ("list", CommandObjectSP (new CommandObjectSourceList (interpreter))); 645 } 646 647 CommandObjectMultiwordSource::~CommandObjectMultiwordSource () 648 { 649 } 650 651