aboutsummaryrefslogtreecommitdiff
path: root/tools/lldb-mi/MICmnLLDBUtilSBValue.cpp
blob: 004be071260ae5bcbfda10a2b7273892b0288894 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//===-- MICmnLLDBUtilSBValue.cpp --------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

//++
// File:		MICmnLLDBUtilSBValue.cpp
//
// Overview:	CMICmnLLDBUtilSBValue implementation.
//
// Environment:	Compilers:	Visual C++ 12.
//							gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
//				Libraries:	See MIReadmetxt. 
//
// Copyright:	None.
//--

// In-house headers:
#include "MICmnLLDBUtilSBValue.h"
#include "MIUtilString.h"
#include "MICmnLLDBDebugSessionInfo.h"

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnLLDBUtilSBValue constructor.
// Type:	Method.
// Args:	vrValue				- (R) The LLDb value object.
//			vbHandleCharType	- (R) True = Yes return text molding to char type, 
//									  False = just return data.
// Return:	None.
// Throws:	None.
//--
CMICmnLLDBUtilSBValue::CMICmnLLDBUtilSBValue( const lldb::SBValue & vrValue, const bool vbHandleCharType /* = false */ )
:	m_rValue( const_cast< lldb::SBValue & >( vrValue ) )
,	m_pUnkwn( "??" )
,	m_bHandleCharType( vbHandleCharType )
{
	m_bValidSBValue = m_rValue.IsValid();
}

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnLLDBUtilSBValue destructor.
// Type:	Method.
// Args:	None.
// Return:	None.
// Throws:	None.
//--
CMICmnLLDBUtilSBValue::~CMICmnLLDBUtilSBValue( void )
{
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve from the LLDB SB Value object the name of the variable. If the name
//			is invalid (or the SBValue object invalid) then "??" is returned.
// Type:	Method.
// Args:	None.
// Return:	CMIUtilString	- Name of the variable or "??" for unknown.
// Throws:	None.
//--
CMIUtilString CMICmnLLDBUtilSBValue::GetName( void ) const
{
	const MIchar * pName = m_bValidSBValue ? m_rValue.GetName() : nullptr;
	const CMIUtilString text( (pName != nullptr) ? pName : m_pUnkwn );
				
	return text;
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve from the LLDB SB Value object the value of the variable described in
//			text. If the value is invalid (or the SBValue object invalid) then "??" is 
//			returned.
// Type:	Method.
// Args:	None.
// Return:	CMIUtilString	- Text description of the variable's value or "??".
// Throws:	None.
//--
CMIUtilString CMICmnLLDBUtilSBValue::GetValue( void ) const
{
	CMIUtilString text; 

	if( m_bHandleCharType && IsCharType() )
	{
		const lldb::addr_t addr = m_rValue.GetLoadAddress();
		text = CMIUtilString::Format( "0x%08x", addr );
		const CMIUtilString cString( GetValueCString() );
		if( !cString.empty() )
			text += CMIUtilString::Format( " %s", cString.c_str() );
	}
	else
	{
		const MIchar * pValue = m_bValidSBValue ? m_rValue.GetValue() : nullptr;
		text = (pValue != nullptr) ? pValue : m_pUnkwn;
	}
	
	return text;
}

//++ ------------------------------------------------------------------------------------
// Details:	If the LLDB SB Value object is a char type then form the text data string
//			otherwise return nothing. m_bHandleCharType must be true to return text data
//			if any.
// Type:	Method.
// Args:	None.
// Return:	CMIUtilString	- Text description of the variable's value.
// Throws:	None.
//--
CMIUtilString CMICmnLLDBUtilSBValue::GetValueCString( void ) const
{
	CMIUtilString text; 

	if( m_bHandleCharType && IsCharType() )
	{
		text = ReadCStringFromHostMemory( m_rValue );
	}
	
	return text;
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the flag stating whether this value object is a char type or some
//			other type. Char type can be signed or unsigned.
// Type:	Method.
// Args:	None.
// Return:	bool	- True = Yes is a char type, false = some other type.
// Throws:	None.
//--
bool CMICmnLLDBUtilSBValue::IsCharType( void ) const
{
	const MIchar * pName = m_rValue.GetName(); MIunused( pName );
	const lldb::BasicType eType = m_rValue.GetType().GetBasicType();
	return ((eType == lldb::eBasicTypeChar) || 
		    (eType == lldb::eBasicTypeSignedChar) || 
			(eType == lldb::eBasicTypeUnsignedChar) );
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the flag stating whether any child value object of *this object is a 
//			char type or some other type. Returns false if there are not children. Char 
//			type can be signed or unsigned.
// Type:	Method.
// Args:	None.
// Return:	bool	- True = Yes is a char type, false = some other type.
// Throws:	None.
//--
bool CMICmnLLDBUtilSBValue::IsChildCharType( void ) const
{
	const MIuint nChildren = m_rValue.GetNumChildren();
	
	// Is it a basic type
	if( nChildren == 0 )
		return false;

	// Is it a composite type
	if( nChildren > 1 )
		return false;

	lldb::SBValue member = m_rValue.GetChildAtIndex( 0 );
	const CMICmnLLDBUtilSBValue utilValue( member );
	return utilValue.IsCharType();			
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the C string data for a child of char type (one and only child) for
//			the parent value object. If the child is not a char type or the parent has
//			more than one child then an empty string is returned. Char type can be 
//			signed or unsigned.
// Type:	Method.
// Args:	None.
// Return:	CMIUtilString	- Text description of the variable's value.
// Throws:	None.
//--
CMIUtilString CMICmnLLDBUtilSBValue::GetChildValueCString( void ) const
{
	CMIUtilString text;
	const MIuint nChildren = m_rValue.GetNumChildren();
	
	// Is it a basic type
	if( nChildren == 0 )
		return text;

	// Is it a composite type
	if( nChildren > 1 )
		return text;

	lldb::SBValue member = m_rValue.GetChildAtIndex( 0 );
	const CMICmnLLDBUtilSBValue utilValue( member );
	if( m_bHandleCharType && utilValue.IsCharType() )
	{
		text = ReadCStringFromHostMemory( member );
	}

	return text;
}
	
//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the C string data of value object by read the memory where the 
//			variable is held. 
// Type:	Method.
// Args:	vrValueObj	- (R) LLDB SBValue variable object.
// Return:	CMIUtilString	- Text description of the variable's value.
// Throws:	None.
//--
CMIUtilString CMICmnLLDBUtilSBValue::ReadCStringFromHostMemory( const lldb::SBValue & vrValueObj ) const
{
	CMIUtilString text;

	lldb::SBValue & rValue = const_cast< lldb::SBValue & >( vrValueObj );
	const lldb::addr_t addr = rValue.GetLoadAddress();
	CMICmnLLDBDebugSessionInfo & rSessionInfo( CMICmnLLDBDebugSessionInfo::Instance() ); 
	const MIuint nBytes( 128 );
	const MIchar * pBufferMemory = new MIchar[ nBytes ];
	lldb::SBError error;
	const MIuint64 nReadBytes = rSessionInfo.m_lldbProcess.ReadMemory( addr, (void *) pBufferMemory, nBytes, error ); MIunused( nReadBytes );
	text = CMIUtilString::Format( "\\\"%s\\\"", pBufferMemory );
	delete [] pBufferMemory;
	
	return text;
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the state of the value object's name. 
// Type:	Method.
// Args:	None.
// Return:	bool	- True = yes name is indeterminate, false = name is valid.
// Throws:	None.
//--
bool CMICmnLLDBUtilSBValue::IsNameUnknown( void ) const
{
	const CMIUtilString name( GetName() );
	return (name == m_pUnkwn);
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the state of the value object's value data. 
// Type:	Method.
// Args:	None.
// Return:	bool	- True = yes value is indeterminate, false = value valid.
// Throws:	None.
//--
bool CMICmnLLDBUtilSBValue::IsValueUnknown( void ) const
{
	const CMIUtilString value( GetValue() );
	return (value == m_pUnkwn);
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the value object's type name if valid.
// Type:	Method.
// Args:	None.
// Return:	CMIUtilString	- The type name or "??".
// Throws:	None.
//--
CMIUtilString CMICmnLLDBUtilSBValue::GetTypeName( void ) const
{
	const MIchar * pName = m_bValidSBValue ? m_rValue.GetTypeName() : nullptr;
	const CMIUtilString text( (pName != nullptr) ? pName : m_pUnkwn );
				
	return text;
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the value object's display type name if valid. 
// Type:	Method.
// Args:	None.
// Return:	CMIUtilString	- The type name or "??".
// Throws:	None.
//--
CMIUtilString CMICmnLLDBUtilSBValue::GetTypeNameDisplay( void ) const
{
	const MIchar * pName = m_bValidSBValue ? m_rValue.GetDisplayTypeName() : nullptr;
	const CMIUtilString text( (pName != nullptr) ? pName : m_pUnkwn );
				
	return text;
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve whether the value object's is valid or not. 
// Type:	Method.
// Args:	None.
// Return:	bool	- True = valid, false = not valid.
// Throws:	None.
//--
bool CMICmnLLDBUtilSBValue::IsValid( void ) const
{
	return m_bValidSBValue;
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the value object' has a name. A value object can be valid but still
//			have no name which suggest it is not a variable. 
// Type:	Method.
// Args:	None.
// Return:	bool	- True = valid, false = not valid.
// Throws:	None.
//--
bool CMICmnLLDBUtilSBValue::HasName( void ) const
{
	bool bHasAName = false;

	const MIchar * pName = m_bValidSBValue ? m_rValue.GetDisplayTypeName() : nullptr;
	if( pName != nullptr )
	{
		bHasAName = (CMIUtilString( pName ).length() > 0);
	}

	return bHasAName;
}

//++ ------------------------------------------------------------------------------------
// Details:	Determine if the value object' respresents a LLDB variable i.e. "$0".
// Type:	Method.
// Args:	None.
// Return:	bool	- True = Yes LLDB variable, false = no.
// Throws:	None.
//--
bool CMICmnLLDBUtilSBValue::IsLLDBVariable( void ) const
{
	return (GetName().at( 0 ) == '$' );
}