The problem

-gcFLAGS -gcflags -gcflags -gcflags -gcflags -gcflags -gcflags -gcflags Where can I see the document

To solve

Google to solve the problem,

[What ‘s the Go CMD option’ gcflags’ all possible values] (stackoverflow.com/questions/6…).

Gcflags parameter values are the parameters passed to go Tool compile, so you can see all possible values with the command (below)

go tool compile -help
Copy the code

The actual scene

Pile of escape

Reference:

A silly mistake to make with Go struct!

func main(a) {
 a := new(struct{})
 b := new(struct{})
 println(a, b, a == b)

 c := new(struct{})
 d := new(struct{})
 fmt.Println(c, d)
 println(c, d, c == d)
}
Copy the code
go run -gcflags="-m -l" main.go
Copy the code

-m print optimization decisions

-l disable inlining

go run -gcflags="-N -l" main.go 
Copy the code

-n disable optimizations

assembly

Viewing assembly code

go tool compile -S struct_t.go
go run -gcflags="-S" struct_t.go
Copy the code

Assembly Learning See why assembly learning

other

What is inline optimization

Inlining is simply expanding the short function at the place where it was called.

Reference:

Go language inline inline policies and restrictions

Inline optimization in GO

Go Startup process

Reference:

Do you know what G0 and M0 are?

Since Go1.11, debugging information is compressed in order to reduce the binary size. What is the meaning of DWARF that makes it difficult to understand compression when using GDB on MacOS

$ GOFLAGS="-ldflags=-compressdwarf=false" go build 
Copy the code
(gdb) info files Symbols from "/Users/firaga/goasm/gostart". Local exec file: `/Users/firaga/goasm/gostart', file type mach-o-x86-64. Entry point: 0x1065600 0x0000000001001000 - 0x00000000010a2e8a is .text 0x00000000010a2ea0 - 0x00000000010a2fa2 is __TEXT.__symbol_stub1 0x00000000010a2fc0 - 0x00000000010e9947 is __TEXT.__rodata 0x00000000010e9960 - 0x00000000010ea0e8  is __TEXT.__typelink 0x00000000010ea100 - 0x00000000010ea168 is __TEXT.__itablink 0x00000000010ea168 - 0x00000000010ea168 is __TEXT.__gosymtab 0x00000000010ea180 - 0x0000000001148940 is __TEXT.__gopclntab 0x0000000001149000  - 0x0000000001149020 is __DATA.__go_buildinfo 0x0000000001149020 - 0x0000000001149178 is __DATA.__nl_symbol_ptr 0x0000000001149180 - 0x00000000011574a4 is __DATA.__noptrdata 0x00000000011574c0 - 0x000000000115e8f0 is .data 0x000000000115e900 - 0x000000000118c170 is .bss 0x000000000118c180 - 0x00000000011912f0 is __DATA.__noptrbssCopy the code
(gdb) b *0x1065600 Breakpoint 1 at 0x1065600: The file/usr/local/Cellar/go / 1.16 / libexec/SRC/runtime/rt0_darwin_amd64. S, line 8.Copy the code

m0

M0 is the first system thread created by the Go Runtime. A Go process has only one M0, also known as the main thread.

In many ways:

  • Data structure: M0 is no different from any other CREATED M.
  • Creation process: M0 should be compiled and copied directly to m0 when the process is started, other subsequent m’s are created by Go Runtime itself.
  • Variable declaration: m0 is defined as mvar m0 mNothing special.

g0

G is generally divided into three types, namely:

  • Those who perform user tasks are called G.
  • performruntime.mainThe main goroutine.
  • The name of the scheduling task is G0.

G0 is special in that there is only one G0 for each M (and only one g0), and only one g0 is bound to each M. The assignment of g0 is also done by assembly, and the rest of the subsequent creation is regular G.

In many ways:

  • Data structure: G0 is the same data structure as the other created GS, but there are stack differences. The stack on G0 is allocated to the system stack. On Linux, the stack size is fixed by default at 8MB and cannot be expanded or shrunk. Conventional G starts at 2KB and can be expanded.
  • Running state: Unlike regular G, G0 has fewer running states and is not preempted by the scheduler, which itself runs on G0.
  • Variable declarations: g0 and general g, defined asvar g0 gNothing special.

GDB debugging

Text debugging

gdb main
Copy the code

Tui debugging

gdb main -tui
Copy the code
Commonly used parameters

Common test parameters:

list l

The run to perform

Start Enables debugging.

Break b Sets a breakpoint

Next n The next line

S step into

Continue executes to the next breakpoint

quit

Enter help + command for detailed parameter lookup

Reference :GDB Debugging Guide