mac上配置GDB遇到的坑及解决方法

本文介绍了解决MacOSX环境下创建codesign时遇到未知错误的方法,并提供了解决GDB启动时出现SIGTRAP信号的具体步骤。

一.创建codesign遇到未知错误-2,147,414,007

ps:具体怎么创建codesign 网上都有具体的方式,可以google(用下面的指令绑定)

sudo killall taskgated | sudo codesign -fs gdb-cert /usr/local/bin/gdb

怎么解决上面的问题,可以参考以下方式(https://opensource.apple.com/source/lldb/lldb-69/docs/code-signing.txt)

On MacOSX lldb needs to be code signed. The Debug and Release builds 
are set to code sign using a code signing certificate named 
lldb_codesign. 

If you don't have one yet you will need to:
- Launch /Applications/Utilities/Keychain Access.app

- In Keychain Access select the "login" keychain in the "Keychains"
  list in the upper left hand corner of the window.

- Select the following menu item:

	Keychain Access->Certificate Assistant->Create a Certificate...

- Set the following settings

	Name = lldb_codesign
	Identity Type = Self Signed Root
	Certificate Type = Code Signing

- Click Continue
- Click Continue
- Click Done
- Click on the "My Certificates"
- Double click on your new lldb_codesign certificate
- Turn down the "Trust" disclosure triangle

	Change:
		When using this certificate: Always Trust

- Enter your login password to confirm and make it trusted

The next steps are necessary on SnowLeopard, but are probably because of a bug
how Keychain Access makes certificates.

- Option-drag the new lldb_codesign certificate from the login keychain to
  the System keychain in the Keychains pane of the main Keychain Access window
  to make a copy of this certificate in the System keychain.  You'll have to
  authorize a few more times, set it to be "Always trusted" when asked.
- Switch to the System keychain, and drag the copy of lldb_codesign you just
  made there onto the desktop.
- Switch to Terminal, and run the following:

sudo security add-trust -d -r trustRoot -p basic -p codeSign -k /Library/Keychains/System.keychain ~/Desktop/lldb_codesign.cer

- Right click on the "lldb_codesign" certificate in the "System" keychain (NOT
  "login", but the one in "System"), and select "Delete" to delete it from
  the "System" keychain.
- Reboot
- Clean and rebuild lldb and you should be able to debug.

That should do it.

ps:我的机器上根据上面的方法做了之后,再创建gdbsign就不会有问题了

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

解决方式,回退gdb版本至8.0.1

Unlink current gdb: brew unlink gdb

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

ps: 当遇到sip的问题时

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


03-22
### 安装与配置 GDB 的步骤 #### 环境准备 在 macOS 上使用 GDB 需要先安装 Homebrew 工具,这是一个包管理器,可以简化软件的安装过程。打开终端并输入以下命令来验证是否已安装 Homebrew: ```bash brew --version ``` 如果未安装,则可以通过以下命令安装 Homebrew[^2]。 ```bash /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` #### 安装 GDB 一旦 Homebrew 成功安装,可以在终端中运行以下命令来安装 GDB: ```bash brew install gdb ``` 此命令会自动下载并编译最新版本的 GDB 并将其添加到系统的路径中。 #### 添加证书 由于安全原因,在 macOS 上运行 GDB 前需为其签署代码签名证书。以下是具体步骤: 1. **创建自定义证书** 打开 `Keychain Access` 应用程序,选择菜单栏中的 `Keychain Access -> Certificate Assistant -> Create a Certificate...`。 输入名称(如 `gdb-cert`),选择类型为 `Code Signing`,勾选 `Let me override defaults` 和 `Allow all applications to access this item` 后点击继续直至完成。 2. **签署 GDB** 使用以下命令对刚安装的 GDB 进行签名: ```bash codesign -s gdb-cert $(which gdb) ``` 如果遇到权限问题,可能需要提升管理员权限,可尝试加上 `sudo` 来重新执行上述命令。 #### 解决 r 命令卡住的问题 有时当启动 GDB 时可能会因为默认设置而使调试过程中某些指令被挂起。为了防止这种情况发生,可在每次启动 GDB 时加入如下设置命令[^4]: ```bash set startup-with-shell off ``` 也可以将该命令写入 `.gdbinit` 文件以便于长期生效。`.gdbinit` 文件通常位于用户的主目录下。 #### 在 VS Code 中配置 GDB 调试环境 对于希望集成开发环境 (IDE) 提供更好支持的用户,VS Code 是一种不错的选择。其内置扩展 C/C++ 支持多种调试场景,包括通过 Launch 或 Attach 方式调用 GDB[^5]。 ##### 创建 launch.json 文件 进入项目文件夹下的 `.vscode` 子目录,编辑或新建名为 `launch.json` 的 JSON 配置文件。一个基本的例子如下所示: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/your_program_name", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb" } ] } ``` 其中 `"${workspaceFolder}/your_program_name"` 表示待调试的目标二进制文件位置。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值