]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/tools/lldb-mi/MICmnLLDBProxySBValue.cpp
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / tools / lldb-mi / MICmnLLDBProxySBValue.cpp
1 //===-- MICmnLLDBProxySBValue.cpp -------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include <stdlib.h>
10
11 // Third Party Headers:
12 #include "lldb/API/SBError.h"
13
14 // In-house headers:
15 #include "MICmnLLDBDebugSessionInfo.h"
16 #include "MICmnLLDBProxySBValue.h"
17 #include "MIUtilString.h"
18
19 //++
20 // Details: Retrieve the numerical value from the SBValue object. If the
21 // function fails
22 //          it could indicate the SBValue object does not represent an internal
23 //          type.
24 // Type:    Static method.
25 // Args:    vrValue - (R) The SBValue object to get a value from.
26 //          vwValue - (W) The numerical value.
27 // Return:  MIstatus::success - Functionality succeeded.
28 //          MIstatus::failure - Functionality failed.
29 // Throws:  None.
30 //--
31 bool CMICmnLLDBProxySBValue::GetValueAsUnsigned(const lldb::SBValue &vrValue,
32                                                 MIuint64 &vwValue) {
33   lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
34   bool bCompositeType = true;
35   MIuint64 nFailValue = 0;
36   MIuint64 nValue = rValue.GetValueAsUnsigned(nFailValue);
37   if (nValue == nFailValue) {
38     nFailValue = 5; // Some arbitrary number
39     nValue = rValue.GetValueAsUnsigned(nFailValue);
40     if (nValue != nFailValue) {
41       bCompositeType = false;
42       vwValue = nValue;
43     }
44   } else {
45     bCompositeType = false;
46     vwValue = nValue;
47   }
48
49   return (bCompositeType ? MIstatus::failure : MIstatus::success);
50 }
51
52 //++
53 // Details: Retrieve the numerical value from the SBValue object. If the
54 // function fails
55 //          it could indicate the SBValue object does not represent an internal
56 //          type.
57 // Type:    Static method.
58 // Args:    vrValue - (R) The SBValue object to get a value from.
59 //          vwValue - (W) The numerical value.
60 // Return:  MIstatus::success - Functionality succeeded.
61 //          MIstatus::failure - Functionality failed.
62 // Throws:  None.
63 //--
64 bool CMICmnLLDBProxySBValue::GetValueAsSigned(const lldb::SBValue &vrValue,
65                                               MIint64 &vwValue) {
66   lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
67   bool bCompositeType = true;
68   MIuint64 nFailValue = 0;
69   MIuint64 nValue = rValue.GetValueAsSigned(nFailValue);
70   if (nValue == nFailValue) {
71     nFailValue = 5; // Some arbitrary number
72     nValue = rValue.GetValueAsSigned(nFailValue);
73     if (nValue != nFailValue) {
74       bCompositeType = false;
75       vwValue = nValue;
76     }
77   } else {
78     bCompositeType = false;
79     vwValue = nValue;
80   }
81
82   return (bCompositeType ? MIstatus::failure : MIstatus::success);
83 }
84
85 //++
86 // Details: Retrieve the NUL terminated string from the SBValue object if it of
87 // the type
88 //          unsigned char *.
89 // Type:    Static method.
90 // Args:    vrValue     - (R) The SBValue object to get a value from.
91 //          vwCString   - (W) The text data '\0' terminated.
92 // Return:  MIstatus::success - Functionality succeeded.
93 //          MIstatus::failure - Functionality failed, not suitable type.
94 // Throws:  None.
95 //--
96 bool CMICmnLLDBProxySBValue::GetCString(const lldb::SBValue &vrValue,
97                                         CMIUtilString &vwCString) {
98   lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
99   const char *pCType = rValue.GetTypeName();
100   if (pCType == nullptr)
101     return MIstatus::failure;
102
103   const char *pType = "unsigned char *";
104   if (!CMIUtilString::Compare(pCType, pType))
105     return MIstatus::failure;
106
107   const CMIUtilString strAddr(rValue.GetValue());
108   MIint64 nNum = 0;
109   if (!strAddr.ExtractNumber(nNum))
110     return MIstatus::failure;
111
112   CMICmnLLDBDebugSessionInfo &rSessionInfo(
113       CMICmnLLDBDebugSessionInfo::Instance());
114   lldb::SBProcess sbProcess = rSessionInfo.GetProcess();
115   MIuint nBufferSize = 64;
116   bool bNeedResize = false;
117   char *pBuffer = static_cast<char *>(::malloc(nBufferSize));
118   do {
119     lldb::SBError error;
120     const size_t nReadSize = sbProcess.ReadCStringFromMemory(
121         (lldb::addr_t)nNum, pBuffer, nBufferSize, error);
122     if (nReadSize == (nBufferSize - 1)) {
123       bNeedResize = true;
124       nBufferSize = nBufferSize << 1;
125       pBuffer = static_cast<char *>(::realloc(pBuffer, nBufferSize));
126     } else
127       bNeedResize = false;
128   } while (bNeedResize);
129
130   vwCString = pBuffer;
131   free((void *)pBuffer);
132
133   return MIstatus::success;
134 }