C语言 如何在 Ubuntu 的 Eclipse 中以 root 身份调试应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2891356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:30:03  来源:igfitidea点击:

how to debug application as root in eclipse in Ubuntu?

ceclipsedebuggingrootlibpcap

提问by futang

I'm programming application using libpcap. when I debug the application in normal mode, pcap cannot get the network device. it seems that I have to debug the application in root. How can I debug the application in root? I have the root password. I think eclipse has such an option that can add root for the debugging application,but I don't know how to do it. please help.

我正在使用 libpcap 编程应用程序。当我在正常模式下调试应用程序时,pcap 无法获取网络设备。看来我必须在 root 中调试应用程序。如何在 root 中调试应用程序?我有root密码。我认为eclipse有这样一个选项可以为调试应用程序添加root,但我不知道该怎么做。请帮忙。

回答by xmoex

  1. Enable your user to run gdbas root without being asked for any password:
    sudo visudo
    Add the following line after all other rules:
    <youruser> ALL=(root) NOPASSWD:/usr/bin/gdb
  2. Create or modify a debug configuration in eclipseto run gdb as root
    e.g. in Run > Debug Configurations > C/C++ Application > YourProject Debug:
    change Debugger > Main > GDB debugger from gdbto sudo -u <youruser> gdb
  1. 使您的用户能够以 root身份运行gdb,而无需输入任何密码:
    sudo visudo
    在所有其他规则之后添加以下行:
    <youruser> ALL=(root) NOPASSWD:/usr/bin/gdb
  2. Eclipse 中创建或修改调试配置以作为 root 运行 gdb
    例如在 Run > Debug Configurations > C/C++ Application > YourProject Debug:
    将 Debugger > Main > GDB debugger 从gdb更改为sudo -u <youruser> gdb

Update (and warning!):

更新(和警告!):

In his comment nategoose pointed out that this answer should come with a warning:

nategoose 在他的评论中指出,这个答案应该带有警告:

Enabling a user to use sudofor gdblike suggested in my answer in fact gives admin privileges to him/herwhich in many cases might be an undesired side effect. I therefore consider the answer appropriate in an environment where it's not assumed that the user would try to harm the system (e.g. it's your own personal computer or a virtual machine)

允许用户使用我的回答建议sudogdb类似功能实际上为他/她提供了管理员权限,这在许多情况下可能是不希望的副作用。因此,我认为在不假设用户会试图损害系统的环境中的答案是合适的(例如,它是您自己的个人计算机或虚拟机)

For a multi-(non-trusted)-user environment I think it might be a better idead to utilize unix' file capabilitiesto enable gdbto debug an application without the need of admin privileges

对于多(非信任)用户环境,我认为这可能是一个更好的idead利用UNIX”文件的能力,使gdb而不需要管理员权限来调试应用程序

回答by sean

You can use gdbserver on localhost to attach a existing process, the following is the command line:

您可以在 localhost 上使用 gdbserver 来附加现有进程,以下是命令行:

sudo gdbserver :<listening port> --attach <pid>

Or you can create a new process using gdbserver:

或者您可以使用 gdbserver 创建一个新进程:

sudo gdbserver :<listening port> <process executable>

Then you can create a debugging configuration in Eclipse, in the debugger tab, the debugger item, select gdbserver, and input the listening port in the connection tab in the bellow.

然后就可以在Eclipse中创建调试配置了,在debugger选项卡,debugger项,选择gdbserver,在下面的connection选项卡输入监听端口。

回答by davidag

Launch Eclipse with sudo (just for completeness: http://www.eclipse.org/forums/index.php?t=msg&goto=516838&)

使用 sudo 启动 Eclipse(只是为了完整性:http: //www.eclipse.org/forums/index.php?t=msg&goto=516838 &

Update: Follow xmoex solution. If you run Eclipse as root (ie. using sudo) your files will be root-owned... which you probably don't want.

更新:遵循 xmoex 解决方案。如果您以 root 身份运行 Eclipse(即使用 sudo),您的文件将是 root 拥有的……您可能不想要。

回答by Cédric Julien

Another solution is to grant you (or the gdb executable) the rights to make some pcap captures as mentioned here. With something like this :

另一种解决方案是授予您(或 gdb 可执行文件)进行一些 pcap 捕获的权限,如此处所述。像这样:

setcap cap_net_raw,cap_net_admin=eip /usr/bin/gdb

you should be able to allow to capture packets to gdb without being root.

您应该能够允许在没有 root 的情况下将数据包捕获到 gdb。

回答by yehudahs

this question was asked a long time ago but if this will help to anybody I open a bug in bugzilla and this short thread solved the problem: bugzilla bug

这个问题很久以前就有人问过了,但如果这对任何人有帮助,我在 bugzilla 中打开一个错误,这个简短的线程解决了这个问题: bugzilla bug

回答by MattR

Here's how I did it:

这是我如何做到的:

Create a C/C++ Remote Application

创建C/C++ 远程应用程序

  1. On the target, make sure your sudo does not prompt for a PW
  2. Look at Debug ConfigurationsDebuggerPort number
  3. Edit Debug ConfigurationsMainCommands to execute before application
  1. 在目标上,确保你的 sudo 没有提示输入密码
  2. 查看调试配置调试器端口号
  3. 编辑调试配置主要在应用程序之前执行的命令

Change to:

改成:

sudo gdbserver :<port number> <path to application>;exit #

sudo gdbserver :<port number> <path to application>;exit #

This will basically run the gdbserverthat would normally be executed by eclipse inside the sudo, the trailing '#' will keep the eclipse command from executing.

这将基本上运行sudo 中通常由 eclipse 执行的gdbserver,尾随的“ #”将阻止 eclipse 命令执行。

回答by mad

easiest way, try sudo ./eclipse, then debug as usual

最简单的方法,尝试 sudo ./eclipse,然后像往常一样调试

回答by nategoose

From the console in the directory with your executable:

从包含可执行文件的目录中的控制台:

sudo gdb ./my_program

If eclipse supports remote debugging then you could do that even though it is running locally.

如果 Eclipse 支持远程调试,那么即使它在本地运行,您也可以这样做。

From the console:

从控制台:

sudo gdbserver localhost:<port_number> ./my_program

And then tell Eclipse the address (localhost and the port number you chose).

然后告诉 Eclipse 地址(本地主机和您选择的端口号)。

Oh yeah, you said the reason you were doing this was because you were using libpcap, so you may not want to use remote debugging over TCP because you may end up capturing your debugging connection packets in addition to your other network traffic.

哦,是的,你说你这样做的原因是因为你正在使用libpcap,所以你可能不想通过 TCP 使用远程调试,因为除了其他网络流量之外,你最终可能会捕获调试连接数据包。

In that case you do your remote (but really local) debugging over a serial port. I have never done this on a local machine, but you could use two actual serial ports (attaching them though a null modem) or try using a psudoterminal:

在这种情况下,您可以通过串行端口进行远程(但实际上是本地)调试。我从来没有在本地机器上这样做过,但你可以使用两个实际的串行端口(通过空调制解调器连接它们)或尝试使用 psudoterminal:

sudo gdbserver /dev/ptmx ./my_program

This will create the psudo-terminal under /dev/pts/but you'll have to figure out the name of it, and it might also create it with restrictive permissions. You can get around those. Unless you are running lots of terminal windows as root, it is not likely that you have many entries under /dev/ptsthat belong to root, so take note of the one that does after running the above command and then sudo chmodor sudo chownit to make it usable for your normal user and then tell your debugger to use that as your serial connection to your remote debugging target.

这将在下面创建 psudo-terminal,/dev/pts/但您必须弄清楚它的名称,它也可能使用限制性权限创建它。你可以绕过这些。除非您以 root 身份运行大量终端窗口,否则您不太可能有很多/dev/pts属于 root 的条目,因此请注意在运行上述命令后执行的那个,然后sudo chmodsudo chown它以使其可用于您的正常用户,然后告诉您的调试器将其用作到远程调试目标的串行连接。