• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

使用 dlv 调试go 程序

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

by 白振轩

对于程序运行过程中碰到的莫名其妙的问题,比如不明原因的阻塞,命令行 dlv 调试可能比 ide 调试效果更好

一 正常启动 smartraiden

./smartraiden --datadir=.smartraiden --api-address=0.0.0.0:5001 --listen-address=127.0.0.1:40001  --address="0x292650fee408320D888e06ed89D938294Ea42f99" --keystore-path ~/privnet3/keystore --registry-contract-address 0xf450955d87F23DF5DFc7297ed6DdDF4fb896Eff2  --password-file 123      --verbosity 5    --debug  --conditionquit "{\"QuitEvent\":\"EventSendRevealSecretBeforex\"}" --debugcrash  --eth-rpc-endpoint ws://127.0.0.1:8546

二 dlv 调试

  1. 出了问题,但是没有崩溃,直接 attach 即可
  2. 出了问题,但是崩溃了,可以提前设置断点

三 dlv attach

attach 成功以后,程序会停止运行

goroutines -s 查看所有 goroutine 的栈

解决问题

  1. 如果觉得某个 goroutine 有问题
    goroutine 108 就可以切换到这个 goroutine, 进行调试
  2. bt 查看堆栈
(dlv) goroutine 108
Switched from 0 to 108 (thread 4644478)
(dlv) bt
0  0x000000000402f66a in runtime.gopark
  at /usr/local/go/src/runtime/proc.go:292
1  0x000000000403f150 in runtime.selectgo
  at /usr/local/go/src/runtime/select.go:392
2  0x0000000004608329 in github.com/SmartMeshFoundation/SmartRaiden/blockchain.(*Events).startListenEvent.func1
  at /Volumes/dev/smdev2/src/github.com/SmartMeshFoundation/SmartRaiden/blockchain/events.go:275
3  0x000000000405c3d1 in runtime.goexit
  at /usr/local/go/src/runtime/asm_amd64.s:2361
  1. 在栈之间移动up/down
up
up

会显示当前可以控制的栈

Frame 2: /Volumes/dev/smdev2/src/github.com/SmartMeshFoundation/SmartRaiden/blockchain/events.go:275 (PC: 4608329)
  270:			go func(name string) {
  271:				ch := be.LogChannelMap[name]
  272:				sub := be.Subscribes[name]
  273:				defer rpanic.PanicRecover(fmt.Sprintf("startListenEvent %s", name))
  274:				for {
=> 275:					select {
  276:					case l, ok := <-ch:
  277:						if !ok {
  278:							//channel closed
  279:							return
  280:						}
(dlv)
  1. 查看局部变量
    locals 可以显示目前所有的局部变量,也可以通过p 来打印具体的局部变量和全局变量.
(dlv) p ch
chan github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log {
	qcount: 0,
	dataqsiz: 10,
	buf: *[10]struct github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log [
		(*github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log)(0xc420335500),
		(*github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log)(0xc4203355a8),
		(*github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log)(0xc420335650),
		(*github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log)(0xc4203356f8),
		(*github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log)(0xc4203357a0),
		(*github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log)(0xc420335848),
		(*github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log)(0xc4203358f0),
		(*github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log)(0xc420335998),
		(*github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log)(0xc420335a40),
		(*github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log)(0xc420335ae8),
	],
	elemsize: 168,
	closed: 0,
	elemtype: *runtime._type {
		size: 168,
		ptrdata: 56,
		hash: 3390961113,
		tflag: tflagUncommon|tflagExtraStar|tflagNamed,
		align: 8,
		fieldalign: 8,
		kind: 25,
		alg: *(*runtime.typeAlg)(0x5218c40),
		gcdata: *72,
		str: 56993,
		ptrToThis: 1205856,},
	sendx: 0,
	recvx: 0,
	recvq: waitq<github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log> {
		first: *(*sudog<github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log>)(0xc4304ae4e0),
		last: *(*sudog<github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log>)(0xc4304ae4e0),},
	sendq: waitq<github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log> {
		first: *sudog<github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log> nil,
		last: *sudog<github.com/SmartMeshFoundation/SmartRaiden/vendor/github.com/ethereum/go-ethereum/core/types.Log> nil,},
	lock: runtime.mutex {key: 0},}
(dlv)

可以看到 ch 这个 chann 目前没有任何数据,所以阻塞.
5. 也可以查看全局变量.也就是包级别的变量.

(dlv) p "github.com/SmartMeshFoundation/SmartRaiden/params".MobileMode
false
(dlv) p "github.com/SmartMeshFoundation/SmartRaiden/params".ChainID
*math/big.Int {
	neg: false,
	abs: math/big.nat len: 1, cap: 5, [8888],}
(dlv)

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
gosocket服务端处理多用户发布时间:2022-07-10
下一篇:
GO语言学习笔记(四、数组Array)发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap