GDB或delve调试Go程序查看变量显示<optimized out>解决办法

编译程序的时候使用如下命令行:
go build -o main -gcflags "all=-N -l" main.go

如果你没有源码,不能编译,那只能通过汇编查内存了

调试

程序代码test.go

package main
 
import "fmt"
 
func main(){
  msg := "hello, world"
  fmt.Println(msg)
}

GDB

(gdb) b main.main
Breakpoint 1 at 0x494880: file /root/test.go, line 5.
(gdb) r
Starting program: /root/test 

Breakpoint 1, main.main () at /root/test.go:5
5       func main(){
(gdb) info locals
msg = 0x40513a ""
(gdb) n
6         msg := "hello, world"
(gdb) p msg
$1 = 0x40513a ""
(gdb) n
7         fmt.Println(msg)
1: msg = 0x4abd31 "hello, world"
(gdb) p msg
$2 = 0x4abd31 "hello, world"

delve

# /root/go/bin/dlv exec ./test
Type 'help' for list of commands.
(dlv) b main.main
Breakpoint 1 set at 0x49488a for main.main() ./test.go:5
(dlv) r
Process restarted with PID 9405
(dlv) locals
(no locals)
(dlv) n
> main.main() ./test.go:5 (hits goroutine(1):1 total:1) (PC: 0x49488a)
     1: package main
     2:  
     3: import "fmt"
     4:  
=>   5: func main(){
     6:   msg := "hello, world"
     7:   fmt.Println(msg)
     8: }
     9:
(dlv) n
> main.main() ./test.go:6 (PC: 0x494898)
     1: package main
     2:  
     3: import "fmt"
     4:  
     5: func main(){
=>   6:   msg := "hello, world"
     7:   fmt.Println(msg)
     8: }
     9:
(dlv) n
> main.main() ./test.go:7 (PC: 0x4948ad)
     2:  
     3: import "fmt"
     4:  
     5: func main(){
     6:   msg := "hello, world"
=>   7:   fmt.Println(msg)
     8: }
     9:
(dlv) p msg
"hello, world"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值