38

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.

xmoex
  • 2,602
  • 22
  • 36
futang
  • 381
  • 1
  • 3
  • 3

8 Answers8

68
  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

xmoex
  • 2,602
  • 22
  • 36
  • 2
    Brilliant answer, note for others a little problem I ran into doing this: I have LD_LIBRARY_PATH set as my user but root does not have it, so I added it to my sudo command but sudo doesn't allow you to set LD_LIBRARY_PATH so I added this in visudo 'Defaults env_keep += "LD_LIBRARY_PATH"' – stu Jun 15 '15 at 19:33
  • I get error `Could not determine GDB version using command: sudo -u debug gdb --version` How to solve it? – fralbo Nov 28 '18 at 15:32
  • I have been able to debug *udisksd* as root in KDevelop with the slightly modified 2 steps from the answer: 1. The first step is convenient but not necessary because KDevelop can be launched from terminal and the root password can be entered there at a prompt when triggering the Debug action. 2. In KDevelop's main menu->Run->Configure Launches...->Debug page set */usr/local/bin/run-as-sudo* in the *Shell script* field. This local script must be created and marked as executable. Its contents (2 lines): `#!/usr/bin/env bash \n sudo "$@"`. This answer's `sudo -u ` didn't work for me. – vedg Jan 19 '19 at 15:02
3

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.

sean
  • 1,252
  • 3
  • 14
  • 29
3

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.

David
  • 2,663
  • 3
  • 24
  • 41
  • 1
    This is not a good idea as explained here: http://stackoverflow.com/questions/2580279/how-do-i-run-my-application-as-superuser-from-eclipse#comment2588681_2580560 – Paolo M Mar 20 '15 at 14:51
  • 1
    I agree with you Paolo, xmoex answer is a much better solution. – David Mar 21 '15 at 20:06
1

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.

MattR
  • 85
  • 3
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 :

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.

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • This worked when run as super user. Also made remote debugging easier by also granting access to pcap captures to `gdbserver` – fredk Dec 26 '15 at 13:25
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

yehudahs
  • 2,488
  • 8
  • 34
  • 54
-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.

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.

nategoose
  • 12,054
  • 27
  • 42
-2

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

mad
  • 1