Debugging in XCode as root

19,180

Solution 1

Update: For Xcode 4.5 and later, see this answer instead.


The only way I'm aware of to do what you're asking is to run Xcode as root.

>> sudo /Developer/Applications/Xcode.app/Contents/MacOS/Xcode

Once you're running as root, anything processes launched from Xcode will also run as root. Note, though, that if you create or edit any files, they will be owned by root, which means that you'll have to chown them before you can edit them as your normal user first.

I'd love a way for Xcode to say "Launch process as root", but as far as I know, no such functionality is available.

Note that you can also run the application within the command-line debugger gdb to debug your application. Run

>> sudo gdb /path/to/my/application

Then you could keep Xcode open, modify at will, and debug your program within gdb. This is what I generally do.

EDIT: Readers from the future: see the answer from Alexander Stavonin; it talks about how to do this. If you're okay with ssh keys and enabling the root user on your system, his answer is the way to go.

Solution 2

In Xcode 4.5 Product->Edit Scheme: Look in the Info tab under Debug Process As and choose the root option.

Solution 3

Xcode is able to run the debugging application as root. For this purpose the following steps should be done:

  1. Enable the root user for local machine.

    a. Run “Directory Utility” (/System/Library/CoreServices/ Directory Utility.app)

    b. Choose “Enable Root User” from the Edit menu and enter the root password.

  2. Enable remote loging.

    a. In System Preferences… -> Sharing, check Remote Login. This option turns on the ssh demon.

  3. Create ssh public/private keys and copy public key in the .ssh/authorized_keys folder for root user.

    a. Open terminal on local machine and type ssh-keygen -t rsa

    b. Accept the default location and type password for the root.

    c. Login as root su - and create ~/.ssh directory. (~ == /var/root)

    d. Copy the public key to the root: cat ~/.ssh/id_rsa.pub | ssh root@localhost "cat - >> ~/.ssh/authorized_keys"

    e. Check whether everything is Ok. Type ssh root@localhost. It shouldn’t ask for the root password.

  4. Enable remote debugging via ssh in Xcode.

    a. Select «Get Info» from drop-down menu on «Executables».

    b. In the "Debugging" settings check «Debug executable remotely via ssh» and put root@localhost as the «Connect to» info.

  5. Everything should be Ok now ☺

From blog

Solution 4

XCode no longer supports remote debugging rendering most of these answers obsolete.

For debugging a program as root it is now easy. Under the Product menu, use "Edit Scheme…" and in your "Run" schemes "Info" pane, select "Debug process as root". You will have to authenticate once.

As of Xcode 4.5 I got a strange error until I quite Xcode and restarted.

Share:
19,180
Anton
Author by

Anton

Updated on June 17, 2022

Comments

  • Anton
    Anton almost 2 years

    In my program I need to create sockets and bind them to listen HTTP port (80). The program works fine when I launch it from command line with sudo, escalating permissions to root. Running under XCode gives a 'permission denied' error on the call to binding function (asio::ip::tcp::acceptor::bind()).

    How can I do debugging under XCode?

    All done in C++ and boost.asio on Mac OS X 10.5 with XCode 3.1.2.