golang coredump分析「建议收藏」

背景最近在分析golang的一个内存泄漏问题。一般来讲,使用golang自带的pprof工具就可以分析内存的使用,协程情况,是否有block等情况。但是我们项目中调用了C库,导致C库的一些东西没法通过pprof来进行监控分析。实际上通过pprof来监控程序的话,内存是稳定的,但是占用Linux的内存是一直增长的,即RES一直增长,实际上程序是有泄漏的。怀疑是使用C库导致,所以通过coredump…

大家好,又见面了,我是你们的朋友全栈君。

背景

最近在分析golang的一个内存泄漏问题。一般来讲,使用golang自带的pprof工具就可以分析内存的使用,协程情况,是否有block等情况。但是我们项目中调用了C库,导致C库的一些东西没法通过pprof来进行监控分析。实际上通过pprof来监控程序的话,内存是稳定的,但是占用Linux的内存是一直增长的,即RES一直增长,实际上程序是有泄漏的。怀疑是使用C库导致,所以通过coredump文件来进行分析。下面主要介绍golang coredump的使用。

分析

主要参考golang官网Debugging Go Code with GDB
其中:
The GOTRACEBACK variable controls the amount of output generated when a Go program fails due to an unrecovered panic or an unexpected runtime condition. By default, a failure prints a stack trace for the current goroutine, eliding functions internal to the run-time system, and then exits with exit code 2. The failure prints stack traces for all goroutines if there is no current goroutine or the failure is internal to the run-time. GOTRACEBACK=none omits the goroutine stack traces entirely. GOTRACEBACK=single (the default) behaves as described above. GOTRACEBACK=all adds stack traces for all user-created goroutines. GOTRACEBACK=system is like “all” but adds stack frames for run-time functions and shows goroutines created internally by the run-time. GOTRACEBACK=crash is like “system” but crashes in an operating system-specific manner instead of exiting. For example, on Unix systems, the crash raises SIGABRT to trigger a core dump. For historical reasons, the GOTRACEBACK settings 0, 1, and 2 are synonyms for none, all, and system, respectively. The runtime/debug package’s SetTraceback function allows increasing the amount of output at run time, but it cannot reduce the amount below that specified by the environment variable. See https://golang.org/pkg/runtime/debug/#SetTraceback.
GOTRACEBACK变量可以控制程序在崩溃时,stack的输出情况。下面结合具体地程序来分析。

package main

import (
	"time"

	"github.com/astaxie/beego/logs"
)

func main() {
	logs.Info("Start...")
	defer logs.Info("exit.")
	i := 0
	c := make(chan int, 1)
	for {
		go func(i int) {
			mem := make([]int, 100*1024*1024)
			logs.Info("i=%d,mem:%p", i, mem)
			mem[0] = <-c
		}(i)
		i++
		time.Sleep(200 * time.Microsecond)
	}
}

1、首先打开coredump的生成开关: ulimit -c unlimited
2、执行 GOTRACEBACK=crash ./testcoredump,会产生coredump文件

goroutine 279 [running]:
	goroutine running on other thread; stack unavailable
created by main.main
	/opt/gopath/src/test/coredump_test/testcoredump.go:15 +0xdf

goroutine 290 [running]:
	goroutine running on other thread; stack unavailable
created by main.main
	/opt/gopath/src/test/coredump_test/testcoredump.go:15 +0xdf
Aborted (core dumped)

3、gdb可以进行调试,查看程序运行的详细情况

 gdb testcoredump core.15956
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-110.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
...
(gdb) start
Temporary breakpoint 1 at 0x618c50: file /opt/gopath/src/test/coredump_test/testcoredump.go, line 9.
Starting program: /opt/gopath/src/test/coredump_test/testcoredump
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7ffff77f1700 (LWP 15980)]
[New Thread 0x7ffff6ff0700 (LWP 15981)]
[New Thread 0x7ffff5fee700 (LWP 15983)]
[New Thread 0x7ffff67ef700 (LWP 15982)]
[New Thread 0x7ffff57ed700 (LWP 15984)]

Temporary breakpoint 1, main.main () at /opt/gopath/src/test/coredump_test/testcoredump.go:9
9	func main() {
(gdb)

gdb常用命令:

start		//开始调试
n		//一条一条执行
step/s		//执行下一条,如果函数进入函数
backtrace/bt		//查看函数调用栈帧
info/i locals		//查看当前栈帧局部变量
frame/f		//选择栈帧,再查看局部变量
print/p		//打印变量的值
finish		//运行到当前函数返回
set var sum=0		//修改变量值
list/l 行号或函数名		//列出源码
display/undisplay sum		//每次停下显示变量的值/取消跟踪
break/b  行号或函数名		//设置断点
continue/c		//连续运行
info/i breakpoints		//查看已经设置的断点
delete breakpoints 2		//删除某个断点
disable/enable breakpoints 3		//禁用/启用某个断点
break 7 if ok == true		//满足条件才激活断点
run/r		//重新从程序开头连续执行
watch input[7]		//设置观察点
info/i watchpoints		//查看设置的观察点
x/7b input		//打印存储器内容,b--每个字节一组,7--7组
disassemble		//反汇编当前函数或指定函数
 si		// 一条指令一条指令调试 而 s 是一行一行代码
 info registers		// 显示所有寄存器的当前值
x/20 $esp		//查看内存中开始的20个数

总结

程序崩溃,可以通过coredump详细地查看程序调用栈的相关信息,可以更迅速的定位到程序的问题,特别是引起程序崩溃的bug:内存泄漏,一些panic等,当然在写程序时尽量多些log更方便调试。golang自带的pprof在涉及到c库的调用时,会监测不到,这时coredump结合gdb进行调试会比较有用。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/128584.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Microsoft Enterprise Library 6.0 下载

    Microsoft Enterprise Library 6.0 下载一不留神entLib也发布了6.0版本程序及源代码的下载:http://www.microsoft.com/en-us/download/details.aspx?id=38789附:MSDN的参考资料:http://msdn.microsoft.com/en-us/library/dn169621.aspx转载于:https://www.cnbl…

    2022年10月20日
    4
  • iframe嵌套页面拒绝访问_苹果cms模板修改导航

    iframe嵌套页面拒绝访问_苹果cms模板修改导航网站底部友链,后台设置新窗口打开,前端页面点击无效,还是当前页面打开<ahref=”{$field.url}”{$field.target}target=”_blank”title=”{$field.title}”>{$field.title}</a>在模板里面友情链接里面得A标签里面添加{$field.target}标签即可{eyou:flinktype=’text’row=’100’titlelen=’20’}<ahref=”{$fi.

    2022年10月6日
    5
  • pycharm怎么新建python项目_pycharm怎么新建一个项目

    pycharm怎么新建python项目_pycharm怎么新建一个项目如果选择新建虚拟环境并且没有加入本地解释器的库的话会导致没有代码提示的一、如果选择新建虚拟环境的话二、选择系统解释器,这样可能会导致多个项目时依赖库太多三、如果不是这个原因导致没有代码提示的话,可以看看下面的其他注意事项1.2.3.看看这里的解释器是否正常,一般都是默认正常的…

    2022年8月25日
    8
  • windows oracle11g安装教程_oracle11g安装包

    windows oracle11g安装教程_oracle11g安装包1、Oracle11gR2安装手册(图文教程)ForWindows安装前大家需要确认以下几点:你的内存没有问题(这一点很重要,如果你的机子经常蓝屏那就不要装了,不然有你哭的)你的系统已经激活计算机已安装.NetFramework4.0,不然第一步就会有弹出框告诉你“oui.exe已停止工作”即使是64位的系统也可以安装32位的Oracle2.解压两个压缩包到同一目录,即”database”…

    2025年11月25日
    3
  • Godaddy域名与空间解析教程(全)「建议收藏」

    Godaddy域名与空间解析教程(全)「建议收藏」最近忙着开自己的独立博客,既然以后是打算从事外贸相关的电子商务,我打算从Godaddy下手学习外贸空间的选择。1.购买Godaddy,为什么选狗爹,便宜、稳定、再加上可以用支付宝,省去代购的繁琐。这

    2022年8月5日
    7
  • AssertValid函数学习

    AssertValid函数学习转自http://tsitao.blog.163.com/blog/static/29795822006914105840496/VC的调试中,AssertValid和Dump函数的应用CObject

    2022年7月1日
    24

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号