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 or fully qualified name of the variable.
40 const lldb::SymbolFileTypeSP &symfile_type_sp,
42 SymbolContextScope *context,
43 Declaration* decl_ptr,
44 const DWARFExpression& location,
50 m_mangled (ConstString(mangled)),
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
72 const ConstString &name = m_mangled.GetName();
79 Variable::NameMatches (const RegularExpression& regex) const
81 if (regex.Execute (m_name.AsCString()))
83 return m_mangled.NameMatches (regex);
89 if (m_symfile_type_sp)
90 return m_symfile_type_sp->GetType();
95 Variable::Dump(Stream *s, bool show_context) const
97 s->Printf("%p: ", static_cast<const void*>(this));
99 *s << "Variable" << (const UserID&)*this;
102 *s << ", name = \"" << m_name << "\"";
104 if (m_symfile_type_sp)
106 Type *type = m_symfile_type_sp->GetType();
109 *s << ", type = {" << type->GetID() << "} " << (void*)type << " (";
110 type->DumpTypeName(s);
115 if (m_scope != eValueTypeInvalid)
117 s->PutCString(", scope = ");
120 case eValueTypeVariableGlobal: s->PutCString(m_external ? "global" : "static"); break;
121 case eValueTypeVariableArgument: s->PutCString("parameter"); break;
122 case eValueTypeVariableLocal: s->PutCString("local"); break;
123 default: *s << "??? (" << m_scope << ')';
127 if (show_context && m_owner_scope != nullptr)
129 s->PutCString(", context = ( ");
130 m_owner_scope->DumpSymbolContext(s);
134 bool show_fullpaths = false;
135 m_declaration.Dump(s, show_fullpaths);
137 if (m_location.IsValid())
139 s->PutCString(", location = ");
140 lldb::addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
141 if (m_location.IsLocationList())
143 SymbolContext variable_sc;
144 m_owner_scope->CalculateSymbolContext(&variable_sc);
145 if (variable_sc.function)
146 loclist_base_addr = variable_sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
151 ModuleSP module_sp (m_owner_scope->CalculateSymbolContextModule());
153 abi = ABI::FindPlugin (module_sp->GetArchitecture()).get();
155 m_location.GetDescription(s, lldb::eDescriptionLevelBrief, loclist_base_addr, abi);
159 s->PutCString(", external");
162 s->PutCString(", artificial");
168 Variable::DumpDeclaration (Stream *s, bool show_fullpaths, bool show_module)
170 bool dumped_declaration_info = false;
174 m_owner_scope->CalculateSymbolContext(&sc);
176 sc.line_entry.Clear();
177 bool show_inlined_frames = false;
178 const bool show_function_arguments = true;
179 const bool show_function_name = true;
181 dumped_declaration_info = sc.DumpStopContext (s,
187 show_function_arguments,
193 if (m_declaration.DumpStopContext (s, false))
194 dumped_declaration_info = true;
195 return dumped_declaration_info;
199 Variable::MemorySize() const
201 return sizeof(Variable);
206 Variable::CalculateSymbolContext (SymbolContext *sc)
210 m_owner_scope->CalculateSymbolContext(sc);
218 Variable::LocationIsValidForFrame (StackFrame *frame)
220 // Is the variable is described by a single location?
221 if (!m_location.IsLocationList())
223 // Yes it is, the location is valid.
229 Function *function = frame->GetSymbolContext(eSymbolContextFunction).function;
232 TargetSP target_sp (frame->CalculateTarget());
234 addr_t loclist_base_load_addr = function->GetAddressRange().GetBaseAddress().GetLoadAddress (target_sp.get());
235 if (loclist_base_load_addr == LLDB_INVALID_ADDRESS)
237 // It is a location list. We just need to tell if the location
238 // list contains the current address when converted to a load
240 return m_location.LocationListContainsAddress (loclist_base_load_addr,
241 frame->GetFrameCodeAddress().GetLoadAddress (target_sp.get()));
248 Variable::LocationIsValidForAddress (const Address &address)
250 // Be sure to resolve the address to section offset prior to
251 // calling this function.
252 if (address.IsSectionOffset())
255 CalculateSymbolContext(&sc);
256 if (sc.module_sp == address.GetModule())
258 // Is the variable is described by a single location?
259 if (!m_location.IsLocationList())
261 // Yes it is, the location is valid.
267 addr_t loclist_base_file_addr = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
268 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
270 // It is a location list. We just need to tell if the location
271 // list contains the current address when converted to a load
273 return m_location.LocationListContainsAddress (loclist_base_file_addr,
274 address.GetFileAddress());
282 Variable::IsInScope (StackFrame *frame)
286 case eValueTypeRegister:
287 case eValueTypeRegisterSet:
288 return frame != nullptr;
290 case eValueTypeConstResult:
291 case eValueTypeVariableGlobal:
292 case eValueTypeVariableStatic:
295 case eValueTypeVariableArgument:
296 case eValueTypeVariableLocal:
299 // We don't have a location list, we just need to see if the block
300 // that this variable was defined in is currently
301 Block *deepest_frame_block = frame->GetSymbolContext(eSymbolContextBlock).block;
302 if (deepest_frame_block)
304 SymbolContext variable_sc;
305 CalculateSymbolContext (&variable_sc);
306 // Check for static or global variable defined at the compile unit
307 // level that wasn't defined in a block
308 if (variable_sc.block == nullptr)
311 if (variable_sc.block == deepest_frame_block)
313 return variable_sc.block->Contains (deepest_frame_block);
325 Variable::GetValuesForVariableExpressionPath (const char *variable_expr_path,
326 ExecutionContextScope *scope,
327 GetVariableCallback callback,
329 VariableList &variable_list,
330 ValueObjectList &valobj_list)
333 if (variable_expr_path && callback)
335 switch (variable_expr_path[0])
339 error = Variable::GetValuesForVariableExpressionPath (variable_expr_path + 1,
347 for (uint32_t i=0; i<valobj_list.GetSize(); )
350 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(i)->Dereference(tmp_error));
351 if (tmp_error.Fail())
353 variable_list.RemoveVariableAtIndex (i);
354 valobj_list.RemoveValueObjectAtIndex (i);
358 valobj_list.SetValueObjectAtIndex (i, valobj_sp);
365 error.SetErrorString ("unknown error");
373 error = Variable::GetValuesForVariableExpressionPath (variable_expr_path + 1,
381 for (uint32_t i=0; i<valobj_list.GetSize(); )
384 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(i)->AddressOf(tmp_error));
385 if (tmp_error.Fail())
387 variable_list.RemoveVariableAtIndex (i);
388 valobj_list.RemoveValueObjectAtIndex (i);
392 valobj_list.SetValueObjectAtIndex (i, valobj_sp);
399 error.SetErrorString ("unknown error");
407 static RegularExpression g_regex ("^([A-Za-z_:][A-Za-z_0-9:]*)(.*)");
408 RegularExpression::Match regex_match(1);
409 if (g_regex.Execute(variable_expr_path, ®ex_match))
411 std::string variable_name;
412 if (regex_match.GetMatchAtIndex(variable_expr_path, 1, variable_name))
414 variable_list.Clear();
415 if (callback (baton, variable_name.c_str(), variable_list))
418 while (i < variable_list.GetSize())
420 VariableSP var_sp (variable_list.GetVariableAtIndex (i));
421 ValueObjectSP valobj_sp;
424 ValueObjectSP variable_valobj_sp(ValueObjectVariable::Create (scope, var_sp));
425 if (variable_valobj_sp)
427 const char *variable_sub_expr_path = variable_expr_path + variable_name.size();
428 if (*variable_sub_expr_path)
430 const char* first_unparsed = nullptr;
431 ValueObject::ExpressionPathScanEndReason reason_to_stop;
432 ValueObject::ExpressionPathEndResultType final_value_type;
433 ValueObject::GetValueForExpressionPathOptions options;
434 ValueObject::ExpressionPathAftermath final_task_on_target;
436 valobj_sp = variable_valobj_sp->GetValueForExpressionPath (variable_sub_expr_path,
441 &final_task_on_target);
444 error.SetErrorStringWithFormat ("invalid expression path '%s' for variable '%s'",
445 variable_sub_expr_path,
446 var_sp->GetName().GetCString());
451 // Just the name of a variable with no extras
452 valobj_sp = variable_valobj_sp;
457 if (!var_sp || !valobj_sp)
459 variable_list.RemoveVariableAtIndex (i);
463 valobj_list.Append(valobj_sp);
468 if (variable_list.GetSize() > 0)
476 error.SetErrorStringWithFormat ("unable to extract a variable name from '%s'", variable_expr_path);
481 error.SetErrorString ("unknown error");
486 Variable::DumpLocationForAddress (Stream *s, const Address &address)
488 // Be sure to resolve the address to section offset prior to
489 // calling this function.
490 if (address.IsSectionOffset())
493 CalculateSymbolContext(&sc);
494 if (sc.module_sp == address.GetModule())
499 ModuleSP module_sp (m_owner_scope->CalculateSymbolContextModule());
501 abi = ABI::FindPlugin (module_sp->GetArchitecture()).get();
504 const addr_t file_addr = address.GetFileAddress();
507 if (sc.function->GetAddressRange().ContainsFileAddress(address))
509 addr_t loclist_base_file_addr = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
510 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
512 return m_location.DumpLocationForAddress (s,
513 eDescriptionLevelBrief,
514 loclist_base_file_addr,
519 return m_location.DumpLocationForAddress (s,
520 eDescriptionLevelBrief,
521 LLDB_INVALID_ADDRESS,
531 PrivateAutoComplete (StackFrame *frame,
532 const std::string &partial_path,
533 const std::string &prefix_path, // Anything that has been resolved already will be in here
534 const ClangASTType& clang_type,
536 bool &word_complete);
539 PrivateAutoCompleteMembers (StackFrame *frame,
540 const std::string &partial_member_name,
541 const std::string &partial_path,
542 const std::string &prefix_path, // Anything that has been resolved already will be in here
543 const ClangASTType& clang_type,
545 bool &word_complete);
548 PrivateAutoCompleteMembers (StackFrame *frame,
549 const std::string &partial_member_name,
550 const std::string &partial_path,
551 const std::string &prefix_path, // Anything that has been resolved already will be in here
552 const ClangASTType& clang_type,
557 // We are in a type parsing child members
558 const uint32_t num_bases = clang_type.GetNumDirectBaseClasses();
562 for (uint32_t i = 0; i < num_bases; ++i)
564 ClangASTType base_class_type (clang_type.GetDirectBaseClassAtIndex (i, nullptr));
566 PrivateAutoCompleteMembers (frame,
570 base_class_type.GetCanonicalType(),
576 const uint32_t num_vbases = clang_type.GetNumVirtualBaseClasses();
580 for (uint32_t i = 0; i < num_vbases; ++i)
582 ClangASTType vbase_class_type (clang_type.GetVirtualBaseClassAtIndex(i,nullptr));
584 PrivateAutoCompleteMembers (frame,
588 vbase_class_type.GetCanonicalType(),
594 // We are in a type parsing child members
595 const uint32_t num_fields = clang_type.GetNumFields();
599 for (uint32_t i = 0; i < num_fields; ++i)
601 std::string member_name;
603 ClangASTType member_clang_type = clang_type.GetFieldAtIndex (i, member_name, nullptr, nullptr, nullptr);
605 if (partial_member_name.empty() ||
606 member_name.find(partial_member_name) == 0)
608 if (member_name == partial_member_name)
610 PrivateAutoComplete (frame,
612 prefix_path + member_name, // Anything that has been resolved already will be in here
613 member_clang_type.GetCanonicalType(),
619 matches.AppendString (prefix_path + member_name);
627 PrivateAutoComplete (StackFrame *frame,
628 const std::string &partial_path,
629 const std::string &prefix_path, // Anything that has been resolved already will be in here
630 const ClangASTType& clang_type,
634 // printf ("\nPrivateAutoComplete()\n\tprefix_path = '%s'\n\tpartial_path = '%s'\n", prefix_path.c_str(), partial_path.c_str());
635 std::string remaining_partial_path;
637 const lldb::TypeClass type_class = clang_type.GetTypeClass();
638 if (partial_path.empty())
640 if (clang_type.IsValid())
645 case eTypeClassArray:
646 case eTypeClassBlockPointer:
647 case eTypeClassBuiltin:
648 case eTypeClassComplexFloat:
649 case eTypeClassComplexInteger:
650 case eTypeClassEnumeration:
651 case eTypeClassFunction:
652 case eTypeClassMemberPointer:
653 case eTypeClassReference:
654 case eTypeClassTypedef:
655 case eTypeClassVector:
657 matches.AppendString (prefix_path);
658 word_complete = matches.GetSize() == 1;
662 case eTypeClassClass:
663 case eTypeClassStruct:
664 case eTypeClassUnion:
665 if (prefix_path.back() != '.')
666 matches.AppendString (prefix_path + '.');
669 case eTypeClassObjCObject:
670 case eTypeClassObjCInterface:
672 case eTypeClassObjCObjectPointer:
673 case eTypeClassPointer:
675 bool omit_empty_base_classes = true;
676 if (clang_type.GetNumChildren (omit_empty_base_classes) > 0)
677 matches.AppendString (prefix_path + "->");
680 matches.AppendString (prefix_path);
681 word_complete = true;
691 const bool get_file_globals = true;
693 VariableList *variable_list = frame->GetVariableList(get_file_globals);
697 const size_t num_variables = variable_list->GetSize();
698 for (size_t i=0; i<num_variables; ++i)
700 Variable *variable = variable_list->GetVariableAtIndex(i).get();
701 matches.AppendString (variable->GetName().AsCString());
709 const char ch = partial_path[0];
713 if (prefix_path.empty())
715 PrivateAutoComplete (frame,
716 partial_path.substr(1),
725 if (prefix_path.empty())
727 PrivateAutoComplete (frame,
728 partial_path.substr(1),
737 if (partial_path[1] == '>' && !prefix_path.empty())
741 case lldb::eTypeClassPointer:
743 ClangASTType pointee_type(clang_type.GetPointeeType());
746 // If there is more after the "->", then search deeper
747 PrivateAutoComplete (frame,
748 partial_path.substr(2),
750 pointee_type.GetCanonicalType(),
756 // Nothing after the "->", so list all members
757 PrivateAutoCompleteMembers (frame,
761 pointee_type.GetCanonicalType(),
773 if (clang_type.IsValid())
777 case lldb::eTypeClassUnion:
778 case lldb::eTypeClassStruct:
779 case lldb::eTypeClassClass:
782 // If there is more after the ".", then search deeper
783 PrivateAutoComplete (frame,
784 partial_path.substr(1),
793 // Nothing after the ".", so list all members
794 PrivateAutoCompleteMembers (frame,
808 if (isalpha(ch) || ch == '_' || ch == '$')
810 const size_t partial_path_len = partial_path.size();
812 while (pos < partial_path_len)
814 const char curr_ch = partial_path[pos];
815 if (isalnum(curr_ch) || curr_ch == '_' || curr_ch == '$')
823 std::string token(partial_path, 0, pos);
824 remaining_partial_path = partial_path.substr(pos);
826 if (clang_type.IsValid())
828 PrivateAutoCompleteMembers (frame,
830 remaining_partial_path,
838 // We haven't found our variable yet
839 const bool get_file_globals = true;
841 VariableList *variable_list = frame->GetVariableList(get_file_globals);
846 const size_t num_variables = variable_list->GetSize();
847 for (size_t i=0; i<num_variables; ++i)
849 Variable *variable = variable_list->GetVariableAtIndex(i).get();
854 const char *variable_name = variable->GetName().AsCString();
855 if (strstr(variable_name, token.c_str()) == variable_name)
857 if (strcmp (variable_name, token.c_str()) == 0)
859 Type *variable_type = variable->GetType();
862 ClangASTType variable_clang_type (variable_type->GetClangForwardType());
863 PrivateAutoComplete (frame,
864 remaining_partial_path,
865 prefix_path + token, // Anything that has been resolved already will be in here
866 variable_clang_type.GetCanonicalType(),
872 matches.AppendString (prefix_path + variable_name);
875 else if (remaining_partial_path.empty())
877 matches.AppendString (prefix_path + variable_name);
891 Variable::AutoComplete (const ExecutionContext &exe_ctx,
892 const char *partial_path_cstr,
896 word_complete = false;
897 std::string partial_path;
898 std::string prefix_path;
899 ClangASTType clang_type;
900 if (partial_path_cstr && partial_path_cstr[0])
901 partial_path = partial_path_cstr;
903 PrivateAutoComplete (exe_ctx.GetFramePtr(),
910 return matches.GetSize();