1 //===-- MICmnLLDBUtilSBValue.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 //===----------------------------------------------------------------------===//
11 // File: MICmnLLDBUtilSBValue.cpp
13 // Overview: CMICmnLLDBUtilSBValue implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
23 #include "MICmnLLDBUtilSBValue.h"
24 #include "MIUtilString.h"
25 #include "MICmnLLDBDebugSessionInfo.h"
27 //++ ------------------------------------------------------------------------------------
28 // Details: CMICmnLLDBUtilSBValue constructor.
30 // Args: vrValue - (R) The LLDb value object.
31 // vbHandleCharType - (R) True = Yes return text molding to char type,
32 // False = just return data.
36 CMICmnLLDBUtilSBValue::CMICmnLLDBUtilSBValue(const lldb::SBValue &vrValue, const bool vbHandleCharType /* = false */)
37 : m_rValue(const_cast<lldb::SBValue &>(vrValue))
39 , m_bHandleCharType(vbHandleCharType)
41 m_bValidSBValue = m_rValue.IsValid();
44 //++ ------------------------------------------------------------------------------------
45 // Details: CMICmnLLDBUtilSBValue destructor.
51 CMICmnLLDBUtilSBValue::~CMICmnLLDBUtilSBValue(void)
55 //++ ------------------------------------------------------------------------------------
56 // Details: Retrieve from the LLDB SB Value object the name of the variable. If the name
57 // is invalid (or the SBValue object invalid) then "??" is returned.
60 // Return: CMIUtilString - Name of the variable or "??" for unknown.
64 CMICmnLLDBUtilSBValue::GetName(void) const
66 const MIchar *pName = m_bValidSBValue ? m_rValue.GetName() : nullptr;
67 const CMIUtilString text((pName != nullptr) ? pName : m_pUnkwn);
72 //++ ------------------------------------------------------------------------------------
73 // Details: Retrieve from the LLDB SB Value object the value of the variable described in
74 // text. If the value is invalid (or the SBValue object invalid) then "??" is
78 // Return: CMIUtilString - Text description of the variable's value or "??".
82 CMICmnLLDBUtilSBValue::GetValue(void) const
86 if (m_bHandleCharType && IsCharType())
88 const lldb::addr_t addr = m_rValue.GetLoadAddress();
89 text = CMIUtilString::Format("0x%08x", addr);
90 const CMIUtilString cString(GetValueCString());
92 text += CMIUtilString::Format(" %s", cString.c_str());
96 const MIchar *pValue = m_bValidSBValue ? m_rValue.GetValue() : nullptr;
97 text = (pValue != nullptr) ? pValue : m_pUnkwn;
103 //++ ------------------------------------------------------------------------------------
104 // Details: If the LLDB SB Value object is a char type then form the text data string
105 // otherwise return nothing. m_bHandleCharType must be true to return text data
109 // Return: CMIUtilString - Text description of the variable's value.
113 CMICmnLLDBUtilSBValue::GetValueCString(void) const
117 if (m_bHandleCharType && IsCharType())
119 text = ReadCStringFromHostMemory(m_rValue);
125 //++ ------------------------------------------------------------------------------------
126 // Details: Retrieve the flag stating whether this value object is a char type or some
127 // other type. Char type can be signed or unsigned.
130 // Return: bool - True = Yes is a char type, false = some other type.
134 CMICmnLLDBUtilSBValue::IsCharType(void) const
136 const MIchar *pName = m_rValue.GetName();
138 const lldb::BasicType eType = m_rValue.GetType().GetBasicType();
139 return ((eType == lldb::eBasicTypeChar) || (eType == lldb::eBasicTypeSignedChar) || (eType == lldb::eBasicTypeUnsignedChar));
142 //++ ------------------------------------------------------------------------------------
143 // Details: Retrieve the flag stating whether any child value object of *this object is a
144 // char type or some other type. Returns false if there are not children. Char
145 // type can be signed or unsigned.
148 // Return: bool - True = Yes is a char type, false = some other type.
152 CMICmnLLDBUtilSBValue::IsChildCharType(void) const
154 const MIuint nChildren = m_rValue.GetNumChildren();
156 // Is it a basic type
160 // Is it a composite type
164 lldb::SBValue member = m_rValue.GetChildAtIndex(0);
165 const CMICmnLLDBUtilSBValue utilValue(member);
166 return utilValue.IsCharType();
169 //++ ------------------------------------------------------------------------------------
170 // Details: Retrieve the C string data for a child of char type (one and only child) for
171 // the parent value object. If the child is not a char type or the parent has
172 // more than one child then an empty string is returned. Char type can be
173 // signed or unsigned.
176 // Return: CMIUtilString - Text description of the variable's value.
180 CMICmnLLDBUtilSBValue::GetChildValueCString(void) const
183 const MIuint nChildren = m_rValue.GetNumChildren();
185 // Is it a basic type
189 // Is it a composite type
193 lldb::SBValue member = m_rValue.GetChildAtIndex(0);
194 const CMICmnLLDBUtilSBValue utilValue(member);
195 if (m_bHandleCharType && utilValue.IsCharType())
197 text = ReadCStringFromHostMemory(member);
203 //++ ------------------------------------------------------------------------------------
204 // Details: Retrieve the C string data of value object by read the memory where the
207 // Args: vrValueObj - (R) LLDB SBValue variable object.
208 // Return: CMIUtilString - Text description of the variable's value.
212 CMICmnLLDBUtilSBValue::ReadCStringFromHostMemory(const lldb::SBValue &vrValueObj) const
216 lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValueObj);
217 const lldb::addr_t addr = rValue.GetLoadAddress();
218 CMICmnLLDBDebugSessionInfo &rSessionInfo(CMICmnLLDBDebugSessionInfo::Instance());
219 const MIuint nBytes(128);
220 const MIchar *pBufferMemory = new MIchar[nBytes];
222 const MIuint64 nReadBytes = rSessionInfo.m_lldbProcess.ReadMemory(addr, (void *)pBufferMemory, nBytes, error);
223 MIunused(nReadBytes);
224 text = CMIUtilString::Format("\\\"%s\\\"", pBufferMemory);
225 delete[] pBufferMemory;
230 //++ ------------------------------------------------------------------------------------
231 // Details: Retrieve the state of the value object's name.
234 // Return: bool - True = yes name is indeterminate, false = name is valid.
238 CMICmnLLDBUtilSBValue::IsNameUnknown(void) const
240 const CMIUtilString name(GetName());
241 return (name == m_pUnkwn);
244 //++ ------------------------------------------------------------------------------------
245 // Details: Retrieve the state of the value object's value data.
248 // Return: bool - True = yes value is indeterminate, false = value valid.
252 CMICmnLLDBUtilSBValue::IsValueUnknown(void) const
254 const CMIUtilString value(GetValue());
255 return (value == m_pUnkwn);
258 //++ ------------------------------------------------------------------------------------
259 // Details: Retrieve the value object's type name if valid.
262 // Return: CMIUtilString - The type name or "??".
266 CMICmnLLDBUtilSBValue::GetTypeName(void) const
268 const MIchar *pName = m_bValidSBValue ? m_rValue.GetTypeName() : nullptr;
269 const CMIUtilString text((pName != nullptr) ? pName : m_pUnkwn);
274 //++ ------------------------------------------------------------------------------------
275 // Details: Retrieve the value object's display type name if valid.
278 // Return: CMIUtilString - The type name or "??".
282 CMICmnLLDBUtilSBValue::GetTypeNameDisplay(void) const
284 const MIchar *pName = m_bValidSBValue ? m_rValue.GetDisplayTypeName() : nullptr;
285 const CMIUtilString text((pName != nullptr) ? pName : m_pUnkwn);
290 //++ ------------------------------------------------------------------------------------
291 // Details: Retrieve whether the value object's is valid or not.
294 // Return: bool - True = valid, false = not valid.
298 CMICmnLLDBUtilSBValue::IsValid(void) const
300 return m_bValidSBValue;
303 //++ ------------------------------------------------------------------------------------
304 // Details: Retrieve the value object' has a name. A value object can be valid but still
305 // have no name which suggest it is not a variable.
308 // Return: bool - True = valid, false = not valid.
312 CMICmnLLDBUtilSBValue::HasName(void) const
314 bool bHasAName = false;
316 const MIchar *pName = m_bValidSBValue ? m_rValue.GetDisplayTypeName() : nullptr;
317 if (pName != nullptr)
319 bHasAName = (CMIUtilString(pName).length() > 0);
325 //++ ------------------------------------------------------------------------------------
326 // Details: Determine if the value object' respresents a LLDB variable i.e. "$0".
329 // Return: bool - True = Yes LLDB variable, false = no.
333 CMICmnLLDBUtilSBValue::IsLLDBVariable(void) const
335 return (GetName().at(0) == '$');