11
typedef struct _LIST_ENTRY {
12
struct _LIST_ENTRY *Flink;
13
struct _LIST_ENTRY *Blink;
19
class TypeInfoTest( unittest.TestCase ):
22
self.assertEqual( "structTest", pykd.typeInfo( "structTest" ).name() )
23
self.assertEqual( "structTest", pykd.typeInfo( target.moduleName + "!structTest" ).name() )
24
self.assertEqual( "structTest", pykd.typeInfo( "g_structTest" ).name() )
25
self.assertEqual( "structTest", pykd.typeInfo( target.moduleName + "!g_structTest" ).name() )
26
self.assertEqual( "Int1B", pykd.typeInfo( "Int1B" ).name() )
28
def testCreateByName( self ):
29
""" creating typeInfo by the type name """
30
self.assertEqual( "Int4B*", target.module.type("Int4B*").name() )
31
self.assertEqual( "structTest", target.module.type( "structTest" ).name() )
32
self.assertEqual( "structTest**", target.module.type( "structTest**" ).name() )
33
self.assertEqual( "Int4B[2][3]", target.module.type("Int4B[2][3]").name() )
34
self.assertEqual( "Int4B(*[4])[2][3]", target.module.type("Int4B(*[4])[2][3]").name() )
35
self.assertEqual( "Int4B(*)[2][3]", target.module.type("Int4B((*))[2][3]").name() )
36
self.assertEqual( "Int4B*", pykd.typeInfo("Int4B*").name() )
38
def testCreateBySymbol(self):
39
""" creating typeInfo by the symbol name """
40
self.assertEqual( "structTest[2]", target.module.type("g_testArray").name() )
41
self.assertEqual( "Int4B[2][3]", target.module.type("intMatrix").name() )
42
self.assertEqual( "structTest*", target.module.type("g_structTestPtr").name() )
43
self.assertEqual( "structTest**", target.module.type("g_structTestPtrPtr").name() )
44
self.assertEqual( "Char*[2]", target.module.type("strArray").name() )
45
self.assertEqual( "Char*(*)[2]", target.module.type("ptrStrArray").name() )
46
self.assertEqual( "Int4B(*[4])[2][3]", target.module.type("arrIntMatrixPtrs").name() )
47
self.assertEqual( "Int4B(*)[2][3]", target.module.type("ptrIntMatrix").name() )
49
def testGetField( self ):
50
""" get field of the complex type """
51
ti1 = target.module.type( "structTest" )
52
self.assertTrue( "UInt4B", ti1.m_field0.name() )
53
self.assertTrue( "m_field0" in ti1 )
54
self.assertFalse( "not_exist" in ti1)
55
self.assertRaises( AttributeError, lambda t: t.not_exists, ti1)
58
def testBaseTypes( self ):
59
self.assertEqual("Int1B", pykd.typeInfo( "Int1B" ).name() )
60
self.assertEqual("Int2B", pykd.typeInfo( "Int2B" ).name() )
61
self.assertEqual("Int4B", pykd.typeInfo( "Int4B" ).name() )
62
self.assertEqual("Int8B", pykd.typeInfo( "Int8B" ).name() )
63
self.assertEqual("UInt1B", pykd.typeInfo( "UInt1B" ).name() )
64
self.assertEqual("UInt2B", pykd.typeInfo( "UInt2B" ).name() )
65
self.assertEqual("UInt4B", pykd.typeInfo( "UInt4B" ).name() )
66
self.assertEqual("UInt8B", pykd.typeInfo( "UInt8B" ).name() )
68
self.assertEqual("Long", pykd.typeInfo( "Long" ).name() )
69
self.assertEqual("ULong", pykd.typeInfo( "ULong" ).name() )
70
self.assertEqual("Bool", pykd.typeInfo( "Bool" ).name() )
71
self.assertEqual("Char", pykd.typeInfo("Char").name() )
72
self.assertEqual("WChar", pykd.typeInfo("WChar").name() )
74
self.assertEqual( 1, pykd.typeInfo("Int1B").size() )
75
self.assertEqual( 1, pykd.typeInfo("UInt1B").size() )
76
self.assertEqual( 2, pykd.typeInfo("Int2B").size() )
77
self.assertEqual( 2, pykd.typeInfo("UInt2B").size() )
78
self.assertEqual( 4, pykd.typeInfo("Int4B").size() )
79
self.assertEqual( 4, pykd.typeInfo("UInt4B").size() )
80
self.assertEqual( 8, pykd.typeInfo("Int8B").size() )
81
self.assertEqual( 8, pykd.typeInfo("UInt8B").size() )
83
self.assertEqual( 4, pykd.typeInfo("Long" ).size() )
84
self.assertEqual( 4, pykd.typeInfo("ULong" ).size() )
85
self.assertEqual( 1, pykd.typeInfo("Bool" ).size() )
86
self.assertEqual( 1, pykd.typeInfo("Char").size() )
87
self.assertEqual( 2, pykd.typeInfo("WChar").size() )
90
self.assertEqual("Int9B", pykd.typeInfo( "Int9B" ).name() )
91
except pykd.SymbolException:
94
def testBaseTypePtr(self):
95
self.assertEqual("Int1B*", pykd.typeInfo( "Int1B*" ).name() )
96
self.assertEqual("Int1B", pykd.typeInfo( "Int1B*" ).deref().name() )
98
def testBaseTypeArray(self):
99
self.assertEqual("Int4B[20]", pykd.typeInfo( "Int4B[20]" ).name() )
101
def testName( self ):
102
ti1 = target.module.type( "classChild" )
103
self.assertEqual( "classChild", ti1.name() )
104
self.assertEqual( "Int4B", ti1.m_childField.name() )
105
self.assertEqual( "structTest", ti1.m_childField3.name() )
106
self.assertEqual( "structTest", target.module.type("g_structTest").name() )
108
def testVarName( self ):
109
self.assertEqual( "structTest", target.module.type( "g_structTest").name() )
110
self.assertRaises( pykd.TypeException, target.module.type, "g_testArray[0]" )
111
self.assertRaises( pykd.TypeException, target.module.type, "*g_structTestPtr" )
113
def testOffset( self ):
114
ti1 = target.module.type( "structTest" )
115
self.assertEqual( 0, ti1.fieldOffset("m_field0") )
116
self.assertEqual( 4, ti1.fieldOffset("m_field1") )
117
self.assertEqual( 12, ti1.fieldOffset("m_field2") )
118
self.assertEqual( 14, ti1.fieldOffset("m_field3") )
120
ti2 = target.module.type( "unionTest" )
121
self.assertEqual( 0, ti2.fieldOffset("m_value") )
122
self.assertEqual( 0, ti2.fieldOffset("m_structValue") )
124
ti3 = target.module.type( "structWithNested" )
125
self.assertEqual( ti3.fieldOffset( "m_unnameStruct"), ti3.fieldOffset( "m_unnameStruct.m_field2" ) )
127
def testSize( self ):
128
ti1 = target.module.type( "structTest" )
129
self.assertEqual( 16 + pykd.ptrSize(), ti1.size() )
130
self.assertEqual( pykd.ptrSize(), target.module.type("structTest**").size() )
131
self.assertEqual( pykd.sizeof("structTest"), target.module.type("structTest").size() )
132
self.assertEqual( pykd.sizeof("structTest**"), target.module.type("structTest**").size() )
133
self.assertEqual( pykd.sizeof("Int1B"), pykd.typeInfo("Int1B").size() )
135
def testBitField( self ):
136
ti = target.module.type( "g_structWithBits" )
137
self.assertEqual( 0, ti.fieldOffset("m_bit6_8") )
138
self.assertEqual( 4, ti.m_bit6_8.size() )
139
self.assertEqual( "UInt4B:3", ti.m_bit6_8.name() )
140
self.assertEqual( 3, ti.m_bit6_8.bitWidth() )
141
self.assertEqual( 6, ti.m_bit6_8.bitOffset() )
144
ti = target.module.type("enumType")
145
self.assertTrue( hasattr( ti, "TWO" ) )
146
self.assertEqual( 4, ti.TWO.size() )
148
ti = target.module.type("classChild")
149
self.assertEqual( "enumType", ti.m_enumField.name() )
152
self.assertEqual( "UInt8B*", target.module.type( "pbigValue" ).name() )
153
self.assertEqual( "structTest*", target.module.type( "structTest*" ).name() )
154
self.assertEqual( "UInt2B*", pykd.typeInfo( "UInt2B*" ).name() )
155
self.assertEqual( "Void*", target.module.type( "voidPtr" ).name() )
156
self.assertEqual( "Void*[3]", target.module.type( "voidPtrArray" ).name())
161
ti = target.module.type("unionTest")
162
self.assertEqual( 0, ti.fieldOffset("m_doubleValue") )
163
self.assertEqual( 0, ti.fieldOffset("m_value") )
164
self.assertEqual( 0, ti.fieldOffset("m_structValue") )
165
self.assertEqual( ti.size(), ti.m_structValue.size() )
168
ti = pykd.typeInfo("Int1B*")
169
self.assertEqual( "Int1B", ti.deref().name() )
171
ti = target.module.type("structTest*")
172
self.assertEqual( "structTest", ti.deref().name() )
174
ti = pykd.typeInfo("structTest[2]")
175
self.assertEqual( "structTest", ti.deref().name() )
177
ti = target.module.type("classChild")
178
self.assertRaises( pykd.TypeException, ti.deref )
180
def testNestedStruct( self ):
181
ti = target.module.type("structWithNested")
182
self.assertTrue( ti.hasField("m_field"))
183
self.assertTrue( ti.hasField("m_field3"))
184
self.assertTrue( ti.hasField("m_unnameStruct"))
185
self.assertTrue( ti.m_unnameStruct.hasField("m_field2"))
186
self.assertFalse( ti.hasField( "m_field2" ) )
187
self.assertFalse( ti.hasField( "m_nestedFiled" ) )
188
ti = target.module.type("structWithNested::Nested")
189
self.assertTrue( ti.hasField( "m_nestedFiled" ) )
192
self.assertTrue( str(target.module.type( "ucharVar" ) ) )
193
self.assertTrue( str(target.module.type( "ushortVar" ) ) )
194
self.assertTrue( str(target.module.type( "ulongVar" ) ) )
195
self.assertTrue( str(target.module.type( "ulonglongVar" ) ) )
196
self.assertTrue( str(target.module.type( "g_structWithBits" ) ) )
197
self.assertTrue( str(target.module.type( "g_structTest" ) ) )
198
self.assertTrue( str(target.module.type( "g_structTest1" ) ) )
199
self.assertTrue( str(target.module.type( "g_testArray" ) ) )
200
self.assertTrue( str(target.module.type( "g_structTestPtr" ) ) )
201
self.assertTrue( str(target.module.type( "g_structTestPtrPtr" ) ) )
202
self.assertTrue( str(target.module.type( "longlongArray" ) ) )
203
self.assertTrue( str(target.module.type( "intMatrix4" ) ) )
204
self.assertTrue( str(target.module.type( "ptrIntMatrix" ) ) )
205
self.assertTrue( str(target.module.type( "g_classChild" ) ) )
211
def testTypedef(self):
212
self.assertEqual( "structTest", pykd.typeInfo( "g_structTypeDef" ).name() )
213
self.assertEqual( "structTest", pykd.typeInfo( "structTestTypeDef" ).name() )
215
def testStaticField(self):
216
ti = pykd.typeInfo( "g_classChild" )
217
self.assertNotEqual( 0, ti.staticOffset( "m_staticField" ) )
218
self.assertNotEqual( 0, ti.staticOffset("m_stdstr") )
219
if not ti.staticOffset("m_staticConst"):
220
self.assertFalse( "MS DIA bug: https://connect.microsoft.com/VisualStudio/feedback/details/737430" )
222
def testVfnTable(self):
223
ti = pykd.typeInfo( "g_classChild" )
224
self.assertTrue( hasattr( ti, "__VFN_table" ) )
226
def testUdtSubscribe(self):
227
ti = pykd.typeInfo( "g_virtChild" )
228
self.assertEqual( 6, len(ti) )
232
def testEnumSubscribe(self):
233
ti = pykd.typeInfo( "enumType" )
234
self.assertEqual( 3, len(ti) )
235
self.assertEqual( [ 1, 2, 3], [ int(field) for field in ti ] )
236
self.assertEqual( [ ( "ONE", 1), ("TWO", 2), ("THREE", 3) ], ti.fields() )
238
def testStructNullSize(self):
239
ti = target.module.type("structNullSize")
240
self.assertEqual( 0, len(ti) )
242
def testDerefName(self):
243
entry = pykd.typedVar("g_listHead").flink
244
self.assertEqual( "listEntry*", entry.type().name() )
247
ti = pykd.typeInfo("UInt8B").ptrTo()
248
self.assertTrue( "UInt8B*", ti.name() )
249
self.assertNotEqual( 0, ti.size() )
251
def testArrayOf(self):
252
ti = pykd.typeInfo("UInt8B").arrayOf(10)
253
self.assertTrue( "UInt8B[10]", ti.name() )
255
def testCompareWithNone(self):
256
ti = pykd.typeInfo("UInt8B")
257
self.assertFalse(ti == None)
258
self.assertTrue(ti != None)
259
self.assertFalse(not ti)
262
def testFunction(self):
263
functype = target.module.typedVar( "CdeclFuncPtr" ).type().deref()
264
self.assertTrue( functype.isFunction() )
266
functype = target.module.typedVar( "g_variadicFuncPtr" ).type().deref()
267
self.assertTrue( functype.isFunction() )
269
def testFunctionArgs(self):
270
functype = target.module.typedVar( "CdeclFuncPtr" ).type().deref()
271
self.assertEqual( [ arg.name() for arg in functype ], ["Int4B", "Float"] )
273
functype = target.module.typedVar( "g_variadicFuncPtr" ).type().deref()
274
self.assertEqual( [ arg.name() for arg in functype ], ["Int4B", "NoType"] )
276
def testFunctionCallConv(self):
277
functype = target.module.typedVar( "CdeclFuncPtr" ).type().deref()
278
self.assertEqual( functype.getCallingConvention(), pykd.callingConvention.NearC )
280
def testFunctionThis(self):
281
functype = target.module.typedVar( "MethodPtr" ).type().deref()
282
self.assertEqual( [ arg.name() for arg in functype ], ["FuncTestClass*"] )
284
functype = target.module.typedVar( "CdeclStaticMethodPtr" ).type().deref()
285
self.assertEqual( [ arg.name() for arg in functype ], [] )
287
def testFunctionName(self):
288
functype = target.module.typedVar( "CdeclFuncPtr" ).type().deref()
289
self.assertEqual(functype.name(), "Void(__cdecl)(Int4B, Float)")
291
functype = target.module.typedVar( "MethodPtr" ).type().deref()
292
self.assertEqual(functype.name(), "Void(__thiscall FuncTestClass::)()")
294
functype = target.module.typedVar( "ArrayOfCdeclFuncPtr" ).type()[0].deref()
295
self.assertEqual(functype.name(), "Void(__cdecl)(Int4B, Float)")
297
functype = target.module.typedVar( "ArrayOfMethodPtr" ).type()[0].deref()
298
self.assertEqual(functype.name(), "Void(__thiscall FuncTestClass::)()")
300
functype = target.module.typedVar( "g_variadicFuncPtr" ).type().deref()
301
self.assertEqual(functype.name(), "Void(__cdecl)(Int4B, ...)")
303
def testFunctionPtrName(self):
304
funcptrtype = target.module.typedVar( "CdeclFuncPtr" ).type()
305
self.assertEqual(funcptrtype.name(), "Void(__cdecl*)(Int4B, Float)")
307
functype = target.module.typedVar( "MethodPtr" ).type()
308
self.assertEqual(functype.name(), "Void(__thiscall FuncTestClass::*)()")
310
funcptrtype = target.module.typedVar( "g_variadicFuncPtr" ).type()
311
self.assertEqual(funcptrtype.name(), "Void(__cdecl*)(Int4B, ...)")
313
def testFunctionArrName(self):
314
funcptrtype = target.module.typedVar( "ArrayOfCdeclFuncPtr" ).type()
315
self.assertEqual(funcptrtype.name(), "Void(__cdecl*[3])(Int4B, Float)")
317
functype = target.module.typedVar( "ArrayOfMethodPtr" ).type()
318
self.assertEqual(functype.name(), "Void(__thiscall FuncTestClass::*[2])()")
321
ti = target.module.type("structTest")
322
self.assertEqual(5, len(dir(ti)))
323
self.assertTrue("m_field3" in dir(ti))
324
self.assertFalse("m_field33" in dir(ti))
326
def testEnumTypes(self):
327
lst = target.module.enumTypes()
328
self.assertNotEqual([], lst)
329
lst = target.module.enumTypes("structTest")
330
self.assertEqual(["structTest"], lst)
331
lst = target.module.enumTypes("NonExsistType")
332
self.assertEqual([],lst)
334
def testArrayOverflow(self):
335
self.assertRaises(pykd.TypeException, pykd.baseTypes.UInt8B.arrayOf, 0xFFFFFFFFFFFFFFFF)
337
def testMethodCount(self):
338
self.assertEqual( 14, target.module.type("classChild").getNumberMethods() )
340
def testGetMethod(self):
341
self.assertEqual( "Int4B(__thiscall classChild::)(Int4B)", target.module.type("classChild").method("childMethod").name() )
342
self.assertEqual( "Int4B(__thiscall classChild::)(Int4B)", target.module.type("classChild").childMethod.name() )
343
self.assertEqual( "Int4B(__thiscall classChild::)(Int4B)", target.module.type("classChild").method(1).name() )
345
def testMethods(self):
346
self.assertEqual( 14, len(target.module.type("classChild").methods()))
348
def testGetBaseClass(self):
349
classChild = target.module.type("classChild")
350
self.assertEqual( ["classBase1", "classBase2"], [ classChild.baseClass(i).name() for i in range(classChild.getNumberBaseClasses()) ] )
351
self.assertEqual( ["classBase1", "classBase2"], [ name for name, _, _ in classChild.baseClasses()] )
353
def testGetBaseClassOffset(self):
354
classChild = target.module.type("classChild")
355
self.assertEqual( classChild.baseClassOffset(0), classChild.baseClassOffset('classBase1'))
356
self.assertEqual(classChild.baseClassOffset(1), classChild.baseClassOffset('classBase2'))
358
def testPdbTypeProvider(self):
359
pdb = target.module.symfile()
360
typeProvider = pykd.getTypeInfoProviderFromPdb(pdb)
361
self.assertEqual("structTest", typeProvider.getTypeByName("structTest").name())
362
self.assertEqual("structTest", typeProvider.structTest.name())
363
self.assertEqual(15, len(list(typeProvider.typeIterator("*struct*"))))
365
def testScopeName(self):
366
self.assertEqual( target.module.name(), pykd.typeInfo( "structTest" ).scopeName() )
367
self.assertEqual( target.module.name(), pykd.typeInfo( "structWithNested::Nested" ).scopeName() )
369
def testContain(self):
370
ti = target.module.type( "structTest" )
371
self.assertTrue("m_field1" in ti)
372
self.assertFalse("NotExist" in ti)
373
self.assertRaises(Exception, lambda t : 2 in t, ti)
375
def testTemplateType(self):
376
ti = target.module.type("g_testTemplateTwoTypes")
377
self.assertTrue(ti.isTemplate)
378
self.assertEqual(['int', 'TestClassTemplate<int>'], ti.getTemplateArgs() )
380
def testMembers(self):
382
['__VFN_table', '__VFN_table', 'm_staticConst', 'm_staticField', 'm_childField',
383
'm_childField2', 'm_childField3', 'm_enumField'],
384
[ member[0] for member in pykd.typeInfo( "classChild" ).members() ])
386
def testIsStaticField(self):
387
ti = pykd.typeInfo("classChild")
388
self.assertTrue(ti.isStaticField("m_staticField"))
389
self.assertFalse(ti.isStaticField("m_baseField"))
391
def testIsConstField(self):
392
ti = pykd.typeInfo("classChild")
393
self.assertTrue(ti.isStaticField("m_staticConst"))
394
self.assertFalse(ti.isConstField("m_staticConst"))
404
def testClangCompile(self):
405
typesProvider = pykd.getTypeInfoProviderFromSource(typesSourceCode)
406
ti = typesProvider.getTypeByName('_LIST_ENTRY')
407
self.assertEqual (ti.name(), '_LIST_ENTRY')
408
self.assertEqual (ti.Flink.name(), '_LIST_ENTRY*')
409
self.assertEqual (ti.Flink.deref().name(), '_LIST_ENTRY')
410
self.assertEqual (ti.Blink.name(), '_LIST_ENTRY*')
411
self.assertEqual (ti.Blink.deref().name(), '_LIST_ENTRY')
413
(typesProvider, outMsg) = pykd.getTypeInfoProviderFromSourceEx('1; abc; ', "-w")
414
self.assertTrue ('abc' in outMsg)