python - 如何使用 PYKD 读取堆

标签 python windbg pykd

this question ,我想通过 API 调用替换 DbgCommand("dt ...")PYKD 命令 typedVar() 来到了救援。

因此,我的 heap_stat 脚本(扩展了 m_nSizem_nCount 信息)现在运行速度快了三倍。

为了您的信息,我已经完成了计算 STL 集合中成员数量的替换:

Replace: collection_Size = dbgCommand(("dt 0x" + pointer_format + " %s m_nSize") % (ptr,type_name)).split(' : ').[-1].split('\n')[0]
By:      collection_Size = typedVar(type_name, ptr).m_nSize

由于这次成功,我想用 API 调用替换其他 DbgCommand 请求。

对于 dbgCommand('!heap -h 0') 的情况,这似乎不是那么简单(一些示例):

>>> for t in targetHeapIterator():
...   print t
... 
Traceback (most recent call last):
  File "<console>", line 1, in <module>
RuntimeError: This class cannot be instantiated from Python

>>> for t in targetHeap().entries:
...   print t
... 
Traceback (most recent call last):
  File "<console>", line 1, in <module>
RuntimeError: This class cannot be instantiated from Python

>>> for t in targetProcess().getManagedHeap().entries:
...   print t
... 
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'instancemethod' object is not iterable

我如何遍历进程的堆(替换 !heap -h 0)?

附言即使 targetHeap() 不能用作 !heap -h 0 的替代品,出于调查目的,我仍然想知道如何使用它。

最佳答案

targetHeapIterator() - 仅用于托管堆,不能直接创建,只能通过特殊类创建。

for entry in targetProcess.getManagedHeap().entries():
    pass # enumerate managed heap

要枚举 native 堆,您需要编写自己的脚本。

也许对你有用: https://githomelab.ru/pykd/pykdwin

这个包有堆枚举器但有限制:

  • 不支持LFH
  • 不支持段堆

来自文档的示例:

from pykdwin.heap import *
for heap in get_heaps():
    for entry in heap.entries():
        print( hex(entry.address), entry.size )

关于python - 如何使用 PYKD 读取堆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53393285/

相关文章:

.net - Windbg 可以显示线程名称吗?

python - 如何避免将 DbgCommand 命令写入日志文件

python - 当从遗留数据库生成模型类时,如何在 django-rest 中序列化多个模型数据

windows - 故障转储中的套接字调查

winapi - 检测 Windows 7 蓝牙堆栈

python - 将 Pykd 与 SOS 结合使用

windbg - pykd 中的 searchMemory 函数

python - Python 的 argparse 可以像 gnu getopt 一样置换参数顺序吗?

python - Pandas - 确定 Churn 是否发生缺失年份

python - Z3python 异或和?