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