13

It is possible to run my application with root priviliges using sudo, like this:

sudo ./MyApp

Is it possible to debug my Qt application using root priviliges using QtCreator? How can I do this on an Ubuntu system?

0

6 Answers 6

19
  1. Go to Tools-> Options-> Environment
  2. In the Tab General under **System** Group there is a Terminal Option.
  3. The default value is set to/usr/bin/xterm -e. Replace it with /usr/bin/xterm -e sudo or /usr/bin/gnome-terminal -x sudo.
  4. Press Apply and OK Buttons.
  5. Under Mode Selector click on Projects, select Run Option. Under Run Group Box select Run in Terminal.

Remember : sudo option must be there

Edit /etc/sudoers using sudo visudo

Next time when you run the program it will be invoked in a new xterm or gnome-terminal with Super User Permission and will ask for the root password.

screen shot of the result

4
  • 2
    This is the right solution
    – Ray
    Aug 11, 2016 at 20:22
  • 2
    what should we add in /etc/sudoers?
    – Necktwi
    Nov 16, 2016 at 10:09
  • 4
    Does not work for me, after entering sudo password Application Output reports "Debugging has finished", no one of breakponts really breaks
    – user636443
    Jan 21, 2017 at 9:29
  • For the record, "Mode Selector" is the left most button panel with Welcome at the very top
    – Eric
    Aug 6, 2019 at 0:51
3

I solved it by starting Qt Creator as root.

sudo /usr/bin/qtcreator

Now It will get the root permission to the application when I compile and debug. Now its possible to debug my application with root privilege.

4
  • 3
    That's a very very very stupid thing to do. I am not trying to be offensive. STUPID in the sense that it's not a smart thing to do. And I am sure that most people would NEVER do this.
    – iharob
    Sep 10, 2015 at 23:39
  • 1
    Don't be a windows user. Don't run everything as root.
    – Shahbaz
    Feb 18, 2016 at 20:25
  • 1
    Don't do this! This can change the ownership of the source files that you save under this condition, plus the ownership your .pro.user, and other Qt Creator configuration files. When you switch back to running as a regular user, you'll run into errors and have to jump through some hoops to restore everything. I learned this the hard way...
    – BuvinJ
    Apr 18, 2019 at 21:08
3

I would run the program from a terminal with a GDB server (as root) and then connect to the server with gdb from QtCreator. You would start gdbserver with something like this:

$ sudo gdbserver host:2345 ./MyApp

Here, you are hosting the server with port 2345. You can also check if your application is indeed running with UID 0.

$ sudo ps -u

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.

1

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 qtcreator from a tty and auth sudo before you launch qtcreator, or configure your user for passwordless sudo (not recommended). I'll code a more complete solution later.

It took me to 1-2 hours code/debug with no prior knowledge of QT. Most that time was spent waiting for the compilation to complete

https://gist.github.com/ppetraki/a0080da047047ea184c6

--- qtcreator-3.0.1.orig/src/plugins/debugger/gdb/gdbprocess.cpp +++ qtcreator-3.0.1/src/plugins/debugger/gdb/gdbprocess.cpp @@ -59,7 +59,11 @@ QByteArray GdbProcess::readAllStandardEr

void GdbProcess::start(const QString &cmd, const QStringList &args) { - m_gdbProc.setCommand(cmd, Utils::QtcProcess::joinArgs(args)); + QStringList sudoArgs; + sudoArgs << cmd << args; + QString sudoCmd = QString::fromUtf8("/usr/bin/sudo"); + + m_gdbProc.setCommand(sudoCmd, Utils::QtcProcess::joinArgs(sudoArgs)); m_gdbProc.start(); }

1

The following actions were recommended on Qt support forum:

In Qt Creator, add remote Linux device in Options -> Devices. Set its address as localhost and user as root. Create a pair of authentication keys and paste public key to /root/.ssh/authorized_keys. Then clone Desktop kit in Options -> Kits and set the device for new kit to the device you've created on previous step.

Now, when you start debugging, Qt Creator should automatically connect over ssh to localhost as root, start gdbserver and perform debugging.

It works for running without debugging too.

Perhaps you should set up installation of your program on remote host, but that's a different story and it's done differently for Qmake and QBS.

https://forum.qt.io/post/185983

0

One easy way is to run the project in a terminal check Run as a Root user.

Here is Screenshot to show steps:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .