In golang source code, there is some code like:
MOVL (g_sched+gobuf_sp)(SI), SP // sp = m->g0->sched.sp
And in runtime/stubs.go
I found
func getg() *g
But i can not find getg
function or g
struct, where is it?
In golang source code, there is some code like:
MOVL (g_sched+gobuf_sp)(SI), SP // sp = m->g0->sched.sp
And in runtime/stubs.go
I found
func getg() *g
But i can not find getg
function or g
struct, where is it?
The struct is in runtime2.go
.
type g struct { // Stack parameters. // stack describes the actual stack memory: [stack.lo, stack.hi). // stackguard0 is the stack pointer compared in the Go stack growth prologue.
You can use godef
to quickly answer questions like that on your own.
The function's comment describes how that works:
The compiler rewrites calls to this function into instructions that fetch the g directly (from TLS or from the dedicated register).
To see how the compiler does this, check cmd/compile/internal/gc/typecheck.go
:
if ..... && n.Left.Sym.Name == "getg" { ...