原文链接:分享如何浏览Go语言源码
前言
asongGo
Go源码包含哪些?
GoGoGosrc
GoGoGoGo
查看规范库源代码
UML
GDBdemofmt.Println
<img src=”https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/020ee5d660264362b42dab2a142bc369~tplv-k3u1fbpfcp-zoom-1.image” />
查看Go语言底层实现
GogoroutinemapGoGosrcruntimeGoruntimeGoGoGo
<img src=”https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/43155623d60042d98539d4885bb73020~tplv-k3u1fbpfcp-zoom-1.image” alt=”” />
剖析汇编代码
GoruntimeGoruntimeGoplan9x86plan9Goplan9makeslicedemo
// main.go
import "fmt"
func main() {
s := make([]int, 10, 20)
fmt.Println(s)
}
有两种形式能够查看汇编代码:
1. go tool compile -S -N -l main.go
2. go build main.go && go tool objdump ./main
.o
<img src=”https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6071b1f1459144f5b891c3c4d60559df~tplv-k3u1fbpfcp-zoom-1.image” />
s := make([]int, 10, 20) runtime.makeslice(SB)runtimemakesliceruntime/slice.go
<img src=”https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d9905faa0ef2479b997279691ddba98f~tplv-k3u1fbpfcp-zoom-1.image” />
在线调试
reviewGoGDBLLDBDelveDelveGoDelveGoDelveDelvedlvGonildlvdemo
import "fmt"
func main() {
var s []int
s = append(s, 1)
fmt.Println(s)
}
dlv debug
$ dlv debug
Type 'help' for list of commands.
(dlv)
appendappend
(dlv) break main.go:7
Breakpoint 1 set at 0x10aba57 for main.main() ./main.go:7
continue
(dlv) continue
> main.main() ./main.go:7 (hits goroutine(1):1 total:1) (PC: 0x10aba57)
2:
3: import "fmt"
4:
5: func main() {
6: var s []int
=> 7: s = append(s, 1)
8: fmt.Println(s)
9: }
disassemblemain
(dlv) disassemble
TEXT main.main(SB) /Users/go/src/asong.cloud/Golang_Dream/code_demo/src_code/main.go
main.go:5 0x10aba20 4c8d6424e8 lea r12, ptr [rsp-0x18]
main.go:5 0x10aba25 4d3b6610 cmp r12, qword ptr [r14+0x10]
main.go:5 0x10aba29 0f86f6000000 jbe 0x10abb25
main.go:5 0x10aba2f 4881ec98000000 sub rsp, 0x98
main.go:5 0x10aba36 4889ac2490000000 mov qword ptr [rsp+0x90], rbp
main.go:5 0x10aba3e 488dac2490000000 lea rbp, ptr [rsp+0x90]
main.go:6 0x10aba46 48c744246000000000 mov qword ptr [rsp+0x60], 0x0
main.go:6 0x10aba4f 440f117c2468 movups xmmword ptr [rsp+0x68], xmm15
main.go:7 0x10aba55 eb00 jmp 0x10aba57
=> main.go:7 0x10aba57* 488d05a2740000 lea rax, ptr [rip+0x74a2]
main.go:7 0x10aba5e 31db xor ebx, ebx
main.go:7 0x10aba60 31c9 xor ecx, ecx
main.go:7 0x10aba62 4889cf mov rdi, rcx
main.go:7 0x10aba65 be01000000 mov esi, 0x1
main.go:7 0x10aba6a e871c3f9ff call $runtime.growslice
main.go:7 0x10aba6f 488d5301 lea rdx, ptr [rbx+0x1]
main.go:7 0x10aba73 eb00 jmp 0x10aba75
main.go:7 0x10aba75 48c70001000000 mov qword ptr [rax], 0x1
main.go:7 0x10aba7c 4889442460 mov qword ptr [rsp+0x60], rax
main.go:7 0x10aba81 4889542468 mov qword ptr [rsp+0x68], rdx
main.go:7 0x10aba86 48894c2470 mov qword ptr [rsp+0x70], rcx
main.go:8 0x10aba8b 440f117c2450 movups xmmword ptr [rsp+0x50], xmm15
main.go:8 0x10aba91 488d542450 lea rdx, ptr [rsp+0x50]
main.go:8 0x10aba96 4889542448 mov qword ptr [rsp+0x48], rdx
main.go:8 0x10aba9b 488b442460 mov rax, qword ptr [rsp+0x60]
main.go:8 0x10abaa0 488b5c2468 mov rbx, qword ptr [rsp+0x68]
main.go:8 0x10abaa5 488b4c2470 mov rcx, qword ptr [rsp+0x70]
main.go:8 0x10abaaa e8f1dff5ff call $runtime.convTslice
main.go:8 0x10abaaf 4889442440 mov qword ptr [rsp+0x40], rax
main.go:8 0x10abab4 488b542448 mov rdx, qword ptr [rsp+0x48]
main.go:8 0x10abab9 8402 test byte ptr [rdx], al
main.go:8 0x10ababb 488d35be640000 lea rsi, ptr [rip+0x64be]
main.go:8 0x10abac2 488932 mov qword ptr [rdx], rsi
main.go:8 0x10abac5 488d7a08 lea rdi, ptr [rdx+0x8]
main.go:8 0x10abac9 833d30540d0000 cmp dword ptr [runtime.writeBarrier], 0x0
main.go:8 0x10abad0 7402 jz 0x10abad4
main.go:8 0x10abad2 eb06 jmp 0x10abada
main.go:8 0x10abad4 48894208 mov qword ptr [rdx+0x8], rax
main.go:8 0x10abad8 eb08 jmp 0x10abae2
main.go:8 0x10abada e8213ffbff call $runtime.gcWriteBarrier
main.go:8 0x10abadf 90 nop
main.go:8 0x10abae0 eb00 jmp 0x10abae2
main.go:8 0x10abae2 488b442448 mov rax, qword ptr [rsp+0x48]
main.go:8 0x10abae7 8400 test byte ptr [rax], al
main.go:8 0x10abae9 eb00 jmp 0x10abaeb
main.go:8 0x10abaeb 4889442478 mov qword ptr [rsp+0x78], rax
main.go:8 0x10abaf0 48c784248000000001000000 mov qword ptr [rsp+0x80], 0x1
main.go:8 0x10abafc 48c784248800000001000000 mov qword ptr [rsp+0x88], 0x1
main.go:8 0x10abb08 bb01000000 mov ebx, 0x1
main.go:8 0x10abb0d 4889d9 mov rcx, rbx
main.go:8 0x10abb10 e8aba8ffff call $fmt.Println
main.go:9 0x10abb15 488bac2490000000 mov rbp, qword ptr [rsp+0x90]
main.go:9 0x10abb1d 4881c498000000 add rsp, 0x98
main.go:9 0x10abb24 c3 ret
main.go:5 0x10abb25 e8f61efbff call $runtime.morestack_noctxt
.:0 0x10abb2a e9f1feffff jmp $main.main
runtime.growslice
(dlv) break runtime.growslice
Breakpoint 2 set at 0x1047dea for runtime.growslice() /usr/local/opt/go/libexec/src/runtime/slice.go:162
continue
(dlv) continue
> runtime.growslice() /usr/local/opt/go/libexec/src/runtime/slice.go:162 (hits goroutine(1):1 total:1) (PC: 0x1047dea)
Warning: debugging optimized function
157: // NOT to the new requested capacity.
158: // This is for codegen convenience. The old slice's length is used immediately
159: // to calculate where to write new values during an append.
160: // TODO: When the old backend is gone, reconsider this decision.
161: // The SSA backend might prefer the new length or to return only ptr/cap and save stack space.
=> 162: func growslice(et *_type, old slice, cap int) slice {
163: if raceenabled {
164: callerpc := getcallerpc()
165: racereadrangepc(old.array, uintptr(old.len*int(et.size)), callerpc, funcPC(growslice))
166: }
167: if msanenabled {
nilgrowslice
上文咱们介绍调试汇编的一个根本流程,上面在介绍两个我在看源代码时常常应用的命令;
goroutinesgoroutinegoroutine (alias: gr)gourtine
(dlv) grs
* Goroutine 1 - User: ./main.go:7 main.main (0x10aba6f) (thread 218565)
Goroutine 2 - User: /usr/local/opt/go/libexec/src/runtime/proc.go:367 runtime.gopark (0x1035232) [force gc (idle)]
Goroutine 3 - User: /usr/local/opt/go/libexec/src/runtime/proc.go:367 runtime.gopark (0x1035232) [GC sweep wait]
Goroutine 4 - User: /usr/local/opt/go/libexec/src/runtime/proc.go:367 runtime.gopark (0x1035232) [GC scavenge wait]
Goroutine 5 - User: /usr/local/opt/go/libexec/src/runtime/proc.go:367 runtime.gopark (0x1035232) [finalizer wait]
stackstack
(dlv) bt
0 0x0000000001047e15 in runtime.growslice
at /usr/local/opt/go/libexec/src/runtime/slice.go:183
1 0x00000000010aba6f in main.main
at ./main.go:7
2 0x0000000001034e13 in runtime.main
at /usr/local/opt/go/libexec/src/runtime/proc.go:255
3 0x000000000105f9c1 in runtime.goexit
at /usr/local/opt/go/libexec/src/runtime/asm_amd64.s:1581
regsregs
(dlv) regs
Rip = 0x0000000001047e15
Rsp = 0x000000c00010de68
Rax = 0x00000000010b2f00
Rbx = 0x0000000000000000
Rcx = 0x0000000000000000
Rdx = 0x0000000000000008
Rsi = 0x0000000000000001
Rdi = 0x0000000000000000
Rbp = 0x000000c00010ded0
R8 = 0x0000000000000000
R9 = 0x0000000000000008
R10 = 0x0000000001088c40
R11 = 0x0000000000000246
R12 = 0x000000c00010df60
R13 = 0x0000000000000000
R14 = 0x000000c0000001a0
R15 = 0x00000000000000c8
Rflags = 0x0000000000000202 [IF IOPL=0]
Cs = 0x000000000000002b
Fs = 0x0000000000000000
Gs = 0x0000000000000000
localslocals
(dlv) locals
newcap = 1
doublecap = 0
总结
看源代码的过程是没有捷径可走的,如果说有,那就是能够先看一些大佬输入的底层原理的文章,而后参照其文章一步步入门源码浏览,最终还是要本人去克服这个艰难,本文介绍了我本人查看源码的一些形式,你是否有更简便的形式呢?欢送评论区分享进去~。
好啦,本文到这里就完结了,我是asong,咱们下期见。
欢送关注公众号:Golang梦工厂