pykd
1
2import pykd3
4#it's exact name of the std::string class. We need it later.
5stringClassName = "basic_string<char,std::char_traits<char>,std::allocator<char> >"6fullStringClassName = "std::" + stringClassName7
8# get a malloc function. May be we have not its prototype in pdb file, so we need to define prototype manually
9PVoid = pykd.typeInfo("Void*")10size_t = pykd.typeInfo("Int8B") if pykd.getCPUMode() == pykd.CPUType.AMD64 else pykd.typeInfo("Int4B")11mallocProto = pykd.defineFunction( PVoid, pykd.callingConvention.NearC )12mallocProto.append("size", size_t)13
14malloc = pykd.typedVar(mallocProto, pykd.getOffset("malloc") ) #getOffset("malloc") may take a long time15
16# get a type of a std::string
17stringClass = pykd.typeInfo(fullStringClassName)18
19# allocate memory
20buffer = malloc( stringClass.size() )21
22# get a typed access to memory. As you may see the instance of the std::string is not initialized
23stringVar = pykd.typedVar( stringClass, buffer )24
25# set up parameters for a constructor call.
26param = pykd.stackAlloc(100)27
28pykd.writeCStr(param, "hello")29
30# call ctor for initalizing. std::string has several forms of constructor so we need to note prototype
31ctor = stringVar.method( stringClassName, "Void(__thiscall)(Char*)" )32ctor(param)33
34#check result:
35print( pykd.loadCStr( stringVar.c_str() ) )36
37pykd.stackFree(100)38