PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/source/Core/ValueObjectVariable.cpp

https://gitlab.com/jorjpimm/lldb
C++ | 413 lines | 328 code | 47 blank | 38 comment | 56 complexity | a2dea6e48c128bd2171cc50d411c49ac MD5 | raw file
  1. //===-- ValueObjectVariable.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. #include "lldb/Core/ValueObjectVariable.h"
  10. // C Includes
  11. // C++ Includes
  12. // Other libraries and framework includes
  13. // Project includes
  14. #include "lldb/Core/Module.h"
  15. #include "lldb/Core/RegisterValue.h"
  16. #include "lldb/Core/ValueObjectList.h"
  17. #include "lldb/Core/Value.h"
  18. #include "lldb/Symbol/Function.h"
  19. #include "lldb/Symbol/ObjectFile.h"
  20. #include "lldb/Symbol/SymbolContext.h"
  21. #include "lldb/Symbol/SymbolContextScope.h"
  22. #include "lldb/Symbol/Type.h"
  23. #include "lldb/Symbol/Variable.h"
  24. #include "lldb/Target/ExecutionContext.h"
  25. #include "lldb/Target/Process.h"
  26. #include "lldb/Target/RegisterContext.h"
  27. #include "lldb/Target/Target.h"
  28. #include "lldb/Target/Thread.h"
  29. using namespace lldb_private;
  30. lldb::ValueObjectSP
  31. ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
  32. {
  33. return (new ValueObjectVariable (exe_scope, var_sp))->GetSP();
  34. }
  35. ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) :
  36. ValueObject(exe_scope),
  37. m_variable_sp(var_sp)
  38. {
  39. // Do not attempt to construct one of these objects with no variable!
  40. assert (m_variable_sp.get() != NULL);
  41. m_name = var_sp->GetName();
  42. }
  43. ValueObjectVariable::~ValueObjectVariable()
  44. {
  45. }
  46. ClangASTType
  47. ValueObjectVariable::GetClangTypeImpl ()
  48. {
  49. Type *var_type = m_variable_sp->GetType();
  50. if (var_type)
  51. return var_type->GetClangForwardType();
  52. return ClangASTType();
  53. }
  54. ConstString
  55. ValueObjectVariable::GetTypeName()
  56. {
  57. Type * var_type = m_variable_sp->GetType();
  58. if (var_type)
  59. return var_type->GetName();
  60. return ConstString();
  61. }
  62. ConstString
  63. ValueObjectVariable::GetDisplayTypeName()
  64. {
  65. Type * var_type = m_variable_sp->GetType();
  66. if (var_type)
  67. return var_type->GetClangForwardType().GetDisplayTypeName();
  68. return ConstString();
  69. }
  70. ConstString
  71. ValueObjectVariable::GetQualifiedTypeName()
  72. {
  73. Type * var_type = m_variable_sp->GetType();
  74. if (var_type)
  75. return var_type->GetQualifiedName();
  76. return ConstString();
  77. }
  78. size_t
  79. ValueObjectVariable::CalculateNumChildren()
  80. {
  81. ClangASTType type(GetClangType());
  82. if (!type.IsValid())
  83. return 0;
  84. const bool omit_empty_base_classes = true;
  85. return type.GetNumChildren(omit_empty_base_classes);
  86. }
  87. uint64_t
  88. ValueObjectVariable::GetByteSize()
  89. {
  90. ClangASTType type(GetClangType());
  91. if (!type.IsValid())
  92. return 0;
  93. return type.GetByteSize();
  94. }
  95. lldb::ValueType
  96. ValueObjectVariable::GetValueType() const
  97. {
  98. if (m_variable_sp)
  99. return m_variable_sp->GetScope();
  100. return lldb::eValueTypeInvalid;
  101. }
  102. bool
  103. ValueObjectVariable::UpdateValue ()
  104. {
  105. SetValueIsValid (false);
  106. m_error.Clear();
  107. Variable *variable = m_variable_sp.get();
  108. DWARFExpression &expr = variable->LocationExpression();
  109. if (variable->GetLocationIsConstantValueData())
  110. {
  111. // expr doesn't contain DWARF bytes, it contains the constant variable
  112. // value bytes themselves...
  113. if (expr.GetExpressionData(m_data))
  114. m_value.SetContext(Value::eContextTypeVariable, variable);
  115. else
  116. m_error.SetErrorString ("empty constant data");
  117. // constant bytes can't be edited - sorry
  118. m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
  119. }
  120. else
  121. {
  122. lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
  123. ExecutionContext exe_ctx (GetExecutionContextRef());
  124. Target *target = exe_ctx.GetTargetPtr();
  125. if (target)
  126. {
  127. m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
  128. m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
  129. }
  130. if (expr.IsLocationList())
  131. {
  132. SymbolContext sc;
  133. variable->CalculateSymbolContext (&sc);
  134. if (sc.function)
  135. loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
  136. }
  137. Value old_value(m_value);
  138. if (expr.Evaluate (&exe_ctx, NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
  139. {
  140. m_resolved_value = m_value;
  141. m_value.SetContext(Value::eContextTypeVariable, variable);
  142. ClangASTType clang_type = GetClangType();
  143. if (clang_type.IsValid())
  144. m_value.SetClangType(clang_type);
  145. Value::ValueType value_type = m_value.GetValueType();
  146. switch (value_type)
  147. {
  148. case Value::eValueTypeFileAddress:
  149. SetAddressTypeOfChildren(eAddressTypeFile);
  150. break;
  151. case Value::eValueTypeHostAddress:
  152. SetAddressTypeOfChildren(eAddressTypeHost);
  153. break;
  154. case Value::eValueTypeLoadAddress:
  155. case Value::eValueTypeScalar:
  156. case Value::eValueTypeVector:
  157. SetAddressTypeOfChildren(eAddressTypeLoad);
  158. break;
  159. }
  160. switch (value_type)
  161. {
  162. case Value::eValueTypeVector:
  163. // fall through
  164. case Value::eValueTypeScalar:
  165. // The variable value is in the Scalar value inside the m_value.
  166. // We can point our m_data right to it.
  167. m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
  168. break;
  169. case Value::eValueTypeFileAddress:
  170. case Value::eValueTypeLoadAddress:
  171. case Value::eValueTypeHostAddress:
  172. // The DWARF expression result was an address in the inferior
  173. // process. If this variable is an aggregate type, we just need
  174. // the address as the main value as all child variable objects
  175. // will rely upon this location and add an offset and then read
  176. // their own values as needed. If this variable is a simple
  177. // type, we read all data for it into m_data.
  178. // Make sure this type has a value before we try and read it
  179. // If we have a file address, convert it to a load address if we can.
  180. Process *process = exe_ctx.GetProcessPtr();
  181. if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive())
  182. {
  183. lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
  184. if (file_addr != LLDB_INVALID_ADDRESS)
  185. {
  186. SymbolContext var_sc;
  187. variable->CalculateSymbolContext(&var_sc);
  188. if (var_sc.module_sp)
  189. {
  190. ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
  191. if (objfile)
  192. {
  193. Address so_addr(file_addr, objfile->GetSectionList());
  194. lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
  195. if (load_addr != LLDB_INVALID_ADDRESS)
  196. {
  197. m_value.SetValueType(Value::eValueTypeLoadAddress);
  198. m_value.GetScalar() = load_addr;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. if (!CanProvideValue())
  205. {
  206. // this value object represents an aggregate type whose
  207. // children have values, but this object does not. So we
  208. // say we are changed if our location has changed.
  209. SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
  210. }
  211. else
  212. {
  213. // Copy the Value and set the context to use our Variable
  214. // so it can extract read its value into m_data appropriately
  215. Value value(m_value);
  216. value.SetContext(Value::eContextTypeVariable, variable);
  217. m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
  218. SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
  219. }
  220. break;
  221. }
  222. SetValueIsValid (m_error.Success());
  223. }
  224. else
  225. {
  226. // could not find location, won't allow editing
  227. m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
  228. }
  229. }
  230. return m_error.Success();
  231. }
  232. bool
  233. ValueObjectVariable::IsInScope ()
  234. {
  235. const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
  236. if (exe_ctx_ref.HasFrameRef())
  237. {
  238. ExecutionContext exe_ctx (exe_ctx_ref);
  239. StackFrame *frame = exe_ctx.GetFramePtr();
  240. if (frame)
  241. {
  242. return m_variable_sp->IsInScope (frame);
  243. }
  244. else
  245. {
  246. // This ValueObject had a frame at one time, but now we
  247. // can't locate it, so return false since we probably aren't
  248. // in scope.
  249. return false;
  250. }
  251. }
  252. // We have a variable that wasn't tied to a frame, which
  253. // means it is a global and is always in scope.
  254. return true;
  255. }
  256. lldb::ModuleSP
  257. ValueObjectVariable::GetModule()
  258. {
  259. if (m_variable_sp)
  260. {
  261. SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
  262. if (sc_scope)
  263. {
  264. return sc_scope->CalculateSymbolContextModule();
  265. }
  266. }
  267. return lldb::ModuleSP();
  268. }
  269. SymbolContextScope *
  270. ValueObjectVariable::GetSymbolContextScope()
  271. {
  272. if (m_variable_sp)
  273. return m_variable_sp->GetSymbolContextScope();
  274. return NULL;
  275. }
  276. bool
  277. ValueObjectVariable::GetDeclaration (Declaration &decl)
  278. {
  279. if (m_variable_sp)
  280. {
  281. decl = m_variable_sp->GetDeclaration();
  282. return true;
  283. }
  284. return false;
  285. }
  286. const char *
  287. ValueObjectVariable::GetLocationAsCString ()
  288. {
  289. if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
  290. return GetLocationAsCStringImpl(m_resolved_value,
  291. m_data);
  292. else
  293. return ValueObject::GetLocationAsCString();
  294. }
  295. bool
  296. ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
  297. {
  298. if (!UpdateValueIfNeeded())
  299. {
  300. error.SetErrorString("unable to update value before writing");
  301. return false;
  302. }
  303. if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
  304. {
  305. RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
  306. ExecutionContext exe_ctx(GetExecutionContextRef());
  307. RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
  308. RegisterValue reg_value;
  309. if (!reg_info || !reg_ctx)
  310. {
  311. error.SetErrorString("unable to retrieve register info");
  312. return false;
  313. }
  314. error = reg_value.SetValueFromCString(reg_info, value_str);
  315. if (error.Fail())
  316. return false;
  317. if (reg_ctx->WriteRegister (reg_info, reg_value))
  318. {
  319. SetNeedsUpdate();
  320. return true;
  321. }
  322. else
  323. {
  324. error.SetErrorString("unable to write back to register");
  325. return false;
  326. }
  327. }
  328. else
  329. return ValueObject::SetValueFromCString(value_str, error);
  330. }
  331. bool
  332. ValueObjectVariable::SetData (DataExtractor &data, Error &error)
  333. {
  334. if (!UpdateValueIfNeeded())
  335. {
  336. error.SetErrorString("unable to update value before writing");
  337. return false;
  338. }
  339. if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
  340. {
  341. RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
  342. ExecutionContext exe_ctx(GetExecutionContextRef());
  343. RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
  344. RegisterValue reg_value;
  345. if (!reg_info || !reg_ctx)
  346. {
  347. error.SetErrorString("unable to retrieve register info");
  348. return false;
  349. }
  350. error = reg_value.SetValueFromData(reg_info, data, 0, true);
  351. if (error.Fail())
  352. return false;
  353. if (reg_ctx->WriteRegister (reg_info, reg_value))
  354. {
  355. SetNeedsUpdate();
  356. return true;
  357. }
  358. else
  359. {
  360. error.SetErrorString("unable to write back to register");
  361. return false;
  362. }
  363. }
  364. else
  365. return ValueObject::SetData(data, error);
  366. }