本文面向使用过Solaris的mdb但是没有使用过Linux的crash的同学。比如说我自己,mdb用了很多年,现在全面转向Linux平台,于是很好奇Linux有没有类似的工具。熟悉Solaris的同学都知道,事后分析crashdump或者在线调试操作系统内核的强大工具就是mdb。而在Linux (以CentOS为例)中,类似的工具则是crash。当然,mdb不支持源码级的调试,只支持汇编级的调试。而crash则支持源码级的调试,因为跟gdb结合得非常紧密。

$ man -s8 crash
NAME
crash - Analyze Linux crash dump data or a live system SYNOPSIS
crash [OPTION]... NAMELIST MEMORY-IMAGE[@ADDRESS] (dumpfile form)
crash [OPTION]... [NAMELIST] (live system form)
...<snip>...

mdb的使用比crash要简单一些,因为不需要自己安装NAMELIST。但crash比mdb要好的地方在于支持源码级调试。

下面将介绍如何在CentOS上使用crash分析一个crashdump文件。

第一次使用crash的感觉很蒙圈,因为搞不懂神马是NAMELIST。

注意:使用mdb进行live的调试,就不需要神马NAMELIST。很简单, -K(大K)只能在console上使用,可以让整个内核挂起。例如:

root# mdb -k    #<-- Live kernel
or
root# mdb -K #<-- kmdb

这种情况下只能问度娘和qwant(在家上不了Google)了,幸运的是找到了Red Hat Enterprise Linux 7 Kernel Crash Dump Guide。于是按照Guide的提示一步一步来。有关kdump的简介截图如下:

1. 检查kexec-tools是否已经安装

[root@idorax9 tmp]# rpm -q kexec-tools
kexec-tools-2.0.7-50.el7.x86_64

我用的是CentOS 7, 默认已安装。

2. 安装system-config-kdump

[root@idorax9 tmp]# rpm -q system-config-kdump
package system-config-kdump is not installed
[root@idorax9 tmp]#
[root@idorax9 tmp]# yum install system-config-kdump

3. 通过GUI对kdump进行配置 (goto Applications->System Tools->Kernel crash dumps)

选Basic Settings -> Manual settings, 将New kdump Memory设置为128M, 其他都使用默认设置,然后点Apply

点击apply后,会遇到"Unable to handle kdump services"的错误,不用理会,接下来需要reboot

在reboot之前,查看一下kdump是否active,果然不是

[root@idorax9 tmp]# systemctl is-active kdump
failed

4. reboot

5. reboot后,再次检查kdump是否active,必须是啦

[root@idorax9 tmp]# systemctl is-active kdump
active

6. 创建一个crashdump (类似solaris的reboot -d)

[root@idorax9 tmp]# echo 1 > /proc/sys/kernel/sysrq
[root@idorax9 tmp]# echo c > /proc/sysrq-trigger

7. 系统重启回来后,查看/var/tmp/vmcore

8. 尝试使用crash分析vmcore

8.1 安装NAMELIST,首先得启用CentOS-Debuginfo.repo

[root@idorax9 tmp]# cp /etc/yum.repos.d/CentOS-Debuginfo.repo /tmp
[root@idorax9 tmp]# vi /etc/yum.repos.d/CentOS-Debuginfo.repo
[root@idorax9 tmp]# diff /tmp/CentOS-Debuginfo.repo /etc/yum.repos.d/CentOS-Debuginfo.repo
20c20
< enabled=0
---
> enabled=1

8.2 安装NAMELIST, 注意/usr/lib/debug下面的内容为空,在安装之前

[root@idorax9 tmp]# yum clean all
[root@idorax9 tmp]# yum makecache [root@idorax9 tmp]# ls -lF /usr/lib/debug/
total 0
lrwxrwxrwx. 1 root root  7 May 31 20:33 bin -> usr/bin/
lrwxrwxrwx. 1 root root  7 May 31 20:33 lib -> usr/lib/
lrwxrwxrwx. 1 root root  9 May 31 20:33 lib64 -> usr/lib64/
lrwxrwxrwx. 1 root root  8 May 31 20:33 sbin -> usr/sbin/
drwxr-xr-x. 6 root root 65 May 31 20:33 usr/ [root@idorax9 tmp]# rpm -q kernel
kernel-3.10.0-514.el7.x86_64 # NOTE: The debuginfo should be kernel-debuginfo-3.10.0-514.el7.x86_64
[root@idorax9 tmp]# debuginfo-install kernel # NOTE: It took about 20m to download kernel-debuginfo-3.10.0-514.el7.x86_64.rpm

8.3 安装完成NAMELIST后

[root@idorax9 tmp]# ls /usr/lib/debug/lib/modules/
3.10.0-514.el7.x86_64

8.4 开始分析vmcore

[root@idorax9 tmp]# NAMELIST=/usr/lib/debug/lib/modules/3.10.0-514.el7.x86_64/vmlinux
[root@idorax9 tmp]# VMCORE=/var/crash/127.0.0.1-2017-05-31-22\:47\:32/vmcore
[root@idorax9 tmp]# crash $NAMELIST $VMCORE crash 7.1.5-2.el7
Copyright (C) 2002-2016 Red Hat, Inc.
...<snip>...............................................
This GDB was configured as "x86_64-unknown-linux-gnu"... KERNEL: /usr/lib/debug/lib/modules/3.10.0-514.el7.x86_64/vmlinux
DUMPFILE: /var/crash/127.0.0.1-2017-05-31-22:47:32/vmcore [PARTIAL DUMP]
CPUS: 1
DATE: Wed May 31 22:47:29 2017
UPTIME: 00:05:44
LOAD AVERAGE: 0.04, 0.41, 0.26
TASKS: 368
NODENAME: idorax9
RELEASE: 3.10.0-514.el7.x86_64
VERSION: #1 SMP Tue Nov 22 16:42:41 UTC 2016
MACHINE: x86_64 (2195 Mhz)
MEMORY: 1 GB
PANIC: "SysRq : Trigger a crash"
PID: 3408
COMMAND: "bash"
TASK: ffff88000a4b8fb0 [THREAD_INFO: ffff88003b564000]
CPU: 0
STATE: TASK_RUNNING (SYSRQ) crash>

终于进入crash> 的操作界面了!

NAMELIST就是/usr/lib/debug/lib/modules/3.10.0-514.el7.x86_64/vmlinux, 这也忒长了...Orz (不得不说,Linux在CLI易用性上比Solaris差远了)

o 看看Backtrace (用bt命令,mdb用$C)

o 再反汇编看看 (disas /m <mem addr>, 而mdb的语法是<mem addr>::dis)

结论: 显然,系统之所以panic,是因为向内存地址0x0处写入0x1。到此为止,我们从零开始,完整体验了一把使用crash工具定位Linux系统panic的root cause的全过程. 有关crash工具的使用及技术原理,请阅读后面列出的参考资料。(特别推荐参考资料6:Crash Dump Analysis

扩展:Solaris/Linux的工具链对比

# Solaris Linux(CentOS)
1 cc gcc
2 dbx gdb
3 mdb crash
4 dtrace systemtab, ftrace, LTTng
5 truss strace

 参考资料:

1. Red Hat Enterprise Linux 7 Kernel Crash Dump Guide

2.Analyzing Linux kernel crash dumps with crash - The one tutorial that has it all

3. http://elixir.free-electrons.com/linux/v4.12-rc3/source/Documentation/kdump/kdump.txt

4. https://github.com/crash-utility/crash

6. Crash Dump Analysiscrash | mdb

从mdb到crash的更多相关文章

  1. 【腾讯Bugly干货分享】聊聊苹果的Bug - iOS 10 nano_free Crash

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/hnwj24xqrtOhcjEt_TaQ9w 作者:张 ...

  2. iOS 10 开发适配系列 之 权限Crash问题

    升级 iOS 10 之后目测坑还是挺多的,记录一下吧,看看到时候会不会成为一个系列. 直入正题吧 今天用一个项目小小练下手,发现调用相机,崩了.试试看调用相册,又特么崩了.然后看到控制台输出了以下信息 ...

  3. BZOJ 2154: Crash的数字表格 [莫比乌斯反演]

    2154: Crash的数字表格 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 2924  Solved: 1091[Submit][Status][ ...

  4. erlang 虚机crash

    现网服务,每次更新一个服务时,另外一个集群所有node 都跟着同时重启一遍,这么调皮,这是闹哪样啊.. 看系统日志:/var/log/messages Oct 30 15:19:41 localhos ...

  5. Breakpad Google的crash捕获、抓取开源库

    简介: Breadpad为google chrominum项目下用于处理dump的一套工具:内部采用跨平台方式实现捕获.生成.解析与平台无关的dump,便于统一处理:支持进程内与进程外捕获,当为进程外 ...

  6. SQL Server通过File Header Page来进行Crash Recovery

    SQL Server通过File Header Page来进行Crash Recovery 看了盖总的一篇文章 http://www.eygle.com/archives/2008/11/oracle ...

  7. ios crash的原因与抓取crash日志的方法

    首先我们经常会闪退的异常有哪些呢?crash的产生来源于两种问题:违反iOS策略被干掉,以及自身的代码bug. 1.IOS策略 1.1 低内存闪退 前面提到大多数crash日志都包含着执行线程的栈调用 ...

  8. iOS --------Crash 分析(一)

    iOS Crash 分析(文一)- 开始 1. 名词解释 1. UUID 一个字符串,在iOS上每个可执行文件或库文件都包含至少一个UUID.目的是为了唯一识别这个文件. 2. dwarfdump 苹 ...

  9. C#+arcengine10.0+SP5实现鹰眼(加载的是mdb数据库中的数据)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

随机推荐

  1. EJB3.0 EJB开发消息驱动bean

    (7)EJB3.0 EJB开发消息驱动bean JMS 一: Java消息服务(Java Message Service) 二:jms中的消息 消息传递系统的中心就是消息.一条 Message 由三个 ...

  2. Postgresql 分区表 一

    Postgres 10 新特性 分区表 http://francs3.blog.163.com/blog/static/40576727201742103158135/ Postgres 10 之前分 ...

  3. sqlserver排名函数

    在做开发的时候,排名函数是sqlserver经常用到的函数,在分页的时候需要用,分组的时候也要用,主要排名函数有row-number,rank(),dense-rank(),NTILE()接下来详细说 ...

  4. http与https通信

    HTTP协议 http协议与https协议的区别 GET请求和POST请求的说明与比较 发送GET和POST请求(使用NSURLSession)

  5. 【12c OCP】CUUG OCP认证071考试原题解析(36)

    36.choose the best answer View the Exhibits and examine the structures of the PRODUCTS, SALES, and C ...

  6. Android中线程和线程池

    我们知道线程是CPU调度的最小单位.在Android中主线程是不能够做耗时操作的,子线程是不能够更新UI的.在Android中,除了Thread外,扮演线程的角色有很多,如AsyncTask,Inte ...

  7. [CSS3] :nth-child的用法

    :nth-child(2)选取第几个标签,“2可以是你想要的数字” .demo01 li:nth-child(2){background:#090} :nth-child(n+4)选取大于等于4标签, ...

  8. HTML5语义化标签总结

    1.语义化标签总结 基础布局标签 <header></header> <nav></nav> <main></main> < ...

  9. 【javascrpt】——图片预览和上传,兼容IE 9-

    下载DEMO:https://github.com/CaptainLiao/zujian/tree/master/Upload 对于现代浏览器来说,要实现图片预览非常简单: 1.fileReader. ...

  10. [Objective-C语言教程]多态(26)

    多态性这个词表示有许多形式. 通常,当存在类的层次结构并且通过继承相关时,会发生多态性. Objective-C多态表示对成员函数的调用将导致执行不同的函数,具体取决于调用该函数的对象的类型. 考虑下 ...