Delve 是一款很不错的 Golang 调试工具,可以实现类似 Visual Studio 的断点调试功能,也可以用来在程序 Crash 的时候生成 Coredump 文件,此外 Delve 也适合用于调试 Web Server。

Delve 项目

Github链接

安装 Delve

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

Delve 常用命令

命令 功能
dlv attach 后面跟 pid,用来 Debug 编译好的 Golang 程序
dlv core 用于 coredump
dlv debug 后面跟要调试的 go 文件,进入 Debug
dlv test Debug test 函数

编写用于 Debug 的 Golang Web Server

main.go, 写一个 Hello World 的 Web Server

package mainimport ("net/http"
)const endpoint = ":8000"type helloHandler struct{}func (h *helloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {w.Write([]byte("Hello, world!"))
}func main() {http.Handle("/", &helloHandler{})http.ListenAndServe(endpoint, nil)
}

进行断点调试

➜  test dlv debug ./main.go
Type 'help' for list of commands.
(dlv) help
The following commands are available:args ------------------------ Print function arguments.break (alias: b) ------------ Sets a breakpoint.breakpoints (alias: bp) ----- Print out info for active breakpoints.call ------------------------ Resumes process, injecting a function call (EXPERIMENTAL!!!)clear ----------------------- Deletes breakpoint.clearall -------------------- Deletes multiple breakpoints.condition (alias: cond) ----- Set breakpoint condition.config ---------------------- Changes configuration parameters.continue (alias: c) --------- Run until breakpoint or program termination.deferred -------------------- Executes command in the context of a deferred call.disassemble (alias: disass) - Disassembler.down ------------------------ Move the current frame down.edit (alias: ed) ------------ Open where you are in $DELVE_EDITOR or $EDITORexit (alias: quit | q) ------ Exit the debugger.frame ----------------------- Set the current frame, or execute command on a different frame.funcs ----------------------- Print list of functions.goroutine (alias: gr) ------- Shows or changes current goroutinegoroutines (alias: grs) ----- List program goroutines.help (alias: h) ------------- Prints the help message.libraries ------------------- List loaded dynamic librarieslist (alias: ls | l) -------- Show source code.locals ---------------------- Print local variables.next (alias: n) ------------- Step over to next source line.on -------------------------- Executes a command when a breakpoint is hit.print (alias: p) ------------ Evaluate an expression.regs ------------------------ Print contents of CPU registers.restart (alias: r) ---------- Restart process.set ------------------------- Changes the value of a variable.source ---------------------- Executes a file containing a list of delve commandssources --------------------- Print list of source files.stack (alias: bt) ----------- Print stack trace.step (alias: s) ------------- Single step through program.step-instruction (alias: si)  Single step a single cpu instruction.stepout (alias: so) --------- Step out of the current function.thread (alias: tr) ---------- Switch to the specified thread.threads --------------------- Print out info for every traced thread.trace (alias: t) ------------ Set tracepoint.types ----------------------- Print list of typesup -------------------------- Move the current frame up.vars ------------------------ Print package variables.whatis ---------------------- Prints type of an expression.
Type help followed by a command for full documentation.

在这个交换环境下可以进行断点调试,支持的命令主要有:

命令 alias 功能
break b 设置断点
continue c 运行至断点
print p 打印变量
funcs 显示支持的函数

查看包名/函数名包含 main 的函数

funcs main
(dlv) funcs main
crypto/x509.domainToReverseLabels
crypto/x509.matchDomainConstraint
internal/x/net/http/httpproxy.(*domainMatch).match
internal/x/net/http/httpproxy.domainMatch.match
main.(*helloHandler).ServeHTTP
main.init
main.main
net.absDomainName
net.isDomainName
net/http.(*body).bodyRemains
net/http.requestBodyRemains
runtime.main
runtime.main.func1
runtime.main.func2
type..eq.internal/x/net/http/httpproxy.domainMatch
type..hash.internal/x/net/http/httpproxy.domainMatch

funcs [param] 默认是返回包含 param 的func,可以看到 main package 包含3个 func

main.(*helloHandler).ServeHTTP
main.init
main.main

设置断点

break [name] <linespec>
  1. 在 main 处设置一个断点,break 的参数可以是 funcs 查看等到的 func name

    break main.main
    
    (dlv) break main.main
    Breakpoint 1 set at 0x1331513 for main.main() ./main.go:15
    
  2. 设置断点的另外一种方式

    <filename>:<line>
    
    (dlv) break ./main.go:15
    Breakpoint 1 set at 0x1331513 for main.main() ./main.go:15
    
  3. linespec 支持的完整形式
    linespec说明

    *<address> Specifies the location of memory address address. address can be specified as a decimal, hexadecimal or octal number<filename>:<line> Specifies the line line in filename. filename can be the partial path to a file or even just the base name as long as the expression remains unambiguous.<line> Specifies the line line in the current file+<offset> Specifies the line offset lines after the current one-<offset> Specifies the line offset lines before the current one<function>[:<line>] Specifies the line line inside function. The full syntax for function is <package>.(*<receiver type>).<function name> however the only required element is the function name, everything else can be omitted as long as the expression remains unambiguous. For setting a breakpoint on an init function (ex: main.init), the <filename>:<line> syntax should be used to break in the correct init function at the correct location./<regex>/ Specifies the location of all the functions matching regex
    

运行至断点

continue 命令运行至断点

continue
(dlv) continue
> main.main() ./main.go:15 (hits goroutine(1):1 total:1) (PC: 0x1331513)10:11:   func (h *helloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {12:       w.Write([]byte("Hello, world!"))13:   }14:
=>  15: func main() {16:        http.Handle("/", &helloHandler{})17:      http.ListenAndServe(endpoint, nil)18:   }

打印变量

  1. 可以继续设置断点至 ServeHTTP 函数,并执行 continue 开启 Web Server 服务

    (dlv) break main.(*helloHandler).ServeHTTP
    Breakpoint 2 set at 0x1331443 for main.(*helloHandler).ServeHTTP() ./main.go:11
    (dlv) c
    
  2. 访问 Web Server
    这时候 Web Server 的 Delve 需要输入

    ➜  curl http://localhost:8000
    Hello, world!%
    
  3. 在执行至断点 ServeHTTP 时,可输入 print 查询变量

    • 输入 n 回车,单步执行,
    • 输入 args 打印出所有的方法参数信息
    • 输入 locals 打印所有的本地变量
    (dlv) c
    > main.(*helloHandler).ServeHTTP() ./main.go:11 (hits goroutine(26):1 total:13) (PC: 0x1331443)6:7:  const endpoint = ":8000"8:9: type helloHandler struct{}10:
    =>  11: func (h *helloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {12:       w.Write([]byte("Hello, world!"))13:   }14:15: func main() {16:        http.Handle("/", &helloHandler{})
    (dlv) n
    > main.(*helloHandler).ServeHTTP() ./main.go:12 (PC: 0x1331451)7:    const endpoint = ":8000"8:9: type helloHandler struct{}10:11:    func (h *helloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {=>  12:     w.Write([]byte("Hello, world!"))13:   }14:15: func main() {16:        http.Handle("/", &helloHandler{})17:      http.ListenAndServe(endpoint, nil)
    (dlv) args
    h = (*main.helloHandler)(0x16485d0)
    w = net/http.ResponseWriter(*net/http.response) 0xc00010b998
    r = ("*net/http.Request")(0xc0001c0200)
    (dlv) locals
    (no locals)
    (dlv) print r.Method
    "GET"
    (dlv)
    

Golang调试工具Delve使用简介相关推荐

  1. golang调试工具Delve

    转自:http://www.cnblogs.com/li-peng/p/8522592.html Devle是一个非常棒的golang 调试工具,支持多种调试方式,直接运行调试,或者attach到一个 ...

  2. delve应该安装到哪_golang调试工具Delve

    转自:http://www.cnblogs.com/li-peng/p/8522592.html Devle是一个非常棒的golang 调试工具,支持多种调试方式,直接运行调试,或者attach到一个 ...

  3. Golang 微框架 Gin 简介

    Golang 微框架 Gin 简介 框架一直是敏捷开发中的利器,能让开发者很快的上手并做出应用,甚至有的时候,脱离了框架,一些开发者都不会写程序了.成长总不会一蹴而就,从写出程序获取成就感,再到精通框 ...

  4. Golang爬虫框架 colly 简介

    Golang爬虫框架 colly 简介 colly是一个采用Go语言编写的Web爬虫框架,旨在提供一个能够些任何爬虫/采集器/蜘蛛的简介模板,通过Colly.你可以轻松的从网站提取结构化数据,然后进行 ...

  5. Golang groupcache LRU 缓存简介与用法

    1.LRU LRU(Least Recently Used,最近最久未使用算法)是一种常见的缓存淘汰算法,当缓存满时,淘汰最近最久未使用的元素,在很多分布式缓存系统(如Redis, Memcached ...

  6. golang 依赖管理_简介:如何管理Golang项目依赖项

    golang 依赖管理 by Ying Kit Yuen 英杰苑 简介:如何管理Golang项目依赖项 (An intro to dep: How to manage your Golang proj ...

  7. golang 调试工具dlv 各个命令的用法

    应用dlv debug **.go 在Golang中,dlv是一个常用的调试工具,可以通过一系列命令来控制调试器的行为,下面是各个命令的用法: 1. `break`:设置一个断点. - `break` ...

  8. delve应该安装到哪_Golang /Go调试工具delve

    安装 这里提供了支持的所有平台(操作系统)上安装Delve的说明. 请注意您必须安装Go 1.5或更高版本. 此外如果使用Go 1.5,您必须设置GO15VENDOREXPERIMENT = 1,然后 ...

  9. delve应该安装到哪_Golang调试工具Delve安装及使用

    一.安装 照着 github 上 delve 项目的安装说明操作, go mod 模式下推荐使用第二种方式. 1.拉取最新 delve 项目代码到本地,编译安装. # cd $GOPATH/src/ ...

最新文章

  1. Dropout、梯度消失/爆炸、Adam优化算法,神经网络优化算法看这一篇就够了
  2. 面试官:谈谈什么是死锁?如何解决死锁?
  3. 琴弦文字 - wpf行为
  4. 送书福利 | 大数据智能:数据驱动的自然语言处理技术
  5. Unity热更新方案探索与讨论
  6. 写在Github被微软收购之际 - Github的那些另类用法
  7. 如何更改ubuntu的用户密码
  8. java和python对比----1:
  9. 博图如何上载wincc程序_WINCC 博途 以太网下载方式
  10. java面试准备之---Struts2体系知识点,系统复习,struts2原理,ognl,el支持.---随时更新
  11. 数学建模算法与应用(一)线性规划
  12. android手机刷机后驱动更新失败,安卓手机刷机失败怎么恢复
  13. 总结openstack nuetron网络架构图
  14. 商品分类无限层级递归
  15. 计算机专业职业生涯规划200字,计算机职业生涯规划 200字
  16. cmd运行javac解析中文乱码
  17. html水晶按钮图片,20个纯CSS3实现的彩色透明水晶按钮
  18. 2.5css ps切图、
  19. 阅读3Hierarchical integrated machine learning model for predicting flight departure delays and...
  20. 【论文阅读】Adaptive Cross-Modal Prototypes for Cross-Domain Visual-Language Retrieval

热门文章

  1. U3D log Flash shader 效果(标题流光效果)
  2. 调制解调器 拨号音 原理_为什么拨号调制解调器这么吵?
  3. 中国超级电容器电池行业概述
  4. 上传图片并预览 限制个数
  5. JS 案例 飘浮广告
  6. 自制Lex-词法分析器生成器(C++)
  7. # Revit2017二次开发遇到的调试问题
  8. 封禁135、137、139、445端口
  9. Palo Alto 220防火墙升级 Software版本
  10. Javabase到easyui的知识总结