简体   繁体   English

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

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

Returning the Python string "" from a gdb convenience function 's invoke method appears to produce a result of 0x0 instead of the expected "" .gdb 便利 functioninvoke方法返回 Python 字符串""似乎产生的结果是0x0而不是预期的""

This is because gdb.Value("") yields 0x0 :这是因为gdb.Value("")产生0x0

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

which is what'll happen when a Python string is returned.这就是返回 Python 字符串时会发生的情况。

Is there any sensible way to get gdb to yield a gdb.Value for a char[] or char* that points to \x00 , ie the string "" ?有没有任何明智的方法可以让 gdb 产生一个gdb.Valuechar[]char*指向\x00 ,即字符串""

It certainly isn't a good way, but the workaround I'm using for now is to construct my own raw gdb.Value from a buffer where I explicitly supply a char[] type (python3):这当然不是一个方法,但我现在使用的解决方法是从我明确提供char[]类型(python3)的缓冲区构建我自己的原始gdb.Value

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

eg a gdb convenience function:例如 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()

But surely this can't be the right way, so I'm hoping someone will come along with a better solution.但这肯定不是正确的方法,所以我希望有人会提出更好的解决方案。

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

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