Golang中 delve实战分析一个常驻进程挂起的问题

本文详细介绍如何使用Delve调试工具解决Golang程序中的各种调试难题,包括安装配置、调试常驻进程、查看goroutine状态及堆栈跟踪,提供了一系列实用的Delve命令行操作指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介:

Delve目的就是为了解决开发者在使用 GDB 调试中遇到的各种各样的问题

1. 首先安装

获取前,请确保 GOPATH 已经设置。Go 1.8 版本之后,GOPATH 默认在用户目录的 go 文件夹

go get -u github.com/go-delve/delve/cmd/dlv

参考:https://github.com/go-delve/delve/blob/master/Documentation/installation/linux/install.md

2.找到出问题的常驻进程 duration的pid,然后attach

[www@test3 gogin]$ pidof duration
18824

[www@test3 gogin]$ dlv attach 18824
Type 'help' for list of commands.
(dlv) 

3.通过goroutines命令查看程序中的goroutine

(dlv) goroutine
Thread 18824 at /usr/local/go/src/runtime/sys_linux_amd64.s:536
Goroutine 1:
        Runtime: /usr/local/go/src/runtime/proc.go:305 runtime.gopark (0x431150)
        User: /home/www/go/pkg/mod/github.com/gomodule/redigo@v2.0.0+incompatible/redis/pool.go:278 github.com/gomodule/redigo/redis.(*Pool).get (0x835585)
        Go: /usr/local/go/src/runtime/asm_amd64.s:220 runtime.rt0_go (0x45a3a4)
        Start: /usr/local/go/src/runtime/proc.go:113 runtime.main (0x430b60)

(dlv) goroutine 1
Switched from 0 to 1 (thread 18824)
(dlv) bt
 0  0x0000000000431150 in runtime.gopark
    at /usr/local/go/src/runtime/proc.go:305
 1  0x0000000000407828 in runtime.goparkunlock
    at /usr/local/go/src/runtime/proc.go:310
 2  0x0000000000407828 in runtime.chanrecv
    at /usr/local/go/src/runtime/chan.go:524
 3  0x00000000004074eb in runtime.chanrecv1
    at /usr/local/go/src/runtime/chan.go:406
 4  0x0000000000835585 in github.com/gomodule/redigo/redis.(*Pool).get
    at /home/www/go/pkg/mod/github.com/gomodule/redigo@v2.0.0+incompatible/redis/pool.go:278
 5  0x00000000008345b7 in github.com/gomodule/redigo/redis.(*Pool).Get
    at /home/www/go/pkg/mod/github.com/gomodule/redigo@v2.0.0+incompatible/redis/pool.go:179
 6  0x0000000000a17f0f in gogin/console/alisls/lib.(*Rds).GetWorkReading
    at ./console/alisls/lib/redis.go:82
 7  0x0000000000a1874d in gogin/console/alisls/lib.SaveDurationToAdb
    at ./console/alisls/lib/redis.go:115
 8  0x0000000000a1aa02 in main.main
    at ./console/alisls/duration.go:6
 9  0x0000000000430d7e in runtime.main
    at /usr/local/go/src/runtime/proc.go:203
10  0x000000000045c571 in runtime.goexit
    at /usr/local/go/src/runtime/asm_amd64.s:1357

从输出可以看到main的函数调用链为

main.main => lib.SaveDurationToAdb => lib.(*Rds).GetWorkReading => redis.(*Pool).Get

从调用链跟踪代码过去到  /home/www/go/pkg/mod/github.com/gomodule/redigo@v2.0.0+incompatible/redis/pool.go:278

<-p.ch

发现是连接池等待获取链接,推断是连接池满了,没有释放,去代码释放即可

 

命令行界面
命令

命令	描述
args	打印函数参数
break	设置一个断点
breakpoints	打印激活的断点信息
clear	删除断点
clearall	删除所有的断点
condition	设置断点条件
continue	运行到断点或程序终止
disassemble	拆解器
exit	退出debugger
frame	在不同的框架上执行的命令
funcs	打印函数列表
goroutine	显示或更改当前goroutine
goroutines	列出程序的全部goroutines
help	打印出帮助信息
list	显示源代码
locals	打印局部变量
next	跳到下一行
on	在遇到断点时执行一个命令
print	评估表达式
regs	打印CPU寄存器的内容
restart	重启进程
set	更改变量的值
source	执行包含delve命令列表的文件
sources	打印源文件列表
stack	打印堆栈跟踪
step	单步执行程序
step-instruction	单步单个执行cpu指令
thread	切换到指定的线程
threads	打印每一个跟踪线程的信息
trace	设置跟踪点
types	打印类型列表
vars	打印某个包内的(全局)变量

 

### 实现Layui框架中的图片上传并支持删除功能 为了实现在 Layui 框架中进行图片上传以及提供相应的删除功能,可以采用如下方法: #### HTML部分 定义文件输入框用于选择个文件,并准备一个容器来展示已选的图片预览。 ```html <div class="layui-upload"> <button type="button" class="layui-btn">选择</button> <div class="layui-upload-list"> <img id="demo1" src="" alt=""> <p>预览区</p> </div> </div> <!-- 展示所选图片 --> <ul id="LAY_demo_upload"></ul> ``` #### JavaScript 部分 (基于Layui) 通过调用 `upload.render()` 方法配置上传组件的行为参数,包括设置允许的最大数量、监听事件处理程序等。对于每张成功加载后的像,在页面上创建对应的缩略链接;同时为这些新加入DOM节点绑定点击移除自身的逻辑[^4]。 ```javascript layui.use('upload', function(){ var $ = layui.jquery ,upload = layui.upload; // 图片上传实例 var uploadInst = upload.render({ elem: '#testList' // 绑定元素 ,url: '/api/upload/' // 接口地址 ,accept: 'images' ,multiple: true ,before: function(obj){ // 预读本地文件示例,不支持ie8 obj.preview(function(index, file, result){ $('#LAY_demo_upload').append('<li><img src="'+ result +'" title="'+ file.name +'"><span class="close">×</span></li>') }); } ,done: function(res){ console.log(res); } ,error: function(){ // 请求异常回调 } }); $(document).on('click','.close',(function(e){ e.preventDefault(); $(this).parent().remove(); })); }); ``` 上述代码片段展示了如何利用Layui库构建一个文件上传界面,并实现了基本的前端交互效果——即用户可以选择若干张照片并通过AJAX方式提交给服务器端保存;与此同时,还提供了简单的可视化反馈机制让用户能够即时看到自己挑选出来的素材列表,并且可以通过关闭按钮轻松撤销某项选择操作[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值