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