High Sierra & GDB

虽然是出于安全性的方面的考虑,但是苹果在安全性方面的新举措往往会波及到一些我们常用的工具,使其无法在最新的macOS上运行。GDB就是这一方面的受害者。这两天我在运行OMNeT++时,需要安装GDB调试器。但是在macOS上安装GDB调试器遇到了比较大的困难。事实上使用Homebrew来安装GDB还是非常简单的

$ brew install gdb

这个命令会为你的macOS安装上最新的8.1版本的GDB。但是如果尝试运行gdb,则会遇到

During startup program terminated with signal ?. Unknown signal

或者

During startup program terminated with signal SIGTRAP, Trace/breakpoint trap.

形式的错误。

Code Sign and Initial Settings

根据gdb的文档(brew info gdb),我们需要做两个必要的改动来让gdb能够正确地运行。

You will need to codesign the binary. For instructions, see:

  https://sourceware.org/gdb/wiki/BuildingOnDarwin

On 10.12 (Sierra) or later with SIP, you need to run this:

  echo "set startup-with-shell off" >> ~/.gdbinit

事实上这个部分已经有人专门写了文章来描述流程,我就不再赘述了。参考mac 安装gdb调试的方法.

GDB 8.1的问题

然而,在macOS 10.13.4 (High Sierra)上运行8.1的GDB,在进行了上述的改动之后仍然无法工作,仍然会出现

During startup program terminated with signal SIGTRAP, Trace/breakpoint trap.

我的解决办法是降级安装8.0.1的GDB。那么怎么安装8.0.1的GDB呢?毕竟Homebrew不支持安装历史版本。这时我们需要给Homebrew手动指定gdb 8.0.1的.rb文件。运行如下命令

$ brew install https://github.com/Homebrew/homebrew-core/raw/9ec9fb27a33698fc7636afce5c1c16787e9ce3f3/Formula/gdb.rb

注意如果你已经安装了8.1的GDB,要先进行卸载

brew uninstall gdb

Now it works.


Just in case, 我把8.0.1的.rb文件保留在这里:

class Gdb < Formula
  desc "GNU debugger"
  homepage "https://www.gnu.org/software/gdb/"
  url "https://ftp.gnu.org/gnu/gdb/gdb-8.0.1.tar.xz"
  mirror "https://ftpmirror.gnu.org/gdb/gdb-8.0.1.tar.xz"
  sha256 "3dbd5f93e36ba2815ad0efab030dcd0c7b211d7b353a40a53f4c02d7d56295e3"

  bottle do
    sha256 "e98ad847402592bd48a9b1468fefb2fac32aff1fa19c2681c3cea7fb457baaa0" => :high_sierra
    sha256 "0fdd20562170c520cfb16e63d902c13a01ec468cb39a85851412e7515b6241e9" => :sierra
    sha256 "f51136c70cff44167dfb8c76b679292d911bd134c2de3fef40777da5f1f308a0" => :el_capitan
    sha256 "2b32a51703f6e254572c55575f08f1e0c7bc2f4e96778cb1fa6582eddfb1d113" => :yosemite
  end

  deprecated_option "with-brewed-python" => "with-python"
  deprecated_option "with-guile" => "[email protected]"

  option "with-python", "Use the Homebrew version of Python; by default system Python is used"
  option "with-version-suffix", "Add a version suffix to program"
  option "with-all-targets", "Build with support for all targets"

  depends_on "pkg-config" => :build
  depends_on "python" => :optional
  depends_on "[email protected]" => :optional

  fails_with :clang do
    build 600
    cause <<~EOS
      clang: error: unable to execute command: Segmentation fault: 11
      Test done on: Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
    EOS
  end

  def install
    args = [
      "--prefix=#{prefix}",
      "--disable-debug",
      "--disable-dependency-tracking",
    ]

    args << "--with-guile" if build.with? "[email protected]"
    args << "--enable-targets=all" if build.with? "all-targets"

    if build.with? "python"
      args << "--with-python=#{HOMEBREW_PREFIX}"
    else
      args << "--with-python=/usr"
    end

    if build.with? "version-suffix"
      args << "--program-suffix=-#{version.to_s.slice(/^\d/)}"
    end

    system "./configure", *args
    system "make"

    # Don't install bfd or opcodes, as they are provided by binutils
    inreplace ["bfd/Makefile", "opcodes/Makefile"], /^install:/, "dontinstall:"

    system "make", "install"
  end

  def caveats; <<~EOS
    gdb requires special privileges to access Mach ports.
    You will need to codesign the binary. For instructions, see:
      https://sourceware.org/gdb/wiki/BuildingOnDarwin
    On 10.12 (Sierra) or later with SIP, you need to run this:
      echo "set startup-with-shell off" >> ~/.gdbinit
    EOS
  end

  test do
    system bin/"gdb", bin/"gdb", "-configuration"
  end
end

你可能感兴趣的:(High Sierra & GDB)