summaryrefslogtreecommitdiffstats
path: root/lldb/tools/lldb-mi/MICmnLLDBDebugSessionInfoVarObj.cpp
blob: d519875993fe4a23ece1ddf6acff3664047c11e0 (plain)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//===-- MICmnLLDBDebugSessionInfoVarObj.cpp ---------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

//++
// File:		MICmnLLDBDebugSessionInfoVarObj.cpp
//
// Overview:	CMICmnLLDBDebugSessionInfoVarObj 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 "MICmnLLDBDebugSessionInfoVarObj.h"
#include "MICmnLLDBProxySBValue.h"
#include "MICmnLLDBUtilSBValue.h"

// Instantiations:
const MIchar * CMICmnLLDBDebugSessionInfoVarObj::ms_aVarFormatStrings[] =
{
	// CODETAG_SESSIONINFO_VARFORMAT_ENUM
	// *** Order is import here.
	"<Invalid var format>",
	"binary",
	"octal",
	"decimal",
	"hexadecimal",
	"natural"
};
const MIchar * CMICmnLLDBDebugSessionInfoVarObj::ms_aVarFormatChars[] =
{
	// CODETAG_SESSIONINFO_VARFORMAT_ENUM
	// *** Order is import here.
	"<Invalid var format>",
	"t",
	"o",
	"d",
	"x",
	"N"
};
CMICmnLLDBDebugSessionInfoVarObj::MapKeyToVarObj_t	CMICmnLLDBDebugSessionInfoVarObj::ms_mapVarIdToVarObj;
MIuint												CMICmnLLDBDebugSessionInfoVarObj::ms_nVarUniqueId = 0; // Index from 0

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnLLDBDebugSessionInfoVarObj constructor.
// Type:	Method.
// Args:	None.
// Return:	None.
// Throws:	None.
//--
CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj( void )
:	m_eVarFormat( eVarFormat_Natural )
,	m_eVarType( eVarType_Internal )
{
	// Do not call UpdateValue() in here as not necessary
}

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnLLDBDebugSessionInfoVarObj constructor.
// Type:	Method.
// Args:	vrStrNameReal	- (R) The actual name of the variable, the expression.
//			vrStrName		- (R) The name given for *this var object.
//			vrValue			- (R) The LLDB SBValue object represented by *this object.
// Return:	None.
// Throws:	None.
//--
CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj( const CMIUtilString & vrStrNameReal, const CMIUtilString & vrStrName, const lldb::SBValue & vrValue )
:	m_eVarFormat( eVarFormat_Natural )
,	m_eVarType( eVarType_Internal )
,	m_strName( vrStrName )
,	m_SBValue( vrValue )
,	m_strNameReal( vrStrNameReal )
{
	UpdateValue();
}

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnLLDBDebugSessionInfoVarObj constructor.
// Type:	Method.
// Args:	vrStrNameReal			- (R) The actual name of the variable, the expression.
//			vrStrName				- (R) The name given for *this var object.
//			vrValue					- (R) The LLDB SBValue object represented by *this object.
//			vrStrVarObjParentName	- (R) The var object parent to *this var object (LLDB SBValue equivalent).
// Return:	None.
// Throws:	None.
//--
CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj( const CMIUtilString & vrStrNameReal, const CMIUtilString & vrStrName, const lldb::SBValue & vrValue, const CMIUtilString & vrStrVarObjParentName )
:	m_eVarFormat( eVarFormat_Natural )
,	m_eVarType( eVarType_Internal )
,	m_strName( vrStrName )
,	m_SBValue( vrValue )
,	m_strNameReal( vrStrNameReal )
,	m_strVarObjParentName( vrStrVarObjParentName )
{
	UpdateValue();
}

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnLLDBDebugSessionInfoVarObj copy constructor.
// Type:	Method.
// Args:	vrOther	- (R) The object to copy from.
// Return:	None.
// Throws:	None.
//--
CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj( const CMICmnLLDBDebugSessionInfoVarObj & vrOther )
{
	CopyOther( vrOther );
}

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnLLDBDebugSessionInfoVarObj copy constructor.
// Type:	Method.
// Args:	vrOther	- (R) The object to copy from.
// Return:	None.
// Throws:	None.
//--
CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj( CMICmnLLDBDebugSessionInfoVarObj & vrOther )
{
	CopyOther( vrOther );
}

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnLLDBDebugSessionInfoVarObj move constructor.
// Type:	Method.
// Args:	vrwOther	- (R) The object to copy from.
// Return:	None.
// Throws:	None.
//--
CMICmnLLDBDebugSessionInfoVarObj::CMICmnLLDBDebugSessionInfoVarObj( CMICmnLLDBDebugSessionInfoVarObj && vrwOther )
{
	MoveOther( vrwOther );
}

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnLLDBDebugSessionInfoVarObj assignment operator.
// Type:	Method.
// Args:	vrOther	- (R) The object to copy from.
// Return:	CMICmnLLDBDebugSessionInfoVarObj & - Updated *this object.
// Throws:	None.
//--
CMICmnLLDBDebugSessionInfoVarObj & CMICmnLLDBDebugSessionInfoVarObj::operator= ( const CMICmnLLDBDebugSessionInfoVarObj & vrOther )
{
	CopyOther( vrOther );

	return *this;
}

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnLLDBDebugSessionInfoVarObj assignment operator.
// Type:	Method.
// Args:	vrwOther	- (R) The object to copy from.
// Return:	CMICmnLLDBDebugSessionInfoVarObj & - Updated *this object.
// Throws:	None.
//--
CMICmnLLDBDebugSessionInfoVarObj & CMICmnLLDBDebugSessionInfoVarObj::operator= ( CMICmnLLDBDebugSessionInfoVarObj && vrwOther )
{
	MoveOther( vrwOther );

	return *this;
}

//++ ------------------------------------------------------------------------------------
// Details:	Copy the other instance of that object to *this object.
// Type:	Method.
// Args:	vrOther	- (R) The object to copy from.
// Return:	MIstatus::success - Functional succeeded.
//			MIstatus::failure - Functional failed.
// Throws:	None.
//--
bool CMICmnLLDBDebugSessionInfoVarObj::CopyOther( const CMICmnLLDBDebugSessionInfoVarObj & vrOther )
{
	// Check for self-assignment
	if( this == &vrOther )
		return MIstatus::success;

	m_eVarFormat = vrOther.m_eVarFormat;
	m_eVarType = vrOther.m_eVarType;
	m_strName = vrOther.m_strName;
	m_SBValue = vrOther.m_SBValue;
	m_strNameReal = vrOther.m_strNameReal;
	m_strFormattedValue = vrOther.m_strFormattedValue;
	m_strVarObjParentName = vrOther.m_strVarObjParentName;

	return MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details:	Move that object to *this object.
// Type:	Method.
// Args:	vrwOther	- (RW) The object to copy from.
// Return:	MIstatus::success - Functional succeeded.
//			MIstatus::failure - Functional failed.
// Throws:	None.
//--
bool CMICmnLLDBDebugSessionInfoVarObj::MoveOther( CMICmnLLDBDebugSessionInfoVarObj & vrwOther )
{
	// Check for self-assignment
	if( this == &vrwOther )
		return MIstatus::success;

	CopyOther( vrwOther );
	vrwOther.m_eVarFormat = eVarFormat_Natural;
	vrwOther.m_eVarType = eVarType_Internal;
	vrwOther.m_strName.clear();
	vrwOther.m_SBValue.Clear();
	vrwOther.m_strNameReal.clear();
	vrwOther.m_strFormattedValue.clear();
	vrwOther.m_strVarObjParentName.clear();

	return MIstatus::success;
}

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

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the var format enumeration for the specified string.
// Type:	Static method.
// Args:	vrStrFormat	- (R) Text description of the var format.
// Return:	varFormat_e	- Var format enumeration.
//						- No match found return eVarFormat_Invalid.
// Throws:	None.
//--
CMICmnLLDBDebugSessionInfoVarObj::varFormat_e CMICmnLLDBDebugSessionInfoVarObj::GetVarFormatForString( const CMIUtilString & vrStrFormat )
{
	// CODETAG_SESSIONINFO_VARFORMAT_ENUM
	for( MIuint i = 0; i < eVarFormat_count; i++ )
	{
		const MIchar * pVarFormatString = ms_aVarFormatStrings[ i ];
		if( vrStrFormat == pVarFormatString )
			return static_cast< varFormat_e >( i );
	}

	return eVarFormat_Invalid;
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the var format enumeration for the specified character.
// Type:	Static method.
// Args:	vrcFormat	- (R) Character representing the var format.
// Return:	varFormat_e	- Var format enumeration.
//						- No match found return eVarFormat_Invalid.
// Throws:	None.
//--
CMICmnLLDBDebugSessionInfoVarObj::varFormat_e CMICmnLLDBDebugSessionInfoVarObj::GetVarFormatForChar( const MIchar & vrcFormat )
{
	if( 'r' == vrcFormat )
		return eVarFormat_Hex;

	// CODETAG_SESSIONINFO_VARFORMAT_ENUM
	for( MIuint i = 0; i < eVarFormat_count; i++ )
	{
		const MIchar * pVarFormatChar = ms_aVarFormatChars[ i ];
		if( *pVarFormatChar == vrcFormat )
			return static_cast< varFormat_e >( i );
	}

	return eVarFormat_Invalid;
}

//++ ------------------------------------------------------------------------------------
// Details:	Return the equivalent var value formatted string for the given value type.
//			The SBValue vrValue parameter is checked by LLDB private code for valid 
//			scalar type via MI Driver proxy function as the valued returned can also be
//			an error condition. The proxy function determines if the check was valid 
//			otherwise return an error condition state by other means saying so.
// Type:	Static method.
// Args:	vrValue		- (R) The var value object.
//			veVarFormat	- (R) Var format enumeration.
// Returns:	CMIUtilString	- Value formatted string.
// Throws:	None.
//--
CMIUtilString CMICmnLLDBDebugSessionInfoVarObj::GetValueStringFormatted( const lldb::SBValue & vrValue, const CMICmnLLDBDebugSessionInfoVarObj::varFormat_e veVarFormat )
{
	CMIUtilString strFormattedValue;
	
	MIuint64 nValue = 0;
	if( CMICmnLLDBProxySBValue::GetValueAsUnsigned( vrValue, nValue ) == MIstatus::success )
	{
		lldb::SBValue & rValue = const_cast< lldb::SBValue & >( vrValue );
		strFormattedValue = GetStringFormatted( nValue, rValue.GetValue(), veVarFormat );
	}
	else
	{
		// Composite variable type i.e. struct
		strFormattedValue = "{...}";
	}
	
	return strFormattedValue;
}

//++ ------------------------------------------------------------------------------------
// Details:	Return nuber formatted string according to the given value type.
// Type:	Static method.
// Args:	vnValue				- (R) The number value to get formatted.
//			vpStrValueNatural	- (R) The natural representation of the number value.
//			veVarFormat			- (R) Var format enumeration.
// Returns:	CMIUtilString	- Numerical formatted string.
// Throws:	None.
//--
CMIUtilString CMICmnLLDBDebugSessionInfoVarObj::GetStringFormatted( const MIuint64 vnValue, const MIchar * vpStrValueNatural, const CMICmnLLDBDebugSessionInfoVarObj::varFormat_e veVarFormat )
{
	CMIUtilString strFormattedValue;
	
	switch( veVarFormat )
	{
	case eVarFormat_Binary:
		strFormattedValue = CMIUtilString::FormatBinary( vnValue );
		break;
	case eVarFormat_Octal:
		strFormattedValue = CMIUtilString::Format( "0%llo", vnValue );
		break;
	case eVarFormat_Decimal:
		strFormattedValue = CMIUtilString::Format( "%lld", vnValue );
		break;
	case eVarFormat_Hex:
		strFormattedValue = CMIUtilString::Format( "0x%llx", vnValue );
		break;
	case eVarFormat_Natural:
	default:
	{
		strFormattedValue = (vpStrValueNatural != nullptr) ? vpStrValueNatural : "";
	}
	}

	return strFormattedValue;
}

//++ ------------------------------------------------------------------------------------
// Details:	Delete internal container contents.
// Type:	Static method.
// Args:	None.
// Returns:	None.
// Throws:	None.
//--
void CMICmnLLDBDebugSessionInfoVarObj::VarObjClear( void )
{
	ms_mapVarIdToVarObj.clear();
}

//++ ------------------------------------------------------------------------------------
// Details:	Add a var object to the internal container.
// Type:	Static method.
// Args:	vrVarObj	- (R) The var value object.
// Returns:	None.
// Throws:	None.
//--
void CMICmnLLDBDebugSessionInfoVarObj::VarObjAdd( const CMICmnLLDBDebugSessionInfoVarObj & vrVarObj )
{
	VarObjDelete( vrVarObj.GetName() );
	MapPairKeyToVarObj_t pr( vrVarObj.GetName(), vrVarObj );
	ms_mapVarIdToVarObj.insert( pr );
}
	
//++ ------------------------------------------------------------------------------------
// Details:	Delete the var object from the internal container matching the specified name.
// Type:	Static method.
// Args:	vrVarName	- (R) The var value name.
// Returns:	None.
// Throws:	None.
//--
void CMICmnLLDBDebugSessionInfoVarObj::VarObjDelete( const CMIUtilString & vrVarName )
{
	const MapKeyToVarObj_t::const_iterator it = ms_mapVarIdToVarObj.find( vrVarName );
	if( it != ms_mapVarIdToVarObj.end() )
	{
		ms_mapVarIdToVarObj.erase( it );
	}
}

//++ ------------------------------------------------------------------------------------
// Details:	Update an existing var object in the internal container.
// Type:	Static method.
// Args:	vrVarObj	- (R) The var value object.
// Returns:	None.
// Throws:	None.
//--
void CMICmnLLDBDebugSessionInfoVarObj::VarObjUpdate( const CMICmnLLDBDebugSessionInfoVarObj & vrVarObj )
{
	VarObjAdd( vrVarObj );
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the var object matching the specified name.
// Type:	Static method.
// Args:	vrVarName	- (R) The var value name.
//			vrwVarObj	- (W) A var object.
// Returns:	bool	- True = object found, false = object not found.
// Throws:	None.
//--
bool CMICmnLLDBDebugSessionInfoVarObj::VarObjGet( const CMIUtilString & vrVarName, CMICmnLLDBDebugSessionInfoVarObj & vrwVarObj )
{
	const MapKeyToVarObj_t::const_iterator it = ms_mapVarIdToVarObj.find( vrVarName );
	if( it != ms_mapVarIdToVarObj.end() )
	{
		const CMICmnLLDBDebugSessionInfoVarObj & rVarObj = (*it).second;
		vrwVarObj = rVarObj;
		return true;
	}

	return false;
}

//++ ------------------------------------------------------------------------------------
// Details:	A count is kept of the number of var value objects created. This is count is
//			used to ID the var value object. Reset the count to 0.
// Type:	Static method.
// Args:	None.
// Returns:	None.
// Throws:	None.
//--
void CMICmnLLDBDebugSessionInfoVarObj::VarObjIdResetToZero( void )
{
	ms_nVarUniqueId = 0;
}

//++ ------------------------------------------------------------------------------------
// Details:	A count is kept of the number of var value objects created. This is count is
//			used to ID the var value object. Increment the count by 1.
// Type:	Static method.
// Args:	None.
// Returns:	None.
// Throws:	None.
//--
void CMICmnLLDBDebugSessionInfoVarObj::VarObjIdInc( void )
{
	ms_nVarUniqueId++;
}

//++ ------------------------------------------------------------------------------------
// Details:	A count is kept of the number of var value objects created. This is count is
//			used to ID the var value object. Retrieve ID.
// Type:	Static method.
// Args:	None.
// Returns:	None.
// Throws:	None.
//--
MIuint CMICmnLLDBDebugSessionInfoVarObj::VarObjIdGet( void )
{
	return ms_nVarUniqueId;
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the value formatted object's name.
// Type:	Method.
// Args:	None.
// Returns:	CMIUtilString &	- Value's var%u name text.
// Throws:	None.
//--
const CMIUtilString & CMICmnLLDBDebugSessionInfoVarObj::GetName( void ) const
{
	return m_strName;
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the value formatted object's variable name as given in the MI command
//			to create the var object.
// Type:	Method.
// Args:	None.
// Returns:	CMIUtilString &	- Value's real name text.
// Throws:	None.
//--
const CMIUtilString & CMICmnLLDBDebugSessionInfoVarObj::GetNameReal( void ) const
{
	return m_strNameReal;
}
	
//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the value formatted string.
// Type:	Method.
// Args:	None.
// Returns:	CMIUtilString &	- Value formatted string.
// Throws:	None.
//--
const CMIUtilString & CMICmnLLDBDebugSessionInfoVarObj::GetValueFormatted( void ) const
{
	return m_strFormattedValue;
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the LLDB Value object.
// Type:	Method.
// Args:	None.
// Returns:	lldb::SBValue &	- LLDB Value object.
// Throws:	None.
//--
const lldb::SBValue & CMICmnLLDBDebugSessionInfoVarObj::GetValue( void ) const
{
	return m_SBValue;
}

//++ ------------------------------------------------------------------------------------
// Details:	Set the var format type for *this object and upate the formatting.
// Type:	Method.
// Args:	None.
// Return:	MIstatus::success - Functional succeeded.
//			MIstatus::failure - Functional failed.
// Throws:	None.
//--
bool CMICmnLLDBDebugSessionInfoVarObj::SetVarFormat( const varFormat_e veVarFormat )
{
	if( veVarFormat >= eVarFormat_count )
		return MIstatus::failure;

	m_eVarFormat = veVarFormat;
	UpdateValue();
	return MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details:	Update *this var obj. Update it's value and type.
// Type:	Method.
// Args:	None.
// Returns:	None.
// Throws:	None.
//--
void CMICmnLLDBDebugSessionInfoVarObj::UpdateValue( void )
{
	m_strFormattedValue = CMICmnLLDBDebugSessionInfoVarObj::GetValueStringFormatted( m_SBValue, m_eVarFormat );

	MIuint64 nValue = 0;
	if( CMICmnLLDBProxySBValue::GetValueAsUnsigned( m_SBValue, nValue ) == MIstatus::failure )
		m_eVarType = eVarType_Composite;

	CMICmnLLDBDebugSessionInfoVarObj::VarObjUpdate( *this );
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the enumeration type of the var object.
// Type:	Method.
// Args:	None.
// Returns:	varType_e	- Enumeration value.
// Throws:	None.
//--
CMICmnLLDBDebugSessionInfoVarObj::varType_e CMICmnLLDBDebugSessionInfoVarObj::GetType( void ) const
{
	return m_eVarType;
}
	
//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the parent var object's name, the parent var object  to *this var 
//			object (if assigned). The parent is equivalent to LLDB SBValue variable's 
//			parent.
// Type:	Method.
// Args:	None.
// Returns:	CMIUtilString &	- Pointer to var object, NULL = no parent.
// Throws:	None.
//--
const CMIUtilString & CMICmnLLDBDebugSessionInfoVarObj::GetVarParentName( void ) const
{
	return m_strVarObjParentName;
}
	
OpenPOWER on IntegriCloud