如何在 Ubuntu 的 eclipse 中以 root 身份调试应用程序?

发布于 2024-09-02 13:17:35 字数 154 浏览 7 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

久隐师 2024-09-09 13:17:35
  1. 让您的用户无需询问即可以 root 身份运行 对于任何
    密码:
    sudo visudo
    在所有其他规则之后添加以下行:
    <您的用户> ALL=(root) NOPASSWD:/usr/bin/gdb
  2. 以 root 身份运行 gdb
    例如在“运行”中>调试配置> C/C++应用>您的项目调试:
    更改调试器>主要> GDB 调试器从 gdbsudo -ugdb

更新(和警告!):

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

Enabling a user to use sudo for gdb 就像我的回答中所建议的实际上给了他/她管理员权限,这在许多情况下可能是一个不希望的副作用。因此,我认为答案在不假设用户会尝试损害系统的环境中是合适的(例如,它是您自己的个人计算机或虚拟机)

对于多(不可信)用户环境,我认为它可能最好利用 unix 的文件功能来启用 gdb 无需管理员权限即可调试应用程序

  1. Enable your user to run as 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 to run gdb as root
    e.g. in Run > Debug Configurations > C/C++ Application > YourProject Debug:
    change Debugger > Main > GDB debugger from gdb to sudo -u <youruser> gdb

Update (and warning!):

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

Enabling a user to use sudo for gdb like suggested in my answer in fact gives admin privileges to him/her which 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)

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

方觉久 2024-09-09 13:17:35

你可以在 localhost 上使用 gdbserver 来附加一个现有的进程,下面是命令行:

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

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

sudo gdbserver :<listening port> <process executable>

然后你可以在 Eclipse 中创建一个调试配置,在 debugger 选项卡,debugger 项,选择 gdbserver ,并在下面的连接选项卡中输入监听端口。

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

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

Or you can create a new process using 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.

要走干脆点 2024-09-09 13:17:35

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

更新:遵循 xmoex 解决方案。如果您以root身份运行Eclipse(即使用sudo),您的文件将由root拥有...您可能不希望这样。

Launch Eclipse with sudo (just for completeness: 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.

哀由 2024-09-09 13:17:35

另一个解决方案是授予您(或 gdb 可执行文件)进行一些 pcap 捕获的权限 这里提到。有了这样的东西:

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

你应该能够在不成为 root 的情况下捕获数据包到 gdb。

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

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.

晌融 2024-09-09 13:17:35

我是这样做的:

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

  1. 在目标上,确保您的 sudo 不会提示输入 PW
  2. 查看调试配置 strong> → 调试器端口号
  3. 编辑调试配置主要在应用程序之前执行的命令< /strong>

更改为:

sudo gdbserver :<端口号>;exit #

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

Here's how I did it:

Create a C/C++ Remote Application

  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

Change to:

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

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

无声情话 2024-09-09 13:17:35

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

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

怀中猫帐中妖 2024-09-09 13:17:35

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

sudo gdb ./my_program

如果 eclipse 支持远程调试,那么即使它在本地运行,您也可以执行此操作。

从控制台:

sudo gdbserver localhost:<port_number> ./my_program

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

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

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

sudo gdbserver /dev/ptmx ./my_program

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

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.

From the console:

sudo gdbserver localhost:<port_number> ./my_program

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

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.

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:

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/pts that belong to root, so take note of the one that does after running the above command and then sudo chmod or sudo chown it 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.

风吹雨成花 2024-09-09 13:17:35

最简单的方法,尝试 sudo ./eclipse,然后照常调试

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文