如何避免DbgCommand命令被写入日志文件

2024-10-03 23:25:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Pykd.pyd上遇到了一个恼人的问题:我在一个脚本中使用它,启动了几个DbgCommand函数,例如:

DbgCommand("dt 0x000000eab8748430 CMap<int,int,CUIntArray *,CUIntArray *> m_nCount")

用于获取CMap对象的大小。由于这是在一个脚本中完成的,使用了大量的对象,所以我使用了Windbg(菜单editOpen/Close Log File)中的日志文件,这里有一个要点:
Windbg窗口中查看时,我只看到DbgCommand调用的结果,但在日志文件中我也看到了命令本身:

Windbg窗口:

^{pr2}$

日志文件:

dt 0x000000eab87488f0 CMap<int,int,CUIntArray *,CUIntArray *> m_nCount // DbgCommand command
000000eab87488f0    CMap<int,int,CUIntArray *,CUIntArray *>  Size:[0]  // formatted DbgCommand result
dt 0x000000eab8748930 CMap<int,int,CUIntArray *,CUIntArray *> m_nCount // DbgCommand command
000000eab8748930    CMap<int,int,CUIntArray *,CUIntArray *>  Size:[0]  // formatted DbgCommand result

如何避免将DbgCommand命令写入日志文件?在

同时,我发现存在一个参数suppressoutput,它可以用来不在屏幕上显示命令的结果,但这不是我的意思,正如您在以下摘录中所看到的:

测试脚本摘要:

dprintln("1 : x /2 <application_name>!CStringList::CStringList, true")
dbgCommand("x /2 <application_name>!CStringList::CStringList", True)
dprintln("2 : x /2 <application_name>!CStringList::CStringList, false")
dbgCommand("x /2 <application_name>!CStringList::CStringList", False)
dprintln("3")

屏幕上的结果:

1 : x /2 <application_name>!CStringList::CStringList, true
2 : x /2 <application_name>!CStringList::CStringList, false
004b6d3e          <application_name>!CStringList::CStringList
3

日志文件中的结果:

1 : x /2 <Application>!CStringList::CStringList, true
x /2 <Application>!CStringList::CStringList
004b6d3e          <Application>!CStringList::CStringList
2 : x /2 <Application>!CStringList::CStringList, false
x /2 <Application>!CStringList::CStringList
004b6d3e          <Application>!CStringList::CStringList
3

似乎suppressoutput是为了不在屏幕上显示DbgCommand结果,而我感兴趣的是不在日志中显示{}输入。在

让我解释一下typedvar解决方案的问题:

由于某些奇怪的原因,CMapCArray对象不能以简单的方式使用(标准的windbg命令),正如您在这里看到的:

0000000000335e90    <application_name>!CMap<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,wchar_t const * __ptr64,int,int>

如您所见,dt不适用于此:

dt 0x0000000000335e90 <application_name>!CMap<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,wchar_t const * __ptr64,int,int>
Symbol <application_name>!CMap<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,wchar_t const * __ptr64,int,int> not found

这可以通过删除<application_name>__ptr64(不要忘记空格)来处理:

0:000> dt 0x0000000000335e90 CMap<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,wchar_t const *,int,int>
<application_name>!CMap<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,wchar_t const *,int,int>
   +0x000 __VFN_table : 0x00000001`3fc77ac8 
   +0x008 m_pHashTable     : (null) 
   +0x010 m_nHashTableSize : 0x11
   +0x018 m_nCount         : 0n0
   +0x020 m_pFreeList      : (null) 
   +0x028 m_pBlocks        : (null) 
   +0x030 m_nBlockSize     : 0n10

这似乎与以下结果相对应:

00000001`3fc77ac8 <application_name>!CMap<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,wchar_t const * __ptr64,int,int>::`vftable'

但是,如果我尝试使用typedVar它似乎不起作用:

if type_name.find("CMap<") != -1:
    dprintln("original type name : [%s]" % type_name)
    dprintln(("pointer : [0x" + pointer_format + "]") % ptr)
    var =  typedVar(type_name, ptr) # before translation of __ptr64
    nieuwe_grootte1 = var.m_nCount
    type_name = type_name.replace(application_name + "!","") # Normally type_name starts with "<application_name>!CMap<...", it must become "CMap<..."
    type_name = type_name.replace(" __ptr64","")             # apparently, when the CMap definition contains __ptr64, it doesn't work
                                                             # dt 0x000000eab8748430 CMap<int,int,CUIntArray *,CUIntArray *> m_nCount seems to work

    dprintln("after replacements type name : [%s]" % type_name)
    var =  typedVar(type_name, ptr) # after translation of __ptr64
    nieuwe_grootte2 = var.m_nCount

    grootte_result = dbgCommand(("dt 0x" + pointer_format + " %s m_nCount") % (ptr,type_name)).split(' : ')
    grootte = grootte_result[-1].split('\n')[0] # list[-1] is Python for "take the last entry of a list"
    grootte = grootte.replace("0n","")
    dprintln((pointer_format + "\t%s\t Size:[%s, %d, %d]") % (ptr, type_name, grootte, nieuwe_grootte1, nieuwe_grootte2))

结果如下:

original type name : [<application_name>!CMap<unsigned int,unsigned int,enum CService::PARAMETER_TYPE,enum CService::PARAMETER_TYPE>]
pointer : [0x00000000003355e0]
after replacements type name : [CMap<unsigned int,unsigned int,enum CService::PARAMETER_TYPE,enum CService::PARAMETER_TYPE>]
00000000003355e0    CMap<unsigned int,unsigned int,enum CService::PARAMETER_TYPE,enum CService::PARAMETER_TYPE>  Size:[105, 105, 105]
original type name : [<application_name>!CMap<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,wchar_t const * __ptr64,int,int>]
pointer : [0x0000000000335640]

Traceback (most recent call last):

  File "C:\Program Files (x86)\Windows Kits\10\Debuggers\x86\heap_stat_extra.py", line 215, in <module>
    var =  typedVar(type_name, ptr) # before translation of __ptr64

TypeException: <application_name>!CMap<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,wchar_t const * __ptr64,int,int> : invalid type name

我已经尝试过删除<application_name>和/或{},但似乎无法解决问题。此外,dt和“x/2的结果!(格式不正确)清楚地表明所述类型存在。{{cd21>

编辑
我刚刚尝试过,使用pykd_团队的Python命令,但它似乎仍然不起作用(这次是针对CArray对象),如您所见:

启动脚本将得到以下结果:

  File "C:\Temp_Folder\blabla\heap_stat_logs_backup.py", line 232, in <module>
    collection_Size = typedVar(type_name, ptr).m_nCount

TypeException: CArray<CSyncRules::VersionRestriction *,CSyncRules::VersionRestriction * const &> : invalid type name

尝试调查什么是正确的类名:

0:000> !py
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> app = pykd.module("<Application>")
>>> for tp in app.enumTypes("*CArray*"):
...   print tp
... 
CArray<CSyncRules::VersionRestriction *,CSyncRules::VersionRestriction * const &>
...

如您所见,类名是完全匹配的,但是有一个TypeException。你知道现在该怎么办吗?(顺便说一下,我正在与Visual Studio Professional 2017合作)

新编辑

这个问题很可能是由PYKD引起的,它不能处理类型定义中的星号(*)。为了避免上述问题,我可以使用转义符吗?在


Tags: nameapplicationtypedtintcmapconstatl
3条回答

尝试改变:

dt 0x000000eab87488f0 CMap<int,int,CUIntArray *,CUIntArray *> m_nCount

收件人:

^{pr2}$

您可以访问CMap类的任何字段并使用它,因为它们是自然的python类型

如果要多次获取typedVar,可以缓存类型信息以避免性能问题:

CMap = typeInfo("CMap<int,int,CUIntArray *,CUIntArray *>")
var = typedVar( CMap, 0x000000eab87488f0 )

使用pykd.typedVar型班级。它是专门为这种情况设计的

尝试枚举模块的类型(使用通配符掩码),并找到CMap的确切符号名。在

app = pykd.module("<application_name>")
for tp in app.enumTypes("CMap*"):
   print tp

我可以尝试在一个小示例中重现一个问题,但我需要知道您的visualstudio版本。在

相关问题 更多 >