软件调试技术(2)-- coredump如何实现
软件调试技术(2)-- coredump如何实现
最近在工作中了解到应用软件在崩溃的时候可以设置生成转存文件,这个功能就是coredump。不论是应用程序崩溃还是kernel崩溃转存功能都是很实用技术。本文是想从实现原理角度来理解这两项技术实现方法。首先分析一下应用程序coredump的实现方法。什么条件触发转存
这个转存发生在应用程序收到致命的signal,在处理这个signal的时候会执行应用程序的转存,并在之后退出执行。有一点必须注意,如果用户给这些致命signal安装了自定义的handler,转存是不会发生的。OK,那么这里总结出两点:
1,致命signal会出发转存发生,这些signal包括如下内容。
- 398 #define SIG_KERNEL_COREDUMP_MASK(\
- 399 rt_sigmask(SIGQUIT)|rt_sigmask(SIGILL)|\
- 400 rt_sigmask(SIGTRAP)|rt_sigmask(SIGABRT)|\
- 401 rt_sigmask(SIGFPE)|rt_sigmask(SIGSEGV)|\
- 402 rt_sigmask(SIGBUS)|rt_sigmask(SIGSYS)|\
- 403 rt_sigmask(SIGXCPU)|rt_sigmask(SIGXFSZ)|\
- 404 SIGEMT_MASK
- 2264if(ka->sa.sa_handler!=SIG_DFL){
- 2265/*Run the handler.*/
- 2266 ksig->ka=*ka;
- 2267
- 2268if(ka->sa.sa_flags&SA_ONESHOT)
- 2269 ka->sa.sa_handler=SIG_DFL;
- 2270
- 2271 break;/*will return non-zero"signr"value*/
- 2272}
触发coredump执行的分支
- 2335if(sig_kernel_coredump(signr)){
- 2336if(print_fatal_signals)
- 2337 print_fatal_signal(ksig->info.si_signo);
- 2338 proc_coredump_connector(current);
- 2339/*
- 2340*Ifit was abletodump core,this kills all
- 2341*other threadsinthe groupandsynchronizes with
- 2342*their demise.Ifwe lost the race with another
- 2343*thread getting here,itsetgroup_exit_code
- 2344*firstandour do_group_exitcallbelow will use
- 2345*that valueandignore the one we pass it.
- 2346*/
- 2347 do_coredump(&ksig->info);
- 2348}
将致命signal打印到控制台的Tips
如果你开启如下proc目录下的option,将开启打印致命signal到控制台的功能。
/proc/sys/kernel/print-fatal-signals
coredump主要步骤
其实将这个应用程序都写入到磁盘的过程肯定是非常负责的。这里我不会扣没一个细节,有一些原理性的细节可能推到后面的部分再详细学习和分析。但是,这里我们需要了解如下几个关于coredump的关键处理流程。
1, coredump之间,首先要检查线程组中所有线程是不是已经都进入到了休眠状态。我们要coredump必须保证我们在拷贝这个进程的内存空间的时候进程中的某些线程还在运行并改写内存空间。
2, 对于直接将进程内存转存到文件的情况,没有什么特别之处,就是打开一个文件,并开始写入。
3,对于制定了用户空间脚本,进行一些压缩操作的情况,这里会启动一个用户态进程,并在kernel和用户态进程之间建立一个管道,
kernel=========读出===========用户态进程(压缩)-----------写入-------->文件
对于第一步处理,kernel想进程组中所有的线程发送kill signal信号,然后等待所有进程都不处在运行状态。
对于第二步,处理相对简单,只是之后如果有兴趣还可深入研究一下coredump文件的格式到底是什么样子的。
第三步,相对比较复杂,比较巧妙的地方就是在kernel中建立了一条管道,kernel处理signal部分将coredump写入管道,而新建的进程中管道的另外一端被设置到了标准输入上面。
对于压缩进程转存的方法
我们可以兴建一个shell执行文件
#!/bin/sh
execgzip->/root/$1.core.$2.gz
然后在/proc/sys/kernel/core_pattern 中写入
|/usr/sbin/core_helper%e%p

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

OnePlus'sister brand iQOO has a 2023-4 product cycle that might be nearlyover; nevertheless, the brand has declared that it is not done with itsZ9series just yet. Its final, and possibly highest-end,Turbo+variant has just beenannouncedas predicted. T
