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)
上一篇 2022年4月12日 上午7:40
下一篇 2022年4月12日 上午8:00


相关推荐

  • 静态方法与非静态方法区别

    静态方法与非静态方法区别一 静态方法静态方法又称类方法 是加 staic 关键字修饰的方法 在类定义的时候就被分配和装载入内存中 静态方法属于类所有 类实例化前即可使用 同时不能使用 super 和 this 关键字 静态方法只能调用静态方法以及访问静态数据域 不能调用实例方法或者访问实例数据域 二 非静态方法非静态方法 又称为实例方法 成员方法 是不加 staic 关键字修饰的方法 在类定义时不菲配内存 只有在类被实例化为对象后 对象调用该方法时才被分配内存 因此非静态方法属于对象 不属于类 非静态方法即能调用静态方法以及访问静态数

    2026年3月19日
    2
  • lombok插件失效原因_lombok插件下载

    lombok插件失效原因_lombok插件下载昨晚项目出了个小问题,lombok插件失效了。由于使用了lombok的@Data注解生成实体类的getter和setter,项目运行时所有的使用getter和setter方法的地方全都报错。看了网上的解决方案,一个一个试,发现没有一个能解决我的问题最后,我发现在pom.xml文件里面的<build></build>里面缺少了lombok的部分,把他加上即可<build><plugins><plugin

    2025年8月30日
    12
  • tcpdump抓包命令详解_tcpdump抓udp包命令详解

    tcpdump抓包命令详解_tcpdump抓udp包命令详解linux抓包-tcpdump命令:enp4s0f0—ifconfig—>网卡名称-s0—->不限制抓包的大小port25228—->只抓取服务器上25228端口的数据流量-whk2.pcap—>保存的文件名称-v—>显示抓包的大小数字

    2022年8月20日
    10
  • 机器学习常见问题

    机器学习常见问题

    2021年11月19日
    57
  • vue 计数器_74161计数器

    vue 计数器_74161计数器计数器的实现在页面上简单实现一个计数器<!DOCTYPEhtml><htmllang="en"><head><metach

    2022年8月7日
    8
  • NTP校时服务器(NTP校时器)资料详解

    NTP校时服务器(NTP校时器)资料详解NTP校时服务器(NTP校时器)资料详解NTP校时服务器(NTP校时器)资料详解NTP网络时间同步摘要:首先对时间同步进行了背景介绍,然后讨论了不同的时间同步网络技术,最后指出了建立全球或区域时间同步网存在的问题。  一、概述  在通信领域,“同步”概念是指频率的同步,即网络各个节点的时钟频率和相位同步,其误差应符合标准的规定。目前,在通信网中,频率和相位同步问题已经基本解决,而时间…

    2022年6月28日
    29

发表回复

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

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