Debug a C/C++ application which requires root privileges using Eclipse CDT

Hi! It's been a long time, but this blog is not dead! It just happened that so far, I haven't ran into a case which hadn't been documented by someone else already. I am glad to inform that today, I found good enough reason to make a post, since the information was already in the web, but scattered among different sites. So I am putting it together here for future reference.

Going back to the problem described in the title: initially, you would think that for a scenario like this, the only way is to run Eclipse itself with root privileges. However, security concerns aside, doing so would imply losing all the Eclipse settings, because they would be associated to the original non-root user.

The optimal solution would be to refactor the application to avoid requiring root privileges. Sometimes, that can be achieved without even touching the app's source code. For example, it might be possible to define a user group with the specific access rights, and add the user that runs the program to that group. If that is not possible, the code which requires root privileges should be separated as much as possible from the rest, maybe in a separate binary. If this degree of change is not feasible, however, a good compromise would be the following:

  • Run Eclipse as usual (without root privileges).
  • Run the debugger, typically gdb, with root privileges.

Configuring Eclipse to run gdb with root privileges is quite straightforward. First, locate the Debug configurations dropdown widget. Then, select your debug configuration and click the gear icon in order to edit it. In my box, the aforementioned gear icon is located here (red circle):

In this example, the debug configuration we want to edit is called "scamper". You can also get there via Debug - Debug Configurations ... from the main menu. Inside that form, go to the Debugger tab. There, assuming you are running Ubuntu or some similar distro, you want to prepend sudo to gdb, like so (red rounded rectangle):

For other Linux distros, you will need to replace sudo with their equivalent. However, this is not enough, because by default sudo requires the user to input his password. When Eclipse tries to run gdb, it will return an error along the lines of needing to specify the root user password. To get around this, it is possible to configure sudo to not ask for a password for specific programs. This can be done via the terminal with the following command: sudo visudo.

This will open the sudo configuration file for editing. Look for the #includedir /etc/sudoers.d line near the end, and below it, add a line for the gdb binary, and another one for the C++ application. In my case, it looks like this (added text highlighted with red):

You will need to replace "dario" in both lines with your (non-root) user name. Also, your path to gdb might be different. You can check by running which gdb in the terminal. The path to the C/C++ application to debug will also be different, so edit it accordingly. After saving the changes to this file, for the changes to be effective, you will need to:

  • Restart Ubuntu OR log out and log in OR sudo service sudo restart (I restarted Ubuntu).
  • Restart Eclipse.

Like I said, this is not the best security-wise, but it gets the job done. Happy debugging!

Comments

  1. terima kasih atas tutorialnya bro, this is a good example bro! thank you

    software engineer blog

    ReplyDelete

Post a Comment

Popular posts from this blog

VB.NET: Raise base class events from a derived class

Apache Kafka - I - High level architecture and concepts

Upgrading Lodash from 3.x to 4.x