1 //===-- MICmnLLDBDebugSessionInfoVarObj.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: MICmnLLDBDebugSessionInfoVarObj.cpp
13 // Overview: CMICmnLLDBDebugSessionInfoVarObj implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
23 #include "MICmnLLDBDebugSessionInfoVarObj.h"
24 #include "MICmnLLDBProxySBValue.h"
25 #include "MICmnLLDBUtilSBValue.h"
28 const MIchar *CMICmnLLDBDebugSessionInfoVarObj::ms_aVarFormatStrings[] = {
29 // CODETAG_SESSIONINFO_VARFORMAT_ENUM
30 // *** Order is import here.
31 "<Invalid var format>", "binary", "octal", "decimal", "hexadecimal", "natural"};
32 const MIchar *CMICmnLLDBDebugSessionInfoVarObj::ms_aVarFormatChars[] = {
33 // CODETAG_SESSIONINFO_VARFORMAT_ENUM
34 // *** Order is import here.
35 "<Invalid var format>", "t", "o", "d", "x", "N"};
36 CMICmnLLDBDebugSessionInfoVarObj::MapKeyToVarObj_t CMICmnLLDBDebugSessionInfoVarObj::ms_mapVarIdToVarObj;
37 MIuint CMICmnLLDBDebugSessionInfoVarObj::ms_nVarUniqueId = 0; // Index from 0
39 //++ ------------------------------------------------------------------------------------
40 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
46 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(void)
47 : m_eVarFormat(eVarFormat_Natural)
48 , m_eVarType(eVarType_Internal)
50 // Do not call UpdateValue() in here as not necessary
53 //++ ------------------------------------------------------------------------------------
54 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
56 // Args: vrStrNameReal - (R) The actual name of the variable, the expression.
57 // vrStrName - (R) The name given for *this var object.
58 // vrValue - (R) The LLDB SBValue object represented by *this object.
62 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(const CMIUtilString &vrStrNameReal, const CMIUtilString &vrStrName,
63 const lldb::SBValue &vrValue)
64 : m_eVarFormat(eVarFormat_Natural)
65 , m_eVarType(eVarType_Internal)
66 , m_strName(vrStrName)
68 , m_strNameReal(vrStrNameReal)
73 //++ ------------------------------------------------------------------------------------
74 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
76 // Args: vrStrNameReal - (R) The actual name of the variable, the expression.
77 // vrStrName - (R) The name given for *this var object.
78 // vrValue - (R) The LLDB SBValue object represented by *this object.
79 // vrStrVarObjParentName - (R) The var object parent to *this var object (LLDB SBValue equivalent).
83 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(const CMIUtilString &vrStrNameReal, const CMIUtilString &vrStrName,
84 const lldb::SBValue &vrValue, const CMIUtilString &vrStrVarObjParentName)
85 : m_eVarFormat(eVarFormat_Natural)
86 , m_eVarType(eVarType_Internal)
87 , m_strName(vrStrName)
89 , m_strNameReal(vrStrNameReal)
90 , m_strVarObjParentName(vrStrVarObjParentName)
95 //++ ------------------------------------------------------------------------------------
96 // Details: CMICmnLLDBDebugSessionInfoVarObj copy constructor.
98 // Args: vrOther - (R) The object to copy from.
102 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(const CMICmnLLDBDebugSessionInfoVarObj &vrOther)
107 //++ ------------------------------------------------------------------------------------
108 // Details: CMICmnLLDBDebugSessionInfoVarObj copy constructor.
110 // Args: vrOther - (R) The object to copy from.
114 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(CMICmnLLDBDebugSessionInfoVarObj &vrOther)
119 //++ ------------------------------------------------------------------------------------
120 // Details: CMICmnLLDBDebugSessionInfoVarObj move constructor.
122 // Args: vrwOther - (R) The object to copy from.
126 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(CMICmnLLDBDebugSessionInfoVarObj &&vrwOther)
131 //++ ------------------------------------------------------------------------------------
132 // Details: CMICmnLLDBDebugSessionInfoVarObj assignment operator.
134 // Args: vrOther - (R) The object to copy from.
135 // Return: CMICmnLLDBDebugSessionInfoVarObj & - Updated *this object.
138 CMICmnLLDBDebugSessionInfoVarObj &CMICmnLLDBDebugSessionInfoVarObj::operator=(const CMICmnLLDBDebugSessionInfoVarObj &vrOther)
145 //++ ------------------------------------------------------------------------------------
146 // Details: CMICmnLLDBDebugSessionInfoVarObj assignment operator.
148 // Args: vrwOther - (R) The object to copy from.
149 // Return: CMICmnLLDBDebugSessionInfoVarObj & - Updated *this object.
152 CMICmnLLDBDebugSessionInfoVarObj &CMICmnLLDBDebugSessionInfoVarObj::operator=(CMICmnLLDBDebugSessionInfoVarObj &&vrwOther)
159 //++ ------------------------------------------------------------------------------------
160 // Details: Copy the other instance of that object to *this object.
162 // Args: vrOther - (R) The object to copy from.
163 // Return: MIstatus::success - Functional succeeded.
164 // MIstatus::failure - Functional failed.
168 CMICmnLLDBDebugSessionInfoVarObj::CopyOther(const CMICmnLLDBDebugSessionInfoVarObj &vrOther)
170 // Check for self-assignment
171 if (this == &vrOther)
172 return MIstatus::success;
174 m_eVarFormat = vrOther.m_eVarFormat;
175 m_eVarType = vrOther.m_eVarType;
176 m_strName = vrOther.m_strName;
177 m_SBValue = vrOther.m_SBValue;
178 m_strNameReal = vrOther.m_strNameReal;
179 m_strFormattedValue = vrOther.m_strFormattedValue;
180 m_strVarObjParentName = vrOther.m_strVarObjParentName;
182 return MIstatus::success;
185 //++ ------------------------------------------------------------------------------------
186 // Details: Move that object to *this object.
188 // Args: vrwOther - (RW) The object to copy from.
189 // Return: MIstatus::success - Functional succeeded.
190 // MIstatus::failure - Functional failed.
194 CMICmnLLDBDebugSessionInfoVarObj::MoveOther(CMICmnLLDBDebugSessionInfoVarObj &vrwOther)
196 // Check for self-assignment
197 if (this == &vrwOther)
198 return MIstatus::success;
201 vrwOther.m_eVarFormat = eVarFormat_Natural;
202 vrwOther.m_eVarType = eVarType_Internal;
203 vrwOther.m_strName.clear();
204 vrwOther.m_SBValue.Clear();
205 vrwOther.m_strNameReal.clear();
206 vrwOther.m_strFormattedValue.clear();
207 vrwOther.m_strVarObjParentName.clear();
209 return MIstatus::success;
212 //++ ------------------------------------------------------------------------------------
213 // Details: CMICmnLLDBDebugSessionInfoVarObj destructor.
219 CMICmnLLDBDebugSessionInfoVarObj::~CMICmnLLDBDebugSessionInfoVarObj(void)
223 //++ ------------------------------------------------------------------------------------
224 // Details: Retrieve the var format enumeration for the specified string.
225 // Type: Static method.
226 // Args: vrStrFormat - (R) Text description of the var format.
227 // Return: varFormat_e - Var format enumeration.
228 // - No match found return eVarFormat_Invalid.
231 CMICmnLLDBDebugSessionInfoVarObj::varFormat_e
232 CMICmnLLDBDebugSessionInfoVarObj::GetVarFormatForString(const CMIUtilString &vrStrFormat)
234 // CODETAG_SESSIONINFO_VARFORMAT_ENUM
235 for (MIuint i = 0; i < eVarFormat_count; i++)
237 const MIchar *pVarFormatString = ms_aVarFormatStrings[i];
238 if (vrStrFormat == pVarFormatString)
239 return static_cast<varFormat_e>(i);
242 return eVarFormat_Invalid;
245 //++ ------------------------------------------------------------------------------------
246 // Details: Retrieve the var format enumeration for the specified character.
247 // Type: Static method.
248 // Args: vrcFormat - (R) Character representing the var format.
249 // Return: varFormat_e - Var format enumeration.
250 // - No match found return eVarFormat_Invalid.
253 CMICmnLLDBDebugSessionInfoVarObj::varFormat_e
254 CMICmnLLDBDebugSessionInfoVarObj::GetVarFormatForChar(const MIchar &vrcFormat)
256 if ('r' == vrcFormat)
257 return eVarFormat_Hex;
259 // CODETAG_SESSIONINFO_VARFORMAT_ENUM
260 for (MIuint i = 0; i < eVarFormat_count; i++)
262 const MIchar *pVarFormatChar = ms_aVarFormatChars[i];
263 if (*pVarFormatChar == vrcFormat)
264 return static_cast<varFormat_e>(i);
267 return eVarFormat_Invalid;
270 //++ ------------------------------------------------------------------------------------
271 // Details: Return the equivalent var value formatted string for the given value type.
272 // The SBValue vrValue parameter is checked by LLDB private code for valid
273 // scalar type via MI Driver proxy function as the valued returned can also be
274 // an error condition. The proxy function determines if the check was valid
275 // otherwise return an error condition state by other means saying so.
276 // Type: Static method.
277 // Args: vrValue - (R) The var value object.
278 // veVarFormat - (R) Var format enumeration.
279 // Returns: CMIUtilString - Value formatted string.
283 CMICmnLLDBDebugSessionInfoVarObj::GetValueStringFormatted(const lldb::SBValue &vrValue,
284 const CMICmnLLDBDebugSessionInfoVarObj::varFormat_e veVarFormat)
286 CMIUtilString strFormattedValue;
289 if (CMICmnLLDBProxySBValue::GetValueAsUnsigned(vrValue, nValue) == MIstatus::success)
291 lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
292 strFormattedValue = GetStringFormatted(nValue, rValue.GetValue(), veVarFormat);
296 // Composite variable type i.e. struct
297 strFormattedValue = "{...}";
300 return strFormattedValue;
303 //++ ------------------------------------------------------------------------------------
304 // Details: Return nuber formatted string according to the given value type.
305 // Type: Static method.
306 // Args: vnValue - (R) The number value to get formatted.
307 // vpStrValueNatural - (R) The natural representation of the number value.
308 // veVarFormat - (R) Var format enumeration.
309 // Returns: CMIUtilString - Numerical formatted string.
313 CMICmnLLDBDebugSessionInfoVarObj::GetStringFormatted(const MIuint64 vnValue, const MIchar *vpStrValueNatural,
314 const CMICmnLLDBDebugSessionInfoVarObj::varFormat_e veVarFormat)
316 CMIUtilString strFormattedValue;
320 case eVarFormat_Binary:
321 strFormattedValue = CMIUtilString::FormatBinary(vnValue);
323 case eVarFormat_Octal:
324 strFormattedValue = CMIUtilString::Format("0%llo", vnValue);
326 case eVarFormat_Decimal:
327 strFormattedValue = CMIUtilString::Format("%lld", vnValue);
330 strFormattedValue = CMIUtilString::Format("0x%llx", vnValue);
332 case eVarFormat_Natural:
335 strFormattedValue = (vpStrValueNatural != nullptr) ? vpStrValueNatural : "";
339 return strFormattedValue;
342 //++ ------------------------------------------------------------------------------------
343 // Details: Delete internal container contents.
344 // Type: Static method.
350 CMICmnLLDBDebugSessionInfoVarObj::VarObjClear(void)
352 ms_mapVarIdToVarObj.clear();
355 //++ ------------------------------------------------------------------------------------
356 // Details: Add a var object to the internal container.
357 // Type: Static method.
358 // Args: vrVarObj - (R) The var value object.
363 CMICmnLLDBDebugSessionInfoVarObj::VarObjAdd(const CMICmnLLDBDebugSessionInfoVarObj &vrVarObj)
365 VarObjDelete(vrVarObj.GetName());
366 MapPairKeyToVarObj_t pr(vrVarObj.GetName(), vrVarObj);
367 ms_mapVarIdToVarObj.insert(pr);
370 //++ ------------------------------------------------------------------------------------
371 // Details: Delete the var object from the internal container matching the specified name.
372 // Type: Static method.
373 // Args: vrVarName - (R) The var value name.
378 CMICmnLLDBDebugSessionInfoVarObj::VarObjDelete(const CMIUtilString &vrVarName)
380 const MapKeyToVarObj_t::const_iterator it = ms_mapVarIdToVarObj.find(vrVarName);
381 if (it != ms_mapVarIdToVarObj.end())
383 ms_mapVarIdToVarObj.erase(it);
387 //++ ------------------------------------------------------------------------------------
388 // Details: Update an existing var object in the internal container.
389 // Type: Static method.
390 // Args: vrVarObj - (R) The var value object.
395 CMICmnLLDBDebugSessionInfoVarObj::VarObjUpdate(const CMICmnLLDBDebugSessionInfoVarObj &vrVarObj)
400 //++ ------------------------------------------------------------------------------------
401 // Details: Retrieve the var object matching the specified name.
402 // Type: Static method.
403 // Args: vrVarName - (R) The var value name.
404 // vrwVarObj - (W) A var object.
405 // Returns: bool - True = object found, false = object not found.
409 CMICmnLLDBDebugSessionInfoVarObj::VarObjGet(const CMIUtilString &vrVarName, CMICmnLLDBDebugSessionInfoVarObj &vrwVarObj)
411 const MapKeyToVarObj_t::const_iterator it = ms_mapVarIdToVarObj.find(vrVarName);
412 if (it != ms_mapVarIdToVarObj.end())
414 const CMICmnLLDBDebugSessionInfoVarObj &rVarObj = (*it).second;
422 //++ ------------------------------------------------------------------------------------
423 // Details: A count is kept of the number of var value objects created. This is count is
424 // used to ID the var value object. Reset the count to 0.
425 // Type: Static method.
431 CMICmnLLDBDebugSessionInfoVarObj::VarObjIdResetToZero(void)
436 //++ ------------------------------------------------------------------------------------
437 // Details: A count is kept of the number of var value objects created. This is count is
438 // used to ID the var value object. Increment the count by 1.
439 // Type: Static method.
445 CMICmnLLDBDebugSessionInfoVarObj::VarObjIdInc(void)
450 //++ ------------------------------------------------------------------------------------
451 // Details: A count is kept of the number of var value objects created. This is count is
452 // used to ID the var value object. Retrieve ID.
453 // Type: Static method.
459 CMICmnLLDBDebugSessionInfoVarObj::VarObjIdGet(void)
461 return ms_nVarUniqueId;
464 //++ ------------------------------------------------------------------------------------
465 // Details: Retrieve the value formatted object's name.
468 // Returns: CMIUtilString & - Value's var%u name text.
471 const CMIUtilString &
472 CMICmnLLDBDebugSessionInfoVarObj::GetName(void) const
477 //++ ------------------------------------------------------------------------------------
478 // Details: Retrieve the value formatted object's variable name as given in the MI command
479 // to create the var object.
482 // Returns: CMIUtilString & - Value's real name text.
485 const CMIUtilString &
486 CMICmnLLDBDebugSessionInfoVarObj::GetNameReal(void) const
488 return m_strNameReal;
491 //++ ------------------------------------------------------------------------------------
492 // Details: Retrieve the value formatted string.
495 // Returns: CMIUtilString & - Value formatted string.
498 const CMIUtilString &
499 CMICmnLLDBDebugSessionInfoVarObj::GetValueFormatted(void) const
501 return m_strFormattedValue;
504 //++ ------------------------------------------------------------------------------------
505 // Details: Retrieve the LLDB Value object.
508 // Returns: lldb::SBValue & - LLDB Value object.
511 const lldb::SBValue &
512 CMICmnLLDBDebugSessionInfoVarObj::GetValue(void) const
517 //++ ------------------------------------------------------------------------------------
518 // Details: Set the var format type for *this object and upate the formatting.
521 // Return: MIstatus::success - Functional succeeded.
522 // MIstatus::failure - Functional failed.
526 CMICmnLLDBDebugSessionInfoVarObj::SetVarFormat(const varFormat_e veVarFormat)
528 if (veVarFormat >= eVarFormat_count)
529 return MIstatus::failure;
531 m_eVarFormat = veVarFormat;
533 return MIstatus::success;
536 //++ ------------------------------------------------------------------------------------
537 // Details: Update *this var obj. Update it's value and type.
544 CMICmnLLDBDebugSessionInfoVarObj::UpdateValue(void)
546 m_strFormattedValue = CMICmnLLDBDebugSessionInfoVarObj::GetValueStringFormatted(m_SBValue, m_eVarFormat);
549 if (CMICmnLLDBProxySBValue::GetValueAsUnsigned(m_SBValue, nValue) == MIstatus::failure)
550 m_eVarType = eVarType_Composite;
552 CMICmnLLDBDebugSessionInfoVarObj::VarObjUpdate(*this);
555 //++ ------------------------------------------------------------------------------------
556 // Details: Retrieve the enumeration type of the var object.
559 // Returns: varType_e - Enumeration value.
562 CMICmnLLDBDebugSessionInfoVarObj::varType_e
563 CMICmnLLDBDebugSessionInfoVarObj::GetType(void) const
568 //++ ------------------------------------------------------------------------------------
569 // Details: Retrieve the parent var object's name, the parent var object to *this var
570 // object (if assigned). The parent is equivalent to LLDB SBValue variable's
574 // Returns: CMIUtilString & - Pointer to var object, NULL = no parent.
577 const CMIUtilString &
578 CMICmnLLDBDebugSessionInfoVarObj::GetVarParentName(void) const
580 return m_strVarObjParentName;