Mac安装GDB
今天在mac上想用gdb调试一个程序, 当我在shell中输入 gdb ./bin/kldserver 查看该程序是否有调试信息时,提示没有安装gdb:
ios:kldsvc shinson$ gdb ./bin/kldserver
-bash: gdb: command not found.
于是使用mac上的包管理工具Homebrew搜索一下gdb:
ios:kldsvc shinson$ brew search gdb
==> Searching local taps...
gdbm ✔ cgdb gdb i386-elf-gdb pgdbf
==> Searching taps on GitHub...
homebrew/linuxbrew-core/gdb homebrew/linuxbrew-core/i386-elf-gdb
==> Searching blacklisted, migrated and deleted formulae...
能搜索到,于是安装gdb,但报访问权限问题:
ios:kldsvc shinson$ brew install gdb
touch: /usr/local/Homebrew/.git/FETCH_HEAD: Permission denied
Updating Homebrew...
于是ctrl+c结束更新Homebrew,使用chown获取访问权限,本想直接获取/usr/local的权限,但mac很注重安全,获取失败,只能尝试获取/usr/local/Homebrew的权限,结果成功了:
ios:kldsvc shinson$ sudo chown -R $(whoami) /usr/local
chown: /usr/local: Operation not permitted
ios:kldsvc shinson$ sudo chown -R $(whoami) /usr/local/Homebrew/
ios:kldsvc shinson$
然后开心的重新尝试安装gdb,结果还自动更新了Homebrew,但提示我的Xcode版本太低:
ios:kldsvc shinson$ brew install gdb
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (caskroom/cask, ethereum/ethereum).
==> Updated Formulae
ethereum/ethereum/cpp-ethereum ethereum/ethereum/solidity
ethereum/ethereum/ethereum
Error: Your Xcode (1) is too outdated.
Please update to Xcode 9.1 (or delete it).
Xcode can be updated from
https://developer.apple.com/download/more/
吓得我赶紧查看一下Xcode的版本:
ios:kldsvc shinson$ xcodebuild -version
Xcode 10.1
Build version 10B61
结果我的Xcode版本是比较新的,所以只能重置一下Homebrew:
ios:kldsvc shinson$ brew update-reset
==> Fetching /usr/local/Homebrew...
remote: Enumerating objects: 6335, done.
remote: Counting objects: 100% (6335/6335), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 26201 (delta 6320), reused 6328 (delta 6316), pack-reused 19866
Receiving objects: 100% (26201/26201), 6.90 MiB | 33.00 KiB/s, done.
Resolving deltas: 100% (19627/19627), completed with 952 local objects.
From https://github.com/Homebrew/brew
2225febfb..15495f0bd master -> origin/master
* [new tag] 1.3.8 -> 1.3.8
* [new tag] 1.3.9 -> 1.3.9
* [new tag] 1.4.0 -> 1.4.0
* [new tag] 1.4.1 -> 1.4.1
* [new tag] 1.4.2 -> 1.4.2
* [new tag] 1.4.3 -> 1.4.3
* [new tag] 1.5.0 -> 1.5.0
* [new tag] 1.5.1 -> 1.5.1
* [new tag] 1.5.10 -> 1.5.10
* [new tag] 1.5.11 -> 1.5.11
* [new tag] 1.5.12 -> 1.5.12
* [new tag] 1.5.13 -> 1.5.13
* [new tag] 1.5.14 -> 1.5.14
* [new tag] 1.5.2 -> 1.5.2
* [new tag] 1.5.3 -> 1.5.3
* [new tag] 1.5.4 -> 1.5.4
* [new tag] 1.5.5 -> 1.5.5
* [new tag] 1.5.6 -> 1.5.6
* [new tag] 1.5.7 -> 1.5.7
* [new tag] 1.5.8 -> 1.5.8
* [new tag] 1.5.9 -> 1.5.9
* [new tag] 1.6.0 -> 1.6.0
* [new tag] 1.6.1 -> 1.6.1
* [new tag] 1.6.10 -> 1.6.10
* [new tag] 1.6.11 -> 1.6.11
* [new tag] 1.6.12 -> 1.6.12
* [new tag] 1.6.13 -> 1.6.13
* [new tag] 1.6.14 -> 1.6.14
* [new tag] 1.6.15 -> 1.6.15
* [new tag] 1.6.16 -> 1.6.16
* [new tag] 1.6.17 -> 1.6.17
* [new tag] 1.6.2 -> 1.6.2
* [new tag] 1.6.3 -> 1.6.3
* [new tag] 1.6.4 -> 1.6.4
* [new tag] 1.6.5 -> 1.6.5
* [new tag] 1.6.6 -> 1.6.6
* [new tag] 1.6.7 -> 1.6.7
* [new tag] 1.6.8 -> 1.6.8
* [new tag] 1.6.9 -> 1.6.9
* [new tag] 1.7.0 -> 1.7.0
* [new tag] 1.7.1 -> 1.7.1
* [new tag] 1.7.2 -> 1.7.2
* [new tag] 1.7.3 -> 1.7.3
* [new tag] 1.7.4 -> 1.7.4
* [new tag] 1.7.5 -> 1.7.5
* [new tag] 1.7.6 -> 1.7.6
* [new tag] 1.7.7 -> 1.7.7
* [new tag] 1.8.0 -> 1.8.0
* [new tag] 1.8.1 -> 1.8.1
* [new tag] 1.8.2 -> 1.8.2
* [new tag] 1.8.3 -> 1.8.3
* [new tag] 1.8.4 -> 1.8.4
* [new tag] 1.8.5 -> 1.8.5
* [new tag] 1.8.6 -> 1.8.6
* [new tag] 1.9.0 -> 1.9.0
* [new tag] 1.9.1 -> 1.9.1
* [new tag] 1.9.2 -> 1.9.2
* [new tag] 1.9.3 -> 1.9.3
* [new tag] 2.0.0 -> 2.0.0
* [new tag] 2.0.1 -> 2.0.1
* [new tag] 2.0.2 -> 2.0.2
* [new tag] 2.0.3 -> 2.0.3
* [new tag] 2.0.4 -> 2.0.4
* [new tag] 2.0.5 -> 2.0.5
==> Resetting /usr/local/Homebrew...
Branch 'master' set up to track remote branch 'master' from 'origin'.
Switched to and reset branch 'master'
==> Fetching /usr/local/Homebrew/Library/Taps/caskroom/homebrew-cask...
==> Resetting /usr/local/Homebrew/Library/Taps/caskroom/homebrew-cask...
Branch 'master' set up to track remote branch 'master' from 'origin'.
Reset branch 'master'
Your branch is up to date with 'origin/master'.
==> Fetching /usr/local/Homebrew/Library/Taps/ethereum/homebrew-ethereum...
==> Resetting /usr/local/Homebrew/Library/Taps/ethereum/homebrew-ethereum...
Branch 'master' set up to track remote branch 'master' from 'origin'.
Reset branch 'master'
Your branch is up to date with 'origin/master'.
==> Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
remote: Enumerating objects: 25954, done.
remote: Counting objects: 100% (25954/25954), done.
remote: Compressing objects: 100% (6926/6926), done.
remote: Total 24068 (delta 17841), reused 23324 (delta 17140), pack-reused 0
Receiving objects: 100% (24068/24068), 8.88 MiB | 32.00 KiB/s, done.
Resolving deltas: 100% (17841/17841), completed with 1874 local objects.
From https://github.com/Homebrew/homebrew-core
9e6d6e17b..d7538ec53 master -> origin/master
==> Resetting /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
Checking out files: 100% (5010/5010), done.
Branch 'master' set up to track remote branch 'master' from 'origin'.
Reset branch 'master'
Your branch is up to date with 'origin/master'.
Homebrew重置成功后,再次尝试安装gdb,终于安装成功了:
ios:kldsvc shinson$ brew install gdb
==> Downloading https://homebrew.bintray.com/bottles-portable-ruby/portable-ruby-2.3.7.mavericks.bottle.tar.gz
######################################################################## 100.0%
==> Pouring portable-ruby-2.3.7.mavericks.bottle.tar.gz
==> Downloading https://homebrew.bintray.com/bottles/gdb-8.2.1.mojave.bottle.tar
######################################################################## 100.0%
==> Pouring gdb-8.2.1.mojave.bottle.tar.gz
==> Caveats
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
==> Summary
? /usr/local/Cellar/gdb/8.2.1: 55 files, 26.9MB
==> `brew cleanup` has not been run in 30 days, running now...
Pruned 0 symbolic links and 6 directories from /usr/local
ios:kldsvc shinson$
查看一下gdb的版本:
ios:kldsvc shinson$ gdb -version
GNU gdb (GDB) 8.2.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.
然后回到最开始的目的,检查程序是否有调试信息,-q 表示启动gdb时不显示那一长段包含版本、版权等提示信息。结果中的 (no debugging symbols found) 表示不包含调试信息,表示需要在编译程序时加上 -g 选项。最后输入 q 并 回车 退出gdb:
ios:kldsvc shinson$ gdb ./bin/kdlserver
Reading symbols from ./write...(no debugging symbols found)...done.
(gdb) q
ios:kldsvc shinson$