Table of Contents
软件调试技术(2)-- coredump如何实现
Home php教程 php手册 软件调试技术(2)-- coredump如何实现

软件调试技术(2)-- coredump如何实现

Jun 13, 2016 am 08:48 AM
android

软件调试技术(2)-- coredump如何实现

最近在工作中了解到应用软件在崩溃的时候可以设置生成转存文件,这个功能就是coredump。不论是应用程序崩溃还是kernel崩溃转存功能都是很实用技术。本文是想从实现原理角度来理解这两项技术实现方法。首先分析一下应用程序coredump的实现方法。

什么条件触发转存
这个转存发生在应用程序收到致命的signal,在处理这个signal的时候会执行应用程序的转存,并在之后退出执行。有一点必须注意,如果用户给这些致命signal安装了自定义的handler,转存是不会发生的。OK,那么这里总结出两点:
1,致命signal会出发转存发生,这些signal包括如下内容。

  1. 398 #define SIG_KERNEL_COREDUMP_MASK(\
  2. 399 rt_sigmask(SIGQUIT)|rt_sigmask(SIGILL)|\
  3. 400 rt_sigmask(SIGTRAP)|rt_sigmask(SIGABRT)|\
  4. 401 rt_sigmask(SIGFPE)|rt_sigmask(SIGSEGV)|\
  5. 402 rt_sigmask(SIGBUS)|rt_sigmask(SIGSYS)|\
  6. 403 rt_sigmask(SIGXCPU)|rt_sigmask(SIGXFSZ)|\
  7. 404 SIGEMT_MASK
2,如果上面的致命signal被用户安装了自定义的处理函数,coredump也不会发生。

  1. 2264if(ka->sa.sa_handler!=SIG_DFL){
  2. 2265/*Run the handler.*/
  3. 2266 ksig->ka=*ka;
  4. 2267
  5. 2268if(ka->sa.sa_flags&SA_ONESHOT)
  6. 2269 ka->sa.sa_handler=SIG_DFL;
  7. 2270
  8. 2271 break;/*will return non-zero"signr"value*/
  9. 2272}
在kernel查找需要处理的signal的时候,首先会判断这个signal是不是默认处理方式,如果是默认处理才会继续执行到coredump分支,否则跳出查找环节,执行用户自定义处理方式。

触发coredump执行的分支

  1. 2335if(sig_kernel_coredump(signr)){
  2. 2336if(print_fatal_signals)
  3. 2337 print_fatal_signal(ksig->info.si_signo);
  4. 2338 proc_coredump_connector(current);
  5. 2339/*
  6. 2340*Ifit was abletodump core,this kills all
  7. 2341*other threadsinthe groupandsynchronizes with
  8. 2342*their demise.Ifwe lost the race with another
  9. 2343*thread getting here,itsetgroup_exit_code
  10. 2344*firstandour do_group_exitcallbelow will use
  11. 2345*that valueandignore the one we pass it.
  12. 2346*/
  13. 2347 do_coredump(&ksig->info);
  14. 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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

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

Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

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

IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

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

Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

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 Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

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

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

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

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

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

iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship Sep 10, 2024 am 06:45 AM

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

See all articles