【问题标题】:try..except in interactive Windbg Python session throws me out of the Python sessiontry..except 在交互式 Windbg Python 会话中让我退出 Python 会话
【发布时间】:2019-05-07 09:59:41
【问题描述】:

我正在使用 PYKD 进行转储分析。在 heap_stat 脚本中使用了 PYKD 库,我想以更具交互性的方式使用 PYKD 库,如下所示:

Windbg prompt>!py
Input>dbgCommand("x /2 *!CStringArray*vftable*")

这很好用(我知道这没用,我只是想证明它有效)。

但是,heap_stat 脚本包含以下源代码:

try:
  vftable_candidate = ptrPtr(ptr) # which pointer value is present on that spot in memory?
  dprintln("DDS vftable_candidate [%08x], ptr value [%d], ptr pointer [%08x]" % (vftable_candidate, ptr, ptr))
except:
  continue

当我以交互方式尝试时,这似乎不起作用:

Windbg prompt>!py
Input>ptrPtr(‭48806712‬)

这会产生以下错误,让我退出 Python 会话:

  File "<console>", line 1


Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "C:\Python27\Lib\code.py", line 243, in interact
    more = self.push(line)

  File "C:\Python27\Lib\code.py", line 265, in push
    more = self.runsource(source, self.filename)

  File "C:\Python27\Lib\code.py", line 79, in runsource
    self.showsyntaxerror(filename)

  File "C:\Python27\Lib\code.py", line 139, in showsyntaxerror
    map(self.write, list)

  File "C:\Python27\Lib\code.py", line 171, in write
    sys.stderr.write(data)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 11: ordinal not in range(128)

没关系:将这个函数调用包装在 try..except 子句中是有原因的,所以现在让我们尝试将这个函数包装在交互式 Python 会话中的 try..except 子句中:

Windbg prompt>!py
Input>try: ptrPtr(‭48806712‬) except: continue

=> 这给出了同样的错误,尽管有try..except,我还是被抛出了 Python 会话。这很可能是由于缩进错误,但另一方面,交互式 Windbg Python 会话不允许多行,所以我不能使用 Python 缩进。

有没有办法在 Windbg PYKD Python 会话中使用 try..except 子句?
提前致谢

附:为了您的理解:这种行为(被踢出交互式会话)似乎是 Windbg PYKD 的典型行为,您可以在以下命令提示符 Python 会话中看到:

Windows Prompt>python
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.
>>> print 1/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>>

如您所见,抛出了异常,但我并没有被踢出 Python 会话(观看 &gt;&gt;&gt; 提示)。

【问题讨论】:

  • "有没有办法在 Windbg PYKD Python 会话中使用 try..except 子句?" - 这真的是你的问题吗?这是我能找到的唯一问号。这个问题的答案会对你有帮助吗?或者您想问一个不同的问题,哪个更有帮助?这可能是一个 XY 问题,恕我直言。你想达到什么目的?

标签: python windbg prompt pykd


【解决方案1】:

您似乎使用了复制/粘贴,并且您的输入字符串包含 unicode 符号,python 无法对其进行解码。尝试重新输入 'ptrPtr(48806712)'

【讨论】:

  • 这实际上可能是问题所在:当我一个接一个地键入命令字符时,一切正常。但是当我执行复制粘贴时,整个事情就出错了。
  • 您可能使用 Visual Studio 作为复制/粘贴源?我将尝试使用当前本地字符集将输入转换为 ascii。
  • 别笑:我一直在使用标准的Windows计算器,它似乎在开头和结尾添加了一个不可见的字符:-)
【解决方案2】:

交互式 Windbg Python 会话不允许多行,所以我不能 使用 Python 缩进。

你错了。你可以使用多行

>>> try:
...    1/0
... except:
...   print 0
... 
0
>>> 

【讨论】:

  • 抱歉,我接受您的回答太快了:当我尝试您的建议时,它工作正常。但是当我将1/0 替换为ptrPtr(48806712‬) 时,我会收到以下错误消息:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 11: ordinal not in range(128)
猜你喜欢
  • 2010-10-29
  • 2010-10-31
  • 2019-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
相关资源
最近更新 更多