go program debugging

Debugging program is a necessary skill of program ape. There are many ways to debug program, such as printing console output, checking logs, setting breakpoints, and using debug to do one-step tracking to debug. This article is mainly about go using debug.

GDB

introduce
GDB is a powerful UNIX debugging tool released by GNU Open Source Organization. Installation on the mac, there will be authentication problems, checked some information, failed to solve, gave up. Vagrant is used to build a go environment and install the GDB tool. When using gdb, the code is usually mapped to the linux virtual host through the vagrant directory, and then GDB debugging is carried out. This is not a lot of use, generally using the x command provided by GDB to see the value of memory.
debug target

  1. Setting breakpoints
  2. View the output of variable values
  3. View variable memory address
  4. View variable memory values
  5. Modify the value of a variable

Based on the above four points, we show the basic usage of gdb through program
Usage of gdb
Sample program:

package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Println("go debug.....")
    // Command parameter usage
    var argc = len(os.Args)
    var argv = append([]string{}, os.Args...)
    fmt.Printf("argc:%d\n", argc)
    fmt.Printf("argv:%v\n", argv)

    // View variable memory addresses and values
    var aa = 1
    var bb = -1

    fmt.Println(aa)
    fmt.Println(bb)
}
  1. Compiler
go build -gcflags="-N -l" demo.go // - N-l for closing compiler inline optimization
  1. Start up gdb
gdb demo
  1. Setting breakpoint break
(gdb) b main.main
Breakpoint 1 at 0x488e60: file /home/vagrant/godemo/demo01/demo.go, line 8.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000488e60 in main.main at /home/vagrant/godemo/demo01/demo.go:8
(gdb)

Starter run

(gdb) run arg1 agr2
Starting program: /home/vagrant/godemo/demo01/demo arg1 agr2
[New LWP 5976]
[New LWP 5977]
[New LWP 5978]

Thread 1 "demo" hit Breakpoint 1, main.main () at /home/vagrant/godemo/demo01/demo.go:8
8    func main() {
(gdb)

view code

(gdb) l
4        "fmt"
5        "os"
6    )
7
8    func main() {
9        fmt.Println("go debug.....")
10        // Command parameter usage
11        var argc = len(os.Args)
12        var argv = append([]string{}, os.Args...)
13        fmt.Printf("argc:%d\n", argc)
(gdb)
14        fmt.Printf("argv:%v\n", argv)
15
16        // View variable memory addresses and values
17        var aa = 1
18        var bb = -1
19
20        fmt.Println(aa)
21        fmt.Println(bb)
22    }
(gdb)

Step next

(gdb) n
go debug.....
11        var argc = len(os.Args)
(gdb) n
12        var argv = append([]string{}, os.Args...)
(gdb) n
13        fmt.Printf("argc:%d\n", argc)
(gdb) n
argc:3
14        fmt.Printf("argv:%v\n", argv)
(gdb) n
argv:[/home/vagrant/godemo/demo01/demo arg1 agr2]

Print command p
Print Format
x displays variables in hexadecimal format.
d Displays variables in decimal format.
u displays unsigned integers in hexadecimal format.
o Displays variables in octal format.
t Displays variables in binary format.
a Displays variables in hexadecimal format.
c Displays variables in character format.
f Displays variables in floating-point format.

17        var aa = 1
(gdb) n
18        var bb = -1
(gdb) p aa
$1 = 1
(gdb) p &aa
$2 = (int *) 0xc00007ce40
(gdb) p/x aa
$3 = 0x1

View Memory Value Command x
x/<n/f/u> <addr>

n, f and u are optional parameters.

n is a positive integer representing the length of display memory
f represents the format of the display, see above
u denotes the number of bytes, and GDB defaults to four bytes.
b represents a single byte
h denotes double bytes
w denotes four bytes
g denotes eight bytes

<addr> denotes a memory address

(gdb) p &aa
$2 = (int *) 0xc00007ce40
(gdb) x/1dg 0xc00007ce40
0xc00007ce40:    1
(gdb)

Setting Value Command

(gdb) set aa = 2
(gdb) p aa
$4 = 2
(gdb)

Program call stack bt

Display goroutines

info goroutines // Displays a list of goroutine s currently executed, as shown in the following code, with * representing the currently executed

View Variable Types

whatis

There are only some basic usages listed here. gdb is very powerful, and there are many commands that can go deep into the bottom of the program execution. Through the above commands, you can complete the basic debugging of a program.

delve

delve is a debug tool specially designed for go language, and the debug function of some IDE tools is based on this.
Start the debug service

dlv debug demo.go

Other commands are roughly the same as gdb. Primary debugging, advanced usage functions have not been studied in depth. In addition, it can attach to a running program for debug.

IDE Tool

If you are not accustomed to using the command line, you can use the integrated development tool goland, this debug tool with graphical interface operation, which is more convenient to operate.

If you like, please pay attention to "Stories on the Cloud" and keep updating for you.

Keywords: Go Unix Mac Linux

Added by JohnnyBlaze on Sat, 10 Aug 2019 10:01:53 +0300