7

I know that this question was asked many times on various sites, but I couldn't find any recommendations how to fix my problem. I'm working with GCC 4.8.1 in NetBeans 8.0 and need to (visually) debug a program, which accesses raw devices, so I need to run it via sudo.

My project properties settings are:

  • Run -> Run Command = "sudo ${OUTPUT_PATH}"
  • Run -> Console Type = "External Terminal"
  • Run -> External Terminal Type = "Default"

All the other settings are default ones, including Debug -> Debug Command, which is empty.

So, the program works fine when I run it in NetBeans - sudo asks for a password and then the program continues. However, I can't debug it in the NetBeans - the debugger outputs the text below and stops.

Debugger External Terminal

Any ideas? Please don't suggest running the NetBeans as root - it's too troublesome for me.

(I'm on Xubuntu 3.11, which runs as a guest OS in the VMWare Fusion VM on Mac)

UPDATE FROM 2015/09/16:

According to multiple advices (from the net) I tried to replace the Debug Command in the Tools -> Options -> C/C++ pop-up window by my script with the following content:

#!/bin/bash
PROG=$(which gdb)
sudo $PROG "$@" 

This script works fine from command line. However, when I try to debug my program from NetBeans I get the following pop-up window:

NetBeans message

So, the first problem goes away and the second one pops up. I saw a number of recommendations to clear breakpoints in this case - it didn't help me.

Any ideas what to try next?

(The gdb version is GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04)

HEKTO
  • 3,876
  • 2
  • 24
  • 45
  • have you tried `sudo -E`? That could very well work (also try it when running Netbeans as root). It uses the environment of the user before impersonating root – sehe Jun 03 '14 at 22:25
  • @sehe - yes, I have - it didn't help – HEKTO Jun 07 '14 at 01:15
  • Not exactly what you are looking for, but I was able to use Netbeans together with the gdbserver (first you have to install gdbserver pluign). Launch `sudo gdbserver host:port ./prog args` in the remote machine. Put a breakpoint in your code, now from "Attach Debugger" choose gdbserver, in target put `remote host:port`, press OK and the breakpoint should hit. – Ismael Jul 22 '16 at 20:52

7 Answers7

2

A bit late, but I ran across this question while I was looking for a solution to the exact same problem.

Create a script file with the following:

#!/bin/bash
sudo /usr/bin/gdb $*

Change the permissions of the script file to executable for your user.

In NetBeans, go to Tools -> Options -> C/C++ -> Debugger Command -> script_file_name

Shaun
  • 21
  • 2
  • 1
    I tried that - got a pop-up window with the message `\"/debug.sh\": not in executable format: File format not recognized` – HEKTO Sep 16 '15 at 15:10
1

The best that come up to my mind is launching gdb with sudo and forcing the system to not ask for a password. I believe there is already an answer of your question (although for Eclipse, but essentially is the same thing for Netbeans) here https://stackoverflow.com/a/8143245/3093378

Hope this helps.

Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • I already have this setting in `sudoers`, which allows me to run `gdb` without password. Sorry to say, it doesn't help in case I run the debugger in NetBeans. I suspect there is something else between NetBeans and `gdb` which I need to configure. The `sudo -U gdb` in the "Debug Command" doesn't help either. – HEKTO Jun 03 '14 at 19:38
  • Do any of the following apply in your case? 1) The first is that the file system the sudo binary is located on is mounted with the 'nosuid' mount option, which disables setuid binaries. 2) The other is that sudo is installed on an NFS-mounted file system that is exported without root privileges. By default, NFS file systems are exported with uid 0 mapped to a non-privileged uid (usually -2). – vsoftco Jun 03 '14 at 20:32
  • And can you debug your program from the command line? I.e. run `gdb executable`, then try to step into it (press `s` then keep pressing `Enter`) – vsoftco Jun 03 '14 at 20:33
  • The `sudo` binary is located on the `/dev/sda1` inside the Xubuntu VM, however the binary which I debug is located on the host machine disk, which is mounted as a `vmhgfs` filesystem into the VM. Mount flags are `(rw,ttl=1)`. Yes, I can debug my program from command line, using `sudo gdb `. – HEKTO Jun 03 '14 at 22:23
  • I cannot say for sure what's going on, but my guess is that there is something related to how the host fs is mounted... is there any possibility to copy the source code on the VM guest and see if you still have the error? I did debug before with sudo in netbeans (not in VM) and it worked. – vsoftco Jun 03 '14 at 22:26
  • I've copied the binary into my home on VM - it didn't help. The `sudo -E` option didn't help either. – HEKTO Jun 03 '14 at 23:15
  • This is strange.. have no idea what's going on... I know this is not an answer, but did you try `eclipse`? At least to see if you're getting the same error? – vsoftco Jun 04 '14 at 00:46
  • @HEKTO I know what the problem is. Netbeans is building the project in a temporary filesystem (tmpfs) that is mounted with `nosuid` option. I'm trying to see how to change this. – vsoftco Jun 05 '14 at 16:50
  • @HEKTO I must say I gave up... couldn't find out what is going on, I mounted all temporary filesystems without 'nosuid' flag, still the same behaviour. The interesting thing is that in `eclipse`, one can run `sudo gdb` from the IDE without bumping into your problem. You should try contacting someone from Netbeans. – vsoftco Jun 05 '14 at 20:58
0

You can try one of these,

  1. Run Netbeans as super user. To do this, start Netbeans via terminal as sudo.
  2. Login as root user. Beforethat, you need to enable root account for login (https://askubuntu.com/questions/44418/how-to-enable-root-login). Technically, any operation you do there doesnt ask authentication as you are already su!

Hope this solves your problem,

Good luck

Community
  • 1
  • 1
Tejas jain
  • 742
  • 7
  • 20
  • From my question: "Please don't suggest running the NetBeans as root - it's too troublesome for me." – HEKTO Jun 05 '14 at 14:03
0

Have you tried just "sudo gdb" as Debug command (or running Netbeans as root, kind of last resort)?

  • Yes, I tried to set Debug Command as "sudo gdb" - it doesn't solve the problem, the sudo still complains about effective user id. I suspect the problem is related to the way the NetBeans calls the debugger. So - I see two ways: (1) to hack into the NetBeans (2) to ask NetBeans-aware people (what I'm doing now). – HEKTO Jun 05 '14 at 14:08
  • Another option is to strace Netbeans process and find out the difference between the ways it runs program and calls debugger. In console run as root something like: "#strace -etrace=process -ff -p pid_of_parent_netbeans_process" – Sergey Nechaev Jun 05 '14 at 19:31
  • And another final option: actually your debug command is kind of "gdb sudo ${OUTPUT_PATH}" It won't work that way, because when gdb runs sudo, sudo has the same effective uid as gdb has. Try to remove "sudo" from your run command and add it to debug command instead. – Sergey Nechaev Jun 05 '14 at 19:43
  • That's hacking - I wanted to avoid it at first... As for the `gdb sudo` - you are right, the NetBeans for some reason combines "Debug Command" and "Run Command" into one command, which is a wrong idea. However, I noticed that before and tried various combinations of these commands - no way, nothing worked for me. Anyway, спасибо! – HEKTO Jun 05 '14 at 21:26
0

This is how I use it:

Run -> Run Command = sudo "${OUTPUT_PATH}"

You need to move sudo before the quotation marks.

The other settings shouldn't cause any problems but here is how I am set up:

Run -> Console Type = "Standard Output"

Run -> External Terminal Type = "Default"

Community
  • 1
  • 1
Haz
  • 1
  • 1
0
  1. Run your program, either from NetBeans or from the command-line.
  2. Then in NetBeans go to Debug -> Attach Debugger, specify the relevant project, and attach to your process.
G. Brown
  • 11
  • 2
0

Incase anyone still looking for the solution.

It doesn't solve the problem in a way that the author expected but indeed it solves it to a workable solution.

  1. Allow sshd to be sshable as root user

    • vim /etc/ssh/sshd_config
    • PermitRootLogin yes
    • systemctl restart sshd
  2. Add a host in Netbeans with root username

  3. Select newly added host as default and change the host in project properties.
  4. Clean and Build, It should Debug now without any problem.