Why is gdb requiring root permission to debug user programs?

gdb permission denied 126
vscode debug as root
gdb run as user
how to use gdb
gdb restart program
how to use gdb c++
vsdbg must be running with root permissions
g++ executable permission denied

I have been using gdb quite successfully for a while, but I recently upgraded my version of Ubuntu, and now it seems that I can only get gdb to successfully run my program if I run as root. That is,

~ %  gdb -q sleep -ex 'run 60'
Reading symbols from /bin/sleep...(no debugging symbols found)...done.
Starting program: /bin/sleep 60
tcsh: Permission denied.
During startup program exited with code 1.
(gdb)

fails, whereas

~ %  sudo gdb -q sleep -ex 'run 60'
Reading symbols from /bin/sleep...(no debugging symbols found)...done.
Starting program: /bin/sleep 60
Running .tcshrc
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
^C
Program received signal SIGINT, Interrupt.
0x00007ffff7adada0 in __nanosleep_nocancel () at ../sysdeps/unix/syscall-template.S:82
82  ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb)

works. One clue is that in the first case, the gdb startup doesn't run my .tcshrc file, whereas in the second case it does.

It seems that this is a simple permissions issue, which I must have fixed at one time, because in the past, I have never needed to run gdb as root. After much googling, however, I wasn't able to find what I might have done (if I did in fact do something). One possible fix - set ptrace permissions - didn't seem to work.

Is there something that needs to be done to allow gdb to run programs without root privileges? I know in OSX, gdb has to be codesigned. Is there something similar for Ubuntu/Linux?

here are some ideas for debugging the gdb problem. comments aren't really feasible for such things, so I put them into an answer.

try the -n option to make sure no init file is loaded.

use the echo program instead of sleep 60 to make debugging simpler (the SIGINT thing in your example is probably specific to the sleep program.

run gdb -batch and put the rest into ~/.gdbinit:

file /bin/echo
run

add set verbose on.

don't forget to clean up ~/.gdbinit when done.

linux, Is there something that needs to be done to allow gdb to run programs without root privileges? I know in OSX, gdb has to be codesigned. Is there something  When needing to debug a program that needs root permission (for eg. needs access to RAW socket) then the debugger cannot be used currently. Two possible options: Add ability to specify "sudo" in launch configuration. User can then edit visudo to not ask for a password. Add ability to specify "sudo" in launch configuration.

This isn't a complete answer, but things are becoming clearer. The tip above was very helpful. I created the following .gdbinit file

show environment SHELL
file /bin/echo
run 'Goodbye'

and the results were interesting. If SHELL=/usr/tcsh, I get a permissions error, i.e.

~ %  setenv SHELL /bin/tcsh
~ %  gdb -q -batch
SHELL = /bin/tcsh
tcsh: Permission denied.
/home/calhoun/.gdbinit:12: Error in sourced command file:
During startup program exited with code 1.

Unsetting the shell variable works :

~ %  unsetenv SHELL
~ %  gdb -q -batch
Environment variable "SHELL" not defined.
Goodbye
[Inferior 1 (process 6992) exited normally]

In this case, run uses /bin/sh to expand the argument list. Setting SHELL to /bin/bash or /bin/dash will use those shells to expand the argument list, e.g.

~ %  setenv SHELL /bin/bash
~ %  gdb -q -batch
SHELL = /bin/bash

warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
Goodbye
[Inferior 1 (process 7280) exited normally]

Oddly, the "no loadable sections' error only happens when the shell variable is explicitly set. Another mystery.

Why /bin/tcsh doesn't work is still baffling. In my case, the permissions on /bin/tcsh are

~ %  ls -lh /bin/tcsh
lrwxrwxrwx 1 root root 13 Oct 14  2011 /bin/tcsh -> /usr/bin/tcsh
~ %  ls -lh /usr/bin/tcsh
-rwxr-xr-x 1 root root 382K Oct 14  2011 /usr/bin/tcsh

The problem could also be something in my .tcshrc file that is causing the shell to crash in this non-interactive mode.

Unable to launch debugger (gdb) with root permissions. · Issue #861 , When needing to debug a program that needs root permission (for eg. Debug' which works with gdb when running VSCode as root user. A debugger is a program that runs other programs, allowing the user to exercise control over these programs, and to examine variables when problems arise. GNU Debugger, which is also called gdb, is the most popular debugger for UNIX systems to debug C and C++ programs. GNU Debugger helps you in getting information about

I changed my login shell to bash and gdb no longer needs root permission to debug. Here is the latest :

My .gdbinit file:

(bash) ~ % more .gdbinit
show environment SHELL
file /bin/echo
run 'running .gdbinit'
(bash) ~ % 

and the results of running gdb :

(bash) ~ % gdb -q -batch
SHELL = /bin/bash
running .gdbinit
[Inferior 1 (process 3174) exited normally]
(bash) ~ % 

I still don't understand why tcsh didn't work, though, and am curious to know. So if anyone has a possible explanation, please comment.

How can I run GDB without root permissions on a Mac?, But when I start debugging a program from NetBeans (C++), I get this message: "​Type the name and password of a user in the "Developer Tools" group to allow  1 The only way to debug the setuid application properly under gdb is to run gdb as root. The most sensible way to do this for a setuid application is to attach to the application once it starts. The most sensible way to do this for a setuid application is to attach to the application once it starts.

Check if your program has executable privileges.

ls -l .
-rw-r--r-- 1 opt opt 30010 Aug 16 16:13 test

Something like 'test' maybe caused that when you debug it.

sudo, Running that as root is a horrible idea. Instead, use the source, and make qtcreator use sudo to launch gdb like this. Requires that you run  Now, in Qt Creator, go Debug -> Start Debugging -> Attach to Running Debug Server. Fill in the form the most important is the port and server address. I chose 127.0.0.1 and 2345. You can select break at main to break at the beginning of the program. GDB server doesn't start the program until you connect.

I had the same problem and the reason was that someone had set the sticky bit in the gdb executable:

cruiz> ls -l /usr/bin/gdb

-rwsr-sr-x 1 root root 4190760 2010-05-05 07:55 /usr/bin/gdb*

I changed it (chmod 755 /usr/bin/gdb) and now it works.

Before:

cruiz> gdb
...

(gdb) shell

csh: Permission denied.

After the change:

cruiz> gdb

(gdb) shell

cruiz> 

Debugging with GDB, The access mode attributes set whether GDB may make read or write Often memory mapped device registers require specific sized accesses. Its primary use is post-mortem debugging of a program that crashed while Use path as the system root for the program being debugged. GDB also disables address randomization by default, but the chance that the same address you'll get in GDB and under strace is quite small, as the execution environment under the two tools is quite different. You don't actually need to find the address under strace, you can find it in GDB: catch syscall open; run

GDB Command - Debugging with GDB, Permission is granted to copy, distribute and/or modify this document under the You can use GDB to debug programs written in C and C++. It is also no problem to require modified versions to include notice that they were modified. For instance, the struct type variable is the root and the children will represent the​  Start gdb fail when attach debug a program.(Authentication is needed to run `/usr/bin/gdb' as the super user) It's because gdb can't get the root permission when

Howto debug program that needs to run as root from gdb (Eclipse , Howto debug program that needs to run as root from gdb (Eclipse) as user You may also consider 4: change the program to not require root permissions in the  Debugging in eclipse with root permission Hi, I've been working on a project that needs to be run as root user. however when I want to debug it in eclipse, I can't run it as root user. currently, I'm debugging with GDB in root.

CS107 GDB and Debugging, This page will list the basic gdb commands you will need to use as you 2014 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or If the program you are debugging requires any command-line arguments, you Make inferences about the root cause and run experiments to validate your hypothesis.

Comments
  • I use the system command 'sleep' above to illustrate the behavior I am seeing with my own code. So I don't think the fact that sleep is root-owned is an issue.
  • did you check or modify permissions of .tcshr? why does it run tcsh anyway?
  • I did check permissions on .tcshrc, and changed them to root - that didn't help. And as for why gdb is running tcsh, I don't know. This is all very mysterious to me. I don't want to have to run gdb as root, so would really like to figure out what has changed. The example I gave above has no problem under OSX, so the issue isn't really the permissions on the executable (it seems).
  • All gdb does is execve("/bin/tcsh", ["/bin/tcsh", "-c", "exec /bin/sleep 60"]). tcsh will try to run /etc/csh.cshrc, ~/.tcshrc, and ~/.cshrc, so something in any of those could be causing problems. I'd check for a command that tries to read or write or source a file you don't have access to.
  • Thanks, Mark Plotnick. I am not sure what the problem was, but I reinstalled tcsh, checked .tcshrc permissions, restarted and now all is good. Didn't change anything in my .tcshrc file, so can't point to any dubious calls that may have been the problem. Along the way, I discovered a few cool gdb features/tricks, and so all is not lost. In any case, I'll probably stick with bash, however, since I was using is anyway (by launching bash from the command line) to get a git prompt.
  • I'd guess the latter is true: the permission denied error is from something that tcsh tries to execute and not from execution of tcsh itself. should be simple to find out but why bother?
  • Or, that tcsh is trying to do something that gdb isn't giving it permission to do.
  • I should add that I re-installed tcsh, and ran without a .tcshrc file (I moved it), the permissions denied issue is still a mystery. It is something funny with my setup - I just wish I could figure out what tcsh is trying to run that it doesn't have permission to run (gdb being root).
  • Update : I re=installed tcsh (which didn't change anything), checked (again) permission on .tcshrc, restarted and Voila, all is good with tcsh and gdb. Still no idea what happened. But here is what I learned : (1) gdb calls the shell and (2) the SHELL variable is not set automically by simply calling 'tcsh' or 'bash' from the command line (only when the shell is started as a login shell). So all is not lost.