pytypeinfo.h 15.4 KB
Newer Older
1 2
#pragma  once

3 4
#include <comutil.h>

5
#include "kdlib/typeinfo.h"
6

7
#include "pythreadstate.h"
8
#include "dbgexcept.h"
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

namespace pykd {

inline kdlib::MEMOFFSET_64 getSymbolOffset( const std::wstring &name )
{
    AutoRestorePyState  pystate;
    return kdlib::getSymbolOffset(name);
}

std::wstring findSymbol(  kdlib::MEMOFFSET_64 offset, bool showDisplacement = true );

python::tuple findSymbolAndDisp( ULONG64 offset );

inline size_t getSymbolSize( const std::wstring &name )
{
    AutoRestorePyState  pystate;
    return kdlib::getSymbolSize(name);
}

inline kdlib::TypeInfoPtr defineStruct( const std::wstring &structName, size_t align = 0 ) 
{
    AutoRestorePyState  pystate;
    return kdlib::defineStruct(structName, align);
}

34
inline kdlib::TypeInfoPtr defineUnion( const std::wstring& unionName, size_t align = 0 )
35 36
{
    AutoRestorePyState  pystate;
37
    return kdlib::defineUnion(unionName, align);
38 39 40
}


41 42 43 44 45 46
inline kdlib::TypeInfoPtr defineFunction( const kdlib::TypeInfoPtr& returnType, kdlib::CallingConventionType callconv = kdlib::CallConv_NearC)
{
    AutoRestorePyState  pystate;
    return kdlib::defineFunction(returnType, callconv);
}

47 48
python::object callFunctionByVar( python::tuple& args, python::dict& kwargs );

49 50
python::object callFunctionByOffset( python::tuple& args, python::dict& kwargs);

51 52
python::object callFunctionRaw( python::tuple& args, python::dict& kwargs);

53

54 55 56 57 58 59
inline kdlib::TypeInfoPtr getTypeInfoByName( const std::wstring &name )
{
    AutoRestorePyState  pystate;
    return kdlib::loadType( name );
}

60 61 62 63 64 65 66 67 68 69 70 71
inline kdlib::TypeInfoPtr getTypeFromSource( const std::wstring& sourceCode, const std::wstring& typeName, const std::wstring& compileOptions=L"")
{
    AutoRestorePyState  pystate;
    return kdlib::compileType( sourceCode, typeName, compileOptions);
}

inline kdlib::TypeInfoProviderPtr getTypeInfoProviderFromSource(const std::wstring& sourceCode, const std::wstring& compileOptions=L"")
{
    AutoRestorePyState  pystate;
    return kdlib::getTypeInfoProviderFromSource(sourceCode, compileOptions);
}

ussrhero's avatar
ussrhero committed
72
inline kdlib::SymbolProviderPtr getSymbolProviderFromSource(const std::wstring& sourceCode, const std::wstring& compileOptions = L"")
73 74
{
    AutoRestorePyState  pystate;
ussrhero's avatar
ussrhero committed
75
    return kdlib::getSymbolProviderFromSource(sourceCode, compileOptions);
76 77
}

78 79 80 81 82 83
inline kdlib::TypeInfoProviderPtr getTypeInfoProviderFromPdb(const std::wstring&  fileName, kdlib::MEMOFFSET_64 offset = 0UL)
{
    AutoRestorePyState  pystate;
    return kdlib::getTypeInfoProviderFromPdb(fileName, offset);
}

84 85 86 87 88 89 90 91
struct TypeInfoAdapter : public kdlib::TypeInfo {

    static std::wstring getName( kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getName();
    }

ussrhero's avatar
ussrhero committed
92 93 94 95 96 97
    static std::wstring getScopeName( kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getScopeName();
    }

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
    static size_t getSize( kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getSize();
    }

    static kdlib::BITOFFSET getBitOffset( kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getBitOffset();
    }

    static kdlib::BITOFFSET getBitWidth( kdlib::TypeInfo &typeInfo ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.getBitWidth();
    }

    static kdlib::TypeInfoPtr getBitType( kdlib::TypeInfo &typeInfo ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.getBitType();
    }

    static size_t getElementCount( kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getElementCount();
    }

    static kdlib::MEMOFFSET_32 getElementOffset( kdlib::TypeInfo &typeInfo, const std::wstring &name ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.getElementOffset( name );
    }

134 135 136 137 138 139
    static kdlib::MEMOFFSET_32 getElementOffsetByIndex(kdlib::TypeInfo &typeInfo, size_t index)
    {
        AutoRestorePyState  pystate;
        return typeInfo.getElementOffset(index);
    }

140 141 142 143 144 145 146 147 148 149 150 151
    static std::wstring getElementName( kdlib::TypeInfo &typeInfo, size_t index ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.getElementName(index);
    }

    static kdlib::MEMOFFSET_64 getStaticOffset( kdlib::TypeInfo &typeInfo, const std::wstring &name )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getElementVa( name );
    }

152 153 154 155 156 157 158 159 160 161 162
    static bool isStaticField(const kdlib::TypeInfoPtr &typeInfo, const std::wstring &name)
    {
        AutoRestorePyState  pystate;
        return typeInfo->isStaticMember(name);
    }

    static bool isStaticFieldByIndex(const kdlib::TypeInfoPtr &typeInfo, size_t index)
    {
        AutoRestorePyState  pystate;
        return typeInfo->isStaticMember(index);
    }
163

164 165 166 167 168 169 170 171 172 173 174 175 176

    static bool isConstField(const kdlib::TypeInfoPtr &typeInfo, const std::wstring &name)
    {
        AutoRestorePyState  pystate;
        return typeInfo->isConstMember(name);
    }

    static bool isConstFieldByIndex(const kdlib::TypeInfoPtr &typeInfo, size_t index)
    {
        AutoRestorePyState  pystate;
        return typeInfo->isConstMember(index);
    }

177 178 179 180 181 182
    static kdlib::TypeInfoPtr getElementByName( kdlib::TypeInfo &typeInfo, const std::wstring &name ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.getElement(name);
    }

ussrhero's avatar
ussrhero committed
183 184 185 186
    static bool hasField(kdlib::TypeInfoPtr &typeInfo, const std::wstring &name);

    static bool hasMethod(kdlib::TypeInfoPtr &typeInfo, const std::wstring &fieldName);

187
    static kdlib::TypeInfoPtr getElementAttr(kdlib::TypeInfo &typeInfo, const std::wstring &name);
188

189 190
    static kdlib::TypeInfoPtr getElementByKey(kdlib::TypeInfo &typeInfo, const std::wstring &name);

191 192 193 194 195 196
    static kdlib::TypeInfoPtr getElementByIndex( kdlib::TypeInfo &typeInfo, size_t index )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getElement(index);
    }

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    static size_t getMethodsCount( kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getMethodsCount();
    }

    static kdlib::TypeInfoPtr getMethodByName( kdlib::TypeInfo &typeInfo, const std::wstring& methodName)
    {
        AutoRestorePyState  pystate;
        return typeInfo.getMethod(methodName);
    }

    static kdlib::TypeInfoPtr getMethodByIndex( kdlib::TypeInfo &typeInfo, size_t index )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getMethod(index);
    }

215 216 217 218 219 220
    static std::wstring getMethodName(kdlib::TypeInfo &typeInfo, size_t index)
    {
        AutoRestorePyState  pystate;
        return typeInfo.getMethodName(index);
    }

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    static size_t getBaseClassesCount(kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getBaseClassesCount();
    }

    static kdlib::TypeInfoPtr getBaseClassByName(kdlib::TypeInfo &typeInfo, const std::wstring&  className)
    {
        AutoRestorePyState  pystate;
        return typeInfo.getBaseClass(className);
    }
       
    static kdlib::TypeInfoPtr getBaseClassByIndex(kdlib::TypeInfo &typeInfo, size_t index)
    {
        AutoRestorePyState  pystate;
        return typeInfo.getBaseClass(index);
    }

239 240 241 242 243 244 245 246 247 248 249 250
    static kdlib::MEMOFFSET_32 getBaseClassOffsetByName(kdlib::TypeInfo &typeInfo, const std::wstring&  className)
    {
        AutoRestorePyState  pystate;
        return typeInfo.getBaseClassOffset(className);
    }

    static kdlib::MEMOFFSET_32 getBaseClassOffsetByIndex(kdlib::TypeInfo &typeInfo, size_t index)
    {
        AutoRestorePyState  pystate;
        return typeInfo.getBaseClassOffset(index);
    }

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
    static kdlib::TypeInfoPtr ptrTo( kdlib::TypeInfo &typeInfo, size_t ptrSize = 0 ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.ptrTo(ptrSize);
    }

    static kdlib::TypeInfoPtr deref( kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.deref();
    }

    static kdlib::TypeInfoPtr arrayOf( kdlib::TypeInfo &typeInfo, size_t size ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.arrayOf(size);
    }

    static bool isArray( kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.isArray();
    }

    static bool isPointer( kdlib::TypeInfo &typeInfo ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.isPointer();
    }

    static bool isVoid( kdlib::TypeInfo &typeInfo ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.isVoid();
    }

    static bool isBase( kdlib::TypeInfo &typeInfo ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.isBase();
    }

    static bool isUserDefined( kdlib::TypeInfo &typeInfo ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.isUserDefined();
    }

    static bool isConstant( kdlib::TypeInfo &typeInfo ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.isConstant();
    }

    static bool isEnum( kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.isEnum();
    }

    static bool isBitField( kdlib::TypeInfo &typeInfo ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.isBitField();
    }

    static bool isFunction( kdlib::TypeInfo &typeInfo ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.isFunction();
    }
322 323 324 325 326 327

    static bool isVtbl(kdlib::TypeInfo &typeInfo)
    {
        AutoRestorePyState  pystate;
        return typeInfo.isVtbl();
    }
328 329 330 331 332 333 334

    static bool isNoType(kdlib::TypeInfo &typeInfo)
    {
        AutoRestorePyState  pystate;
        return typeInfo.isNoType();
    }

335 336 337 338 339 340
    static bool isTemplate(const kdlib::TypeInfoPtr &typeInfo)
    {
        AutoRestorePyState  pystate;
        return typeInfo->isTemplate();
    }

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    static void appendField( kdlib::TypeInfo &typeInfo, const std::wstring &fieldName, kdlib::TypeInfoPtr &fieldType )
    {
        AutoRestorePyState  pystate;
        typeInfo.appendField( fieldName, fieldType );
    }

    static kdlib::CallingConventionType getCallingConvention( kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getCallingConvention();
    }

    static kdlib::TypeInfoPtr getClassParent( kdlib::TypeInfo &typeInfo )
    {
        AutoRestorePyState  pystate;
        return typeInfo.getClassParent();
    }

    static std::wstring str( kdlib::TypeInfo &typeInfo ) 
    {
        AutoRestorePyState  pystate;
        return typeInfo.str();
    }

365 366 367
    static python::list getFields( const kdlib::TypeInfoPtr &typeInfo );

    static python::list getMembers(const kdlib::TypeInfoPtr &typeInfo);
368

369 370
    static python::list getMethods(kdlib::TypeInfo &typeInfo);

371 372
    static python::list getElementDir(kdlib::TypeInfo &typeInfo);

373 374
    static python::list getBaseClasses(kdlib::TypeInfo &typeInfo);

375 376
    static python::list getTemplateArgs(const kdlib::TypeInfoPtr &typeInfo);

377 378 379 380
    static bool isZero(kdlib::TypeInfo &typeInfo) {
        return false;
    }

ussrhero's avatar
ussrhero committed
381 382 383 384
    static bool isNotZero(kdlib::TypeInfo &typeInfo) {
        return true;
    }

ussrhero's avatar
ussrhero committed
385
    static bool hasFieldOrMethod(kdlib::TypeInfoPtr& typedVar, const std::wstring& name);
386

387 388
};

389

ussrhero's avatar
ussrhero committed
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
class TypeInfoProviderIterator {

public:

    TypeInfoProviderIterator(kdlib::TypeInfoEnumeratorPtr typeInfoEnum) :
        m_typeInfoEnum(typeInfoEnum)
    {}

    static python::object self(const python::object& obj)
    {
        return obj;
    }

    kdlib::TypeInfoPtr next()
    {
        AutoRestorePyState  pystate;

        kdlib::TypeInfoPtr  typeInfo = m_typeInfoEnum->Next();

        if (!typeInfo)
            throw StopIteration("No more data.");

        return typeInfo;
    }

    
private:

    kdlib::TypeInfoEnumeratorPtr   m_typeInfoEnum;

};

422 423 424 425 426 427 428 429
struct TypeInfoProviderAdapter : public kdlib::TypeInfoProvider
{

    static kdlib::TypeInfoPtr getTypeByName( kdlib::TypeInfoProvider &typeInfoProvider, const std::wstring& name)
    {
        AutoRestorePyState  pystate;
        return typeInfoProvider.getTypeByName(name);
    }
ussrhero's avatar
ussrhero committed
430 431 432 433 434 435 436 437 438 439 440 441 442

    static TypeInfoProviderIterator* getTypeIter( kdlib::TypeInfoProvider &typeInfoProvider )
    {
        return new TypeInfoProviderIterator( typeInfoProvider.getTypeEnumerator() );
    };

    static TypeInfoProviderIterator* getTypeIterWithMask( kdlib::TypeInfoProvider &typeInfoProvider, const std::wstring& mask)
    {
        return new TypeInfoProviderIterator( typeInfoProvider.getTypeEnumerator(mask) );
    }

    static kdlib::TypeInfoPtr getTypeAsAttr(kdlib::TypeInfoProvider &typeInfoProvider, const std::wstring& name);

443 444
};

445 446
struct SymbolEnumeratorAdapter
{
ussrhero's avatar
ussrhero committed
447 448 449 450
    SymbolEnumeratorAdapter(const kdlib::SymbolEnumeratorPtr&  symEnum)
        : m_symEnum(symEnum)
    {}

451

ussrhero's avatar
ussrhero committed
452
    static python::object getIter(const python::object& obj)
453
    {
ussrhero's avatar
ussrhero committed
454
        return obj;
455 456
    }

ussrhero's avatar
ussrhero committed
457
    python::tuple next()
458
    {
ussrhero's avatar
ussrhero committed
459 460 461
        std::wstring name;
        kdlib::MEMOFFSET_64  offset;
        kdlib::TypeInfoPtr  typeInfo;
462

ussrhero's avatar
ussrhero committed
463 464 465 466 467 468 469 470 471 472
        {
            AutoRestorePyState  pystate;

             if (!m_symEnum->Next())
                throw StopIteration("No more data.");

             name = m_symEnum->getName();
             offset = m_symEnum->getOffset();
             typeInfo = m_symEnum->getType();
        }
473

ussrhero's avatar
ussrhero committed
474
        return python::make_tuple(name, offset, typeInfo);
475
    }
ussrhero's avatar
ussrhero committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

private:

    kdlib::SymbolEnumeratorPtr  m_symEnum;
};

struct SymbolProviderAdapter
{
    static SymbolEnumeratorAdapter* getIter(const kdlib::SymbolProviderPtr &symProvider)
    {
        return new SymbolEnumeratorAdapter(symProvider->getSymbolEnumerator());
    };

    static SymbolEnumeratorAdapter* getIterWithMask(const kdlib::SymbolProviderPtr &symProvider, const std::wstring& mask)
    {
        return new SymbolEnumeratorAdapter(symProvider->getSymbolEnumerator(mask));
    }
493 494
};

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
struct BaseTypesEnum {
    static kdlib::TypeInfoPtr getUInt1B() { return pykd::getTypeInfoByName(L"UInt1B"); }
    static kdlib::TypeInfoPtr getUInt2B() { return pykd::getTypeInfoByName(L"UInt2B");  }
    static kdlib::TypeInfoPtr getUInt4B() { return pykd::getTypeInfoByName(L"UInt4B");  }
    static kdlib::TypeInfoPtr getUInt8B() { return pykd::getTypeInfoByName(L"UInt8B");  }
    static kdlib::TypeInfoPtr getInt1B() { return pykd::getTypeInfoByName(L"Int1B"); }
    static kdlib::TypeInfoPtr getInt2B() { return pykd::getTypeInfoByName(L"Int2B"); }
    static kdlib::TypeInfoPtr getInt4B() { return pykd::getTypeInfoByName(L"Int4B"); }
    static kdlib::TypeInfoPtr getInt8B() { return pykd::getTypeInfoByName(L"Int8B"); }
    static kdlib::TypeInfoPtr getLong() { return pykd::getTypeInfoByName(L"Long"); }
    static kdlib::TypeInfoPtr getULong() { return pykd::getTypeInfoByName(L"ULong"); }
    static kdlib::TypeInfoPtr getBool() { return pykd::getTypeInfoByName(L"Bool"); }
    static kdlib::TypeInfoPtr getChar() { return pykd::getTypeInfoByName(L"Char"); }
    static kdlib::TypeInfoPtr getWChar() { return pykd::getTypeInfoByName(L"WChar"); }
    static kdlib::TypeInfoPtr getVoidPtr() { return pykd::getTypeInfoByName(L"Void*"); }
510 511
    static kdlib::TypeInfoPtr getFloat() { return pykd::getTypeInfoByName(L"Float"); }
    static kdlib::TypeInfoPtr getDouble() { return pykd::getTypeInfoByName(L"Double"); }
512 513 514
};

} // end namespace pykd