]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/lldb-mi/MICmnLLDBDebugSessionInfoVarObj.cpp
Vendor import of lldb trunk r290819:
[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 // In-house headers:
11 #include "MICmnLLDBDebugSessionInfoVarObj.h"
12 #include "MICmnLLDBProxySBValue.h"
13 #include "MICmnLLDBUtilSBValue.h"
14
15 // Instantiations:
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;
30
31 //++
32 //------------------------------------------------------------------------------------
33 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
34 // Type:    Method.
35 // Args:    None.
36 // Return:  None.
37 // Throws:  None.
38 //--
39 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj()
40     : m_eVarFormat(eVarFormat_Natural), m_eVarType(eVarType_Internal) {
41   // Do not call UpdateValue() in here as not necessary
42 }
43
44 //++
45 //------------------------------------------------------------------------------------
46 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
47 // Type:    Method.
48 // Args:    vrStrNameReal   - (R) The actual name of the variable, the
49 // expression.
50 //          vrStrName       - (R) The name given for *this var object.
51 //          vrValue         - (R) The LLDB SBValue object represented by *this
52 //          object.
53 // Return:  None.
54 // Throws:  None.
55 //--
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) {
61   UpdateValue();
62 }
63
64 //++
65 //------------------------------------------------------------------------------------
66 // Details: CMICmnLLDBDebugSessionInfoVarObj constructor.
67 // Type:    Method.
68 // Args:    vrStrNameReal           - (R) The actual name of the variable, the
69 // expression.
70 //          vrStrName               - (R) The name given for *this var object.
71 //          vrValue                 - (R) The LLDB SBValue object represented by
72 //          *this object.
73 //          vrStrVarObjParentName   - (R) The var object parent to *this var
74 //          object (LLDB SBValue equivalent).
75 // Return:  None.
76 // Throws:  None.
77 //--
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) {
84   UpdateValue();
85 }
86
87 //++
88 //------------------------------------------------------------------------------------
89 // Details: CMICmnLLDBDebugSessionInfoVarObj copy constructor.
90 // Type:    Method.
91 // Args:    vrOther - (R) The object to copy from.
92 // Return:  None.
93 // Throws:  None.
94 //--
95 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(
96     const CMICmnLLDBDebugSessionInfoVarObj &vrOther) {
97   CopyOther(vrOther);
98 }
99
100 //++
101 //------------------------------------------------------------------------------------
102 // Details: CMICmnLLDBDebugSessionInfoVarObj copy constructor.
103 // Type:    Method.
104 // Args:    vrOther - (R) The object to copy from.
105 // Return:  None.
106 // Throws:  None.
107 //--
108 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(
109     CMICmnLLDBDebugSessionInfoVarObj &vrOther) {
110   CopyOther(vrOther);
111 }
112
113 //++
114 //------------------------------------------------------------------------------------
115 // Details: CMICmnLLDBDebugSessionInfoVarObj move constructor.
116 // Type:    Method.
117 // Args:    vrwOther    - (R) The object to copy from.
118 // Return:  None.
119 // Throws:  None.
120 //--
121 CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj(
122     CMICmnLLDBDebugSessionInfoVarObj &&vrwOther) {
123   MoveOther(vrwOther);
124 }
125
126 //++
127 //------------------------------------------------------------------------------------
128 // Details: CMICmnLLDBDebugSessionInfoVarObj assignment operator.
129 // Type:    Method.
130 // Args:    vrOther - (R) The object to copy from.
131 // Return:  CMICmnLLDBDebugSessionInfoVarObj & - Updated *this object.
132 // Throws:  None.
133 //--
134 CMICmnLLDBDebugSessionInfoVarObj &CMICmnLLDBDebugSessionInfoVarObj::
135 operator=(const CMICmnLLDBDebugSessionInfoVarObj &vrOther) {
136   CopyOther(vrOther);
137
138   return *this;
139 }
140
141 //++
142 //------------------------------------------------------------------------------------
143 // Details: CMICmnLLDBDebugSessionInfoVarObj assignment operator.
144 // Type:    Method.
145 // Args:    vrwOther    - (R) The object to copy from.
146 // Return:  CMICmnLLDBDebugSessionInfoVarObj & - Updated *this object.
147 // Throws:  None.
148 //--
149 CMICmnLLDBDebugSessionInfoVarObj &CMICmnLLDBDebugSessionInfoVarObj::
150 operator=(CMICmnLLDBDebugSessionInfoVarObj &&vrwOther) {
151   MoveOther(vrwOther);
152
153   return *this;
154 }
155
156 //++
157 //------------------------------------------------------------------------------------
158 // Details: Copy the other instance of that object to *this object.
159 // Type:    Method.
160 // Args:    vrOther - (R) The object to copy from.
161 // Return:  MIstatus::success - Functional succeeded.
162 //          MIstatus::failure - Functional failed.
163 // Throws:  None.
164 //--
165 bool CMICmnLLDBDebugSessionInfoVarObj::CopyOther(
166     const CMICmnLLDBDebugSessionInfoVarObj &vrOther) {
167   // Check for self-assignment
168   if (this == &vrOther)
169     return MIstatus::success;
170
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;
178
179   return MIstatus::success;
180 }
181
182 //++
183 //------------------------------------------------------------------------------------
184 // Details: Move that object to *this object.
185 // Type:    Method.
186 // Args:    vrwOther    - (RW) The object to copy from.
187 // Return:  MIstatus::success - Functional succeeded.
188 //          MIstatus::failure - Functional failed.
189 // Throws:  None.
190 //--
191 bool CMICmnLLDBDebugSessionInfoVarObj::MoveOther(
192     CMICmnLLDBDebugSessionInfoVarObj &vrwOther) {
193   // Check for self-assignment
194   if (this == &vrwOther)
195     return MIstatus::success;
196
197   CopyOther(vrwOther);
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();
205
206   return MIstatus::success;
207 }
208
209 //++
210 //------------------------------------------------------------------------------------
211 // Details: CMICmnLLDBDebugSessionInfoVarObj destructor.
212 // Type:    Overridden.
213 // Args:    None.
214 // Return:  None.
215 // Throws:  None.
216 //--
217 CMICmnLLDBDebugSessionInfoVarObj::~CMICmnLLDBDebugSessionInfoVarObj() {}
218
219 //++
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.
226 // Throws:  None.
227 //--
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);
236   }
237
238   return eVarFormat_Invalid;
239 }
240
241 //++
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.
248 // Throws:  None.
249 //--
250 CMICmnLLDBDebugSessionInfoVarObj::varFormat_e
251 CMICmnLLDBDebugSessionInfoVarObj::GetVarFormatForChar(char vcFormat) {
252   if ('r' == vcFormat)
253     return eVarFormat_Hex;
254
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);
260   }
261
262   return eVarFormat_Invalid;
263 }
264
265 //++
266 //------------------------------------------------------------------------------------
267 // Details: Return the equivalent var value formatted string for the given value
268 // type,
269 //          which was prepared for printing (i.e. value was escaped and now it's
270 //          ready
271 //          for wrapping into quotes).
272 //          The SBValue vrValue parameter is checked by LLDB private code for
273 //          valid
274 //          scalar type via MI Driver proxy function as the valued returned can
275 //          also be
276 //          an error condition. The proxy function determines if the check was
277 //          valid
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.
283 // Throws:  None.
284 //--
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()) {
290     MIuint64 nValue = 0;
291     if (CMICmnLLDBProxySBValue::GetValueAsUnsigned(vrValue, nValue)) {
292       lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
293       return GetStringFormatted(nValue, rValue.GetValue(), veVarFormat);
294     }
295   }
296
297   return utilValue.GetValue().AddSlashes();
298 }
299
300 //++
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
306 //          value.
307 //          veVarFormat         - (R) Var format enumeration.
308 // Returns: CMIUtilString       - Numerical formatted string.
309 // Throws:  None.
310 //--
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;
319   }
320
321   switch (veFormat) {
322   case eVarFormat_Binary:
323     strFormattedValue = CMIUtilString::FormatBinary(vnValue);
324     break;
325   case eVarFormat_Octal:
326     strFormattedValue = CMIUtilString::Format("0%llo", vnValue);
327     break;
328   case eVarFormat_Decimal:
329     strFormattedValue = CMIUtilString::Format("%lld", vnValue);
330     break;
331   case eVarFormat_Hex:
332     strFormattedValue = CMIUtilString::Format("0x%llx", vnValue);
333     break;
334   case eVarFormat_Natural:
335   default: {
336     strFormattedValue = (vpStrValueNatural != nullptr) ? vpStrValueNatural : "";
337   }
338   }
339
340   return strFormattedValue;
341 }
342
343 //++
344 //------------------------------------------------------------------------------------
345 // Details: Delete internal container contents.
346 // Type:    Static method.
347 // Args:    None.
348 // Returns: None.
349 // Throws:  None.
350 //--
351 void CMICmnLLDBDebugSessionInfoVarObj::VarObjClear() {
352   ms_mapVarIdToVarObj.clear();
353 }
354
355 //++
356 //------------------------------------------------------------------------------------
357 // Details: Add a var object to the internal container.
358 // Type:    Static method.
359 // Args:    vrVarObj    - (R) The var value object.
360 // Returns: None.
361 // Throws:  None.
362 //--
363 void CMICmnLLDBDebugSessionInfoVarObj::VarObjAdd(
364     const CMICmnLLDBDebugSessionInfoVarObj &vrVarObj) {
365   VarObjDelete(vrVarObj.GetName());
366   MapPairKeyToVarObj_t pr(vrVarObj.GetName(), vrVarObj);
367   ms_mapVarIdToVarObj.insert(pr);
368 }
369
370 //++
371 //------------------------------------------------------------------------------------
372 // Details: Delete the var object from the internal container matching the
373 // specified name.
374 // Type:    Static method.
375 // Args:    vrVarName   - (R) The var value name.
376 // Returns: None.
377 // Throws:  None.
378 //--
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);
385   }
386 }
387
388 //++
389 //------------------------------------------------------------------------------------
390 // Details: Update an existing var object in the internal container.
391 // Type:    Static method.
392 // Args:    vrVarObj    - (R) The var value object.
393 // Returns: None.
394 // Throws:  None.
395 //--
396 void CMICmnLLDBDebugSessionInfoVarObj::VarObjUpdate(
397     const CMICmnLLDBDebugSessionInfoVarObj &vrVarObj) {
398   VarObjAdd(vrVarObj);
399 }
400
401 //++
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.
408 // Throws:  None.
409 //--
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;
417     vrwVarObj = rVarObj;
418     return true;
419   }
420
421   return false;
422 }
423
424 //++
425 //------------------------------------------------------------------------------------
426 // Details: A count is kept of the number of var value objects created. This is
427 // count is
428 //          used to ID the var value object. Reset the count to 0.
429 // Type:    Static method.
430 // Args:    None.
431 // Returns: None.
432 // Throws:  None.
433 //--
434 void CMICmnLLDBDebugSessionInfoVarObj::VarObjIdResetToZero() {
435   ms_nVarUniqueId = 0;
436 }
437
438 //++
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.
443 // Args:    None.
444 // Returns: None.
445 // Throws:  None.
446 //--
447 void CMICmnLLDBDebugSessionInfoVarObj::VarObjSetFormat(
448     varFormat_e eDefaultFormat) {
449   ms_eDefaultFormat = eDefaultFormat;
450 }
451
452 //++
453 //------------------------------------------------------------------------------------
454 // Details: A count is kept of the number of var value objects created. This is
455 // count is
456 //          used to ID the var value object. Increment the count by 1.
457 // Type:    Static method.
458 // Args:    None.
459 // Returns: None.
460 // Throws:  None.
461 //--
462 void CMICmnLLDBDebugSessionInfoVarObj::VarObjIdInc() { ms_nVarUniqueId++; }
463
464 //++
465 //------------------------------------------------------------------------------------
466 // Details: A count is kept of the number of var value objects created. This is
467 // count is
468 //          used to ID the var value object. Retrieve ID.
469 // Type:    Static method.
470 // Args:    None.
471 // Returns: None.
472 // Throws:  None.
473 //--
474 MIuint CMICmnLLDBDebugSessionInfoVarObj::VarObjIdGet() {
475   return ms_nVarUniqueId;
476 }
477
478 //++
479 //------------------------------------------------------------------------------------
480 // Details: Retrieve the value formatted object's name.
481 // Type:    Method.
482 // Args:    None.
483 // Returns: CMIUtilString & - Value's var%u name text.
484 // Throws:  None.
485 //--
486 const CMIUtilString &CMICmnLLDBDebugSessionInfoVarObj::GetName() const {
487   return m_strName;
488 }
489
490 //++
491 //------------------------------------------------------------------------------------
492 // Details: Retrieve the value formatted object's variable name as given in the
493 // MI command
494 //          to create the var object.
495 // Type:    Method.
496 // Args:    None.
497 // Returns: CMIUtilString & - Value's real name text.
498 // Throws:  None.
499 //--
500 const CMIUtilString &CMICmnLLDBDebugSessionInfoVarObj::GetNameReal() const {
501   return m_strNameReal;
502 }
503
504 //++
505 //------------------------------------------------------------------------------------
506 // Details: Retrieve the value formatted string.
507 // Type:    Method.
508 // Args:    None.
509 // Returns: CMIUtilString & - Value formatted string.
510 // Throws:  None.
511 //--
512 const CMIUtilString &
513 CMICmnLLDBDebugSessionInfoVarObj::GetValueFormatted() const {
514   return m_strFormattedValue;
515 }
516
517 //++
518 //------------------------------------------------------------------------------------
519 // Details: Retrieve the LLDB Value object.
520 // Type:    Method.
521 // Args:    None.
522 // Returns: lldb::SBValue & - LLDB Value object.
523 // Throws:  None.
524 //--
525 lldb::SBValue &CMICmnLLDBDebugSessionInfoVarObj::GetValue() {
526   return m_SBValue;
527 }
528
529 //++
530 //------------------------------------------------------------------------------------
531 // Details: Retrieve the LLDB Value object.
532 // Type:    Method.
533 // Args:    None.
534 // Returns: lldb::SBValue & - Constant LLDB Value object.
535 // Throws:  None.
536 //--
537 const lldb::SBValue &CMICmnLLDBDebugSessionInfoVarObj::GetValue() const {
538   return m_SBValue;
539 }
540
541 //++
542 //------------------------------------------------------------------------------------
543 // Details: Set the var format type for *this object and update the formatting.
544 // Type:    Method.
545 // Args:    None.
546 // Return:  MIstatus::success - Functional succeeded.
547 //          MIstatus::failure - Functional failed.
548 // Throws:  None.
549 //--
550 bool CMICmnLLDBDebugSessionInfoVarObj::SetVarFormat(
551     const varFormat_e veVarFormat) {
552   if (veVarFormat >= eVarFormat_count)
553     return MIstatus::failure;
554
555   m_eVarFormat = veVarFormat;
556   UpdateValue();
557   return MIstatus::success;
558 }
559
560 //++
561 //------------------------------------------------------------------------------------
562 // Details: Update *this var obj. Update it's value and type.
563 // Type:    Method.
564 // Args:    None.
565 // Returns: None.
566 // Throws:  None.
567 //--
568 void CMICmnLLDBDebugSessionInfoVarObj::UpdateValue() {
569   m_strFormattedValue = GetValueStringFormatted(m_SBValue, m_eVarFormat);
570
571   MIuint64 nValue = 0;
572   if (CMICmnLLDBProxySBValue::GetValueAsUnsigned(m_SBValue, nValue) ==
573       MIstatus::failure)
574     m_eVarType = eVarType_Composite;
575
576   CMICmnLLDBDebugSessionInfoVarObj::VarObjUpdate(*this);
577 }
578
579 //++
580 //------------------------------------------------------------------------------------
581 // Details: Retrieve the enumeration type of the var object.
582 // Type:    Method.
583 // Args:    None.
584 // Returns: varType_e   - Enumeration value.
585 // Throws:  None.
586 //--
587 CMICmnLLDBDebugSessionInfoVarObj::varType_e
588 CMICmnLLDBDebugSessionInfoVarObj::GetType() const {
589   return m_eVarType;
590 }
591
592 //++
593 //------------------------------------------------------------------------------------
594 // Details: Retrieve the parent var object's name, the parent var object  to
595 // *this var
596 //          object (if assigned). The parent is equivalent to LLDB SBValue
597 //          variable's
598 //          parent.
599 // Type:    Method.
600 // Args:    None.
601 // Returns: CMIUtilString & - Pointer to var object, NULL = no parent.
602 // Throws:  None.
603 //--
604 const CMIUtilString &
605 CMICmnLLDBDebugSessionInfoVarObj::GetVarParentName() const {
606   return m_strVarObjParentName;
607 }