GDB kind of doesn't work on macOS Sierra

匆匆过客 提交于 2019-11-27 10:44:30
Salamit

This is how I easily fixed the issue. [Update: based on feedback received and yet to be verified, it seems that this solution works with macOS Sierra 10.12 but not with macOS Sierra 10.12.2]

See video instructions here

  1. Quit gdb
  2. Using your text editor e.g. Sublime Text, save a file called .gdbinit in your user folder.
  3. In the file add the following: set startup-with-shell off
  4. Save the file
  5. gdb should now work

Sources

https://discussions.apple.com/thread/7684629?start=0&tstart=0

Where is .gdbinit is located and how can I edit it?

https://sourceware.org/gdb/onlinedocs/gdb/Starting.html

I got the same error after updating to macOS Sierra. Temporarily I changed debugger to LLDB using the library lldbmi2:
1. git clone https://github.com/freedib/lldbmi2.git lldbmi2
2. cd lldbmi2
3. mkdir build
4. cd build
5. cmake ../
6. make
7. sudo make install

Once lldbmi2 is installed, you can debug your application by creating a new C/C++ Application in Debug Configurations... and change the GDB debugger (in Debugger tab) from gdb to lldbmi2. Options to lldbmi2 may be set there. Something like /usr/local/bin/lldbmi2 --log.

Everything seems to be working fine, and even better as GDB.

user3869292

This is due to a Runtime Integrity Protection feature in Sierra - you can deactivate it by following the instructions here.

  1. Reboot your system Keep command+R pressed until the Apple logo appears on the screen.
  2. Select the menu Utilities/Terminal Type "csrutil enable --without debug" in the terminal
  3. Finally, reboot your machine again

Note that disabling this will lower the security of your system, so doing the above should really be your decision.

Another impact of this change is that the DYLD_LIBRARY_PATH variable is no longer reset when spawning new processes via the shell. This variable is used by the dynamic linker to find dynamic libraries. It takes precedence over the search path coded in the executables, so is considered as unsafe by the OS. As a result, macOS by default unsets the variable so that the executable you spawn uses its own libraries. We recommend using the DYLD_FALLBACK_LIBRARY_PATH instead, which comes after the application's library search path, in case some libraries are still not found.

Multiple solutions that worked for Sierra 10.12.0 do not work with Sierra 10.12.1. With this version, you need an updated version of GDB (patch committed at FSF on Nov 9), in addition to disabling spawning of shell with 'set startup-with-shell off'. See instructions at http://blog.adacore.com/gnat-on-macos-sierra

assume your MacOS version is 10.12.6.

  1. upgrade your gdb to version 8.0.1 brew upgrade gdb
  2. execute echo "set startup-with-shell off" >> ~/.gdbinit ( I saw this command when I installed gdb by brew )
  3. create a certificate with name gdb-cert and trust this certificate in code signing option
  4. reboot your mac
  5. execute sudo codesign -s gdb-cert /usr/local/bin/gdb
  6. done! have a fun ~

BTW,you can use lldb to replace gdb .

I've been having multiple issues with Sierra.

For starters my code that had worked on previous OSX versions has stopped working on this version. Nor would it compile.In addition GDB from brew is a complete mess. To add on that other 3rd party libraries got broken (e.g. libevent).

("Good job" Apple).

After "upgrading" to Sierra I suggest the following steps to get GDB working:

  1. Install a newer gcc (Should take about 60 minutes - depending on your CPU etc...)

    brew install gcc

  2. Download the source code of GDB

  3. Since gcc and g++ are aliased to the old gcc and g++ make sure to link it to the newer gcc and g++ e.g.:

    export CC=`which gcc-6`

    export CXX=`which gxx-6`

  4. Configure & Compile gdb:

    ./configure

    make CFLAGS=-Wno-error=deprecated-declarations CXXFLAGS=-Wno-error=deprecated-declarations

    sudo make install

fgriberi

This works for me:

  • Unlink current gdb: brew unlink gdb

  • Install gdb 8.0.1: brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/9ec9fb27a33698fc7636afce5c1c16787e9ce3f3/Formula/gdb.rb

  • Optional: avoid upgrade gdb with brew pin gdb

I know my answer is not specifically related to GDB, but since I also had some difficulties making GDB to work myself, I'd like to recommend you to give a try on LLDB. For me, it worked like a charm:

https://developer.apple.com/library/content/documentation/IDEs/Conceptual/gdb_to_lldb_transition_guide/document/lldb-terminal-workflow-tutorial.html

Since Xcode itself now uses LLDB instead of GDB, this can be a more convenient alternative for Mac users. And, in my particular case, it integrated much easier in Eclipse than GDB:

https://wiki.eclipse.org/CDT/User/FAQ#How_do_I_get_the_LLDB_debugger.3F

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!