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