mac OS GDB 调试golang环境

发现问题

gdb调试时golang时

go build -gcflags "-N -l" -ldflags=-compressdwarf=false gdb/main.go

出现

gdb please check gdb is ** - see taskgated()

环境及现有配置

mac OS GDB 调试golang环境_第1张图片

解决问题

1. 执行以下一个脚本

#!/bin/bash

# This script is copied from https://github.com/llvm/llvm-project/blob/master/lldb/scripts/macos-setup-codesign.sh

CERT="gdb_codesign"

function error() {
    echo error: "$@" 1>&2
    exit 1
}

function cleanup {
    # Remove generated files
    rm -f "$TMPDIR/$CERT.tmpl" "$TMPDIR/$CERT.cer" "$TMPDIR/$CERT.key" > /dev/null 2>&1
}

trap cleanup EXIT

# Check if the certificate is already present in the system keychain
security find-certificate -Z -p -c "$CERT" /Library/Keychains/System.keychain > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo Certificate has already been generated and installed
    exit 0
fi

# Create the certificate template
cat <$TMPDIR/$CERT.tmpl
[ req ]
default_bits       = 2048        # RSA key size
encrypt_key        = no          # Protect private key
default_md         = sha512      # MD to use
prompt             = no          # Prompt for DN
distinguished_name = codesign_dn # DN template
[ codesign_dn ]
commonName         = "$CERT"
[ codesign_reqext ]
keyUsage           = critical,digitalSignature
extendedKeyUsage   = critical,codeSigning
EOF

echo Generating and installing gdb_codesign certificate

# Generate a new certificate
openssl req -new -newkey rsa:2048 -x509 -days 3650 -nodes -config "$TMPDIR/$CERT.tmpl" -extensions codesign_reqext -batch -out "$TMPDIR/$CERT.cer" -keyout "$TMPDIR/$CERT.key" > /dev/null 2>&1
[ $? -eq 0 ] || error Something went wrong when generating the certificate

# Install the certificate in the system keychain
sudo security add-trusted-cert -d -r trustRoot -p codeSign -k /Library/Keychains/System.keychain "$TMPDIR/$CERT.cer" > /dev/null 2>&1
[ $? -eq 0 ] || error Something went wrong when installing the certificate

# Install the key for the certificate in the system keychain
sudo security import "$TMPDIR/$CERT.key" -A -k /Library/Keychains/System.keychain > /dev/null 2>&1
[ $? -eq 0 ] || error Something went wrong when installing the key

# Kill task_for_pid access control daemon
sudo pkill -f /usr/libexec/taskgated > /dev/null 2>&1

# Exit indicating the certificate is now generated and installed
exit 0

2. 创建 钥匙串

钥匙串访问-证书助理-创建证书-名称暂时为 gdb-cert,勾选☑️然我覆盖这些默认值
mac OS GDB 调试golang环境_第2张图片
一路点创建,到了这个位置选 系统
mac OS GDB 调试golang环境_第3张图片

3. 执行

security find-certificate -c gdb-cert

4. 信任 钥匙串

右击第二步创建的钥匙串,改为 始终信任
mac OS GDB 调试golang环境_第4张图片

5. 验证是否被信任

如果出现刚刚的 gdb-cert,代表一切顺利

security dump-trust-settings -d |grep gdb-cert

image.png

6. 进行代码签名

  1. 创建一个xml文件,gdb-entitlement.xml.注意名字对齐




    com.apple.security.cs.debugger
    

  1. 执行命令
codesign --entitlements gdb-entitlement.xml -fs gdb-cert $(which gdb)

6. 重启电脑

7. 离成功很近了

再次gdb 查看,需要验证一次密码
mac OS GDB 调试golang环境_第5张图片
然后
mac OS GDB 调试golang环境_第6张图片

其实你看这里更清楚了

你可能感兴趣的