You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac OS下GDB无法运行程序:未设断点却报内存访问错误

解决Mac OS中GDB启动程序时报"Cannot insert breakpoint -1"的问题

我之前在Mac上用GDB调试程序时也碰到过一模一样的问题——没设任何断点却弹出内存访问错误,程序直接启动失败。结合你的报错日志来看,核心原因大多和Mac的系统完整性保护(SIP)限制以及GDB的签名权限有关,下面是我亲测有效的解决步骤:

先还原下你的操作和报错场景,方便其他同学参考:

rihiroshibitekiMacBook-Pro:desktop Richard$ gdb test
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin17.3.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...Reading symbols from /Users/Richard/Desktop/test.dSYM/Contents/Resources/DWARF/test...done.
done.
(gdb) r
Starting program: /Users/Richard/Desktop/test
[New Thread 0x1903 of process 776]
[New Thread 0x1c03 of process 776]
Warning: Cannot insert breakpoint -1.
Cannot access memory at address 0xf726
Command aborted.

解决方案一:给GDB进行代码签名(最关键)

Mac的SIP会限制未经签名的调试器对进程进行调试操作,所以第一步必须给GDB签名:

  • 打开「钥匙串访问」应用,点击顶部菜单栏的「钥匙串访问」->「证书助理」->「创建证书」
  • 在弹出窗口中:
    • 名称填gdb-cert(随便起个好记的名字就行)
    • 身份类型选择「自签名根证书」
    • 证书类型选择「代码签名」
    • 勾选「让我覆盖这些默认值」,然后点击「继续」
    • 后续步骤一路点击「继续」,最后选择存储到「登录」钥匙串,点击「创建」
  • 找到刚创建的gdb-cert证书,右键选择「显示简介」,切换到「信任」标签页,把「代码签名」设置为「始终信任」
  • 打开终端,运行命令给GDB签名(如果你的GDB安装路径不是/usr/local/bin/gdb,请替换成实际路径):
    codesign -fs gdb-cert /usr/local/bin/gdb
    
  • 最后重启系统的任务控制服务,让签名生效:
    sudo killall taskgated
    

解决方案二:放宽SIP的调试限制(如果签名后仍无效)

如果签名后还是报错,那需要调整SIP的设置,允许调试操作:

  • 重启你的Mac,按住Command + R进入恢复模式
  • 在恢复模式里打开「终端」,输入以下命令:
    csrutil enable --without debug
    
  • 输入完成后重启Mac,回到正常系统再尝试用GDB调试

额外检查项

  • 确认你的程序是用-g参数编译的(从你的日志看已经成功读取符号,这一步应该没问题,但还是提一下,比如gcc -g test.c -o test)
  • 如果你的GDB版本比较老(比如你用的8.1),可以尝试更新到最新版本,比如用Homebrew安装brew install gdb,新版本对Mac系统的兼容性更好

内容的提问来源于stack exchange,提问作者Hongwei Li

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:40:32