本文介绍了GDB python API-获取gdb的python API以打印有问题的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用gdb python API向C代码编写一些调试宏.但是在测试脚本时,我看到报告的脚本解释错误中没有行号.

I am trying to use the gdb python API to write a few debugging macro to c code. But while testing the script I see that the interpretation errors in the script are reported without a line number.

例如(gdb)模块显示obj-template 0x7264b0测试条目"Python异常< class'IndexError'>列表索引超出范围:Python命令中发生错误:列表索引超出范围

在这里我看到有一个IndexError,但是脚本没有说出哪个是行号,我该如何获取?

Here I can see that there is an IndexError, but the script does not say which is the line number, how do I get it ?

推荐答案

在脚本中的某个适当位置捕获异常,并根据需要漂亮地打印该异常和回溯.

Catch the exception inside your script, at some proper position, and pretty print the exception and backtrace as you like.

我建议您参考一些用gdbpython编写的gdb插件,包括 peda , gef , pwndbg 等.此类插件实际上使用了gdbpython的所有功能,并且包含编写python gdb插件的很好的实践.健壮的gdbpython插件通常具有用于python异常的漂亮打印机.

I want to suggest you refer to some gdb plugin written in gdbpython, including peda, gef, pwndbg, etc. Such plugins have virtually used every feature of gdbpython, and contain very good practice for writing python gdb plugin. Robust gdbpython plugins usually have pretty-printer for python exceptions.

作为示例,这是gef的backtrace漂亮打印机:(当然,您不能直接使用它,因为它包含太多的gef内部函数)

As an example, here is the backtrace pretty-printer of gef: (of course you cannot use it directly, since it contains too many gef internal functions)

def show_last_exception():
    """Display the last Python exception."""
    print("")
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print(" Exception raised ".center(80, horizontal_line))
    print("{}: {}".format(Color.colorify(exc_type.__name__, attrs="bold underline red"), exc_value))
    print(" Detailed stacktrace ".center(80, horizontal_line))
    for fs in traceback.extract_tb(exc_traceback)[::-1]:
        if PYTHON_MAJOR==2:
            filename, lineno, method, code = fs
        else:
            filename, lineno, method, code = fs.filename, fs.lineno, fs.name, fs.line

        print("""{} File "{}", line {:d}, in {}()""".format(down_arrow, Color.yellowify(filename),
                                                            lineno, Color.greenify(method)))
        print("   {}    {}".format(right_arrow, code))

    print(" Last 10 GDB commands ".center(80, horizontal_line))
    gdb.execute("show commands")
    print(" Runtime environment ".center(80, horizontal_line))
    print("* GDB: {}".format(gdb.VERSION))
    print("* Python: {:d}.{:d}.{:d} - {:s}".format(sys.version_info.major, sys.version_info.minor,
                                                   sys.version_info.micro, sys.version_info.releaselevel))
    print("* OS: {:s} - {:s} ({:s}) on {:s}".format(platform.system(), platform.release(),
                                                    platform.architecture()[0],
                                                    " ".join(platform.dist())))
    print(horizontal_line*80)
    print("")
    return

这篇关于GDB python API-获取gdb的python API以打印有问题的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:55
查看更多