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

时间:2021-09-21 20:44:02

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无法获得网络设备。我似乎必须在根目录中调试应用程序。如何在根目录中调试应用程序?我有根密码。我认为eclipse有这样一个选项,可以为调试应用程序添加根,但是我不知道如何做。请帮助。

7 个解决方案

#1


56  

  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. 允许用户以root身份运行gdb,而不需要任何密码:sudo visudo在所有其他规则之后添加以下一行: all =(root) NOPASSWD:/usr/bin/gdb
  3. 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
  4. 创建或修改eclipse中的调试配置以作为根运行gdb,例如在运行>调试配置> C/ c++应用> YourProject debug:将调试器>主> gdb调试器从gdb改为sudo -u gdb

Update (and warning!):

更新(和警告!):

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

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

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)

在我的回答中,允许用户使用sudo来支持gdb,这实际上给了他/她的管理员特权,在许多情况下可能是不希望的副作用。因此,我认为在不假定用户会试图损害系统(例如,它是您自己的个人计算机或虚拟机)的环境中,答案是适当的。

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

对于一个多(不可信)用户环境,我认为最好使用unix的文件功能,使gdb能够调试应用程序,而不需要管理权限

#2


3  

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中、调试器选项卡、调试器项中创建调试配置,选择gdbserver,并在下面的connection选项卡中输入侦听端口。

#3


3  

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?

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解决方案。如果您将Eclipse作为根运行。使用sudo)你的文件将是根所有的…你可能不想要。

#4


1  

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.

您应该能够允许捕获包到gdb而不是根。

#5


0  

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打开一个bug,这个短线程解决了这个问题:bugzilla bug

#6


-1  

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

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

#7


-2  

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地址(localhost和您选择的端口号)。

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/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.

这将在/dev/pts/下创建psu -terminal,但是您必须确定它的名称,它也可能使用限制性的权限创建它。你可以绕过那些。除非你是作为根用户运行大量的终端窗口,是不可能有很多条目/dev/pts下属于根,那么注意,运行上面的命令之后,然后才可以使用sudo chmod或sudo乔恩为普通用户然后告诉你的调试器使用串行连接到远程调试的目标。

#1


56  

  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. 允许用户以root身份运行gdb,而不需要任何密码:sudo visudo在所有其他规则之后添加以下一行: all =(root) NOPASSWD:/usr/bin/gdb
  3. 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
  4. 创建或修改eclipse中的调试配置以作为根运行gdb,例如在运行>调试配置> C/ c++应用> YourProject debug:将调试器>主> gdb调试器从gdb改为sudo -u gdb

Update (and warning!):

更新(和警告!):

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

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

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)

在我的回答中,允许用户使用sudo来支持gdb,这实际上给了他/她的管理员特权,在许多情况下可能是不希望的副作用。因此,我认为在不假定用户会试图损害系统(例如,它是您自己的个人计算机或虚拟机)的环境中,答案是适当的。

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

对于一个多(不可信)用户环境,我认为最好使用unix的文件功能,使gdb能够调试应用程序,而不需要管理权限

#2


3  

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中、调试器选项卡、调试器项中创建调试配置,选择gdbserver,并在下面的connection选项卡中输入侦听端口。

#3


3  

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?

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解决方案。如果您将Eclipse作为根运行。使用sudo)你的文件将是根所有的…你可能不想要。

#4


1  

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.

您应该能够允许捕获包到gdb而不是根。

#5


0  

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打开一个bug,这个短线程解决了这个问题:bugzilla bug

#6


-1  

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

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

#7


-2  

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地址(localhost和您选择的端口号)。

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/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.

这将在/dev/pts/下创建psu -terminal,但是您必须确定它的名称,它也可能使用限制性的权限创建它。你可以绕过那些。除非你是作为根用户运行大量的终端窗口,是不可能有很多条目/dev/pts下属于根,那么注意,运行上面的命令之后,然后才可以使用sudo chmod或sudo乔恩为普通用户然后告诉你的调试器使用串行连接到远程调试的目标。