美文网首页
go 程序生成 coredump 文件

go 程序生成 coredump 文件

作者: wayyyy | 来源:发表于2022-04-17 00:09 被阅读0次
开启core文件功能
  • 设置core文件限制大小
    使用 ulimit -c 可以查看core文件限制大小,一般来说,默认为0,这种是不能生成core文件的。
    使用下面的命令改为无限制。
    # ulimit -c unlimited  # 针对当前终端此次会话有效
    
  • 设置 GOTRACEBACK 环境变量值为 crash
    # export GOTRACEBACK=crash    # 针对当前终端此次会话有效
    # env | grep GOTRACEBACK
      GOTRACEBACK=crash
    

如果需要针对当前用户一直生效,那么需要在 ~/.bash_profile 追加:

ulimit -c unlimited
GOTRACEBACK=crash

如果需要针对所有用户生效,那么需要在 /etc/profile 追加:

ulimit -c unlimited
GOTRACEBACK=crash

然后再对应执行source ~/.bash_profile 或者 source /etc/profile

测试:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "hello world\n")
    })
    log.Fatal(http.ListenAndServe("localhost:7777", nil))
}

编译运行,然后键盘敲 Ctrl + \,会发现当前目录生成了core文件。

控制core文件格式

/proc/sys/kernel/core_pattern 可以设置格式化的core文件保存位置和文件名。

比如:core-%e-%p-%t 表示在当前目录生成 "core-命令-pid-时间戳" 为文件名的core文件
比如:/cfg/core-%e-%p-%t 表示在/cfg下生成 "core-命令-pid-时间戳" 为文件名的core文件

注意:/proc/sys/kernel/core_pattern 不能直接编辑,可以用 echo core-%e-%p-%t > /proc/sys/kernel/core_pattern

符号 意义
%p insert pid into filename 添加 pid
%u insert current uid into filename 添加当前 uid
%g insert current gid into filename 添加当前 gid
%s insert signal that caused the coredump into the filename 添加导致产生 core 的信号
%t insert UNIX time that the coredump occurred into filename 添加 core 文件生成时的 unix 时间戳
%h insert hostname where the coredump happened into filename 添加主机名
%e insert coredumping executable name into filename 添加命令名

相关文章

网友评论

      本文标题:go 程序生成 coredump 文件

      本文链接:https://www.haomeiwen.com/subject/ffdptrtx.html