Debugging in XCode as root

后端 未结 6 2026
一生所求
一生所求 2020-12-03 04:59

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

相关标签:
6条回答
  • 2020-12-03 05:16

    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

    0 讨论(0)
  • 2020-12-03 05:20

    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.

    0 讨论(0)
  • 2020-12-03 05:21

    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.

    0 讨论(0)
  • 2020-12-03 05:23

    If you want to debug a web server running on port 443, you could instead run it on port 8443 on your normal account and then do a: sudo ssh user@localhost -L 443:localhost:8443 -N

    Then you can still contact your application on the right port, but have the advantage of running the application as a normal user.

    0 讨论(0)
  • 2020-12-03 05:34

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

    0 讨论(0)
  • 2020-12-03 05:34

    Debug as a root via SSH.

    Edit Active Executable -> Debugging -> Debug executable remotely via SSH.

    0 讨论(0)
提交回复
热议问题