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