【问题标题】:gdb - assign pipe output to a variablegdb - 将管道输出分配给变量
【发布时间】:2022-08-14 21:27:52
【问题描述】:

我有以下命令,我想将他的输出分配给一个变量:

(gdb) pipe monitor get info | grep cross2_Release.nss | cut -c 3-13
0x566f80400

试过这个

set $main = \'pipe monitor get info | grep cross2_Release.nss | cut -c 3-14\'

没有加载符号表。使用“文件”命令。

预期结果是 $main 等于 0x566f80400

    标签: gdb pipe


    【解决方案1】:

    起初,我认为这会起作用:

    python output = gdb.execute('pipe monitor get info | grep cross2_Release.nss | cut -c 3-13', to_string=True)
    python gdb.set_convenience_variable('main', gdb.parse_and_eval(output))
    

    但这失败了,因为pipe 命令将输出发送到GDB 的标准输出,而不是gdb.execute 可以捕获并放入字符串的流。所以这里有一些更粗略的东西:

    shell printf 'set $main = ' > ~/.gdbtmp
    pipe monitor get info | grep cross2_Release.nss | cut -c 3-13 >> ~/.gdbtmp
    source ~/.gdbtmp
    

    【讨论】:

      【解决方案2】:

      使用 python subprocess.check_output() 函数相对简单:

      py gdb.set_convenience_variable('main', gdb.parse_and_eval(subprocess.check_output('grep cross2_Release.nss | cut -c 3-13', shell=True, input=gdb.execute('monitor get info', to_string=True), text=True)))
      

      但要输入的内容很多,所以我会将其打包到一个 gdb 便利函数中:

      import subprocess
      
      class Pipe(gdb.Function):
          def __init__(self):
              super(Pipe, self).__init__("pipe")
      
          def invoke(self, gdb_cmd, pipe_cmd):
              return gdb.parse_and_eval(subprocess.check_output(pipe_cmd.string(), shell=True, input=gdb.execute(gdb_cmd.string(), to_string=True), text=True))
      
      Pipe()
      

      然后可以这样使用:

      set $main = $pipe("monitor get info", "grep cross2_Release.nss | cut -c 3-14")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-15
        • 2017-09-28
        • 2012-12-12
        • 2018-05-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多