CommandObjectFrame.cpp revision 567e7f3ba16eb48cb9fd6a2f26f2f7269eb6983c
1//===-- CommandObjectFrame.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 "CommandObjectFrame.h" 11 12// C Includes 13// C++ Includes 14#include <string> 15// Other libraries and framework includes 16// Project includes 17#include "lldb/Core/DataVisualization.h" 18#include "lldb/Core/Debugger.h" 19#include "lldb/Core/Module.h" 20#include "lldb/Core/StreamFile.h" 21#include "lldb/Core/StreamString.h" 22#include "lldb/Core/Timer.h" 23#include "lldb/Core/Value.h" 24#include "lldb/Core/ValueObject.h" 25#include "lldb/Core/ValueObjectVariable.h" 26#include "lldb/Host/Host.h" 27#include "lldb/Interpreter/Args.h" 28#include "lldb/Interpreter/CommandInterpreter.h" 29#include "lldb/Interpreter/CommandReturnObject.h" 30#include "lldb/Interpreter/Options.h" 31#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h" 32#include "lldb/Interpreter/OptionGroupVariable.h" 33#include "lldb/Interpreter/OptionGroupWatchpoint.h" 34#include "lldb/Symbol/ClangASTType.h" 35#include "lldb/Symbol/ClangASTContext.h" 36#include "lldb/Symbol/ObjectFile.h" 37#include "lldb/Symbol/SymbolContext.h" 38#include "lldb/Symbol/Type.h" 39#include "lldb/Symbol/Variable.h" 40#include "lldb/Symbol/VariableList.h" 41#include "lldb/Target/Process.h" 42#include "lldb/Target/StackFrame.h" 43#include "lldb/Target/Thread.h" 44#include "lldb/Target/Target.h" 45 46using namespace lldb; 47using namespace lldb_private; 48 49#pragma mark CommandObjectFrameInfo 50 51//------------------------------------------------------------------------- 52// CommandObjectFrameInfo 53//------------------------------------------------------------------------- 54 55class CommandObjectFrameInfo : public CommandObject 56{ 57public: 58 59 CommandObjectFrameInfo (CommandInterpreter &interpreter) : 60 CommandObject (interpreter, 61 "frame info", 62 "List information about the currently selected frame in the current thread.", 63 "frame info", 64 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused) 65 { 66 } 67 68 ~CommandObjectFrameInfo () 69 { 70 } 71 72 bool 73 Execute (Args& command, 74 CommandReturnObject &result) 75 { 76 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 77 StackFrame *frame = exe_ctx.GetFramePtr(); 78 if (frame) 79 { 80 frame->DumpUsingSettingsFormat (&result.GetOutputStream()); 81 result.SetStatus (eReturnStatusSuccessFinishResult); 82 } 83 else 84 { 85 result.AppendError ("no current frame"); 86 result.SetStatus (eReturnStatusFailed); 87 } 88 return result.Succeeded(); 89 } 90}; 91 92#pragma mark CommandObjectFrameSelect 93 94//------------------------------------------------------------------------- 95// CommandObjectFrameSelect 96//------------------------------------------------------------------------- 97 98class CommandObjectFrameSelect : public CommandObject 99{ 100public: 101 102 class CommandOptions : public Options 103 { 104 public: 105 106 CommandOptions (CommandInterpreter &interpreter) : 107 Options(interpreter) 108 { 109 OptionParsingStarting (); 110 } 111 112 virtual 113 ~CommandOptions () 114 { 115 } 116 117 virtual Error 118 SetOptionValue (uint32_t option_idx, const char *option_arg) 119 { 120 Error error; 121 bool success = false; 122 char short_option = (char) m_getopt_table[option_idx].val; 123 switch (short_option) 124 { 125 case 'r': 126 relative_frame_offset = Args::StringToSInt32 (option_arg, INT32_MIN, 0, &success); 127 if (!success) 128 error.SetErrorStringWithFormat ("invalid frame offset argument '%s'.\n", option_arg); 129 break; 130 131 default: 132 error.SetErrorStringWithFormat ("Invalid short option character '%c'.\n", short_option); 133 break; 134 } 135 136 return error; 137 } 138 139 void 140 OptionParsingStarting () 141 { 142 relative_frame_offset = INT32_MIN; 143 } 144 145 const OptionDefinition* 146 GetDefinitions () 147 { 148 return g_option_table; 149 } 150 151 // Options table: Required for subclasses of Options. 152 153 static OptionDefinition g_option_table[]; 154 int32_t relative_frame_offset; 155 }; 156 157 CommandObjectFrameSelect (CommandInterpreter &interpreter) : 158 CommandObject (interpreter, 159 "frame select", 160 "Select a frame by index from within the current thread and make it the current frame.", 161 NULL, 162 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused), 163 m_options (interpreter) 164 { 165 CommandArgumentEntry arg; 166 CommandArgumentData index_arg; 167 168 // Define the first (and only) variant of this arg. 169 index_arg.arg_type = eArgTypeFrameIndex; 170 index_arg.arg_repetition = eArgRepeatOptional; 171 172 // There is only one variant this argument could be; put it into the argument entry. 173 arg.push_back (index_arg); 174 175 // Push the data for the first argument into the m_arguments vector. 176 m_arguments.push_back (arg); 177 } 178 179 ~CommandObjectFrameSelect () 180 { 181 } 182 183 virtual 184 Options * 185 GetOptions () 186 { 187 return &m_options; 188 } 189 190 191 bool 192 Execute (Args& command, 193 CommandReturnObject &result) 194 { 195 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext()); 196 Thread *thread = exe_ctx.GetThreadPtr(); 197 if (thread) 198 { 199 const uint32_t num_frames = thread->GetStackFrameCount(); 200 uint32_t frame_idx = UINT32_MAX; 201 if (m_options.relative_frame_offset != INT32_MIN) 202 { 203 // The one and only argument is a signed relative frame index 204 frame_idx = thread->GetSelectedFrameIndex (); 205 if (frame_idx == UINT32_MAX) 206 frame_idx = 0; 207 208 if (m_options.relative_frame_offset < 0) 209 { 210 if (frame_idx >= -m_options.relative_frame_offset) 211 frame_idx += m_options.relative_frame_offset; 212 else 213 { 214 if (frame_idx == 0) 215 { 216 //If you are already at the bottom of the stack, then just warn and don't reset the frame. 217 result.AppendError("Already at the bottom of the stack"); 218 result.SetStatus(eReturnStatusFailed); 219 return false; 220 } 221 else 222 frame_idx = 0; 223 } 224 } 225 else if (m_options.relative_frame_offset > 0) 226 { 227 if (num_frames - frame_idx > m_options.relative_frame_offset) 228 frame_idx += m_options.relative_frame_offset; 229 else 230 { 231 if (frame_idx == num_frames - 1) 232 { 233 //If we are already at the top of the stack, just warn and don't reset the frame. 234 result.AppendError("Already at the top of the stack"); 235 result.SetStatus(eReturnStatusFailed); 236 return false; 237 } 238 else 239 frame_idx = num_frames - 1; 240 } 241 } 242 } 243 else 244 { 245 if (command.GetArgumentCount() == 1) 246 { 247 const char *frame_idx_cstr = command.GetArgumentAtIndex(0); 248 frame_idx = Args::StringToUInt32 (frame_idx_cstr, UINT32_MAX, 0); 249 } 250 else 251 { 252 result.AppendError ("invalid arguments.\n"); 253 m_options.GenerateOptionUsage (result.GetErrorStream(), this); 254 } 255 } 256 257 if (frame_idx < num_frames) 258 { 259 thread->SetSelectedFrameByIndex (frame_idx); 260 exe_ctx.SetFrameSP(thread->GetSelectedFrame ()); 261 StackFrame *frame = exe_ctx.GetFramePtr(); 262 if (frame) 263 { 264 bool already_shown = false; 265 SymbolContext frame_sc(frame->GetSymbolContext(eSymbolContextLineEntry)); 266 if (m_interpreter.GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0) 267 { 268 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line); 269 } 270 271 bool show_frame_info = true; 272 bool show_source = !already_shown; 273 uint32_t source_lines_before = 3; 274 uint32_t source_lines_after = 3; 275 if (frame->GetStatus (result.GetOutputStream(), 276 show_frame_info, 277 show_source, 278 source_lines_before, 279 source_lines_after)) 280 { 281 result.SetStatus (eReturnStatusSuccessFinishResult); 282 return result.Succeeded(); 283 } 284 } 285 } 286 result.AppendErrorWithFormat ("Frame index (%u) out of range.\n", frame_idx); 287 } 288 else 289 { 290 result.AppendError ("no current thread"); 291 } 292 result.SetStatus (eReturnStatusFailed); 293 return false; 294 } 295protected: 296 297 CommandOptions m_options; 298}; 299 300OptionDefinition 301CommandObjectFrameSelect::CommandOptions::g_option_table[] = 302{ 303{ LLDB_OPT_SET_1, false, "relative", 'r', required_argument, NULL, 0, eArgTypeOffset, "A relative frame index offset from the current frame index."}, 304{ 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL } 305}; 306 307#pragma mark CommandObjectFrameVariable 308//---------------------------------------------------------------------- 309// List images with associated information 310//---------------------------------------------------------------------- 311class CommandObjectFrameVariable : public CommandObject 312{ 313public: 314 315 CommandObjectFrameVariable (CommandInterpreter &interpreter) : 316 CommandObject (interpreter, 317 "frame variable", 318 "Show frame variables. All argument and local variables " 319 "that are in scope will be shown when no arguments are given. " 320 "If any arguments are specified, they can be names of " 321 "argument, local, file static and file global variables. " 322 "Children of aggregate variables can be specified such as " 323 "'var->child.x'. " 324 "NOTE that '-w' option is not working yet!!! " 325 "You can choose to watch a variable with the '-w' option. " 326 "Note that hardware resources for watching are often limited.", 327 NULL, 328 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused), 329 m_option_group (interpreter), 330 m_option_variable(true), // Include the frame specific options by passing "true" 331 m_option_watchpoint(), 332 m_varobj_options() 333 { 334 CommandArgumentEntry arg; 335 CommandArgumentData var_name_arg; 336 337 // Define the first (and only) variant of this arg. 338 var_name_arg.arg_type = eArgTypeVarName; 339 var_name_arg.arg_repetition = eArgRepeatStar; 340 341 // There is only one variant this argument could be; put it into the argument entry. 342 arg.push_back (var_name_arg); 343 344 // Push the data for the first argument into the m_arguments vector. 345 m_arguments.push_back (arg); 346 347 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 348 m_option_group.Append (&m_option_watchpoint, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 349 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 350 m_option_group.Finalize(); 351 } 352 353 virtual 354 ~CommandObjectFrameVariable () 355 { 356 } 357 358 virtual 359 Options * 360 GetOptions () 361 { 362 return &m_option_group; 363 } 364 365 366 virtual bool 367 Execute 368 ( 369 Args& command, 370 CommandReturnObject &result 371 ) 372 { 373 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 374 StackFrame *frame = exe_ctx.GetFramePtr(); 375 if (frame == NULL) 376 { 377 result.AppendError ("you must be stopped in a valid stack frame to view frame variables."); 378 result.SetStatus (eReturnStatusFailed); 379 return false; 380 } 381 382 Stream &s = result.GetOutputStream(); 383 384 bool get_file_globals = true; 385 386 // Be careful about the stack frame, if any summary formatter runs code, it might clear the StackFrameList 387 // for the thread. So hold onto a shared pointer to the frame so it stays alive. 388 389 VariableList *variable_list = frame->GetVariableList (get_file_globals); 390 391 VariableSP var_sp; 392 ValueObjectSP valobj_sp; 393 394 const char *name_cstr = NULL; 395 size_t idx; 396 397 SummaryFormatSP summary_format_sp; 398 if (!m_option_variable.summary.empty()) 399 DataVisualization::NamedSummaryFormats::GetSummaryFormat(ConstString(m_option_variable.summary.c_str()), summary_format_sp); 400 401 ValueObject::DumpValueObjectOptions options; 402 403 options.SetPointerDepth(m_varobj_options.ptr_depth) 404 .SetMaximumDepth(m_varobj_options.max_depth) 405 .SetShowTypes(m_varobj_options.show_types) 406 .SetShowLocation(m_varobj_options.show_location) 407 .SetUseObjectiveC(m_varobj_options.use_objc) 408 .SetUseDynamicType(m_varobj_options.use_dynamic) 409 .SetUseSyntheticValue((lldb::SyntheticValueType)m_varobj_options.use_synth) 410 .SetFlatOutput(m_varobj_options.flat_output) 411 .SetOmitSummaryDepth(m_varobj_options.no_summary_depth) 412 .SetIgnoreCap(m_varobj_options.ignore_cap); 413 414 if (m_varobj_options.be_raw) 415 options.SetRawDisplay(true); 416 417 if (variable_list) 418 { 419 // If watching a variable, there are certain restrictions to be followed. 420 if (m_option_watchpoint.watch_variable) 421 { 422 if (command.GetArgumentCount() != 1) { 423 result.GetErrorStream().Printf("error: specify exactly one variable when using the '-w' option\n"); 424 result.SetStatus(eReturnStatusFailed); 425 return false; 426 } else if (m_option_variable.use_regex) { 427 result.GetErrorStream().Printf("error: specify your variable name exactly (no regex) when using the '-w' option\n"); 428 result.SetStatus(eReturnStatusFailed); 429 return false; 430 } 431 432 // Things have checked out ok... 433 // m_option_watchpoint.watch_mode specifies the mode for watching. 434 } 435 if (command.GetArgumentCount() > 0) 436 { 437 VariableList regex_var_list; 438 439 // If we have any args to the variable command, we will make 440 // variable objects from them... 441 for (idx = 0; (name_cstr = command.GetArgumentAtIndex(idx)) != NULL; ++idx) 442 { 443 if (m_option_variable.use_regex) 444 { 445 const uint32_t regex_start_index = regex_var_list.GetSize(); 446 RegularExpression regex (name_cstr); 447 if (regex.Compile(name_cstr)) 448 { 449 size_t num_matches = 0; 450 const size_t num_new_regex_vars = variable_list->AppendVariablesIfUnique(regex, 451 regex_var_list, 452 num_matches); 453 if (num_new_regex_vars > 0) 454 { 455 for (uint32_t regex_idx = regex_start_index, end_index = regex_var_list.GetSize(); 456 regex_idx < end_index; 457 ++regex_idx) 458 { 459 var_sp = regex_var_list.GetVariableAtIndex (regex_idx); 460 if (var_sp) 461 { 462 valobj_sp = frame->GetValueObjectForFrameVariable (var_sp, m_varobj_options.use_dynamic); 463 if (valobj_sp) 464 { 465 if (m_option_variable.format != eFormatDefault) 466 valobj_sp->SetFormat (m_option_variable.format); 467 468 if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile()) 469 { 470 bool show_fullpaths = false; 471 bool show_module = true; 472 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module)) 473 s.PutCString (": "); 474 } 475 if (summary_format_sp) 476 valobj_sp->SetCustomSummaryFormat(summary_format_sp); 477 ValueObject::DumpValueObject (result.GetOutputStream(), 478 valobj_sp.get(), 479 options); 480 } 481 } 482 } 483 } 484 else if (num_matches == 0) 485 { 486 result.GetErrorStream().Printf ("error: no variables matched the regular expression '%s'.\n", name_cstr); 487 } 488 } 489 else 490 { 491 char regex_error[1024]; 492 if (regex.GetErrorAsCString(regex_error, sizeof(regex_error))) 493 result.GetErrorStream().Printf ("error: %s\n", regex_error); 494 else 495 result.GetErrorStream().Printf ("error: unkown regex error when compiling '%s'\n", name_cstr); 496 } 497 } 498 else // No regex, either exact variable names or variable expressions. 499 { 500 Error error; 501 uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember; 502 lldb::VariableSP var_sp; 503 valobj_sp = frame->GetValueForVariableExpressionPath (name_cstr, 504 m_varobj_options.use_dynamic, 505 expr_path_options, 506 var_sp, 507 error); 508 if (valobj_sp) 509 { 510 if (m_option_variable.format != eFormatDefault) 511 valobj_sp->SetFormat (m_option_variable.format); 512 if (m_option_variable.show_decl && var_sp && var_sp->GetDeclaration ().GetFile()) 513 { 514 var_sp->GetDeclaration ().DumpStopContext (&s, false); 515 s.PutCString (": "); 516 } 517 if (summary_format_sp) 518 valobj_sp->SetCustomSummaryFormat(summary_format_sp); 519 520 Stream &output_stream = result.GetOutputStream(); 521 ValueObject::DumpValueObject (output_stream, 522 valobj_sp.get(), 523 valobj_sp->GetParent() ? name_cstr : NULL, 524 options); 525 // Process watchpoint if necessary. 526 if (m_option_watchpoint.watch_variable) 527 { 528 AddressType addr_type; 529 lldb::addr_t addr = valobj_sp->GetAddressOf(false, &addr_type); 530 size_t size = 0; 531 if (addr_type == eAddressTypeLoad) { 532 // We're in business. 533 // Find out the size of this variable. 534 size = valobj_sp->GetByteSize(); 535 } 536 uint32_t watch_type = m_option_watchpoint.watch_type; 537 WatchpointLocation *wp_loc = exe_ctx.GetTargetRef().CreateWatchpointLocation(addr, size, watch_type).get(); 538 if (wp_loc) 539 { 540 if (var_sp && var_sp->GetDeclaration().GetFile()) 541 { 542 StreamString ss; 543 // True to show fullpath for declaration file. 544 var_sp->GetDeclaration().DumpStopContext(&ss, true); 545 wp_loc->SetDeclInfo(ss.GetString()); 546 } 547 StreamString ss; 548 output_stream.Printf("Watchpoint created: "); 549 wp_loc->GetDescription(&output_stream, lldb::eDescriptionLevelFull); 550 output_stream.EOL(); 551 result.SetStatus(eReturnStatusSuccessFinishResult); 552 } 553 else 554 { 555 result.AppendErrorWithFormat("Watchpoint creation failed.\n"); 556 result.SetStatus(eReturnStatusFailed); 557 } 558 return (wp_loc != NULL); 559 } 560 } 561 else 562 { 563 const char *error_cstr = error.AsCString(NULL); 564 if (error_cstr) 565 result.GetErrorStream().Printf("error: %s\n", error_cstr); 566 else 567 result.GetErrorStream().Printf ("error: unable to find any variable expression path that matches '%s'\n", name_cstr); 568 } 569 } 570 } 571 } 572 else // No command arg specified. Use variable_list, instead. 573 { 574 const uint32_t num_variables = variable_list->GetSize(); 575 if (num_variables > 0) 576 { 577 for (uint32_t i=0; i<num_variables; i++) 578 { 579 var_sp = variable_list->GetVariableAtIndex(i); 580 bool dump_variable = true; 581 switch (var_sp->GetScope()) 582 { 583 case eValueTypeVariableGlobal: 584 dump_variable = m_option_variable.show_globals; 585 if (dump_variable && m_option_variable.show_scope) 586 s.PutCString("GLOBAL: "); 587 break; 588 589 case eValueTypeVariableStatic: 590 dump_variable = m_option_variable.show_globals; 591 if (dump_variable && m_option_variable.show_scope) 592 s.PutCString("STATIC: "); 593 break; 594 595 case eValueTypeVariableArgument: 596 dump_variable = m_option_variable.show_args; 597 if (dump_variable && m_option_variable.show_scope) 598 s.PutCString(" ARG: "); 599 break; 600 601 case eValueTypeVariableLocal: 602 dump_variable = m_option_variable.show_locals; 603 if (dump_variable && m_option_variable.show_scope) 604 s.PutCString(" LOCAL: "); 605 break; 606 607 default: 608 break; 609 } 610 611 if (dump_variable) 612 { 613 // Use the variable object code to make sure we are 614 // using the same APIs as the the public API will be 615 // using... 616 valobj_sp = frame->GetValueObjectForFrameVariable (var_sp, 617 m_varobj_options.use_dynamic); 618 if (valobj_sp) 619 { 620 if (m_option_variable.format != eFormatDefault) 621 valobj_sp->SetFormat (m_option_variable.format); 622 623 // When dumping all variables, don't print any variables 624 // that are not in scope to avoid extra unneeded output 625 if (valobj_sp->IsInScope ()) 626 { 627 if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile()) 628 { 629 var_sp->GetDeclaration ().DumpStopContext (&s, false); 630 s.PutCString (": "); 631 } 632 if (summary_format_sp) 633 valobj_sp->SetCustomSummaryFormat(summary_format_sp); 634 ValueObject::DumpValueObject (result.GetOutputStream(), 635 valobj_sp.get(), 636 name_cstr, 637 options); 638 } 639 } 640 } 641 } 642 } 643 } 644 result.SetStatus (eReturnStatusSuccessFinishResult); 645 } 646 647 if (m_interpreter.TruncationWarningNecessary()) 648 { 649 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(), 650 m_cmd_name.c_str()); 651 m_interpreter.TruncationWarningGiven(); 652 } 653 654 return result.Succeeded(); 655 } 656protected: 657 658 OptionGroupOptions m_option_group; 659 OptionGroupVariable m_option_variable; 660 OptionGroupWatchpoint m_option_watchpoint; 661 OptionGroupValueObjectDisplay m_varobj_options; 662}; 663 664 665#pragma mark CommandObjectMultiwordFrame 666 667//------------------------------------------------------------------------- 668// CommandObjectMultiwordFrame 669//------------------------------------------------------------------------- 670 671CommandObjectMultiwordFrame::CommandObjectMultiwordFrame (CommandInterpreter &interpreter) : 672 CommandObjectMultiword (interpreter, 673 "frame", 674 "A set of commands for operating on the current thread's frames.", 675 "frame <subcommand> [<subcommand-options>]") 676{ 677 LoadSubCommand ("info", CommandObjectSP (new CommandObjectFrameInfo (interpreter))); 678 LoadSubCommand ("select", CommandObjectSP (new CommandObjectFrameSelect (interpreter))); 679 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectFrameVariable (interpreter))); 680} 681 682CommandObjectMultiwordFrame::~CommandObjectMultiwordFrame () 683{ 684} 685 686