首页 > 其他分享 >golang coredump

golang coredump

时间:2022-11-24 22:23:52浏览次数:62  
标签:runtime err fmt coredump golang func Printf meng

golang生成coredump文件
refer:
    https://blog.csdn.net/xmcy001122/article/details/105665732
    http://t.zoukankan.com/lit10050528-p-4811833.html

1.设置coredump文件
1).临时设置
    echo "/tmp/core-%e-%p-%s-%t" > /proc/sys/kernel/core_pattern
    echo 1 > /proc/sys/kernel/core_pid
2).永久设置
    echo -e "\nkernel.core_pattern=/tmp/core-%e-%p-%s-%t" >> /etc/sysctl.conf
    echo -e "\nkernel.core_uses_pid = 1" >> /etc/sysctl.conf
    sysctl -p /etc/sysctl.conf


2.设置环境变量
echo "GOTRACEBACK=crash" >> ~/.bashrc

3.执行程序
go build -o meng.out meng.go
./meng.out
或者2,3步骤合并:
    GOTRACEBACK=crash ./meng.out
    
4.调试coredump
gdb ./meng.out /tmp/core-meng-399155-6-1669296379    

dlv ./meng.out /tmp/core-meng-399155-6-1669296379    
>>
>>bt
 0  0x000000000045cf41 in runtime.raise
    at /usr/local/src/go/src/runtime/sys_linux_amd64.s:165
 1  0x0000000000443a25 in runtime.dieFromSignal
    at /usr/local/src/go/src/runtime/signal_unix.go:769
 。。。。。。
10  0x000000000048ae1f in main.doCommitTask.func1
    at ./meng.go:27
>>frame 10(报错所在栈)
>>list

5.附:meng.go

 1 package main
 2 
 3 import (
 4     "errors"
 5     "fmt"
 6 
 7     "time"
 8     "runtime"
 9     "path"
10 )
11 
12 func main() {
13     fmt.Printf("Hello world!\n")    
14     taskIds := make([]int32, 0)
15     go doCommitTask(&taskIds)
16     time.Sleep(10*time.Second)
17     fmt.Printf("taskIds=%v\n", taskIds)
18 }
19 
20 func doCommitTask(taskIds *[]int32) {
21     defer func() {
22         err := recover()
23         if err != nil {
24             fmt.Printf("error: err=%v\n", err)
25         }
26         fmt.Printf("defer: now=%v\n", time.Now())
27         panic("now")
28     }()
29     fmt.Printf("doCommitTask: start2\n")
30     panic("jack at home2")
31     for i := int32(0); i < 100; i += 1 {
32         *taskIds = append(*taskIds, i)
33     }
34 }
35 
36 
37 func play() {
38     defer func() {
39         if err := recover(); err != nil {
40             fmt.Errorf("panic in safeGo: %v", err)
41         }
42     }()
43     fmt.Printf("player: hello\n")
44     err := errors.New("player stop")
45     panic(err)
46     fmt.Printf("player: world\n\n")
47 }
48 
49 func SafeGo(fun func()) {
50     defer func() {
51         if err := recover(); err != nil {
52             fmt.Errorf("panic in safeGo: %v", err)
53         }
54     }()
55     fun()
56 }
57 
58 func getRootPath(skip int) string {
59     pc, fileName, line, ok := runtime.Caller(skip)
60     if !ok {
61         fmt.Printf("StartUp: runtime.Caller is error.\n")
62         return ""
63     }
64     funcName := runtime.FuncForPC(pc).Name()
65     rootPath := path.Dir(fileName)
66     fmt.Printf("getRootPath: skip=%d, funcName=%s, rootPath=%s, fileName=%s, line=%d.\n", skip, funcName, rootPath, fileName, line)
67     GetNextZeroTime()
68     return rootPath
69 }
70 
71 //当地时间下一次0点
72 func GetNextZeroTime() int64 {
73     nowTime := time.Now()
74     now := nowTime.Unix()
75     _, offsetSeconds := nowTime.Zone()          //相对于utc时区偏移秒数
76     sec := (now + int64(offsetSeconds)) % 86400 //相对于当前时区0点偏移秒数
77     return now - sec + 86400
78 }
79 
80 func PrintLatestStack(playerId int32, skip int) {
81     fmt.Printf("PrintLatestStack: playerId=%d, traces:\n", playerId)
82     pcs := make([]uintptr, 10)
83     n := runtime.Callers(skip+2, pcs)
84     for i := 0; i < n; i += 1 {
85         pc := pcs[i]
86         runFunc := runtime.FuncForPC(pc)
87         fileName, line := runFunc.FileLine(pc)
88         fmt.Printf("funcName=%s, fileName=%s, line=%d.\n", runFunc.Name(), fileName, line)
89     }
90 }

 

标签:runtime,err,fmt,coredump,golang,func,Printf,meng
From: https://www.cnblogs.com/ginkgo-leaf/p/16923632.html

相关文章

  • HDU:1091 的 python3 和 golang 实现
    python3defhdu_1091():whileTrue:s=input("input")s1=s.split("")ifs1[0]=="0"ands1[1]=="0":break......
  • HDU:1090 的 python3 和 golang 实现
    python3defhdu_1090():a=int(input(""))whilea!=0:s=input("input")s1=s.split("")print(int(s1[0])+int(s1[1]))......
  • golang使用pprof笔记
    ==背景==程序研发完之后,发现程序会异常结束,然后由容器重新拉起,重启的频率不定,为了排查这个问题,准备使用pprof找找线索。 ==相关文档==pprof性能调优读懂pprof生成的......
  • golang 反向代理 Gin框架反向代理详解
    前言想用gin框架做一个反向代理服务,搜索了一圈,全是只讲解些皮毛的帖子,今天我就总结一下gin做反向代理的详细操作和原理正文开始之前我们先了解一些前置知识gin的通配符......
  • 【go】golang 熔断器的实现过程
    熔断器像是一个保险丝。当我们依赖的服务出现问题时,可以及时容错。一方面可以减少依赖服务对自身访问的依赖,防止出现雪崩效应;另一方面降低请求频率以方便上游尽快恢复服务......
  • 解决方案(14) golang pprof接入业务同端口
    前言声明:本文不对pprof基础做讲解,详情可以查阅https://studygolang.com/pkgdoc找到net/http/pprofgolangpprof官方介绍里,是采用了默认6060端口,单独开辟了应用进程的ppr......
  • golang 微信支付计算签名
    原文:1、签名算法(签名校验工具)签名生成的通用步骤如下:第一步,设所有发送或者接收到的数据为集合M,将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),使用URL键......
  • golang算法—— 使用两个栈实现一个队列
    前言阅读本文,假定已经了解了基本数据结构概念。队列:先入先出。栈:先进后出。分析使用两个栈串联,可以实现先进先出。但是,得注意以下两点:队列在入列时,stack2必须为空,stac......
  • golang grpc使用示例
    疑问写前面grpc有内部对心跳的处理吗,还是说,双工需要自己作心跳管理,有懂的留言一下。SEO优化grpc如何双工通信?grpc如何从服务端推送消息给客户端?gprc环境如何搭建?grpc......
  • go /golang 下生成protobuf *.pb.go文件 记录
      如果出现这个状况解决办法记录一下:在指定目录  gitclonehttps://github.com/golang/protobuf 下载源码   进入到以下目录  分别执行 以下图片......