]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmnLLDBUtilSBValue.cpp
Update LLDB snapshot to upstream r225923 (git 2b588ecd)
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmnLLDBUtilSBValue.cpp
1 //===-- MICmnLLDBUtilSBValue.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:        MICmnLLDBUtilSBValue.cpp
12 //
13 // Overview:    CMICmnLLDBUtilSBValue 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 "MICmnLLDBUtilSBValue.h"
24 #include "MIUtilString.h"
25 #include "MICmnLLDBDebugSessionInfo.h"
26
27 //++ ------------------------------------------------------------------------------------
28 // Details: CMICmnLLDBUtilSBValue constructor.
29 // Type:    Method.
30 // Args:    vrValue             - (R) The LLDb value object.
31 //          vbHandleCharType    - (R) True = Yes return text molding to char type,
32 //                                    False = just return data.
33 // Return:  None.
34 // Throws:  None.
35 //--
36 CMICmnLLDBUtilSBValue::CMICmnLLDBUtilSBValue(const lldb::SBValue &vrValue, const bool vbHandleCharType /* = false */)
37     : m_rValue(const_cast<lldb::SBValue &>(vrValue))
38     , m_pUnkwn("??")
39     , m_bHandleCharType(vbHandleCharType)
40 {
41     m_bValidSBValue = m_rValue.IsValid();
42 }
43
44 //++ ------------------------------------------------------------------------------------
45 // Details: CMICmnLLDBUtilSBValue destructor.
46 // Type:    Method.
47 // Args:    None.
48 // Return:  None.
49 // Throws:  None.
50 //--
51 CMICmnLLDBUtilSBValue::~CMICmnLLDBUtilSBValue(void)
52 {
53 }
54
55 //++ ------------------------------------------------------------------------------------
56 // Details: Retrieve from the LLDB SB Value object the name of the variable. If the name
57 //          is invalid (or the SBValue object invalid) then "??" is returned.
58 // Type:    Method.
59 // Args:    None.
60 // Return:  CMIUtilString   - Name of the variable or "??" for unknown.
61 // Throws:  None.
62 //--
63 CMIUtilString
64 CMICmnLLDBUtilSBValue::GetName(void) const
65 {
66     const MIchar *pName = m_bValidSBValue ? m_rValue.GetName() : nullptr;
67     const CMIUtilString text((pName != nullptr) ? pName : m_pUnkwn);
68
69     return text;
70 }
71
72 //++ ------------------------------------------------------------------------------------
73 // Details: Retrieve from the LLDB SB Value object the value of the variable described in
74 //          text. If the value is invalid (or the SBValue object invalid) then "??" is
75 //          returned.
76 // Type:    Method.
77 // Args:    None.
78 // Return:  CMIUtilString   - Text description of the variable's value or "??".
79 // Throws:  None.
80 //--
81 CMIUtilString
82 CMICmnLLDBUtilSBValue::GetValue(void) const
83 {
84     CMIUtilString text;
85
86     if (m_bHandleCharType && IsCharType())
87     {
88         const lldb::addr_t addr = m_rValue.GetLoadAddress();
89         text = CMIUtilString::Format("0x%08x", addr);
90         const CMIUtilString cString(GetValueCString());
91         if (!cString.empty())
92             text += CMIUtilString::Format(" %s", cString.c_str());
93     }
94     else
95     {
96         const MIchar *pValue = m_bValidSBValue ? m_rValue.GetValue() : nullptr;
97         text = (pValue != nullptr) ? pValue : m_pUnkwn;
98     }
99
100     return text;
101 }
102
103 //++ ------------------------------------------------------------------------------------
104 // Details: If the LLDB SB Value object is a char type then form the text data string
105 //          otherwise return nothing. m_bHandleCharType must be true to return text data
106 //          if any.
107 // Type:    Method.
108 // Args:    None.
109 // Return:  CMIUtilString   - Text description of the variable's value.
110 // Throws:  None.
111 //--
112 CMIUtilString
113 CMICmnLLDBUtilSBValue::GetValueCString(void) const
114 {
115     CMIUtilString text;
116
117     if (m_bHandleCharType && IsCharType())
118     {
119         text = ReadCStringFromHostMemory(m_rValue);
120     }
121
122     return text;
123 }
124
125 //++ ------------------------------------------------------------------------------------
126 // Details: Retrieve the flag stating whether this value object is a char type or some
127 //          other type. Char type can be signed or unsigned.
128 // Type:    Method.
129 // Args:    None.
130 // Return:  bool    - True = Yes is a char type, false = some other type.
131 // Throws:  None.
132 //--
133 bool
134 CMICmnLLDBUtilSBValue::IsCharType(void) const
135 {
136     const MIchar *pName = m_rValue.GetName();
137     MIunused(pName);
138     const lldb::BasicType eType = m_rValue.GetType().GetBasicType();
139     return ((eType == lldb::eBasicTypeChar) || (eType == lldb::eBasicTypeSignedChar) || (eType == lldb::eBasicTypeUnsignedChar));
140 }
141
142 //++ ------------------------------------------------------------------------------------
143 // Details: Retrieve the flag stating whether any child value object of *this object is a
144 //          char type or some other type. Returns false if there are not children. Char
145 //          type can be signed or unsigned.
146 // Type:    Method.
147 // Args:    None.
148 // Return:  bool    - True = Yes is a char type, false = some other type.
149 // Throws:  None.
150 //--
151 bool
152 CMICmnLLDBUtilSBValue::IsChildCharType(void) const
153 {
154     const MIuint nChildren = m_rValue.GetNumChildren();
155
156     // Is it a basic type
157     if (nChildren == 0)
158         return false;
159
160     // Is it a composite type
161     if (nChildren > 1)
162         return false;
163
164     lldb::SBValue member = m_rValue.GetChildAtIndex(0);
165     const CMICmnLLDBUtilSBValue utilValue(member);
166     return utilValue.IsCharType();
167 }
168
169 //++ ------------------------------------------------------------------------------------
170 // Details: Retrieve the C string data for a child of char type (one and only child) for
171 //          the parent value object. If the child is not a char type or the parent has
172 //          more than one child then an empty string is returned. Char type can be
173 //          signed or unsigned.
174 // Type:    Method.
175 // Args:    None.
176 // Return:  CMIUtilString   - Text description of the variable's value.
177 // Throws:  None.
178 //--
179 CMIUtilString
180 CMICmnLLDBUtilSBValue::GetChildValueCString(void) const
181 {
182     CMIUtilString text;
183     const MIuint nChildren = m_rValue.GetNumChildren();
184
185     // Is it a basic type
186     if (nChildren == 0)
187         return text;
188
189     // Is it a composite type
190     if (nChildren > 1)
191         return text;
192
193     lldb::SBValue member = m_rValue.GetChildAtIndex(0);
194     const CMICmnLLDBUtilSBValue utilValue(member);
195     if (m_bHandleCharType && utilValue.IsCharType())
196     {
197         text = ReadCStringFromHostMemory(member);
198     }
199
200     return text;
201 }
202
203 //++ ------------------------------------------------------------------------------------
204 // Details: Retrieve the C string data of value object by read the memory where the
205 //          variable is held.
206 // Type:    Method.
207 // Args:    vrValueObj  - (R) LLDB SBValue variable object.
208 // Return:  CMIUtilString   - Text description of the variable's value.
209 // Throws:  None.
210 //--
211 CMIUtilString
212 CMICmnLLDBUtilSBValue::ReadCStringFromHostMemory(const lldb::SBValue &vrValueObj) const
213 {
214     CMIUtilString text;
215
216     lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValueObj);
217     const lldb::addr_t addr = rValue.GetLoadAddress();
218     CMICmnLLDBDebugSessionInfo &rSessionInfo(CMICmnLLDBDebugSessionInfo::Instance());
219     const MIuint nBytes(128);
220     const MIchar *pBufferMemory = new MIchar[nBytes];
221     lldb::SBError error;
222     const MIuint64 nReadBytes = rSessionInfo.m_lldbProcess.ReadMemory(addr, (void *)pBufferMemory, nBytes, error);
223     MIunused(nReadBytes);
224     text = CMIUtilString::Format("\\\"%s\\\"", pBufferMemory);
225     delete[] pBufferMemory;
226
227     return text;
228 }
229
230 //++ ------------------------------------------------------------------------------------
231 // Details: Retrieve the state of the value object's name.
232 // Type:    Method.
233 // Args:    None.
234 // Return:  bool    - True = yes name is indeterminate, false = name is valid.
235 // Throws:  None.
236 //--
237 bool
238 CMICmnLLDBUtilSBValue::IsNameUnknown(void) const
239 {
240     const CMIUtilString name(GetName());
241     return (name == m_pUnkwn);
242 }
243
244 //++ ------------------------------------------------------------------------------------
245 // Details: Retrieve the state of the value object's value data.
246 // Type:    Method.
247 // Args:    None.
248 // Return:  bool    - True = yes value is indeterminate, false = value valid.
249 // Throws:  None.
250 //--
251 bool
252 CMICmnLLDBUtilSBValue::IsValueUnknown(void) const
253 {
254     const CMIUtilString value(GetValue());
255     return (value == m_pUnkwn);
256 }
257
258 //++ ------------------------------------------------------------------------------------
259 // Details: Retrieve the value object's type name if valid.
260 // Type:    Method.
261 // Args:    None.
262 // Return:  CMIUtilString   - The type name or "??".
263 // Throws:  None.
264 //--
265 CMIUtilString
266 CMICmnLLDBUtilSBValue::GetTypeName(void) const
267 {
268     const MIchar *pName = m_bValidSBValue ? m_rValue.GetTypeName() : nullptr;
269     const CMIUtilString text((pName != nullptr) ? pName : m_pUnkwn);
270
271     return text;
272 }
273
274 //++ ------------------------------------------------------------------------------------
275 // Details: Retrieve the value object's display type name if valid.
276 // Type:    Method.
277 // Args:    None.
278 // Return:  CMIUtilString   - The type name or "??".
279 // Throws:  None.
280 //--
281 CMIUtilString
282 CMICmnLLDBUtilSBValue::GetTypeNameDisplay(void) const
283 {
284     const MIchar *pName = m_bValidSBValue ? m_rValue.GetDisplayTypeName() : nullptr;
285     const CMIUtilString text((pName != nullptr) ? pName : m_pUnkwn);
286
287     return text;
288 }
289
290 //++ ------------------------------------------------------------------------------------
291 // Details: Retrieve whether the value object's is valid or not.
292 // Type:    Method.
293 // Args:    None.
294 // Return:  bool    - True = valid, false = not valid.
295 // Throws:  None.
296 //--
297 bool
298 CMICmnLLDBUtilSBValue::IsValid(void) const
299 {
300     return m_bValidSBValue;
301 }
302
303 //++ ------------------------------------------------------------------------------------
304 // Details: Retrieve the value object' has a name. A value object can be valid but still
305 //          have no name which suggest it is not a variable.
306 // Type:    Method.
307 // Args:    None.
308 // Return:  bool    - True = valid, false = not valid.
309 // Throws:  None.
310 //--
311 bool
312 CMICmnLLDBUtilSBValue::HasName(void) const
313 {
314     bool bHasAName = false;
315
316     const MIchar *pName = m_bValidSBValue ? m_rValue.GetDisplayTypeName() : nullptr;
317     if (pName != nullptr)
318     {
319         bHasAName = (CMIUtilString(pName).length() > 0);
320     }
321
322     return bHasAName;
323 }
324
325 //++ ------------------------------------------------------------------------------------
326 // Details: Determine if the value object' respresents a LLDB variable i.e. "$0".
327 // Type:    Method.
328 // Args:    None.
329 // Return:  bool    - True = Yes LLDB variable, false = no.
330 // Throws:  None.
331 //--
332 bool
333 CMICmnLLDBUtilSBValue::IsLLDBVariable(void) const
334 {
335     return (GetName().at(0) == '$');
336 }