]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/lldb-mi/MICmnLLDBDebugSessionInfoVarObj.cpp
Import LLDB as of upstream SVN r225923 (git 2b588ecd)
[FreeBSD/FreeBSD.git] / tools / lldb-mi / MICmnLLDBDebugSessionInfoVarObj.cpp
1 //===-- MICmnLLDBDebugSessionInfoVarObj.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
10 //++
11 // File:        MICmnLLDBDebugSessionInfoVarObj.cpp
12 //
13 // Overview:    CMICmnLLDBDebugSessionInfoVarObj implementation.
14 //
15 // Environment: Compilers:  Visual C++ 12.
16 //                          gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 //              Libraries:  See MIReadmetxt.
18 //
19 // Copyright:   None.
20 //--
21
22 // In-house headers:
23 #include "MICmnLLDBDebugSessionInfoVarObj.h"
24 #include "MICmnLLDBProxySBValue.h"
25 #include "MICmnLLDBUtilSBValue.h"
26
27 // Instantiations:
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
38
39 //++ ------------------------------------------------------------------------------------
40 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
41 // Type:    Method.
42 // Args:    None.
43 // Return:  None.
44 // Throws:  None.
45 //--
46 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(void)
47     : m_eVarFormat(eVarFormat_Natural)
48     , m_eVarType(eVarType_Internal)
49 {
50     // Do not call UpdateValue() in here as not necessary
51 }
52
53 //++ ------------------------------------------------------------------------------------
54 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
55 // Type:    Method.
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.
59 // Return:  None.
60 // Throws:  None.
61 //--
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)
67     , m_SBValue(vrValue)
68     , m_strNameReal(vrStrNameReal)
69 {
70     UpdateValue();
71 }
72
73 //++ ------------------------------------------------------------------------------------
74 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
75 // Type:    Method.
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).
80 // Return:  None.
81 // Throws:  None.
82 //--
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)
88     , m_SBValue(vrValue)
89     , m_strNameReal(vrStrNameReal)
90     , m_strVarObjParentName(vrStrVarObjParentName)
91 {
92     UpdateValue();
93 }
94
95 //++ ------------------------------------------------------------------------------------
96 // Details: CMICmnLLDBDebugSessionInfoVarObj copy constructor.
97 // Type:    Method.
98 // Args:    vrOther - (R) The object to copy from.
99 // Return:  None.
100 // Throws:  None.
101 //--
102 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(const CMICmnLLDBDebugSessionInfoVarObj &vrOther)
103 {
104     CopyOther(vrOther);
105 }
106
107 //++ ------------------------------------------------------------------------------------
108 // Details: CMICmnLLDBDebugSessionInfoVarObj copy constructor.
109 // Type:    Method.
110 // Args:    vrOther - (R) The object to copy from.
111 // Return:  None.
112 // Throws:  None.
113 //--
114 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(CMICmnLLDBDebugSessionInfoVarObj &vrOther)
115 {
116     CopyOther(vrOther);
117 }
118
119 //++ ------------------------------------------------------------------------------------
120 // Details: CMICmnLLDBDebugSessionInfoVarObj move constructor.
121 // Type:    Method.
122 // Args:    vrwOther    - (R) The object to copy from.
123 // Return:  None.
124 // Throws:  None.
125 //--
126 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(CMICmnLLDBDebugSessionInfoVarObj &&vrwOther)
127 {
128     MoveOther(vrwOther);
129 }
130
131 //++ ------------------------------------------------------------------------------------
132 // Details: CMICmnLLDBDebugSessionInfoVarObj assignment operator.
133 // Type:    Method.
134 // Args:    vrOther - (R) The object to copy from.
135 // Return:  CMICmnLLDBDebugSessionInfoVarObj & - Updated *this object.
136 // Throws:  None.
137 //--
138 CMICmnLLDBDebugSessionInfoVarObj &CMICmnLLDBDebugSessionInfoVarObj::operator=(const CMICmnLLDBDebugSessionInfoVarObj &vrOther)
139 {
140     CopyOther(vrOther);
141
142     return *this;
143 }
144
145 //++ ------------------------------------------------------------------------------------
146 // Details: CMICmnLLDBDebugSessionInfoVarObj assignment operator.
147 // Type:    Method.
148 // Args:    vrwOther    - (R) The object to copy from.
149 // Return:  CMICmnLLDBDebugSessionInfoVarObj & - Updated *this object.
150 // Throws:  None.
151 //--
152 CMICmnLLDBDebugSessionInfoVarObj &CMICmnLLDBDebugSessionInfoVarObj::operator=(CMICmnLLDBDebugSessionInfoVarObj &&vrwOther)
153 {
154     MoveOther(vrwOther);
155
156     return *this;
157 }
158
159 //++ ------------------------------------------------------------------------------------
160 // Details: Copy the other instance of that object to *this object.
161 // Type:    Method.
162 // Args:    vrOther - (R) The object to copy from.
163 // Return:  MIstatus::success - Functional succeeded.
164 //          MIstatus::failure - Functional failed.
165 // Throws:  None.
166 //--
167 bool
168 CMICmnLLDBDebugSessionInfoVarObj::CopyOther(const CMICmnLLDBDebugSessionInfoVarObj &vrOther)
169 {
170     // Check for self-assignment
171     if (this == &vrOther)
172         return MIstatus::success;
173
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;
181
182     return MIstatus::success;
183 }
184
185 //++ ------------------------------------------------------------------------------------
186 // Details: Move that object to *this object.
187 // Type:    Method.
188 // Args:    vrwOther    - (RW) The object to copy from.
189 // Return:  MIstatus::success - Functional succeeded.
190 //          MIstatus::failure - Functional failed.
191 // Throws:  None.
192 //--
193 bool
194 CMICmnLLDBDebugSessionInfoVarObj::MoveOther(CMICmnLLDBDebugSessionInfoVarObj &vrwOther)
195 {
196     // Check for self-assignment
197     if (this == &vrwOther)
198         return MIstatus::success;
199
200     CopyOther(vrwOther);
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();
208
209     return MIstatus::success;
210 }
211
212 //++ ------------------------------------------------------------------------------------
213 // Details: CMICmnLLDBDebugSessionInfoVarObj destructor.
214 // Type:    Overridden.
215 // Args:    None.
216 // Return:  None.
217 // Throws:  None.
218 //--
219 CMICmnLLDBDebugSessionInfoVarObj::~CMICmnLLDBDebugSessionInfoVarObj(void)
220 {
221 }
222
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.
229 // Throws:  None.
230 //--
231 CMICmnLLDBDebugSessionInfoVarObj::varFormat_e
232 CMICmnLLDBDebugSessionInfoVarObj::GetVarFormatForString(const CMIUtilString &vrStrFormat)
233 {
234     // CODETAG_SESSIONINFO_VARFORMAT_ENUM
235     for (MIuint i = 0; i < eVarFormat_count; i++)
236     {
237         const MIchar *pVarFormatString = ms_aVarFormatStrings[i];
238         if (vrStrFormat == pVarFormatString)
239             return static_cast<varFormat_e>(i);
240     }
241
242     return eVarFormat_Invalid;
243 }
244
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.
251 // Throws:  None.
252 //--
253 CMICmnLLDBDebugSessionInfoVarObj::varFormat_e
254 CMICmnLLDBDebugSessionInfoVarObj::GetVarFormatForChar(const MIchar &vrcFormat)
255 {
256     if ('r' == vrcFormat)
257         return eVarFormat_Hex;
258
259     // CODETAG_SESSIONINFO_VARFORMAT_ENUM
260     for (MIuint i = 0; i < eVarFormat_count; i++)
261     {
262         const MIchar *pVarFormatChar = ms_aVarFormatChars[i];
263         if (*pVarFormatChar == vrcFormat)
264             return static_cast<varFormat_e>(i);
265     }
266
267     return eVarFormat_Invalid;
268 }
269
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.
280 // Throws:  None.
281 //--
282 CMIUtilString
283 CMICmnLLDBDebugSessionInfoVarObj::GetValueStringFormatted(const lldb::SBValue &vrValue,
284                                                           const CMICmnLLDBDebugSessionInfoVarObj::varFormat_e veVarFormat)
285 {
286     CMIUtilString strFormattedValue;
287
288     MIuint64 nValue = 0;
289     if (CMICmnLLDBProxySBValue::GetValueAsUnsigned(vrValue, nValue) == MIstatus::success)
290     {
291         lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
292         strFormattedValue = GetStringFormatted(nValue, rValue.GetValue(), veVarFormat);
293     }
294     else
295     {
296         // Composite variable type i.e. struct
297         strFormattedValue = "{...}";
298     }
299
300     return strFormattedValue;
301 }
302
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.
310 // Throws:  None.
311 //--
312 CMIUtilString
313 CMICmnLLDBDebugSessionInfoVarObj::GetStringFormatted(const MIuint64 vnValue, const MIchar *vpStrValueNatural,
314                                                      const CMICmnLLDBDebugSessionInfoVarObj::varFormat_e veVarFormat)
315 {
316     CMIUtilString strFormattedValue;
317
318     switch (veVarFormat)
319     {
320         case eVarFormat_Binary:
321             strFormattedValue = CMIUtilString::FormatBinary(vnValue);
322             break;
323         case eVarFormat_Octal:
324             strFormattedValue = CMIUtilString::Format("0%llo", vnValue);
325             break;
326         case eVarFormat_Decimal:
327             strFormattedValue = CMIUtilString::Format("%lld", vnValue);
328             break;
329         case eVarFormat_Hex:
330             strFormattedValue = CMIUtilString::Format("0x%llx", vnValue);
331             break;
332         case eVarFormat_Natural:
333         default:
334         {
335             strFormattedValue = (vpStrValueNatural != nullptr) ? vpStrValueNatural : "";
336         }
337     }
338
339     return strFormattedValue;
340 }
341
342 //++ ------------------------------------------------------------------------------------
343 // Details: Delete internal container contents.
344 // Type:    Static method.
345 // Args:    None.
346 // Returns: None.
347 // Throws:  None.
348 //--
349 void
350 CMICmnLLDBDebugSessionInfoVarObj::VarObjClear(void)
351 {
352     ms_mapVarIdToVarObj.clear();
353 }
354
355 //++ ------------------------------------------------------------------------------------
356 // Details: Add a var object to the internal container.
357 // Type:    Static method.
358 // Args:    vrVarObj    - (R) The var value object.
359 // Returns: None.
360 // Throws:  None.
361 //--
362 void
363 CMICmnLLDBDebugSessionInfoVarObj::VarObjAdd(const CMICmnLLDBDebugSessionInfoVarObj &vrVarObj)
364 {
365     VarObjDelete(vrVarObj.GetName());
366     MapPairKeyToVarObj_t pr(vrVarObj.GetName(), vrVarObj);
367     ms_mapVarIdToVarObj.insert(pr);
368 }
369
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.
374 // Returns: None.
375 // Throws:  None.
376 //--
377 void
378 CMICmnLLDBDebugSessionInfoVarObj::VarObjDelete(const CMIUtilString &vrVarName)
379 {
380     const MapKeyToVarObj_t::const_iterator it = ms_mapVarIdToVarObj.find(vrVarName);
381     if (it != ms_mapVarIdToVarObj.end())
382     {
383         ms_mapVarIdToVarObj.erase(it);
384     }
385 }
386
387 //++ ------------------------------------------------------------------------------------
388 // Details: Update an existing var object in the internal container.
389 // Type:    Static method.
390 // Args:    vrVarObj    - (R) The var value object.
391 // Returns: None.
392 // Throws:  None.
393 //--
394 void
395 CMICmnLLDBDebugSessionInfoVarObj::VarObjUpdate(const CMICmnLLDBDebugSessionInfoVarObj &vrVarObj)
396 {
397     VarObjAdd(vrVarObj);
398 }
399
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.
406 // Throws:  None.
407 //--
408 bool
409 CMICmnLLDBDebugSessionInfoVarObj::VarObjGet(const CMIUtilString &vrVarName, CMICmnLLDBDebugSessionInfoVarObj &vrwVarObj)
410 {
411     const MapKeyToVarObj_t::const_iterator it = ms_mapVarIdToVarObj.find(vrVarName);
412     if (it != ms_mapVarIdToVarObj.end())
413     {
414         const CMICmnLLDBDebugSessionInfoVarObj &rVarObj = (*it).second;
415         vrwVarObj = rVarObj;
416         return true;
417     }
418
419     return false;
420 }
421
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.
426 // Args:    None.
427 // Returns: None.
428 // Throws:  None.
429 //--
430 void
431 CMICmnLLDBDebugSessionInfoVarObj::VarObjIdResetToZero(void)
432 {
433     ms_nVarUniqueId = 0;
434 }
435
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.
440 // Args:    None.
441 // Returns: None.
442 // Throws:  None.
443 //--
444 void
445 CMICmnLLDBDebugSessionInfoVarObj::VarObjIdInc(void)
446 {
447     ms_nVarUniqueId++;
448 }
449
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.
454 // Args:    None.
455 // Returns: None.
456 // Throws:  None.
457 //--
458 MIuint
459 CMICmnLLDBDebugSessionInfoVarObj::VarObjIdGet(void)
460 {
461     return ms_nVarUniqueId;
462 }
463
464 //++ ------------------------------------------------------------------------------------
465 // Details: Retrieve the value formatted object's name.
466 // Type:    Method.
467 // Args:    None.
468 // Returns: CMIUtilString & - Value's var%u name text.
469 // Throws:  None.
470 //--
471 const CMIUtilString &
472 CMICmnLLDBDebugSessionInfoVarObj::GetName(void) const
473 {
474     return m_strName;
475 }
476
477 //++ ------------------------------------------------------------------------------------
478 // Details: Retrieve the value formatted object's variable name as given in the MI command
479 //          to create the var object.
480 // Type:    Method.
481 // Args:    None.
482 // Returns: CMIUtilString & - Value's real name text.
483 // Throws:  None.
484 //--
485 const CMIUtilString &
486 CMICmnLLDBDebugSessionInfoVarObj::GetNameReal(void) const
487 {
488     return m_strNameReal;
489 }
490
491 //++ ------------------------------------------------------------------------------------
492 // Details: Retrieve the value formatted string.
493 // Type:    Method.
494 // Args:    None.
495 // Returns: CMIUtilString & - Value formatted string.
496 // Throws:  None.
497 //--
498 const CMIUtilString &
499 CMICmnLLDBDebugSessionInfoVarObj::GetValueFormatted(void) const
500 {
501     return m_strFormattedValue;
502 }
503
504 //++ ------------------------------------------------------------------------------------
505 // Details: Retrieve the LLDB Value object.
506 // Type:    Method.
507 // Args:    None.
508 // Returns: lldb::SBValue & - LLDB Value object.
509 // Throws:  None.
510 //--
511 const lldb::SBValue &
512 CMICmnLLDBDebugSessionInfoVarObj::GetValue(void) const
513 {
514     return m_SBValue;
515 }
516
517 //++ ------------------------------------------------------------------------------------
518 // Details: Set the var format type for *this object and upate the formatting.
519 // Type:    Method.
520 // Args:    None.
521 // Return:  MIstatus::success - Functional succeeded.
522 //          MIstatus::failure - Functional failed.
523 // Throws:  None.
524 //--
525 bool
526 CMICmnLLDBDebugSessionInfoVarObj::SetVarFormat(const varFormat_e veVarFormat)
527 {
528     if (veVarFormat >= eVarFormat_count)
529         return MIstatus::failure;
530
531     m_eVarFormat = veVarFormat;
532     UpdateValue();
533     return MIstatus::success;
534 }
535
536 //++ ------------------------------------------------------------------------------------
537 // Details: Update *this var obj. Update it's value and type.
538 // Type:    Method.
539 // Args:    None.
540 // Returns: None.
541 // Throws:  None.
542 //--
543 void
544 CMICmnLLDBDebugSessionInfoVarObj::UpdateValue(void)
545 {
546     m_strFormattedValue = CMICmnLLDBDebugSessionInfoVarObj::GetValueStringFormatted(m_SBValue, m_eVarFormat);
547
548     MIuint64 nValue = 0;
549     if (CMICmnLLDBProxySBValue::GetValueAsUnsigned(m_SBValue, nValue) == MIstatus::failure)
550         m_eVarType = eVarType_Composite;
551
552     CMICmnLLDBDebugSessionInfoVarObj::VarObjUpdate(*this);
553 }
554
555 //++ ------------------------------------------------------------------------------------
556 // Details: Retrieve the enumeration type of the var object.
557 // Type:    Method.
558 // Args:    None.
559 // Returns: varType_e   - Enumeration value.
560 // Throws:  None.
561 //--
562 CMICmnLLDBDebugSessionInfoVarObj::varType_e
563 CMICmnLLDBDebugSessionInfoVarObj::GetType(void) const
564 {
565     return m_eVarType;
566 }
567
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
571 //          parent.
572 // Type:    Method.
573 // Args:    None.
574 // Returns: CMIUtilString & - Pointer to var object, NULL = no parent.
575 // Throws:  None.
576 //--
577 const CMIUtilString &
578 CMICmnLLDBDebugSessionInfoVarObj::GetVarParentName(void) const
579 {
580     return m_strVarObjParentName;
581 }