【问题标题】:Programmatic GDB completer interface via Python通过 Python 编程的 GDB 完成器接口
【发布时间】:2017-06-01 22:14:22
【问题描述】:

我正在使用 GDB 的内置 Python 支持。在我的例子中,Python 将为用户提供一个专门的接口。在内部,应用程序将调用各种 GDB 函数来对 C 库执行操作。

当使用 GDB shell 时,GDB 提供了非常好的选项卡补全。但我想使用 Python API。

我想根据用户输入显示命令完成。理想情况下,我想用部分字符串调用 GDB 函数。然后 GDB 应该返回可能的完成。

是否有可用于执行补全的 Python API?

【问题讨论】:

    标签: python c interface gdb


    【解决方案1】:

    是的,有一个 Python API。它记录在here 函数下Command.complete (text, word)

    默认完成程序将完成文件名和子命令完成,但您可以通过提供自己的自定义 complete 方法来扩展它。这是一个例子:

    class MyGdbCommand(gdb.Command):
        def __init__(self):
            super().__init__("mycmd", gdb.COMMAND_USER) # or whatever command class you deem appropriate
    
        def complete(self, arguments_string, last):
            is_brk_char = (len(arguments_string) < len(last))
            args = gdb.string_to_argv(arguments_string)
            if arguments_string == "" or arguments_string[-1:] == " ":
                args.append("") # Add dummy argument to complete
            argc = len(args)
    
            if argc == 1:
                if is_brk_char:
                    return gdb.COMPLETE_NONE
                if args[0] in ['-l', '-o']:
                    return args[0] # it's complete
                return ['-l', '-o'] # valid option flags
    
            if argc == 2 and (args[0] == '-l' or args[0] == '-o'):
                if is_brk_char:
                    return gdb.COMPLETE_FILENAME # -l and -o take file arguments
                (head, tail) = os.path.split(curr)
                return getMatchingFiles(head, tail) # implement appropriate heuristic; pass args[0] flag if it matters
    
            if argc == 3:
                if is_brk_char:
                    return gdb.COMPLETE_NONE
                if args[0] == '-l':
                    return ['-o']
                if args[0] == '-o':
                    return ['-l']
                return []
    
            if argc == 4:
                if is_brk_char:
                    return gdb.COMPLETE_FILENAME
                (head, tail) = os.path.split(curr)
                return getMatchingFiles(head, tail)           
    
            if is_brk_char:
                return gdb.COMPLETE_NONE
            return [] # No more valid options
    

    注意!!完成者不负责验证。一旦你的命令的invoke() 方法被调用,就应该这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-15
      相关资源
      最近更新 更多