• go: play with so source code. Go源代码编译和调试


    工具

    vscode

    clone仓库

    git clone https://github.com/golang/go
    cd golang/go
    
    • 1
    • 2

    编译go

    编译的主要入口是src/make.bash(windows则是src/make.bat):

    # src/make.bash
    
    # GOROOT_FINAL: The expected final Go root, baked into binaries.
    # The default is the location of the Go tree during the build.
    #
    # GOROOT_BOOTSTRAP: A working Go tree >= Go 1.4 for bootstrap.
    # If $GOROOT_BOOTSTRAP/bin/go is missing, $(go env GOROOT) is
    # tried for all "go" in $PATH. $HOME/go1.4 by default.
    if [ ! -f run.bash ]; then
    	echo 'make.bash must be run from $GOROOT/src' 1>&2
    	exit 1
    fi
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    有两个环境变量值得注意:

    • GOROOT_FINAL 最终输出的编译的go的目录
    • GOROOT_BOOTSTRAP 用于编译go的基础go版本,必须>=go1.4

    必须在src目录运行make.bash

    cd src
    rm -rf build_go && mkdir -p build_go
    GOROOT_FINAL=build_go ./make.bash
    
    • 1
    • 2
    • 3

    日志:

    Building Go cmd/dist using /Users/xhd2015/installed/go1.17. (go1.17.5 darwin/amd64)
    Building Go toolchain1 using /Users/xhd2015/installed/go1.17.
    Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
    Building Go toolchain2 using go_bootstrap and Go toolchain1.
    Building Go toolchain3 using go_bootstrap and Go toolchain2.
    Building packages and commands for darwin/amd64.
    ---
    Installed Go for darwin/amd64 in /Users/xhd2015/Projects/gopath/src/github.com/golang/go
    Installed commands in /Users/xhd2015/Projects/gopath/src/github.com/golang/go/bin
    
    The binaries expect /Users/xhd2015/Projects/gopath/src/github.com/golang/go to be copied or moved to build_go
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    运行编译完成的go;

    $ GOROOT=$PWD bin/go version
    go version devel go1.19-416c953960 Sun Jun 26 00:26:59 2022 +0000 darwin/amd64
    
    • 1
    • 2

    误区1: 直接调试 src/cmd/go

    虽然src/cmd/go能够编译成go,但是如果直接使用系统的工具编译:

    cd src/cmd
    go build -o cmd_go ./go
    ./cmd_go # 这里实际是以系统的源码进行编译的
    
    • 1
    • 2
    • 3

    必须使用src/make.bash才能编译出当前目录的go

    调试src/cmd/go

    GOROOT=../.. ../../bin/go build -gcflags=all="-N -l" -o /tmp/go/cmd/cmd_go.bin ./go
    
    • 1

    在这里插入图片描述

    IDE

    vscode需要设置go.goroot:

      "go.goroot": "/Users/xhd2015/Projects/gopath/src/github.com/golang/go",
    
    • 1

    并重启。这样才能解析到对应的符号。

  • 相关阅读:
    GBase 8c V3.0.0数据类型——序列号生成函数
    开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
    F. Rats Rats(二分 or 预处理)[UTPC Contest 09-02-22 Div. 2 (Beginner)]
    基于UiAutomator2+PageObject模式开展APP自动化测试实战
    LeetCode——半有序排列
    基于spring boot开发的快递管理系统开题报告
    OSM+three.js打造3D城市
    SpringMVC ---- SpringMVC的视图
    vue2使用 vis-network 和 vue-vis-network 插件封装一个公用的关联关系图
    JSON实例操作
  • 原文地址:https://blog.csdn.net/xhdxhdxhd/article/details/125468847