设为首页 收藏本站
查看: 777|回复: 0

[经验分享] Installing GDB on OS X Mavericks (mac 上安装gdb)

[复制链接]

尚未签到

发表于 2015-12-31 09:00:02 | 显示全部楼层 |阅读模式
  With its new OS release, Apple has discontinued the use of GDB in OS X. Since 2005 Apple has steadily been moving away from the GNU toolchain in favor of LLVM. This means that Xcode now uses LLDB instead.
  LLDB looks to be a very nice replacement for GDB, and I hope to use it in the future, but currently Xcode is the only graphical front-end that supports its use; pretty much every other debugging GUI uses GDB under the hood, including Eclipse. So, if you want to debug C/C++ code in Eclipse CDT on the Mac, you must install GDB.1
  Here is the procedure that worked for me.2 Others have reported issues with this, so please do let me know in the comments if it doesn't work for you.
Known Issues
  GDB will not be able to breakpoint inside any template function, though it should be able to step into it. This problem may be resolved if you use the MacPorts installation procedure (below) but it may only work if you also compile with Apple's GCC.
  It was also reported in the comments that it cannot breakpoint into a shared library function. I have not confirmed this issue myself.
Installing GDB
  You can install via MacPorts or Homebrew. MacPorts has Apple's official GDB distribution, which is modified for OS X. This is probably the best option (thanks to CC's comment for this tip). However, on my machine this only seems to work if the program is compiled using Apple's GCC, which is no longer supported by Apple. All things being equal, I vastly prefer to avoid MacPorts altogether. So I installed with Homebrew, despite recommending MacPorts. If you have no preference either way, go with MacPorts.
Install with MacPorts

  • Install Xcode and MacPorts, if not already installed.
  • Now install the Apple GCC and GDB from MacPorts:

    $ sudo port install gdb-apple
    $ sudo port install apple-gcc42
  • For the remainder of the tutorial, use /opt/local/bin/gdb-apple as the GDB executable
  • Remember if you want breakpoints in template functions to work, you'll need to change your compiler to g++-apple-4.2 instead of g++! This can be done in your Makefiles or in your IDE settings.
Install with Homebrew

  • Install Xcode and Homebrew, if not already installed.
  • Now install GDB from Homebrew:

    $ brew tap homebrew/dupes
    $ brew install gdb
  • For the remainder of the tutorial, use /usr/local/bin/gdb as the GDB executable
  If that worked, then lucky you! Getting it compiled is where many people seem to have trouble. Now you just need to sign it to give it permission to control OS X processes.
Certifying GDB
  Open up the Keychain Access application (/Applications/Utilities/Keychain Access.app). Navigate via the menu to Keychain Access > Certificate Assistant > Create Certificate...
DSC0000.png
  Enter a name for the certificate. For this how-to, I'll call it "gdb-cert". Set the fields exactly as shown below.
DSC0001.png
  The maximum validity period is 999 days. I don't really want to deal with this again, so I'm going to max it out.
DSC0002.png
  Keep clicking the "Continue" button until you are asked for a location. Set it to "System".3
DSC0003.png
  Success!
DSC0004.png
  Now make sure the cert is always trusted. Right-click the new certificate and select Get Info. Under the Trust section, set Code Signing to Always Trust.
DSC0005.png
DSC0006.png
  Now that we have a certificate, we need to use it to sign GDB. First, we'll restart the taskgatedprocess to make sure it picks up the new certificate. Quit Keychain Access (you must quit Keychain Access!) and return to the Terminal for these final commands.
  Find the taskgated process.


$ ps -e | grep taskgated
56822 ??         0:03.11 /usr/libexec/taskgated -s
60944 ttys002    0:00.00 grep --color=auto taskgated  The first number in the above output is the PID. Use this to kill the process (it will immediately restart itself).


$ sudo kill -9 56822  Now you can finally code sign GDB.


# MacPorts version
$ codesign -s gdb-cert $(which gdb-apple)
# Homebrew version
$ codesign -s gdb-cert $(which gdb)  Now you should be all set! The OS X Keychain may ask for your password the first time you attempt to debug a program, but it should work!
Getting it to Work with Eclipse
  There's one more step for Eclipse users. You need to specify where Eclipse can find the new GDB. Specify the path to GDB in Preferences > C/C++ > Debug > GDB:
DSC0007.png
  If you already have some debug configurations, you may need to edit them individually to point to the correct place (under Run > Debug Configurations...):
DSC0008.png

    • The CDT developers are planning to support LLDB, but they will have to write a whole new interface, and I think most of them only work on Eclipse in their spare time, so it will likely be at least some months before LLDB support is there.
    • The procedure is derived from this StackOverflow post and this GDB Wiki page.
    • If you are unable to save it to the System keychain, then save it to the login keychain. You can later export the cert, and then import it into the System keychain. I didn't have to do this, so comment if you have any problem.


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-158675-1-1.html 上篇帖子: Mac OS X 安装protobuf 2.6.1 下篇帖子: 回顾苹果操作系统Mac OS的发展历史
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表