Home >Backend Development >Golang >What does the 'Unknown Field' in a Panic Stack Trace represent?
While analyzing stack traces from panics, one may encounter an unfamiliar number following the function name. For instance, running the following code:
<code class="go">package main func F(a int) { panic(nil) } func main() { F(1) }</code>
produces:
panic: nil goroutine 1 [running]: main.F(0x1, 0x10436000) /tmp/sandbox090887108/main.go:4 +0x20 main.main() /tmp/sandbox090887108/main.go:8 +0x20
The second number (0x10436000) may seem enigmatic. To decode it, we must delve into the nature of stack trace data.
The data printed in the stack trace consists of function arguments. However, these values do not correspond directly to the arguments passed in. Instead, they represent the raw data stored in memory, specifically in pointer-sized values.
In the Playground environment, a unique situation arises. Its 64-bit word architecture has 32-bit pointers (GOARCH=amd64p32). As a result, each time a function argument is printed, the following occurs:
Consider the following function call:
<code class="go">F(1)</code>
The resulting stack trace shows:
main.F(0x97301, 0x10436000)
In this case, the uint8 argument (1) occupies only the first 8 bits of the 64-bit word (0x97301 & 0x0f). The extra 0x97300 and the entirety of 0x10436000 represent the unused portion of the word.
For more complex functions, such as:
<code class="go">func F(a, b, c uint32)</code>
called with:
<code class="go">F(1, 1, 1)</code>
the stack trace shows:
main.F(0x100000001, 0xc400000001)
because the three 32-bit values occupy two words.
Return values are also allocated on the stack, as seen in the following function:
<code class="go">func F(a int64) (int, int)</code>
On amd64, the stack frame arguments would appear as:
main.F(0xa, 0x1054d60, 0xc420078058)
with one word for the input and two for the return values. Note that return values are not initialized, so this information is of limited use.
The above is the detailed content of What does the 'Unknown Field' in a Panic Stack Trace represent?. For more information, please follow other related articles on the PHP Chinese website!