如何在GDB中将shell命令的输出用作GDB命令的参数? [英] How can I use the output of a shell command as an argument to GDB command within GDB?

查看:158
本文介绍了如何在GDB中将shell命令的输出用作GDB命令的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想花时间搜索要附加的流程的PID,所以我要做类似的事情,

I don't want to spend time searching for the PID of the process I want to attach to, so I do something like this,

gdb -p ($pidof foo)

其中foo是进程名称.

where foo is the process name.

我想知道是否可以在GDB中进行类似的操作.

I want to know if I can so something like this from within GDB.

我正在寻找类似以下的内容.

I am looking for something like the following.

(gdb) attach $(pidof foo)
Illegal process-id: $(pidof foo).
(gdb) attach `pidof foo`
Illegal process-id: `pidof foo`.

GDB中是否有类似的东西?

Is there anything like this in GDB?

推荐答案

Gdb attach 命令需要将进程ID作为参数.因此,您无法直接实现它.您可以分两个步骤进行操作:

Gdb attach command requires process id as an argument. So, you cannot achieve it directly. You can do it in two steps:

(gdb) shell pidof foo
12345
(gdb) attach 12345
Attaching to process 12345
...

但是gdb非常灵活,可以扩展具有内部脚本(称为命令序列),python等.这是一个示例脚本,还可以处理以下情况:找不到进程或找到多个进程(选择了由pidof指定的第一个):

But gdb is quite flexible and can be extended with internal scripting (known as Canned Sequences of Commands), python, etc. Here is an example script that also handles situations when no process found or when several processes found (the first given by pidof is selected):

define attach_pidof
  if $argc != 1
   help attach_pidof
  else
   shell echo -e "\
set \$PID = "$(echo $(pidof $arg0) 0 | cut -d ' ' -f 1)"\n\
if \$PID > 0\n\
  attach "$(pidof -s $arg0)"\n\
else\n\
  print \"Process '"$arg0"' not found\"\n\
end" > /tmp/gdb.pidof
   source /tmp/gdb.pidof
  end
end
document attach_pidof
Attach to process by name.
Usage: attach_pidof PROG_NAME
end

在这里,我使用了@ dbrank0指出的技巧(回显到文件,然后与源代码一起运行).另外,当未找到任何进程时,我正在使用 echo $ {pidof<>)0 提供默认值0.

Here I used trick pointed out by @dbrank0 (echo'ing to file and then running it with source). Also, I am using echo $(pidof <>) 0 to provide default value 0 when no process found.

将此命令放置到〜/.gdbinit 中,以便在gdb启动时,该命令将自动添加到gdb中并可以使用:

Place this to ~/.gdbinit so on gdb start, this command will be automatically added to gdb and can be used:

(gdb) attach_pidof myprog 

如果您的gdb支持python扩展名,则可以将其放置到某个文件中,例如 ext.py :

If your gdb supports python extensions, you can place this to some file, e.g. ext.py:

import gdb
from subprocess import check_output, CalledProcessError

class AttachPidofCommand (gdb.Command):
  "Attach to process by name"

  def __init__ (self):
    super (AttachPidofCommand, self).__init__ ("attach_pidof",
                         gdb.COMMAND_SUPPORT,
                         gdb.COMPLETE_NONE, True)

  def invoke (self, arg, from_tty):
    try:
        pid = check_output(["pidof", arg]).split()[0].decode("utf-8")
    except CalledProcessError:
        gdb.write('process \'%s\' not found\n' % (arg))
        return
    gdb.write('attach to \'%s\' (%s)\n' % (arg, pid))
    gdb.execute('attach %s' % (pid), from_tty)

AttachPidofCommand()

导入并使用相同的方式:

import and use the same way:

(gdb) source ext.py
(gdb) attach_pidof my_prog

这篇关于如何在GDB中将shell命令的输出用作GDB命令的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆