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 #include "MICmnLLDBDebugSessionInfoVarObj.h"
12 #include "MICmnLLDBProxySBValue.h"
13 #include "MICmnLLDBUtilSBValue.h"
16 const char *CMICmnLLDBDebugSessionInfoVarObj::ms_aVarFormatStrings[] = {
17 // CODETAG_SESSIONINFO_VARFORMAT_ENUM
18 // *** Order is import here.
19 "<Invalid var format>", "binary", "octal", "decimal",
20 "hexadecimal", "natural"};
21 const char *CMICmnLLDBDebugSessionInfoVarObj::ms_aVarFormatChars[] = {
22 // CODETAG_SESSIONINFO_VARFORMAT_ENUM
23 // *** Order is import here.
24 "<Invalid var format>", "t", "o", "d", "x", "N"};
25 CMICmnLLDBDebugSessionInfoVarObj::MapKeyToVarObj_t
26 CMICmnLLDBDebugSessionInfoVarObj::ms_mapVarIdToVarObj;
27 MIuint CMICmnLLDBDebugSessionInfoVarObj::ms_nVarUniqueId = 0; // Index from 0
28 CMICmnLLDBDebugSessionInfoVarObj::varFormat_e
29 CMICmnLLDBDebugSessionInfoVarObj::ms_eDefaultFormat = eVarFormat_Natural;
32 //------------------------------------------------------------------------------------
33 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
39 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj()
40 : m_eVarFormat(eVarFormat_Natural), m_eVarType(eVarType_Internal) {
41 // Do not call UpdateValue() in here as not necessary
45 //------------------------------------------------------------------------------------
46 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
48 // Args: vrStrNameReal - (R) The actual name of the variable, the
50 // vrStrName - (R) The name given for *this var object.
51 // vrValue - (R) The LLDB SBValue object represented by *this
56 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(
57 const CMIUtilString &vrStrNameReal, const CMIUtilString &vrStrName,
58 const lldb::SBValue &vrValue)
59 : m_eVarFormat(eVarFormat_Natural), m_eVarType(eVarType_Internal),
60 m_strName(vrStrName), m_SBValue(vrValue), m_strNameReal(vrStrNameReal) {
65 //------------------------------------------------------------------------------------
66 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
68 // Args: vrStrNameReal - (R) The actual name of the variable, the
70 // vrStrName - (R) The name given for *this var object.
71 // vrValue - (R) The LLDB SBValue object represented by
73 // vrStrVarObjParentName - (R) The var object parent to *this var
74 // object (LLDB SBValue equivalent).
78 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(
79 const CMIUtilString &vrStrNameReal, const CMIUtilString &vrStrName,
80 const lldb::SBValue &vrValue, const CMIUtilString &vrStrVarObjParentName)
81 : m_eVarFormat(eVarFormat_Natural), m_eVarType(eVarType_Internal),
82 m_strName(vrStrName), m_SBValue(vrValue), m_strNameReal(vrStrNameReal),
83 m_strVarObjParentName(vrStrVarObjParentName) {
88 //------------------------------------------------------------------------------------
89 // Details: CMICmnLLDBDebugSessionInfoVarObj copy constructor.
91 // Args: vrOther - (R) The object to copy from.
95 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(
96 const CMICmnLLDBDebugSessionInfoVarObj &vrOther) {
101 //------------------------------------------------------------------------------------
102 // Details: CMICmnLLDBDebugSessionInfoVarObj copy constructor.
104 // Args: vrOther - (R) The object to copy from.
108 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(
109 CMICmnLLDBDebugSessionInfoVarObj &vrOther) {
114 //------------------------------------------------------------------------------------
115 // Details: CMICmnLLDBDebugSessionInfoVarObj move constructor.
117 // Args: vrwOther - (R) The object to copy from.
121 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(
122 CMICmnLLDBDebugSessionInfoVarObj &&vrwOther) {
127 //------------------------------------------------------------------------------------
128 // Details: CMICmnLLDBDebugSessionInfoVarObj assignment operator.
130 // Args: vrOther - (R) The object to copy from.
131 // Return: CMICmnLLDBDebugSessionInfoVarObj & - Updated *this object.
134 CMICmnLLDBDebugSessionInfoVarObj &CMICmnLLDBDebugSessionInfoVarObj::
135 operator=(const CMICmnLLDBDebugSessionInfoVarObj &vrOther) {
142 //------------------------------------------------------------------------------------
143 // Details: CMICmnLLDBDebugSessionInfoVarObj assignment operator.
145 // Args: vrwOther - (R) The object to copy from.
146 // Return: CMICmnLLDBDebugSessionInfoVarObj & - Updated *this object.
149 CMICmnLLDBDebugSessionInfoVarObj &CMICmnLLDBDebugSessionInfoVarObj::
150 operator=(CMICmnLLDBDebugSessionInfoVarObj &&vrwOther) {
157 //------------------------------------------------------------------------------------
158 // Details: Copy the other instance of that object to *this object.
160 // Args: vrOther - (R) The object to copy from.
161 // Return: MIstatus::success - Functional succeeded.
162 // MIstatus::failure - Functional failed.
165 bool CMICmnLLDBDebugSessionInfoVarObj::CopyOther(
166 const CMICmnLLDBDebugSessionInfoVarObj &vrOther) {
167 // Check for self-assignment
168 if (this == &vrOther)
169 return MIstatus::success;
171 m_eVarFormat = vrOther.m_eVarFormat;
172 m_eVarType = vrOther.m_eVarType;
173 m_strName = vrOther.m_strName;
174 m_SBValue = vrOther.m_SBValue;
175 m_strNameReal = vrOther.m_strNameReal;
176 m_strFormattedValue = vrOther.m_strFormattedValue;
177 m_strVarObjParentName = vrOther.m_strVarObjParentName;
179 return MIstatus::success;
183 //------------------------------------------------------------------------------------
184 // Details: Move that object to *this object.
186 // Args: vrwOther - (RW) The object to copy from.
187 // Return: MIstatus::success - Functional succeeded.
188 // MIstatus::failure - Functional failed.
191 bool CMICmnLLDBDebugSessionInfoVarObj::MoveOther(
192 CMICmnLLDBDebugSessionInfoVarObj &vrwOther) {
193 // Check for self-assignment
194 if (this == &vrwOther)
195 return MIstatus::success;
198 vrwOther.m_eVarFormat = eVarFormat_Natural;
199 vrwOther.m_eVarType = eVarType_Internal;
200 vrwOther.m_strName.clear();
201 vrwOther.m_SBValue.Clear();
202 vrwOther.m_strNameReal.clear();
203 vrwOther.m_strFormattedValue.clear();
204 vrwOther.m_strVarObjParentName.clear();
206 return MIstatus::success;
210 //------------------------------------------------------------------------------------
211 // Details: CMICmnLLDBDebugSessionInfoVarObj destructor.
217 CMICmnLLDBDebugSessionInfoVarObj::~CMICmnLLDBDebugSessionInfoVarObj() {}
220 //------------------------------------------------------------------------------------
221 // Details: Retrieve the var format enumeration for the specified string.
222 // Type: Static method.
223 // Args: vrStrFormat - (R) Text description of the var format.
224 // Return: varFormat_e - Var format enumeration.
225 // - No match found return eVarFormat_Invalid.
228 CMICmnLLDBDebugSessionInfoVarObj::varFormat_e
229 CMICmnLLDBDebugSessionInfoVarObj::GetVarFormatForString(
230 const CMIUtilString &vrStrFormat) {
231 // CODETAG_SESSIONINFO_VARFORMAT_ENUM
232 for (MIuint i = 0; i < eVarFormat_count; i++) {
233 const char *pVarFormatString = ms_aVarFormatStrings[i];
234 if (vrStrFormat == pVarFormatString)
235 return static_cast<varFormat_e>(i);
238 return eVarFormat_Invalid;
242 //------------------------------------------------------------------------------------
243 // Details: Retrieve the var format enumeration for the specified character.
244 // Type: Static method.
245 // Args: vcFormat - Character representing the var format.
246 // Return: varFormat_e - Var format enumeration.
247 // - No match found return eVarFormat_Invalid.
250 CMICmnLLDBDebugSessionInfoVarObj::varFormat_e
251 CMICmnLLDBDebugSessionInfoVarObj::GetVarFormatForChar(char vcFormat) {
253 return eVarFormat_Hex;
255 // CODETAG_SESSIONINFO_VARFORMAT_ENUM
256 for (MIuint i = 0; i < eVarFormat_count; i++) {
257 const char *pVarFormatChar = ms_aVarFormatChars[i];
258 if (*pVarFormatChar == vcFormat)
259 return static_cast<varFormat_e>(i);
262 return eVarFormat_Invalid;
266 //------------------------------------------------------------------------------------
267 // Details: Return the equivalent var value formatted string for the given value
269 // which was prepared for printing (i.e. value was escaped and now it's
271 // for wrapping into quotes).
272 // The SBValue vrValue parameter is checked by LLDB private code for
274 // scalar type via MI Driver proxy function as the valued returned can
276 // an error condition. The proxy function determines if the check was
278 // otherwise return an error condition state by other means saying so.
279 // Type: Static method.
280 // Args: vrValue - (R) The var value object.
281 // veVarFormat - (R) Var format enumeration.
282 // Returns: CMIUtilString - Value formatted string.
285 CMIUtilString CMICmnLLDBDebugSessionInfoVarObj::GetValueStringFormatted(
286 const lldb::SBValue &vrValue,
287 const CMICmnLLDBDebugSessionInfoVarObj::varFormat_e veVarFormat) {
288 const CMICmnLLDBUtilSBValue utilValue(vrValue, true);
289 if (utilValue.IsIntegerType()) {
291 if (CMICmnLLDBProxySBValue::GetValueAsUnsigned(vrValue, nValue)) {
292 lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
293 return GetStringFormatted(nValue, rValue.GetValue(), veVarFormat);
297 return utilValue.GetValue().AddSlashes();
301 //------------------------------------------------------------------------------------
302 // Details: Return number formatted string according to the given value type.
303 // Type: Static method.
304 // Args: vnValue - (R) The number value to get formatted.
305 // vpStrValueNatural - (R) The natural representation of the number
307 // veVarFormat - (R) Var format enumeration.
308 // Returns: CMIUtilString - Numerical formatted string.
311 CMIUtilString CMICmnLLDBDebugSessionInfoVarObj::GetStringFormatted(
312 const MIuint64 vnValue, const char *vpStrValueNatural,
313 const CMICmnLLDBDebugSessionInfoVarObj::varFormat_e veVarFormat) {
314 CMIUtilString strFormattedValue;
315 CMICmnLLDBDebugSessionInfoVarObj::varFormat_e veFormat = veVarFormat;
316 if (ms_eDefaultFormat != eVarFormat_Invalid &&
317 veVarFormat == eVarFormat_Natural) {
318 veFormat = ms_eDefaultFormat;
322 case eVarFormat_Binary:
323 strFormattedValue = CMIUtilString::FormatBinary(vnValue);
325 case eVarFormat_Octal:
326 strFormattedValue = CMIUtilString::Format("0%llo", vnValue);
328 case eVarFormat_Decimal:
329 strFormattedValue = CMIUtilString::Format("%lld", vnValue);
332 strFormattedValue = CMIUtilString::Format("0x%llx", vnValue);
334 case eVarFormat_Natural:
336 strFormattedValue = (vpStrValueNatural != nullptr) ? vpStrValueNatural : "";
340 return strFormattedValue;
344 //------------------------------------------------------------------------------------
345 // Details: Delete internal container contents.
346 // Type: Static method.
351 void CMICmnLLDBDebugSessionInfoVarObj::VarObjClear() {
352 ms_mapVarIdToVarObj.clear();
356 //------------------------------------------------------------------------------------
357 // Details: Add a var object to the internal container.
358 // Type: Static method.
359 // Args: vrVarObj - (R) The var value object.
363 void CMICmnLLDBDebugSessionInfoVarObj::VarObjAdd(
364 const CMICmnLLDBDebugSessionInfoVarObj &vrVarObj) {
365 VarObjDelete(vrVarObj.GetName());
366 MapPairKeyToVarObj_t pr(vrVarObj.GetName(), vrVarObj);
367 ms_mapVarIdToVarObj.insert(pr);
371 //------------------------------------------------------------------------------------
372 // Details: Delete the var object from the internal container matching the
374 // Type: Static method.
375 // Args: vrVarName - (R) The var value name.
379 void CMICmnLLDBDebugSessionInfoVarObj::VarObjDelete(
380 const CMIUtilString &vrVarName) {
381 const MapKeyToVarObj_t::const_iterator it =
382 ms_mapVarIdToVarObj.find(vrVarName);
383 if (it != ms_mapVarIdToVarObj.end()) {
384 ms_mapVarIdToVarObj.erase(it);
389 //------------------------------------------------------------------------------------
390 // Details: Update an existing var object in the internal container.
391 // Type: Static method.
392 // Args: vrVarObj - (R) The var value object.
396 void CMICmnLLDBDebugSessionInfoVarObj::VarObjUpdate(
397 const CMICmnLLDBDebugSessionInfoVarObj &vrVarObj) {
402 //------------------------------------------------------------------------------------
403 // Details: Retrieve the var object matching the specified name.
404 // Type: Static method.
405 // Args: vrVarName - (R) The var value name.
406 // vrwVarObj - (W) A var object.
407 // Returns: bool - True = object found, false = object not found.
410 bool CMICmnLLDBDebugSessionInfoVarObj::VarObjGet(
411 const CMIUtilString &vrVarName,
412 CMICmnLLDBDebugSessionInfoVarObj &vrwVarObj) {
413 const MapKeyToVarObj_t::const_iterator it =
414 ms_mapVarIdToVarObj.find(vrVarName);
415 if (it != ms_mapVarIdToVarObj.end()) {
416 const CMICmnLLDBDebugSessionInfoVarObj &rVarObj = (*it).second;
425 //------------------------------------------------------------------------------------
426 // Details: A count is kept of the number of var value objects created. This is
428 // used to ID the var value object. Reset the count to 0.
429 // Type: Static method.
434 void CMICmnLLDBDebugSessionInfoVarObj::VarObjIdResetToZero() {
439 //------------------------------------------------------------------------------------
440 // Details: Default format is globally used as the data format when "natural" is
441 // in effect, that is, this overrides the default
442 // Type: Static method.
447 void CMICmnLLDBDebugSessionInfoVarObj::VarObjSetFormat(
448 varFormat_e eDefaultFormat) {
449 ms_eDefaultFormat = eDefaultFormat;
453 //------------------------------------------------------------------------------------
454 // Details: A count is kept of the number of var value objects created. This is
456 // used to ID the var value object. Increment the count by 1.
457 // Type: Static method.
462 void CMICmnLLDBDebugSessionInfoVarObj::VarObjIdInc() { ms_nVarUniqueId++; }
465 //------------------------------------------------------------------------------------
466 // Details: A count is kept of the number of var value objects created. This is
468 // used to ID the var value object. Retrieve ID.
469 // Type: Static method.
474 MIuint CMICmnLLDBDebugSessionInfoVarObj::VarObjIdGet() {
475 return ms_nVarUniqueId;
479 //------------------------------------------------------------------------------------
480 // Details: Retrieve the value formatted object's name.
483 // Returns: CMIUtilString & - Value's var%u name text.
486 const CMIUtilString &CMICmnLLDBDebugSessionInfoVarObj::GetName() const {
491 //------------------------------------------------------------------------------------
492 // Details: Retrieve the value formatted object's variable name as given in the
494 // to create the var object.
497 // Returns: CMIUtilString & - Value's real name text.
500 const CMIUtilString &CMICmnLLDBDebugSessionInfoVarObj::GetNameReal() const {
501 return m_strNameReal;
505 //------------------------------------------------------------------------------------
506 // Details: Retrieve the value formatted string.
509 // Returns: CMIUtilString & - Value formatted string.
512 const CMIUtilString &
513 CMICmnLLDBDebugSessionInfoVarObj::GetValueFormatted() const {
514 return m_strFormattedValue;
518 //------------------------------------------------------------------------------------
519 // Details: Retrieve the LLDB Value object.
522 // Returns: lldb::SBValue & - LLDB Value object.
525 lldb::SBValue &CMICmnLLDBDebugSessionInfoVarObj::GetValue() {
530 //------------------------------------------------------------------------------------
531 // Details: Retrieve the LLDB Value object.
534 // Returns: lldb::SBValue & - Constant LLDB Value object.
537 const lldb::SBValue &CMICmnLLDBDebugSessionInfoVarObj::GetValue() const {
542 //------------------------------------------------------------------------------------
543 // Details: Set the var format type for *this object and update the formatting.
546 // Return: MIstatus::success - Functional succeeded.
547 // MIstatus::failure - Functional failed.
550 bool CMICmnLLDBDebugSessionInfoVarObj::SetVarFormat(
551 const varFormat_e veVarFormat) {
552 if (veVarFormat >= eVarFormat_count)
553 return MIstatus::failure;
555 m_eVarFormat = veVarFormat;
557 return MIstatus::success;
561 //------------------------------------------------------------------------------------
562 // Details: Update *this var obj. Update it's value and type.
568 void CMICmnLLDBDebugSessionInfoVarObj::UpdateValue() {
569 m_strFormattedValue = GetValueStringFormatted(m_SBValue, m_eVarFormat);
572 if (CMICmnLLDBProxySBValue::GetValueAsUnsigned(m_SBValue, nValue) ==
574 m_eVarType = eVarType_Composite;
576 CMICmnLLDBDebugSessionInfoVarObj::VarObjUpdate(*this);
580 //------------------------------------------------------------------------------------
581 // Details: Retrieve the enumeration type of the var object.
584 // Returns: varType_e - Enumeration value.
587 CMICmnLLDBDebugSessionInfoVarObj::varType_e
588 CMICmnLLDBDebugSessionInfoVarObj::GetType() const {
593 //------------------------------------------------------------------------------------
594 // Details: Retrieve the parent var object's name, the parent var object to
596 // object (if assigned). The parent is equivalent to LLDB SBValue
601 // Returns: CMIUtilString & - Pointer to var object, NULL = no parent.
604 const CMIUtilString &
605 CMICmnLLDBDebugSessionInfoVarObj::GetVarParentName() const {
606 return m_strVarObjParentName;