MacOS 用vs code 调试geth(go-ethereum)

寒星I · · 163 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

一、配置编译环境

安装golang

$ brew install golang

输入go env 查看环境变量,通过go get 安装的工程都会放到$GOPATH 目录下。

$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ff/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/ff/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.6/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.6/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/cw/h48n_7rj541gmtr9thyyvp_w0000gn/T/go-build747874846=/tmp/go-build -gno-record-gcc-switches -fno-common"

安装go 调试器delve

$ go get github.com/derekparker/delve

安装xcode

$ xcode-select --install

二、下载编译geth

clone 代码

$ git clone https://github.com/ethereum/go-ethereum.git

执行编译命令,工程根目录下生成geth

$ cd /Users/ff/go/src/github.com/ethereum/go-ethereum
$ go build ./cmd/geth

三、配置私有链

创建创世文件genesis.json,并初始化节点

{
  "config": {
    "chainId": 0,
    "homesteadBlock": 0,
    "eip155Block": 0,
    "eip158Block": 0
  },
  "alloc": {},
  "coinbase": "0x0000000000000000000000000000000000000000",
  "difficulty": "0x20000",
  "extraData": "",
  "gasLimit": "0x2fefd8",
  "nonce": "0x0000000000000042",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp": "0x00"
}
$ ./geth --datadir geth-data0 init ewasm-testnet-geth-config.json

创建两个账户

$ ./geth --datadir geth-data0 account new
$ ./geth --datadir geth-data0 account new

启动节点

$ ./geth \
--datadir geth-data0
--networkid 66 \
--nodiscover \
--rpcport 6001 \
--port 3001 \
--ipcpath geth1.ipc console

attach 节点,后续操作略......

$ ./geth attach ipc:geth-data0/geth1.ipc

四、配置vs code

安装插件“Go”,并配置launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/cmd/geth",
            "args": ["--datadir",
                "/Users/ff/go/src/github.com/ethereum/go-ethereum/geth-data0",
                "--networkid",
                "66",
                "--nodiscover",
                "--rpcport",
                "6001",
                "--port",
                "30001",
                "--ipcpath",
                "geth1.ipc",
                "console"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb"
        }
    ]
}

找到./internal/ethapi/api.go,搜索"Accounts()",然后在该函数中设置断点。
选择Debug > Start Debugging,或直接用快捷键F5,进入debug 模式。
启动完毕后,启动一个命令行窗口attach 该节点。输入eth.accounts查看当前节点账户,即可到达Accounts() 函数中设置的断点处。

$ ./geth attach ipc:geth-data0/geth1.ipc
INFO [08-07|17:56:21.466] Bumping default cache on mainnet         provided=1024 updated=4096
Welcome to the Geth JavaScript console!

instance: Geth/v1.9.2-unstable/darwin-amd64/go1.12.6
coinbase: 0x2d636f052802a791a4114036d2c5f91cf4549539
at block: 0 (Mon, 20 Aug 2018 05:16:36 CST)
 datadir: /Users/ff/go/src/github.com/ethereum/go-ethereum/geth-data0
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> eth.accounts
["0x2d636f052802a791a4114036d2c5f91cf4549539", "0xc5bff4b89d965e00a94827d12ccd8cc70b5e30a5"]
>

欢迎关注我们的微信公众号,每天学习Go知识

本文来自:简书

感谢作者:寒星I

查看原文:MacOS 用vs code 调试geth(go-ethereum)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:977810755

163 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传