Linux段错误(精)

合集下载

linux 异常进程处理机制 -回复

linux 异常进程处理机制 -回复

linux 异常进程处理机制-回复Linux 异常进程处理机制一、引言在Linux操作系统中,异常进程是指在执行期间出现错误或异常的进程。

这些错误或异常可以是由于软件缺陷、资源不足、权限问题或其他系统问题引起的。

为了保证系统的稳定性和安全性,Linux操作系统提供了异常进程处理机制,以便适当地处理这些异常情况,并防止它们对系统造成严重影响。

二、异常进程的分类Linux操作系统将异常进程分为两类:可修复的异常进程和不可修复的异常进程。

可修复的异常进程是指可以通过某种方式修复或恢复正常运行的进程,例如重新启动进程、重新分配资源等。

不可修复的异常进程是指无法通过任何方式修复或恢复正常运行的进程,必须被终止。

三、异常进程处理机制1. 错误检测Linux操作系统会通过监控进程的执行情况来检测是否出现了错误或异常。

这可以通过检查进程返回的错误码、访问权限以及操作系统提供的监控工具来实现。

一旦检测到异常,系统会采取相应的措施来处理。

2. 异常信号Linux操作系统通过向异常进程发送信号来通知它们发生了错误或异常情况。

常见的异常信号包括SIGSEGV(段错误)、SIGFPE(浮点异常)、SIGILL (非法指令)等。

当异常进程接收到信号后,它可以选择忽略、处理或终止。

忽略信号意味着进程将继续执行而不进行任何操作。

处理信号意味着进程会执行一个信号处理函数来处理异常情况。

终止信号意味着进程将被立即终止,不再继续执行。

3. 隔离机制对于可修复的异常进程,Linux操作系统提供了隔离机制来保护系统的其他部分不受其影响。

当出现异常时,操作系统会将异常进程隔离在一个被称为容器的环境中。

容器是一个独立、封闭的执行环境,它与其他部分相互隔离,不会影响其他进程或系统资源。

这种隔离机制可以有效地防止异常进程对系统的进一步破坏,同时允许管理员修复异常进程。

4. 日志记录当异常进程发生时,Linux操作系统还会记录日志以追踪异常情况。

段错误总结

段错误总结

(版权声明:本文欢迎转载,但未经允许不得用于商业目的)
2
Segmentation Fault in Linux
内容提要
本文简单介绍了 Segmentation fault 发生的原因, 结合实 际例子描述了内核向用户态程序发送 SIGSEGV 信号的流 程。 文中以实例回答了常见的一些 SIGSEGV 问 题 ,例如“为 什么函数返回了栈还可以访问?”、“为什么 free()后的内存 仍然 可以使用 ”、“为什 么我遇到的是 SIGSEGV 而不 是 SIGILL 信号”等。最后笔者结合自己的经验,列举了一些预 防 SIGSEGV 的编程习惯,供大家参考。SIGSEGV 严格依赖 操作系统、编译器、硬件平台,本文基于 Linux、GCC、32bit IA32 架构,但对其他平台操作系统也有借鉴意义。
我们引用 wiki 上的一段话来回答这个问题。
A segmentation fault (often shortened to SIGSEGV) is a particular error condition that can occur during the operation of computer software. A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system). Segmentation is one approach to memory management and protection in the operating system. It has been superseded by paging for most purposes, but much of the terminology of segmentation is still used, "segmentation fault" being an example. Some operating systems still have segmentation at some logical level although paging is used as the main memory management policy. On Unix-like operating systems, a process that accesses an invalid memory address receives the SIGSEGV signal. On Microsoft Windows, a process that accesses invalid memory receives the STATUS_ACCESS_VIOLATION exception.

Linux命令行中的进程调试和错误排查技巧

Linux命令行中的进程调试和错误排查技巧

Linux命令行中的进程调试和错误排查技巧在Linux系统中,进程调试和错误排查是开发者和系统管理员必备的技能之一。

通过命令行界面,可以使用一系列工具和技巧来定位和解决进程中的错误和问题。

本文将介绍一些常用的Linux命令行中的进程调试和错误排查技巧,帮助读者快速定位和修复问题。

1. 进程的状态查看与管理在Linux系统中,可以通过以下命令查看和管理进程的状态:- `ps`:显示当前运行的进程列表。

可以使用`ps aux`命令查看所有进程的详细信息,包括PID(进程ID)、CPU使用率、内存占用等。

- `top`:实时动态显示进程列表和系统资源的使用情况。

按键盘上的`q`退出。

- `kill`:终止一个正在运行的进程。

使用`kill PID`命令,其中PID 是要终止的进程的ID。

- `killall`:终止指定进程名对应的所有进程。

使用`killallprocess_name`命令,其中process_name是要终止的进程名。

2. 进程的跟踪与调试当进程发生错误或异常时,可以使用以下命令进行跟踪和调试:- `strace`:跟踪系统调用和信号的发生情况。

使用`strace -p PID`命令,其中PID是要跟踪的进程ID。

- `gdb`:GNU调试器,可以对正在运行的进程进行调试。

使用`gdb -p PID`命令,其中PID是要调试的进程ID。

3. 日志分析与查看Linux系统中的日志文件记录了系统和各个进程的运行状态和错误信息。

以下是一些常用的日志文件和查看命令:- `/var/log/messages`:包含系统运行过程中的大量信息。

使用`tail -f /var/log/messages`命令实时查看该文件的最新内容。

- `/var/log/syslog`:包含系统日志信息,包括内核和其他系统组件的消息。

使用`tail -f /var/log/syslog`命令实时查看该文件的最新内容。

Linux下segment fault的调试

Linux下segment fault的调试

Linux下的段错误产生的原因及调试方法简而言之,产生段错误就是访问了错误的内存段,一般是你没有权限,或者根本就不存在对应的物理内存,尤其常见的是访问0地址.一般来说,段错误就是指访问的内存超出了系统所给这个程序的内存空间,通常这个值是由gdtr来保存的,他是一个48位的寄存器,其中的32位是保存由它指向的gdt表,后13位保存相应于gdt的下标,最后3位包括了程序是否在内存中以及程序的在cpu中的运行级别,指向的gdt是由以64位为一个单位的表,在这张表中就保存着程序运行的代码段以及数据段的起始地址以及与此相应的段限和页面交换还有程序运行级别还有内存粒度等等的信息。

一旦一个程序发生了越界访问,cpu就会产生相应的异常保护,于是segmentation fault就出现了.在编程中以下几类做法容易导致段错误,基本是是错误地使用指针引起的1)访问系统数据区,尤其是往系统保护的内存地址写数据最常见就是给一个指针以0地址2)内存越界(数组越界,变量类型不一致等)访问到不属于你的内存区域解决方法我们在用C/C++语言写程序的时侯,内存管理的绝大部分工作都是需要我们来做的。

实际上,内存管理是一个比较繁琐的工作,无论你多高明,经验多丰富,难免会在此处犯些小错误,而通常这些错误又是那么的浅显而易于消除。

但是手工“除虫”(debug),往往是效率低下且让人厌烦的,本文将就"段错误"这个内存访问越界的错误谈谈如何快速定位这些"段错误"的语句。

下面将就以下的一个存在段错误的程序介绍几种调试方法:1dummy_function(void)2{3unsigned char*ptr=0x00;4*ptr=0x00;5}67int main(void)8{9dummy_function();1011return0;12}作为一个熟练的C/C++程序员,以上代码的bug应该是很清楚的,因为它尝试操作地址为0的内存区域,而这个内存区域通常是不可访问的禁区,当然就会出错了。

段错误(core dumped)

段错误(core dumped)

段错误 (core dumped)当我们的程序崩溃时,内核有可能把该程序当前内存映射到core文件里,方便程序员找到程序出现问题的地方。

最常出现的,几乎所有C程序员都出现过的错误就是“段错误”了。

也是最难查出问题原因的一个错误。

下面我们就针对“段错误”来分析core文件的产生、以及我们如何利用core文件找到出现崩溃的地方。

何谓core文件当一个程序崩溃时,在进程当前工作目录的core文件中复制了该进程的存储图像。

core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的。

使用core文件调试程序看下面的例子:#includechar *str = "test";void core_test(){str[1] ='T';}int main(){core_test();return0;}编译:gcc –g core_dump_test.c -o core_dump_test如果需要调试程序的话,使用gcc编译时加上-g选项,这样调试core文件的时候比较容易找到错误的地方。

执行:./core_dump_test段错误运行core_dump_test程序出现了“段错误”,但没有产生core文件。

这是因为系统默认core文件的大小为0,所以没有创建。

可以用ulimit命令查看和修改core文件的大小。

ulimit -c 0ulimit -c 1000ulimit -c 1000-c指定修改core文件的大小,1000指定了core文件大小。

也可以对core文件的大小不做限制,如:ulimit -c unlimitedulimit -c unlimited如果想让修改永久生效,则需要修改配置文件,如.bash_profile、/etc/profile或/etc/security/limits.conf。

再次执行:./core_dump_test段错误 (core dumped)ls core.*core.6133可以看到已经创建了一个core.6133的文件.6133是core_dump_test程序运行的进程ID。

Linux出错提示信息详解

Linux出错提示信息详解

Linux出错提示信息详解深入地讲:许多人害怕Linux,因为它给出的出错提示信息简直像天书一样。

并且这样的信息有很多。

如果你在Linux Format(译者注:这是一家英国销售量最好的Linux杂志)的论坛上搜索“Error”这个词,你将会得到超过150页的内容。

那些是许多人遇到的许多问题。

用户们遇到的最大的困难并非出错提示信息的数量,而是如何从中找出有用的东西。

例如,“Kernel Oops”是什么意思,或者“PCI can't allocate”是什么意思?Linux的出错提示相当愚钝且很难理解,几乎起不了什么帮助。

这是一个遗憾,因为大绝大多数问题本可以很容易的解决,而且有相当数量的涉及同样问题的出错提示一次又一次的出现。

用商业圈里的话来说,这是最能获得立竿见影效果的部分(译者:不知些句如此翻译妥当否?请指教)。

我们要解决的正是这一部分问题。

启动你的系统不需要你成为一个Linux专家,播放视频文件也无需成为程序员。

然而大部分出错信息都假定他们的用户已掌握了此类专门技术。

我们试图阐明这些常见错误,并提供解决方案,以帮助普通的Linux用户规避这些问题,并使他们的系统回到正常状态。

我们挑选了一些我们认为问题最多的领域,包括启动、常规软件应用、文件系统、网络和发行版的安装。

我们从每个领域中挑选了几个最常见的错误,并解释了相应的解决方案是如何起作用的。

这样做的目的是,即使这些问题不适合你的情况,你也能了解为什么Linux的出错提示信息看上去如此的难以捉摸并令人生畏。

希望你能从中学会找到解决自己问题所需知识的方法。

发行版的安装每一种Linux发行版都有不同的安装例程,并且都会制造一些问题。

Ubuntu也许在一台电脑上工作良好而另外一台则不行。

一台安装了Ubuntu并运行正常的电脑也许根本无法安装Fedora,OpenSUSE, Linux Mint, 或者Mandriva...ERROR Can't boot from CD/DVD(无法从光盘启动)如何你刚开始接触Linux,下述情况通常会是你对该操作系统的第一体验:你将你的新光盘放入光驱并重启电脑,结果发现却是进入了你先前使用的操作系统。

Linux学习-常见错误和快捷操作

Linux学习-常见错误和快捷操作

Linux下命令的一些异常情况命令不全:在命令没有输入完 (引号或括号没有配对),就不小心按下了Enter 键,终端会提示出一个>代表命令不完整,这是可以继续输入,也可以ctrl+c 终止输入,重新再来。

(下面sed命令使用时,还有另外一种命令不全的问题)ct@ehbio:~/ehbio_project$ rename 'ehbio2 >'ct@ehbio:~/ehbio_project$ rename 'ehbio2 >^Cct@ehbio:~/ehbio_project$文件名输入错误: 多一个字母、少一个字母、大小写问题ct@ehbio:~/ehbio_project$lsehbio2.fa ehbio3.fa ehbio4.fa ehbio.fa second.fa# 重命名没有生效ct@ehbio:~/ehbio_project$ rename 'ehbio2''ehbio5' ebio2.fact@ehbio:~/ehbio_project$ lsehbio2.fa ehbio3.fa ehbio4.fa ehbio.fa second.fa# 仔细看是ehbio2.fa写成了ebio2.fa,更正后即可。

Z8vb3e9jtel4m99ss6e7eZ:~/ehbio_project$ rename 'ehbio2''ehbio5' ehbio2.fact@ehbio:~/ehbio_project$ lsehbio3.fa ehbio4.fa ehbio5.fa ehbio.fa second.fa所在目录不对: 访问的文件不存在于当前目录,而又没有提供绝对路径, 或软连接失效ct@ehbio:~/ehbio_project$ lsehbio3.fa ehbio4.fa ehbio5.fa ehbio6.fa ehbio.fa second .fact@ehbio:~/ehbio_project$ ls ../dataehbio2.fa first.fa# 当前目录没有ehbio2.fact@ehbio:~/ehbio_project$ less ehbio2.faehbio2.fa: 没有那个文件或目录# ehbio2.fa在上一层目录的data目录下ct@ehbio:~/ehbio_project$ ls ../data/ehbio2.fa../data/ehbio2.fa# 加上路径依然访问不了ct@ehbio:~/ehbio_project$ less ../data/ehbio2.fa../data/ehbio2.fa: 没有那个文件或目录# 上面的问题是软连接失效,在之前的操作中删掉了原始的ehbio2.fa,所以快捷方式失效# 正确的访问ct@ehbio:~/ehbio_project$ tail -n 3 ../data/first.fa ACGGAGCGAGCTAGTGCAGCGAGGAGCTGAGTCGAGCCAGGACAGGAGCTAendLinux终端常用快捷操作•命令或文件名自动补全:在输入命令或文件名的前几个字母后,按Tab键,系统会自动补全或提示补全•上下箭头:使用上下箭头可以回溯之前的命令,增加命令的重用,减少输入工作量•!加之前输入过的命令的前几个字母,快速获取前面的命令ct@ehbio:~/ehbio_project$ cut -f 1-d ' 'ehbio.fa | tail -n 4 >mYCACGGAGCGAGCTAGTGCAGCGAGGAGCTGAGTCGAGCCAGGACAGGAGCTAendct@ehbio:~/ehbio_project$ man cut # 直接跳到上面运行的cut命令,再执行一次ct@ehbio:~/ehbio_project$ !cut cut -f 1-d ' 'ehbio.fa | tail -n 4 >mYCACGGAGCGAGCTAGTGCAGCGAGGAGCTGAGTCGAGCCAGGACAGGAGCTAend•ctrl+a回到命令的行首,用于修改常命令或注释掉命令# 写完下面的命令,突然不想运行了,又不想一个个删掉ct@ehbio:~/ehbio_project$ cut -f 1-d ' 'ehbio.fa | tail -n 4# 按ctrl+a, 回到行首,再输入`#`号,回车,命令即被注释掉。

linux编译安装时常见错误解决办法

linux编译安装时常见错误解决办法

linux编译安装时常见错误解决办法configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution复制代码代码如下:yum -y install libxslt-develconfigure: error: Could not find net-snmp-config binary. Please check your net-snmp installation.复制代码代码如下:yum -y install net-snmp-develconfigure: error: Please reinstall readline - I cannot find readline.h复制代码代码如下:yum -y install readline-develconfigure: error: Cannot find pspell复制代码代码如下:yum -y install aspell-develchecking for unixODBC support... configure: error: ODBC header file '/usr/include/sqlext.h' not found!复制代码代码如下:yum -y install unixODBC-develconfigure: error: Unable to detect ICU prefix or /usr/bin/icu-config failed. Please verify ICU install prefix and make sure icu-config works.复制代码代码如下:yum -y install libicu-develconfigure: error: utf8mime2text() has new signature, but U8TCANONICAL is missing. This should not happen. Check config.log for additional information.复制代码代码如下:yum -y install libc-client-develconfigure: error: freetype.h not found.复制代码代码如下:yum -y install freetype-develconfigure: error: xpm.h not found.复制代码代码如下:yum -y install libXpm-develconfigure: error: png.h not found.复制代码代码如下:yum -y install libpng-develconfigure: error: vpx_codec.h not found.复制代码代码如下:yum -y install libvpx-develconfigure: error: Cannot find enchant复制代码代码如下:yum -y install enchant-develconfigure: error: Please reinstall the libcurl distribution - easy.h should be in /include/curl/复制代码代码如下:yum -y install libcurl-develLAOGAO added 20140907:configure: error: mcrypt.h not found. Please reinstall libmcrypt.复制代码代码如下:wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gztar zxf libmcrypt-2.5.7.tar.gzcd libmcrypt-2.5.7./configuremake && make installadded 20141003:Cannot find imap复制代码代码如下:ln -s /usr/lib64/libc-client.so /usr/lib/libc-client.soconfigure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing.复制代码代码如下:yum -y install libc-client-develCannot find ldap.h复制代码代码如下:yum -y install openldapyum -y install openldap-develconfigure: error: Cannot find ldap libraries in /usr/lib复制代码代码如下:cp -frp /usr/lib64/libldap* /usr/lib/configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path复制代码代码如下:yum -y install postgresql-develconfigure: error: Please reinstall the lib curl distribution复制代码代码如下:yum -y install curl-develconfigure: error: Could not find net-snmp-config binary. Please check your net-snmp installation.复制代码代码如下:yum -y install net-snmp-develconfigure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution复制代码代码如下:yum -y install libxslt-develchecking for BZip2 support… yes checking for BZip2 in default path… not found configure: error: Please reinstall the BZip2 distribution 复制代码代码如下:yum -y install bzip2-develchecking for cURL support… yes checking if we should use cURL for url streams… no checking for cURL in default path… not found configure: error: Please reinstall the libcurl distribution – easy.h should be in/include/curl/Fix:复制代码代码如下:yum -y install curl-develchecking for curl_multi_strerror in -lcurl… yes checking for QDBM support… no checking for GDBM support… no checking for NDBM support… no configure: error: DBA: Could not find necessary header file(s).Fix:复制代码代码如下:yum -y install db4-develchecking for fabsf… yes checking for floorf… yes configure: error: jpeglib.h not found.Fix:复制代码代码如下:yum -y install libjpeg-develchecking for fabsf… yes checking for floorf… yes checking for jpeg_read_header in -ljpeg… yes configure: error: png.h not found. Fix:复制代码代码如下:yum -y install libpng-develchecking for png_write_image in -lpng… yes If configure fails try –with-xpm-dir=configure: error: freetype.h not found.Fix:复制代码代码如下:Reconfigure your PHP with the following option. --with-xpm-dir=/usrchecking for png_write_image in -lpng… yes configure: error: libXpm.(a|so) not found.Fix:复制代码代码如下:yum -y install libXpm-develchecking for bind_textdomain_codeset in -lc… yes checking for GNU MP support… yes configure: error: Unable to locate gmp.hFix:复制代码代码如下:yum -y install gmp-develchecking for utf8_mime2text signature… new checking for U8T_DECOMPOSE… configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. Check config.log for additional information.Fix:复制代码代码如下:yum -y install libc-client-develchecking for LDAP support… yes, shared checking for LDAP Cyrus SASL support… yes configure: error: Cannot find ldap.hFix:复制代码代码如下:yum -y install openldap-develchecking for mysql_set_character_set in -lmysqlclient… yes checking for mysql_stmt_next_result in -lmysqlclient… no checking for Oracle Database OCI8 support… no checking for unixODBC support… configure: error: ODBC header file ‘/usr/include/sqlext.h' not found!Fix:复制代码代码如下:yum -y install unixODBC-develchecking for PostgreSQL support for PDO… yes, shared checking for pg_config… not found configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation pathFix:复制代码代码如下:yum -y install postgresql-develchecking for sqlite 3 support for PDO… yes, shared checking for PDO includes… (cached) /usr/local/src/php-5.3.7/ext checking for sqlite3 files in default path… not found configure: error: Please reinstall the sqlite3 distributionFix:复制代码代码如下:yum -y install sqlite-develchecking for utsname.domainname… yes checking for PSPELL support… yes configure: error: Cannot find pspellFix:复制代码代码如下:yum -y install aspell-develchecking whether to enable UCD SNMP hack… yes checking for default_store.h… nochecking for kstat_read in -lkstat… no checking for snmp_parse_oid in -lsnmp… no checking for init_snmp in -lsnmp… no configure: error: SNMP sanity check failed. Please check config.log for more information.Fix:复制代码代码如下:yum -y install net-snmp-develchecking whether to enable XMLWriter support… yes, shared checking for xml2-config path… (cached) /usr/bin/xml2-config checking whether libxml build works… (cached) yes checking for XSL support… yes, shared configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distributionFix:复制代码代码如下:yum -y install libxslt-develconfigure: error: xml2-config not found. Please check your libxml2 installation.Fix:复制代码代码如下:yum -y install libxml2-develchecking for PCRE headers location… configure: error: Could not find pcre.h in /usrFix:复制代码代码如下:yum -y install pcre-develconfigure: error: Cannot find MySQL header files under yes. Note that the MySQL client library is not bundled anymore!Fix:复制代码代码如下:yum -y install mysql-develchecking for unixODBC support… configure: error: ODBC header file ‘/usr/include/sqlext.h' not found!Fix:复制代码代码如下:yum -y install unixODBC-develchecking for pg_config… not found configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path Fix:复制代码代码如下:yum -y install postgresql-develconfigure: error: Cannot find pspellFix:复制代码代码如下:yum -y install pspell-develconfigure: error: Could not find net-snmp-config binary. Please check your net-snmp installation.Fix:复制代码代码如下:yum -y install net-snmp-develconfigure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distributionFix:复制代码代码如下:yum -y install libxslt-devel。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1 执行socket文件时,出现段错误 (core dumped产生段错误就是访问了错误的内存段,一般是你没有权限,或者根本就不存在对应的物理内存,尤其常见的是访问0地址.解决方法:利用gdb逐步查找段错误:首先我们需要一个带有调试信息的可执行程序,所以我们加上“-g -rdynamic"的参数进行编译,然后用gdb调试运行这个新编译的程序,具体步骤如下:1、 gcc -g -rdynamic d.c2、 gdb ./a.out3、 r这样就找到了出错位置。

然后在相应位置修改。

2 linux 段错误如何调试linux下的c程序常常会因为内存访问错误等原因造成segment fault(段错误)此时如果系统core dump功能是打开的,那么将会有内存映像转储到硬盘上来,之后可以用gdb对core文件进行分析,还原系统发生段错误时刻的堆栈情况。

这对于我们发现程序bug很有帮助。

使用ulimit -a可以查看系统core文件的大小限制;使用ulimit -c [kbytes]可以设置系统允许生成的core文件大小。

ulimit -c 0 不产生core文件ulimit -c 100 设置core文件最大为100kulimit -c unlimited 不限制core文件大小步骤:1、当发生段错误时,我们查看ulimit -a (core file size (blocks, -c 0)并没有文件,2、设置:ulimit -c unlimited 不限制core文件大小3.、运行程序,发生段错误时会自动记录在core中4、test]$ ls -al core.* 在那个文件下(-rw------- 1 leconte leconte 139264 01-06 22:3 1 core.2065)5、使用gdb 运行程序和段错误记录的文件。

(est]$ gdb ./test core.2065)6、会提哪行有错。

很多系统默认的core文件大小都是0,我们可以通过在Shell的启动脚本/etc/bashrc或者~/.bashrc等地方来加入ulimit -c 命令来指定core文件大小,从而确保core文件能够生成。

除此之外,还可以在/proc/sys/kernel/core_pattern里设置core文件的文件名模板,详情请看core的官方man手册。

3 段错误bug的调试我们在用C/C++语言写程序的时侯,内存管理的绝大部分工作都是需要我们来做的。

实际上,内存管理是一个比较繁琐的工作,无论你多高明,经验多丰富,难免会在此处犯些小错误,而通常这些错误又是那么的浅显而易于消除。

但是手工“除虫”(debug),往往是效率低下且让人厌烦的,本文将就"段错误"这个内存访问越界的错误谈谈如何快速定位这些"段错误"的语句。

下面将就以下的一个存在段错误的程序介绍几种调试方法:1 dummy_function (void2 {3 unsigned char *ptr = 0x00;4 *ptr = 0x00;5 }67 int main (void8 {9 dummy_function (;1011 return 0;12 }作为一个熟练的C/C++程序员,以上代码的bug应该是很清楚的,因为它尝试操作地址为0的内存区域,而这个内存区域通常是不可访问的禁区,当然就会出错了。

我们尝试编译运行它:xiaosuo@gentux test $ ./a.out段错误果然不出所料,它出错并退出了。

1.利用gdb逐步查找段错误:这种方法也是被大众所熟知并广泛采用的方法,首先我们需要一个带有调试信息的可执行程序,所以我们加上“-g -rdynamic"的参数进行编译,然后用gdb调试运行这个新编译的程序,具体步骤如下:xiaosuo@gentux test $ gcc -g -rdynamic d.cxiaosuo@gentux test $ gdb ./a.outGNU gdb 6.5Copyright (C 2006 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions.Type "show copying" to see the conditions.There is absolutely no warranty for GDB. Type "show warranty" for details.This GDB was configured as "i686-pc-linux-gnu"ing host libthread_db library"/lib/libthread_db.so.1".(gdb rStarting program: /home/xiaosuo/test/a.outProgram received signal SIGSEGV, Segmentation fault.0x08048524 in dummy_function ( at d.c:44 *ptr = 0x00;(gdb哦?!好像不用一步步调试我们就找到了出错位置d.c文件的第4行,其实就是如此的简单。

从这里我们还发现进程是由于收到了SIGSEGV信号而结束的。

通过进一步的查阅文档(man 7 signal,我们知道SIGSEGV默认handler的动作是打印”段错误"的出错信息,并产生Core文件,由此我们又产生了方法二。

2.分析Core文件:Core文件是什么呢?The default action of certain signals is to cause a process to terminate and produce a core dump file, a disk file containing an image of the process's memory at the time of termination. A list of the signals which cause a process to dump core can be found in signal(7.以上资料摘自man page(man 5 core。

不过奇怪了,我的系统上并没有找到core文件。

后来,忆起为了渐少系统上的拉圾文件的数量(本人有些洁癖,这也是我喜欢Gentoo的原因之一),禁止了core文件的生成,查看了以下果真如此,将系统的core文件的大小限制在512K大小,再试:xiaosuo@gentux test $ ulimit -cxiaosuo@gentux test $ ulimit -c 1000xiaosuo@gentux test $ ulimit -c1000xiaosuo@gentux test $ ./a.out段错误 (core dumpedxiaosuo@gentux test $ lsa.out core d.c f.c g.c pango.c test_iconv.c test_regex.ccore文件终于产生了,用gdb调试一下看看吧:xiaosuo@gentux test $ gdb ./a.out coreGNU gdb 6.5Copyright (C 2006 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type "show copying" to see the conditions.There is absolutely no warranty for GDB. Type "show warranty" for details.This GDB was configured as "i686-pc-linux-gnu"ing host libthread_db library "/lib/libthread_db.so.1".warning: Can't read pathname for load map: 输入/输出错误.Reading symbols from /lib/libc.so.6...done.Loaded symbols for /lib/libc.so.6Reading symbols from /lib/ld-linux.so.2...done.Loaded symbols for /lib/ld-linux.so.2Core was generated by `./a.out'.Program terminated with signal 11, Segmentation fault.#0 0x08048524 in dummy_function ( at d.c:44 *ptr = 0x00;接着考虑下去,以前用windows系统下的ie的时侯,有时打开某些网页,会出现“运行时错误”,这个时侯如果恰好你的机器上又装有windows的编译器的话,他会弹出来一个对话框,问你是否进行调试,如果你选择是,编译器将被打开,并进入调试状态,开始调试。

Linux下如何做到这些呢?我的大脑飞速地旋转着,有了,让它在SIGSEGV的handler中调用gdb,于是第三个方法又诞生了:3.段错误时启动调试:#include#include#include#includevoid dump(int signo{char buf[1024];char cmd[1024];snprintf(buf, sizeof(buf, "/proc/%d/cmdline", getpid(; if(!(fh = fopen(buf, "r"exit(0;if(!fgets(buf, sizeof(buf, fhexit(0;fclose(fh;if(buf[strlen(buf - 1] == '\n'buf[strlen(buf - 1] = '\0';snprintf(cmd, sizeof(cmd, "gdb %s %d", buf, getpid(; system(cmd;exit(0;}voiddummy_function (void{unsigned char *ptr = 0x00;*ptr = 0x00;}int{signal(SIGSEGV, &dump;dummy_function (;return 0;}编译运行效果如下:xiaosuo@gentux test $ gcc -g -rdynamic f.cxiaosuo@gentux test $ ./a.outGNU gdb 6.5Copyright (C 2006 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type "show copying" to see the conditions.There is absolutely no warranty for GDB. Type "show warranty" for details.This GDB was configured as "i686-pc-linux-gnu"ing host libthread_db library "/lib/libthread_db.so.1".Attaching to program: /home/xiaosuo/test/a.out, process 9563Reading symbols from /lib/libc.so.6...done.Loaded symbols for /lib/libc.so.6Reading symbols from /lib/ld-linux.so.2...done.Loaded symbols for /lib/ld-linux.so.20xffffe410 in __kernel_vsyscall ((gdb bt#0 0xffffe410 in __kernel_vsyscall (#1 0xb7ee4b53 in waitpid ( from /lib/libc.so.6#2 0xb7e925c9 in strtold_l ( from /lib/libc.so.6#3 0x08048830 in dump (signo=11 at f.c:22#4#5 0x0804884c in dummy_function ( at f.c:31#6 0x08048886 in main ( at f.c:38以上方法都是在系统上有gdb的前提下进行的,如果没有呢?其实glibc为我们提供了此类能够dump栈内容的函数簇,详见/usr/include/execinfo.h(这些函数都没有提供man page,难怪我们找不到),另外你也可以通过gnu的手册进行学习。

相关文档
最新文档