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: ", 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 != NULL)
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;
178 dumped_declaration_info = sc.DumpStopContext (s,
183 show_inlined_frames);
188 if (m_declaration.DumpStopContext (s, false))
189 dumped_declaration_info = true;
190 return dumped_declaration_info;
194 Variable::MemorySize() const
196 return sizeof(Variable);
201 Variable::CalculateSymbolContext (SymbolContext *sc)
204 m_owner_scope->CalculateSymbolContext(sc);
210 Variable::LocationIsValidForFrame (StackFrame *frame)
212 // Is the variable is described by a single location?
213 if (!m_location.IsLocationList())
215 // Yes it is, the location is valid.
221 Function *function = frame->GetSymbolContext(eSymbolContextFunction).function;
224 TargetSP target_sp (frame->CalculateTarget());
226 addr_t loclist_base_load_addr = function->GetAddressRange().GetBaseAddress().GetLoadAddress (target_sp.get());
227 if (loclist_base_load_addr == LLDB_INVALID_ADDRESS)
229 // It is a location list. We just need to tell if the location
230 // list contains the current address when converted to a load
232 return m_location.LocationListContainsAddress (loclist_base_load_addr,
233 frame->GetFrameCodeAddress().GetLoadAddress (target_sp.get()));
240 Variable::LocationIsValidForAddress (const Address &address)
242 // Be sure to resolve the address to section offset prior to
243 // calling this function.
244 if (address.IsSectionOffset())
247 CalculateSymbolContext(&sc);
248 if (sc.module_sp == address.GetModule())
250 // Is the variable is described by a single location?
251 if (!m_location.IsLocationList())
253 // Yes it is, the location is valid.
259 addr_t loclist_base_file_addr = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
260 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
262 // It is a location list. We just need to tell if the location
263 // list contains the current address when converted to a load
265 return m_location.LocationListContainsAddress (loclist_base_file_addr,
266 address.GetFileAddress());
274 Variable::IsInScope (StackFrame *frame)
278 case eValueTypeRegister:
279 case eValueTypeRegisterSet:
280 return frame != NULL;
282 case eValueTypeConstResult:
283 case eValueTypeVariableGlobal:
284 case eValueTypeVariableStatic:
287 case eValueTypeVariableArgument:
288 case eValueTypeVariableLocal:
291 // We don't have a location list, we just need to see if the block
292 // that this variable was defined in is currently
293 Block *deepest_frame_block = frame->GetSymbolContext(eSymbolContextBlock).block;
294 if (deepest_frame_block)
296 SymbolContext variable_sc;
297 CalculateSymbolContext (&variable_sc);
298 // Check for static or global variable defined at the compile unit
299 // level that wasn't defined in a block
300 if (variable_sc.block == NULL)
303 if (variable_sc.block == deepest_frame_block)
305 return variable_sc.block->Contains (deepest_frame_block);
317 Variable::GetValuesForVariableExpressionPath (const char *variable_expr_path,
318 ExecutionContextScope *scope,
319 GetVariableCallback callback,
321 VariableList &variable_list,
322 ValueObjectList &valobj_list)
325 if (variable_expr_path && callback)
327 switch (variable_expr_path[0])
331 error = Variable::GetValuesForVariableExpressionPath (variable_expr_path + 1,
339 for (uint32_t i=0; i<valobj_list.GetSize(); )
342 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(i)->Dereference(tmp_error));
343 if (tmp_error.Fail())
345 variable_list.RemoveVariableAtIndex (i);
346 valobj_list.RemoveValueObjectAtIndex (i);
350 valobj_list.SetValueObjectAtIndex (i, valobj_sp);
357 error.SetErrorString ("unknown error");
365 error = Variable::GetValuesForVariableExpressionPath (variable_expr_path + 1,
373 for (uint32_t i=0; i<valobj_list.GetSize(); )
376 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(i)->AddressOf(tmp_error));
377 if (tmp_error.Fail())
379 variable_list.RemoveVariableAtIndex (i);
380 valobj_list.RemoveValueObjectAtIndex (i);
384 valobj_list.SetValueObjectAtIndex (i, valobj_sp);
391 error.SetErrorString ("unknown error");
399 static RegularExpression g_regex ("^([A-Za-z_:][A-Za-z_0-9:]*)(.*)");
400 RegularExpression::Match regex_match(1);
401 if (g_regex.Execute(variable_expr_path, ®ex_match))
403 std::string variable_name;
404 if (regex_match.GetMatchAtIndex(variable_expr_path, 1, variable_name))
406 variable_list.Clear();
407 if (callback (baton, variable_name.c_str(), variable_list))
410 while (i < variable_list.GetSize())
412 VariableSP var_sp (variable_list.GetVariableAtIndex (i));
413 ValueObjectSP valobj_sp;
416 ValueObjectSP variable_valobj_sp(ValueObjectVariable::Create (scope, var_sp));
417 if (variable_valobj_sp)
419 const char *variable_sub_expr_path = variable_expr_path + variable_name.size();
420 if (*variable_sub_expr_path)
422 const char* first_unparsed = NULL;
423 ValueObject::ExpressionPathScanEndReason reason_to_stop;
424 ValueObject::ExpressionPathEndResultType final_value_type;
425 ValueObject::GetValueForExpressionPathOptions options;
426 ValueObject::ExpressionPathAftermath final_task_on_target;
428 valobj_sp = variable_valobj_sp->GetValueForExpressionPath (variable_sub_expr_path,
433 &final_task_on_target);
436 error.SetErrorStringWithFormat ("invalid expression path '%s' for variable '%s'",
437 variable_sub_expr_path,
438 var_sp->GetName().GetCString());
443 // Just the name of a variable with no extras
444 valobj_sp = variable_valobj_sp;
449 if (!var_sp || !valobj_sp)
451 variable_list.RemoveVariableAtIndex (i);
455 valobj_list.Append(valobj_sp);
460 if (variable_list.GetSize() > 0)
468 error.SetErrorStringWithFormat ("unable to extract a variable name from '%s'", variable_expr_path);
473 error.SetErrorString ("unknown error");
478 Variable::DumpLocationForAddress (Stream *s, const Address &address)
480 // Be sure to resolve the address to section offset prior to
481 // calling this function.
482 if (address.IsSectionOffset())
485 CalculateSymbolContext(&sc);
486 if (sc.module_sp == address.GetModule())
491 ModuleSP module_sp (m_owner_scope->CalculateSymbolContextModule());
493 abi = ABI::FindPlugin (module_sp->GetArchitecture()).get();
496 const addr_t file_addr = address.GetFileAddress();
499 if (sc.function->GetAddressRange().ContainsFileAddress(address))
501 addr_t loclist_base_file_addr = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
502 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
504 return m_location.DumpLocationForAddress (s,
505 eDescriptionLevelBrief,
506 loclist_base_file_addr,
511 return m_location.DumpLocationForAddress (s,
512 eDescriptionLevelBrief,
513 LLDB_INVALID_ADDRESS,
523 PrivateAutoComplete (StackFrame *frame,
524 const std::string &partial_path,
525 const std::string &prefix_path, // Anything that has been resolved already will be in here
526 const ClangASTType& clang_type,
528 bool &word_complete);
531 PrivateAutoCompleteMembers (StackFrame *frame,
532 const std::string &partial_member_name,
533 const std::string &partial_path,
534 const std::string &prefix_path, // Anything that has been resolved already will be in here
535 const ClangASTType& clang_type,
537 bool &word_complete);
540 PrivateAutoCompleteMembers (StackFrame *frame,
541 const std::string &partial_member_name,
542 const std::string &partial_path,
543 const std::string &prefix_path, // Anything that has been resolved already will be in here
544 const ClangASTType& clang_type,
549 // We are in a type parsing child members
550 const uint32_t num_bases = clang_type.GetNumDirectBaseClasses();
554 for (uint32_t i = 0; i < num_bases; ++i)
556 ClangASTType base_class_type (clang_type.GetDirectBaseClassAtIndex (i, NULL));
558 PrivateAutoCompleteMembers (frame,
562 base_class_type.GetCanonicalType(),
568 const uint32_t num_vbases = clang_type.GetNumVirtualBaseClasses();
572 for (uint32_t i = 0; i < num_vbases; ++i)
574 ClangASTType vbase_class_type (clang_type.GetVirtualBaseClassAtIndex(i,NULL));
576 PrivateAutoCompleteMembers (frame,
580 vbase_class_type.GetCanonicalType(),
586 // We are in a type parsing child members
587 const uint32_t num_fields = clang_type.GetNumFields();
591 for (uint32_t i = 0; i < num_fields; ++i)
593 std::string member_name;
595 ClangASTType member_clang_type = clang_type.GetFieldAtIndex (i, member_name, NULL, NULL, NULL);
597 if (partial_member_name.empty() ||
598 member_name.find(partial_member_name) == 0)
600 if (member_name == partial_member_name)
602 PrivateAutoComplete (frame,
604 prefix_path + member_name, // Anything that has been resolved already will be in here
605 member_clang_type.GetCanonicalType(),
611 matches.AppendString (prefix_path + member_name);
619 PrivateAutoComplete (StackFrame *frame,
620 const std::string &partial_path,
621 const std::string &prefix_path, // Anything that has been resolved already will be in here
622 const ClangASTType& clang_type,
626 // printf ("\nPrivateAutoComplete()\n\tprefix_path = '%s'\n\tpartial_path = '%s'\n", prefix_path.c_str(), partial_path.c_str());
627 std::string remaining_partial_path;
629 const lldb::TypeClass type_class = clang_type.GetTypeClass();
630 if (partial_path.empty())
632 if (clang_type.IsValid())
637 case eTypeClassArray:
638 case eTypeClassBlockPointer:
639 case eTypeClassBuiltin:
640 case eTypeClassComplexFloat:
641 case eTypeClassComplexInteger:
642 case eTypeClassEnumeration:
643 case eTypeClassFunction:
644 case eTypeClassMemberPointer:
645 case eTypeClassReference:
646 case eTypeClassTypedef:
647 case eTypeClassVector:
649 matches.AppendString (prefix_path);
650 word_complete = matches.GetSize() == 1;
654 case eTypeClassClass:
655 case eTypeClassStruct:
656 case eTypeClassUnion:
657 if (prefix_path.back() != '.')
658 matches.AppendString (prefix_path + '.');
661 case eTypeClassObjCObject:
662 case eTypeClassObjCInterface:
664 case eTypeClassObjCObjectPointer:
665 case eTypeClassPointer:
667 bool omit_empty_base_classes = true;
668 if (clang_type.GetNumChildren (omit_empty_base_classes) > 0)
669 matches.AppendString (prefix_path + "->");
672 matches.AppendString (prefix_path);
673 word_complete = true;
683 const bool get_file_globals = true;
685 VariableList *variable_list = frame->GetVariableList(get_file_globals);
687 const size_t num_variables = variable_list->GetSize();
688 for (size_t i=0; i<num_variables; ++i)
690 Variable *variable = variable_list->GetVariableAtIndex(i).get();
691 matches.AppendString (variable->GetName().AsCString());
698 const char ch = partial_path[0];
702 if (prefix_path.empty())
704 PrivateAutoComplete (frame,
705 partial_path.substr(1),
714 if (prefix_path.empty())
716 PrivateAutoComplete (frame,
717 partial_path.substr(1),
726 if (partial_path[1] == '>' && !prefix_path.empty())
730 case lldb::eTypeClassPointer:
732 ClangASTType pointee_type(clang_type.GetPointeeType());
735 // If there is more after the "->", then search deeper
736 PrivateAutoComplete (frame,
737 partial_path.substr(2),
739 pointee_type.GetCanonicalType(),
745 // Nothing after the "->", so list all members
746 PrivateAutoCompleteMembers (frame,
750 pointee_type.GetCanonicalType(),
762 if (clang_type.IsValid())
766 case lldb::eTypeClassUnion:
767 case lldb::eTypeClassStruct:
768 case lldb::eTypeClassClass:
771 // If there is more after the ".", then search deeper
772 PrivateAutoComplete (frame,
773 partial_path.substr(1),
782 // Nothing after the ".", so list all members
783 PrivateAutoCompleteMembers (frame,
797 if (isalpha(ch) || ch == '_' || ch == '$')
799 const size_t partial_path_len = partial_path.size();
801 while (pos < partial_path_len)
803 const char curr_ch = partial_path[pos];
804 if (isalnum(curr_ch) || curr_ch == '_' || curr_ch == '$')
812 std::string token(partial_path, 0, pos);
813 remaining_partial_path = partial_path.substr(pos);
815 if (clang_type.IsValid())
817 PrivateAutoCompleteMembers (frame,
819 remaining_partial_path,
827 // We haven't found our variable yet
828 const bool get_file_globals = true;
830 VariableList *variable_list = frame->GetVariableList(get_file_globals);
832 const size_t num_variables = variable_list->GetSize();
833 for (size_t i=0; i<num_variables; ++i)
835 Variable *variable = variable_list->GetVariableAtIndex(i).get();
836 const char *variable_name = variable->GetName().AsCString();
837 if (strstr(variable_name, token.c_str()) == variable_name)
839 if (strcmp (variable_name, token.c_str()) == 0)
841 Type *variable_type = variable->GetType();
844 ClangASTType variable_clang_type (variable_type->GetClangForwardType());
845 PrivateAutoComplete (frame,
846 remaining_partial_path,
847 prefix_path + token, // Anything that has been resolved already will be in here
848 variable_clang_type.GetCanonicalType(),
854 matches.AppendString (prefix_path + variable_name);
857 else if (remaining_partial_path.empty())
859 matches.AppendString (prefix_path + variable_name);
873 Variable::AutoComplete (const ExecutionContext &exe_ctx,
874 const char *partial_path_cstr,
878 word_complete = false;
879 std::string partial_path;
880 std::string prefix_path;
881 ClangASTType clang_type;
882 if (partial_path_cstr && partial_path_cstr[0])
883 partial_path = partial_path_cstr;
885 PrivateAutoComplete (exe_ctx.GetFramePtr(),
892 return matches.GetSize();