1 //===-- Variable.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/Variable.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/Stream.h"
14 #include "lldb/Core/RegularExpression.h"
15 #include "lldb/Core/ValueObject.h"
16 #include "lldb/Core/ValueObjectVariable.h"
17 #include "lldb/Symbol/Block.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Symbol/SymbolContext.h"
20 #include "lldb/Symbol/Type.h"
21 #include "lldb/Symbol/VariableList.h"
22 #include "lldb/Target/ABI.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/RegisterContext.h"
25 #include "lldb/Target/StackFrame.h"
26 #include "lldb/Target/Thread.h"
27 #include "lldb/Target/Target.h"
30 using namespace lldb_private;
32 //----------------------------------------------------------------------
33 // Variable constructor
34 //----------------------------------------------------------------------
39 const char *mangled, // The mangled variable name for variables in namespaces
40 const lldb::SymbolFileTypeSP &symfile_type_sp,
42 SymbolContextScope *context,
43 Declaration* decl_ptr,
44 const DWARFExpression& location,
50 m_mangled (ConstString(mangled), true),
51 m_symfile_type_sp(symfile_type_sp),
53 m_owner_scope(context),
54 m_declaration(decl_ptr),
57 m_artificial(artificial)
61 //----------------------------------------------------------------------
63 //----------------------------------------------------------------------
70 Variable::GetName() const
73 return m_mangled.GetName();
78 Variable::NameMatches (const RegularExpression& regex) const
80 if (regex.Execute (m_name.AsCString()))
82 return m_mangled.NameMatches (regex);
88 if (m_symfile_type_sp)
89 return m_symfile_type_sp->GetType();
94 Variable::Dump(Stream *s, bool show_context) const
96 s->Printf("%p: ", static_cast<const void*>(this));
98 *s << "Variable" << (const UserID&)*this;
101 *s << ", name = \"" << m_name << "\"";
103 if (m_symfile_type_sp)
105 Type *type = m_symfile_type_sp->GetType();
108 *s << ", type = {" << type->GetID() << "} " << (void*)type << " (";
109 type->DumpTypeName(s);
114 if (m_scope != eValueTypeInvalid)
116 s->PutCString(", scope = ");
119 case eValueTypeVariableGlobal: s->PutCString(m_external ? "global" : "static"); break;
120 case eValueTypeVariableArgument: s->PutCString("parameter"); break;
121 case eValueTypeVariableLocal: s->PutCString("local"); break;
122 default: *s << "??? (" << m_scope << ')';
126 if (show_context && m_owner_scope != nullptr)
128 s->PutCString(", context = ( ");
129 m_owner_scope->DumpSymbolContext(s);
133 bool show_fullpaths = false;
134 m_declaration.Dump(s, show_fullpaths);
136 if (m_location.IsValid())
138 s->PutCString(", location = ");
139 lldb::addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
140 if (m_location.IsLocationList())
142 SymbolContext variable_sc;
143 m_owner_scope->CalculateSymbolContext(&variable_sc);
144 if (variable_sc.function)
145 loclist_base_addr = variable_sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
150 ModuleSP module_sp (m_owner_scope->CalculateSymbolContextModule());
152 abi = ABI::FindPlugin (module_sp->GetArchitecture()).get();
154 m_location.GetDescription(s, lldb::eDescriptionLevelBrief, loclist_base_addr, abi);
158 s->PutCString(", external");
161 s->PutCString(", artificial");
167 Variable::DumpDeclaration (Stream *s, bool show_fullpaths, bool show_module)
169 bool dumped_declaration_info = false;
173 m_owner_scope->CalculateSymbolContext(&sc);
175 sc.line_entry.Clear();
176 bool show_inlined_frames = false;
177 const bool show_function_arguments = true;
179 dumped_declaration_info = sc.DumpStopContext (s,
185 show_function_arguments);
190 if (m_declaration.DumpStopContext (s, false))
191 dumped_declaration_info = true;
192 return dumped_declaration_info;
196 Variable::MemorySize() const
198 return sizeof(Variable);
203 Variable::CalculateSymbolContext (SymbolContext *sc)
206 m_owner_scope->CalculateSymbolContext(sc);
212 Variable::LocationIsValidForFrame (StackFrame *frame)
214 // Is the variable is described by a single location?
215 if (!m_location.IsLocationList())
217 // Yes it is, the location is valid.
223 Function *function = frame->GetSymbolContext(eSymbolContextFunction).function;
226 TargetSP target_sp (frame->CalculateTarget());
228 addr_t loclist_base_load_addr = function->GetAddressRange().GetBaseAddress().GetLoadAddress (target_sp.get());
229 if (loclist_base_load_addr == LLDB_INVALID_ADDRESS)
231 // It is a location list. We just need to tell if the location
232 // list contains the current address when converted to a load
234 return m_location.LocationListContainsAddress (loclist_base_load_addr,
235 frame->GetFrameCodeAddress().GetLoadAddress (target_sp.get()));
242 Variable::LocationIsValidForAddress (const Address &address)
244 // Be sure to resolve the address to section offset prior to
245 // calling this function.
246 if (address.IsSectionOffset())
249 CalculateSymbolContext(&sc);
250 if (sc.module_sp == address.GetModule())
252 // Is the variable is described by a single location?
253 if (!m_location.IsLocationList())
255 // Yes it is, the location is valid.
261 addr_t loclist_base_file_addr = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
262 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
264 // It is a location list. We just need to tell if the location
265 // list contains the current address when converted to a load
267 return m_location.LocationListContainsAddress (loclist_base_file_addr,
268 address.GetFileAddress());
276 Variable::IsInScope (StackFrame *frame)
280 case eValueTypeRegister:
281 case eValueTypeRegisterSet:
282 return frame != nullptr;
284 case eValueTypeConstResult:
285 case eValueTypeVariableGlobal:
286 case eValueTypeVariableStatic:
289 case eValueTypeVariableArgument:
290 case eValueTypeVariableLocal:
293 // We don't have a location list, we just need to see if the block
294 // that this variable was defined in is currently
295 Block *deepest_frame_block = frame->GetSymbolContext(eSymbolContextBlock).block;
296 if (deepest_frame_block)
298 SymbolContext variable_sc;
299 CalculateSymbolContext (&variable_sc);
300 // Check for static or global variable defined at the compile unit
301 // level that wasn't defined in a block
302 if (variable_sc.block == nullptr)
305 if (variable_sc.block == deepest_frame_block)
307 return variable_sc.block->Contains (deepest_frame_block);
319 Variable::GetValuesForVariableExpressionPath (const char *variable_expr_path,
320 ExecutionContextScope *scope,
321 GetVariableCallback callback,
323 VariableList &variable_list,
324 ValueObjectList &valobj_list)
327 if (variable_expr_path && callback)
329 switch (variable_expr_path[0])
333 error = Variable::GetValuesForVariableExpressionPath (variable_expr_path + 1,
341 for (uint32_t i=0; i<valobj_list.GetSize(); )
344 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(i)->Dereference(tmp_error));
345 if (tmp_error.Fail())
347 variable_list.RemoveVariableAtIndex (i);
348 valobj_list.RemoveValueObjectAtIndex (i);
352 valobj_list.SetValueObjectAtIndex (i, valobj_sp);
359 error.SetErrorString ("unknown error");
367 error = Variable::GetValuesForVariableExpressionPath (variable_expr_path + 1,
375 for (uint32_t i=0; i<valobj_list.GetSize(); )
378 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(i)->AddressOf(tmp_error));
379 if (tmp_error.Fail())
381 variable_list.RemoveVariableAtIndex (i);
382 valobj_list.RemoveValueObjectAtIndex (i);
386 valobj_list.SetValueObjectAtIndex (i, valobj_sp);
393 error.SetErrorString ("unknown error");
401 static RegularExpression g_regex ("^([A-Za-z_:][A-Za-z_0-9:]*)(.*)");
402 RegularExpression::Match regex_match(1);
403 if (g_regex.Execute(variable_expr_path, ®ex_match))
405 std::string variable_name;
406 if (regex_match.GetMatchAtIndex(variable_expr_path, 1, variable_name))
408 variable_list.Clear();
409 if (callback (baton, variable_name.c_str(), variable_list))
412 while (i < variable_list.GetSize())
414 VariableSP var_sp (variable_list.GetVariableAtIndex (i));
415 ValueObjectSP valobj_sp;
418 ValueObjectSP variable_valobj_sp(ValueObjectVariable::Create (scope, var_sp));
419 if (variable_valobj_sp)
421 const char *variable_sub_expr_path = variable_expr_path + variable_name.size();
422 if (*variable_sub_expr_path)
424 const char* first_unparsed = nullptr;
425 ValueObject::ExpressionPathScanEndReason reason_to_stop;
426 ValueObject::ExpressionPathEndResultType final_value_type;
427 ValueObject::GetValueForExpressionPathOptions options;
428 ValueObject::ExpressionPathAftermath final_task_on_target;
430 valobj_sp = variable_valobj_sp->GetValueForExpressionPath (variable_sub_expr_path,
435 &final_task_on_target);
438 error.SetErrorStringWithFormat ("invalid expression path '%s' for variable '%s'",
439 variable_sub_expr_path,
440 var_sp->GetName().GetCString());
445 // Just the name of a variable with no extras
446 valobj_sp = variable_valobj_sp;
451 if (!var_sp || !valobj_sp)
453 variable_list.RemoveVariableAtIndex (i);
457 valobj_list.Append(valobj_sp);
462 if (variable_list.GetSize() > 0)
470 error.SetErrorStringWithFormat ("unable to extract a variable name from '%s'", variable_expr_path);
475 error.SetErrorString ("unknown error");
480 Variable::DumpLocationForAddress (Stream *s, const Address &address)
482 // Be sure to resolve the address to section offset prior to
483 // calling this function.
484 if (address.IsSectionOffset())
487 CalculateSymbolContext(&sc);
488 if (sc.module_sp == address.GetModule())
493 ModuleSP module_sp (m_owner_scope->CalculateSymbolContextModule());
495 abi = ABI::FindPlugin (module_sp->GetArchitecture()).get();
498 const addr_t file_addr = address.GetFileAddress();
501 if (sc.function->GetAddressRange().ContainsFileAddress(address))
503 addr_t loclist_base_file_addr = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
504 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
506 return m_location.DumpLocationForAddress (s,
507 eDescriptionLevelBrief,
508 loclist_base_file_addr,
513 return m_location.DumpLocationForAddress (s,
514 eDescriptionLevelBrief,
515 LLDB_INVALID_ADDRESS,
525 PrivateAutoComplete (StackFrame *frame,
526 const std::string &partial_path,
527 const std::string &prefix_path, // Anything that has been resolved already will be in here
528 const ClangASTType& clang_type,
530 bool &word_complete);
533 PrivateAutoCompleteMembers (StackFrame *frame,
534 const std::string &partial_member_name,
535 const std::string &partial_path,
536 const std::string &prefix_path, // Anything that has been resolved already will be in here
537 const ClangASTType& clang_type,
539 bool &word_complete);
542 PrivateAutoCompleteMembers (StackFrame *frame,
543 const std::string &partial_member_name,
544 const std::string &partial_path,
545 const std::string &prefix_path, // Anything that has been resolved already will be in here
546 const ClangASTType& clang_type,
551 // We are in a type parsing child members
552 const uint32_t num_bases = clang_type.GetNumDirectBaseClasses();
556 for (uint32_t i = 0; i < num_bases; ++i)
558 ClangASTType base_class_type (clang_type.GetDirectBaseClassAtIndex (i, nullptr));
560 PrivateAutoCompleteMembers (frame,
564 base_class_type.GetCanonicalType(),
570 const uint32_t num_vbases = clang_type.GetNumVirtualBaseClasses();
574 for (uint32_t i = 0; i < num_vbases; ++i)
576 ClangASTType vbase_class_type (clang_type.GetVirtualBaseClassAtIndex(i,nullptr));
578 PrivateAutoCompleteMembers (frame,
582 vbase_class_type.GetCanonicalType(),
588 // We are in a type parsing child members
589 const uint32_t num_fields = clang_type.GetNumFields();
593 for (uint32_t i = 0; i < num_fields; ++i)
595 std::string member_name;
597 ClangASTType member_clang_type = clang_type.GetFieldAtIndex (i, member_name, nullptr, nullptr, nullptr);
599 if (partial_member_name.empty() ||
600 member_name.find(partial_member_name) == 0)
602 if (member_name == partial_member_name)
604 PrivateAutoComplete (frame,
606 prefix_path + member_name, // Anything that has been resolved already will be in here
607 member_clang_type.GetCanonicalType(),
613 matches.AppendString (prefix_path + member_name);
621 PrivateAutoComplete (StackFrame *frame,
622 const std::string &partial_path,
623 const std::string &prefix_path, // Anything that has been resolved already will be in here
624 const ClangASTType& clang_type,
628 // printf ("\nPrivateAutoComplete()\n\tprefix_path = '%s'\n\tpartial_path = '%s'\n", prefix_path.c_str(), partial_path.c_str());
629 std::string remaining_partial_path;
631 const lldb::TypeClass type_class = clang_type.GetTypeClass();
632 if (partial_path.empty())
634 if (clang_type.IsValid())
639 case eTypeClassArray:
640 case eTypeClassBlockPointer:
641 case eTypeClassBuiltin:
642 case eTypeClassComplexFloat:
643 case eTypeClassComplexInteger:
644 case eTypeClassEnumeration:
645 case eTypeClassFunction:
646 case eTypeClassMemberPointer:
647 case eTypeClassReference:
648 case eTypeClassTypedef:
649 case eTypeClassVector:
651 matches.AppendString (prefix_path);
652 word_complete = matches.GetSize() == 1;
656 case eTypeClassClass:
657 case eTypeClassStruct:
658 case eTypeClassUnion:
659 if (prefix_path.back() != '.')
660 matches.AppendString (prefix_path + '.');
663 case eTypeClassObjCObject:
664 case eTypeClassObjCInterface:
666 case eTypeClassObjCObjectPointer:
667 case eTypeClassPointer:
669 bool omit_empty_base_classes = true;
670 if (clang_type.GetNumChildren (omit_empty_base_classes) > 0)
671 matches.AppendString (prefix_path + "->");
674 matches.AppendString (prefix_path);
675 word_complete = true;
685 const bool get_file_globals = true;
687 VariableList *variable_list = frame->GetVariableList(get_file_globals);
691 const size_t num_variables = variable_list->GetSize();
692 for (size_t i=0; i<num_variables; ++i)
694 Variable *variable = variable_list->GetVariableAtIndex(i).get();
695 matches.AppendString (variable->GetName().AsCString());
703 const char ch = partial_path[0];
707 if (prefix_path.empty())
709 PrivateAutoComplete (frame,
710 partial_path.substr(1),
719 if (prefix_path.empty())
721 PrivateAutoComplete (frame,
722 partial_path.substr(1),
731 if (partial_path[1] == '>' && !prefix_path.empty())
735 case lldb::eTypeClassPointer:
737 ClangASTType pointee_type(clang_type.GetPointeeType());
740 // If there is more after the "->", then search deeper
741 PrivateAutoComplete (frame,
742 partial_path.substr(2),
744 pointee_type.GetCanonicalType(),
750 // Nothing after the "->", so list all members
751 PrivateAutoCompleteMembers (frame,
755 pointee_type.GetCanonicalType(),
767 if (clang_type.IsValid())
771 case lldb::eTypeClassUnion:
772 case lldb::eTypeClassStruct:
773 case lldb::eTypeClassClass:
776 // If there is more after the ".", then search deeper
777 PrivateAutoComplete (frame,
778 partial_path.substr(1),
787 // Nothing after the ".", so list all members
788 PrivateAutoCompleteMembers (frame,
802 if (isalpha(ch) || ch == '_' || ch == '$')
804 const size_t partial_path_len = partial_path.size();
806 while (pos < partial_path_len)
808 const char curr_ch = partial_path[pos];
809 if (isalnum(curr_ch) || curr_ch == '_' || curr_ch == '$')
817 std::string token(partial_path, 0, pos);
818 remaining_partial_path = partial_path.substr(pos);
820 if (clang_type.IsValid())
822 PrivateAutoCompleteMembers (frame,
824 remaining_partial_path,
832 // We haven't found our variable yet
833 const bool get_file_globals = true;
835 VariableList *variable_list = frame->GetVariableList(get_file_globals);
840 const size_t num_variables = variable_list->GetSize();
841 for (size_t i=0; i<num_variables; ++i)
843 Variable *variable = variable_list->GetVariableAtIndex(i).get();
848 const char *variable_name = variable->GetName().AsCString();
849 if (strstr(variable_name, token.c_str()) == variable_name)
851 if (strcmp (variable_name, token.c_str()) == 0)
853 Type *variable_type = variable->GetType();
856 ClangASTType variable_clang_type (variable_type->GetClangForwardType());
857 PrivateAutoComplete (frame,
858 remaining_partial_path,
859 prefix_path + token, // Anything that has been resolved already will be in here
860 variable_clang_type.GetCanonicalType(),
866 matches.AppendString (prefix_path + variable_name);
869 else if (remaining_partial_path.empty())
871 matches.AppendString (prefix_path + variable_name);
885 Variable::AutoComplete (const ExecutionContext &exe_ctx,
886 const char *partial_path_cstr,
890 word_complete = false;
891 std::string partial_path;
892 std::string prefix_path;
893 ClangASTType clang_type;
894 if (partial_path_cstr && partial_path_cstr[0])
895 partial_path = partial_path_cstr;
897 PrivateAutoComplete (exe_ctx.GetFramePtr(),
904 return matches.GetSize();