编译程序的时候使用如下命令行:
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"