繁体   English   中英

从 Python 方便 function 返回空字符串“”而不是 0x0

[英]Return empty string “” from Python convenience function instead of 0x0

gdb 便利 functioninvoke方法返回 Python 字符串""似乎产生的结果是0x0而不是预期的""

这是因为gdb.Value("")产生0x0

(gdb) python
>print(gdb.Value(""))
>end
0x0

这就是返回 Python 字符串时会发生的情况。

有没有任何明智的方法可以让 gdb 产生一个gdb.Valuechar[]char*指向\x00 ,即字符串""

这当然不是一个方法,但我现在使用的解决方法是从我明确提供char[]类型(python3)的缓冲区构建我自己的原始gdb.Value

(gdb) python print( gdb.Value(b'\x00', gdb.lookup_type('char').array(0) ) )
""

例如 gdb 方便 function:

class Indent(gdb.Function):
    """Generate a string of 'depth' spaces"""

    def __init__(self):
        super(Indent, self).__init__("indent")

    def invoke(self, depth):
        """https://stackoverflow.com/a/66757722/398670"""
        depth = int(depth)
        if (depth <= 0):
            return gdb.Value(b"\x00", gdb.lookup_type('char').array(0))
        else:
            return " " * int(depth)

Indent()

但这肯定不是正确的方法,所以我希望有人会提出更好的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM