GDB Debugging of Golang

Source: Internet
Author: User
Tags gdb debugger
This is a creation in Article, where the information may have evolved or changed.

Original: GDB Debugging Go Program

Description: As a static language, it seems to be necessary to support debugging, and the question that go beginners like to ask is: What IDE do we use? How to debug?

In fact, go is for multicore and concurrency, the real project, you use single-step debugging, originally no problem, may be paged out a problem. A better way to debug is to use the same language as PHP, in the form of a print (log or print).

Of course, the simple small program, if the single-step debugging, you can see some internal operating mechanism, for learning is still very good. The following is the debugging Go program with GDB: (Currently IDE support debugging Go program, with GDB.) GDB 7.1 or above required)

The following is a "Go language learning note" From rain marks (download go resources):

By default, the compiled binaries already contain DWARFv3 debug information, as long as the GDB7.1 or later versions are available for debugging. Under OSX, if you can't execute debug commands, try to execute gdb with sudo.

Remove debug Symbols: Go build-ldflags "-s-w"

    • -S: Removes symbolic information.
    • -W: Removes dwarf debug information.

Close inline optimization: Go build-gcflags "-n-l"

To debug related functions:

    • Runtime. Breakpoint (): Triggers a debugger breakpoint.
    • Runtime/debug. Printstack (): Displays the debug stack.
    • LOG: Suitable for replacing print display debugging information.

GDB Debugging Support:

    • Parameter loading: gdb-d $GCROOT.
    • Manual loading: Source pkg/runtime/runtime-gdb.py.

For more details, please refer to: http://golang.org/doc/gdb

Commissioning Demo: (OSX 10.8.2, Go1.0.3, GDB7.5.1)

1 package main
2
3 import (
4     "fmt"
5     "runtime"
6 )
7
8 func test(s string, x int) (r string) {
9     r = fmt.Sprintf("test: %s %d", s, x)
10     runtime.Breakpoint()
11     returnr
12 }
13 func main() {
14     s := "haha"
15     i := 1234
16     println(test(s, i))
17 }

$ Go build-gcflags "-n-l" //compile, close inline optimization.

$ sUdo GDB demo //Start GDB Debugger and manually load Go Runtime.
GNU gdb (GDB) 7.5.1
Reading symbols from Demo...done.
(GDB) source/usr/local/go/src/pkg/runtime/runtime-gdb.py
Loading Go Runtime Support.

(GDB) l Main.main //View the source code in. Mode.
9 R = Fmt. Sprintf ("Test:%s%d", S, x)
Ten runtime. Breakpoint ()
Return r
12}
13
Func Main () {
S: = "haha"
I: = 1234
println (Test (S, i))
18}

(GDB) l Main.go:8 //To: Way to view the source code.
3 Import (
4 "FMT"
5 "Runtime"
6)
7
8 func Test (s string, x int) (R string) {
9 R = Fmt. Sprintf ("Test:%s%d", S, x)
Ten runtime. Breakpoint ()
Return r
12}

(GDB) b main.main //Set breakpoints in. Mode.
Breakpoint 1 at 0x2131:file Main.go, line 14.

(GDB) b main.go:17 //To set breakpoints by: mode.
Breakpoint 2 at 0x2167:file Main.go, line 17.

(GDB) Info Breakpoints //view all breakpoints.
Num Type Disp Enb Address What
1 breakpoint Keep y 0x0000000000002131 in Main.main at main.go:14
2 Breakpoint Keep Y 0x0000000000002167 in Main.main at main.go:17

(GDB) r //start the process, triggering the first breakpoint.
Starting Program:demo
[New Thread 0X1C03 of Process 4088]
[Switching to Thread 0x1c03 of process 4088]
Breakpoint 1, Main.main () at main.go:14
Func Main () {

(GDB) info goroutines //view goroutines information.
* 1 Running runtime.gosched
* 2 Syscall Runtime.entersyscall

(GDB) goroutine 1 bt //View the Goroutine call stack for the specified ordinal.
#0 0x000000000000f6c0 in runtime.gosched () at pkg/runtime/proc.c:927
#1 0x000000000000e44c in Runtime.main () at pkg/runtime/proc.c:244
#2 0x000000000000e4ef in Schedunlock () at pkg/runtime/proc.c:267
#3 0x0000000000000000 in?? ()

(GDB) goroutine 2 bt //This goroutine seems to be related to GC.
#0 Runtime.entersyscall () at pkg/runtime/proc.c:989
#1 0x000000000000d01d in runtime. Mheap_scavenger () at pkg/runtime/mheap.c:363
#2 0x000000000000e4ef in Schedunlock () at pkg/runtime/proc.c:267
#3 0x0000000000000000 in?? ()

(GDB) c //Continue execution, triggering the next breakpoint.
Continuing.
Breakpoint 2, Main.main () at main.go:17
17!! println (Test (S, i))

(GDB) info goroutines //The current Goroutine sequence number is 1.
* 1 Running runtime.gosched
2 runnable runtime.gosched

(GDB) goroutine 1 bt //Current goroutine call stack.
#0 0x000000000000f6c0 in runtime.gosched () at pkg/runtime/proc.c:927
#1 0x000000000000e44c in Runtime.main () at pkg/runtime/proc.c:244
#2 0x000000000000e4ef in Schedunlock () at pkg/runtime/proc.c:267
#3 0x0000000000000000 in?? ()

(GDB) BT //View the current tune ⽤ stack, which can be compared to the current goroutine call stack.
#0 Main.main () at main.go:17
#1 0x000000000000e44c in Runtime.main () at pkg/runtime/proc.c:244
#2 0x000000000000e4ef in Schedunlock () at pkg/runtime/proc.c:267
#3 0x0000000000000000 in?? ()

(GDB) Info Frame //stack frame information.
Stack level 0, frame at 0x442139f88:
RIP = 0x2167 in Main.main (main.go:17); Saved RIP 0xe44c
Called by frame at 0x442139fb8
Source language go.
Arglist at 0x442139f28, args:
Locals at 0x442139f28, Previous Frame's SP is 0x442139f88
Saved Registers:
RIP at 0x442139f80

(GDB) Info Locals //view local variables.
i = 1234
s = "haha"

(GDB) P S//view variables in Pretty-print way.
$ = "haha"

(GDB) p $len (s) //Get Object Length ($CAP)
$ = 4

(GDB) Whatis i //View object type.
type = int

(GDB) c //Continue execution, triggering the breakpoint () breakpoint.
Continuing.
Program received signal SIGTRAP, Trace/breakpoint trap.
Runtime.breakpoint () at pkg/runtime/asm_amd64.s:81
Bayi RET

(GDB) n //From breakpoint (), execute the source code next line.
Main.test (s= "haha", x=1234, r= "Test:haha 1234″) at Main.go:11
Return r

(GDB) info args //from the parameter information, we can see the value of the named Return parameter.
s = "haha"
x = 1234
r = "Test:haha 1234″

(GDB) x/3xw &r //view R memory data. (pointer 8 + length 4)
0x442139f48:0x42121240 0x00000000 0x0000000f
(GDB) X/15XB 0x42121240//view string byte array
0x42121240:0x74 0x65 0x73 0x74 0x3a 0x20 0x68 0x61
0x42121248:0x68 0x61 0x20 0x31 0x32 0x33 0x34

(GDB) c //Continue execution, end of process.

Continuing.
Test:haha 1234
[Inferior 1 (process 4088) exited normally]

(gdb)   q  //exits gdb.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.