以 root 身份在 XCode 中调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1033026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debugging in XCode as root
提问by Anton
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()).
在我的程序中,我需要创建套接字并将它们绑定到侦听 HTTP 端口 (80)。当我使用 sudo 从命令行启动它时,该程序运行良好,将权限提升到 root。在 XCode 下运行会在调用绑定函数 (asio::ip::tcp::acceptor::bind()) 时出现“权限被拒绝”错误。
How can I do debugging under XCode?
如何在 XCode 下进行调试?
All done in C++ and boost.asio on Mac OS X 10.5 with XCode 3.1.2.
全部使用 C++ 和 boost.asio 在 Mac OS X 10.5 和 XCode 3.1.2 中完成。
采纳答案by BJ Homer
Update: For Xcode 4.5 and later, see this answerinstead.
更新:对于 Xcode 4.5 及更高版本,请参阅此答案。
The only way I'm aware of to do what you're asking is to run Xcode as root.
我知道做你要求的唯一方法是以root身份运行Xcode。
>> 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.
一旦您以 root 身份运行,任何从 Xcode 启动的进程也将以 root 身份运行。但是请注意,如果您创建或编辑任何文件,它们将由 root 拥有,这意味着您必须先拥有chown
它们,然后才能首先以普通用户身份编辑它们。
I'd love a way for Xcode to say "Launch process as root", but as far as I know, no such functionality is available.
我很喜欢 Xcode 说“以 root 身份启动进程”的方式,但据我所知,没有这样的功能可用。
Note that you can also run the application within the command-line debugger gdb
to debug your application. Run
请注意,您还可以在命令行调试器中运行应用程序gdb
来调试您的应用程序。跑
>> 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.
然后你可以保持 Xcode 打开,随意修改,并在 gdb 中调试你的程序。这就是我通常所做的。
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.
编辑:来自未来的读者:请参阅 Alexander Stavonin 的回答;它讨论了如何做到这一点。如果您可以使用 ssh 密钥并在您的系统上启用 root 用户,那么他的答案就是要走的路。
回答by Mike.R
In Xcode 4.5 Product->Edit Scheme: Look in the Infotab under Debug Process Asand choose the rootoption.
在 Xcode 4.5 Product-> 中Edit Scheme:查看Debug Process As下的Info选项卡,然后选择root选项。
回答by Alexander Stavonin
Xcode is able to run the debugging application as root. For this purpose the following steps should be done:
Xcode 能够以 root 身份运行调试应用程序。为此,应执行以下步骤:
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.
Enable remote loging.
a. In System Preferences… -> Sharing, check Remote Login. This option turns on the ssh demon.
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.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.
Everything should be Ok now ?
为本地机器启用 root 用户。
一种。运行“目录实用程序”(/System/Library/CoreServices/ Directory Utility.app)
湾 从编辑菜单中选择“启用根用户”并输入根密码。
启用远程登录。
一种。在系统偏好设置…-> 共享中,勾选远程登录。此选项打开 ssh 恶魔。
为 root 用户创建 ssh 公钥/私钥并在 .ssh/authorized_keys 文件夹中复制公钥。
一种。在本地机器上打开终端并输入
ssh-keygen -t rsa
湾 接受默认位置并键入 root 的密码。
C。以 root 身份登录
su -
并创建 ~/.ssh 目录。(~ == /var/root)d. 将公钥复制到根目录:
cat ~/.ssh/id_rsa.pub | ssh root@localhost "cat - >> ~/.ssh/authorized_keys"
e. 检查是否一切正常。键入
ssh root@localhost
。它不应该要求输入 root 密码。在 Xcode 中通过 ssh 启用远程调试。
一种。选择?获取信息?从下拉菜单?可执行文件?。
湾 在“调试”设置中检查?通过 ssh 远程调试可执行文件?并将 root@localhost 作为 ?Connect to? 信息。
现在一切都应该好了?
回答by bitmusher
XCode no longer supports remote debugging rendering most of these answers obsolete.
XCode 不再支持远程调试,从而使这些答案中的大多数都过时了。
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.
以 root 身份调试程序现在很容易。在“产品”菜单下,使用“编辑方案...”并在“运行”方案“信息”窗格中,选择“以 root 身份调试进程”。您必须进行一次身份验证。
As of Xcode 4.5 I got a strange error until I quite Xcode and restarted.
从 Xcode 4.5 开始,我遇到了一个奇怪的错误,直到我关闭 Xcode 并重新启动。
回答by Michael Gr?nager
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
如果您想调试运行在 443 端口的 Web 服务器,您可以改为在普通帐户的 8443 端口上运行它,然后执行以下操作: 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.
然后您仍然可以在正确的端口上联系您的应用程序,但具有作为普通用户运行应用程序的优势。
回答by Cheng
Debug as a root via SSH.
通过 SSH 以 root 身份进行调试。
Edit Active Executable -> Debugging -> Debug executable remotely via SSH.
通过 SSH 远程编辑活动可执行文件 -> 调试 -> 调试可执行文件。