how to debug application as root in eclipse in Ubuntu?

后端 未结 8 1241

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

相关标签:
8条回答
  • 2020-12-07 16:16

    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.

    0 讨论(0)
  • 2020-12-07 16:18

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

    0 讨论(0)
  • 2020-12-07 16:21

    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.

    0 讨论(0)
  • 2020-12-07 16:24

    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.

    0 讨论(0)
  • 2020-12-07 16:26
    1. Enable your user to run gdb 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 eclipse 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

    0 讨论(0)
  • 2020-12-07 16:27

    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.

    0 讨论(0)
提交回复
热议问题