[Golang] Stack parsing

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

This article is for understanding translation, original address: http://www.goinggo.net/2015/01/stack-traces-in-go.html

Introduction

There are some debugging techniques in the go language that can help us find problems quickly, sometimes you want to record as many exceptions as possible but still don't feel enough, figuring out the meaning of the stack can help locate bugs or record more complete information.   This article discusses the stack trace information and how to identify the parameters passed by the function in the stack.

Functions
Let's start with this piece of code:
Listing 1 Package Main
02
The. Func main () {
Slice: = Make ([]string, 2, 4)
Example (Slice, "hello", 10)
06}
07
Example func (Slice []string, str string, I int) {
Panic ("Want stack Trace")
The example function defines 3 parameters, 1 string-type slice, 1 string and an integer, and throws a panic, running this code to see the result:

Listing 2 panic:want Stack trace

Goroutine 1 [Running]:
Main. Example (0X2080C3F50, 0x2, 0x4, 0x425c0, 0x5, 0xa)
/users/bill/spaces/go/projects/src/github.com/goinaction/code/
Temp/main.go:9 +0x64
Main.main ()
/users/bill/spaces/go/projects/src/github.com/goinaction/code/
Temp/main.go:5 +0x85

Goroutine 2 [runnable]:
Runtime.forcegchelper ()
/users/bill/go/src/runtime/proc.go:90
Runtime.goexit ()
/users/bill/go/src/runtime/asm_amd64.s:2232 +0x1

Goroutine 3 [runnable]:
Runtime.bgsweep ()
/users/bill/go/src/runtime/mgc0.go:82
Runtime.goexit ()
The/users/bill/go/src/runtime/asm_amd64.s:2232 +0x1 stack information shows all goroutines states at panic throw at this time, and panic Goroutine will appear at the top.

Listing 3 Goroutine 1 [running]:
The main. Example (0X2080C3F50, 0x2, 0x4, 0x425c0, 0x5, 0xa)
/users/bill/spaces/go/projects/src/github.com/goinaction/code/
Temp/main.go:9 +0x64
Main.main ()
/users/bill/spaces/go/projects/src/github.com/goinaction/code/
Temp/main.go:5 +0x85 Line 1th shows that the first panic is Goroutine 1, and the second line shows that panic is in main. example, and can navigate to that line of code, in this case the 9th row raises the panic. Here we focus on how the parameters are passed:

Listing 4//Declaration
Main. Example (Slice []string, str string, I int)

Call to Example by main.
Slice: = Make ([]string, 2, 4)
Example (Slice, "hello", 10)

Stack Trace
Main. Example (0X2080C3F50, 0x2, 0x4, 0x425c0, 0x5, 0xa) This shows the stack information when the Example function is called with parameters in Main, and the comparison reveals that the number of parameters is not the same, Example defines 3 parameters, 6 parameters are displayed in the stack. The key problem now is that we have to figure out how they match. The 1th parameter is a string of type slice, and we know that slice is a reference type in the go language, that is, the slice variable structure contains three parts: pointer, Length (lengthe), Capacity (capacity)

Listing 5//Slice parameter value
Slice: = Make ([]string, 2, 4)

Slice Header Values
Pointer:0x2080c3f50
length:0x2
capacity:0x4

Declaration
Main. Example (Slice []string, str string, I int)

Stack Trace
Main. Example (0X2080C3F50, 0x2, 0x4, 0x425c0, 0x5, 0xa) Therefore, the preceding 3 parameters will match the slice as shown in:

Figure 1 Figure provided by Georgi Knox
Let's take a look at the second parameter, which is a string type, and the string type is also a reference type, which consists of two parts: pointer, length.

Listing 6//String parameter value
"Hello"

String Header Values
Pointer:0x425c0
length:0x5

Declaration
Main. Example (Slice []string, str string, I int)

Stack Trace
Main. Example (0X2080C3F50, 0x2, 0x4, 0x425c0, 0x5, 0xa) can determine that the 4th, 52 parameters in the stack information correspond to the string parameter in the code, as shown in:

Figure 2 Figure provided by Georgi Knox
The last parameter, Integer, is the single word value.

Listing 7//Integer parameter value
10

Integer value
Base 16:0xa

Declaration
Main. Example (Slice []string, str string, I int)

Stack Trace
Main. Example (0X2080C3F50, 0x2, 0x4, 0x425c0, 0x5, 0xa) Now we can match the parameters in the code to the stack information.

Figure 3 Figure provided by Georgi Knox

Methods
What happens if we use the example as a struct?

Listing 8 Package Main
02
Import "FMT"
04
Type Trace struct{}
06
The func main () {
Slice: = Make ([]string, 2, 4)
09
Ten Var t trace
T.example (Slice, "hello", 10)
12}
13
+ func (t *trace) Example (slice []string, str string, I int) {
FMT. Printf ("Receiver Address:%p\n", T)
Panic ("Want stack Trace")
17}
Modify the code as shown above, define example as a method of trace, and invoke example by using Trace's instance T.
Run the program again, and you'll find that the stack information is a little different:

Listing 9 Receiver address:0x1553a8
Panic:want Stack Trace

Goroutine 1 [Running]:
The main. (*trace). Example (0x1553a8, 0x2081b7f50, 0x2, 0x4, 0xdc1d0, 0x5, 0xa)
/users/bill/spaces/go/projects/src/github.com/goinaction/code/
Temp/main.go:16 +0x116

Main.main ()
/users/bill/spaces/go/projects/src/github.com/goinaction/code/
Temp/main.go:11 +0xae First Note that the method call on line 2nd uses pointer receiver, which has more "*trace" between the package name and the method name. In addition, the 1th parameter of the parameter list indicates the struct (t) address. We see the internal implementation details from the stack information.

Packing
If more than one parameter can be populated into a single word, these parameter values are packaged in a merge:

Listing Package Main
02
The. Func main () {
Example (True, False, true, 25)
05}
06
Example func (B1, B2, b3 bool, I uint8) {
Panic ("Want stack Trace")
09} This example modifies the example function to 4 parameters: 3 bool Type and a eight-bit unsigned integer. The bool value is also represented by 8 bits, so 4 parameters can be combined into a single word under both 32-bit and 64-bit schemas.

Listing Goroutine 1 [running]:
The main. Example (0x19010001)
/users/bill/spaces/go/projects/src/github.com/goinaction/code/
Temp/main.go:8 +0x64
Main.main ()
/users/bill/spaces/go/projects/src/github.com/goinaction/code/
Temp/main.go:4 +0x32 This is the stack information for this example, see the specific analysis:

Listing//Parameter values
True, False, True, 25

Word value
Bits Binary Hex Value
00-07 0000 0001 True
08-15 0000 0000 False
16-23 0000 0001 True
24-31 0001 1001) 19 25

Declaration
Main. Example (B1, B2, b3 bool, I uint8)

Stack Trace
Main. Example (0x19010001) above shows how the parameter values are matched to 4 parameters. When we see the inclusion of hexadecimal values in the stack information, we need to know how these values are passed.

Conclusion
The Go runtime provides a great deal of information to help us debug our programs. In this post we concentrated on stack traces. The ability to decode the values, the were passed into each function throughout the call stack is huge. It has helped me more than once to identify my bug very quickly. Now so you know how to read stack traces, hopefully you can leverage this knowledge the next time a stack trace happens To you.
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.