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