引起程式崩潰的原因有很多,主要有以下幾種原因(參考eventhelix.com)
1.Invalid Array Indexing 無效的數組索引
Data1 a; // Corrupted when b is indexed with 0xFFFFFFFF (-1) 由於錯誤的數組索引,如b[-1]可能破壞a
int b[100]; // Declaration of b. Keep in mind that array indexing is a signed operation
Data2 c; // Corrupted when index into b is greater than 99 由於錯誤的數組索引,如b[100]可能破壞c
2.Un-initialized Pointer Operations 未初始化的指標操作
3.Unauthorized Buffer Operations 未經許可的緩衝操作
4.Illegal Stack Operations 非法的堆棧操作
5.Invalid Processor Operations 無效的處理器操作
--Divide by zero attempted by application
--Program running in user mode attempted to execute an instruction that can only be executed in supervisor (kernel) mode.
--Program attempted access to an illegal address. The address might be out of range or the program might not have the privilege to perform the access. For example, a program attempting to write to read only segment will result in an exception.
--Misaligned access to memory also results in an exception. Most modern processors restrict long word reads to addresses divisible by 4. An exception will be raised if a long word operation is attempted at an address that is not divisible by 4. (See the byte alignment and ordering article for details)
6.Infinite Loop 無限迴圈
常用的檢測和排錯方法
1.斷言Assert
2.輸出中間結果print/Log
3.使用調試器GDB/DDD
4.使用偵查工具memwatch,Valgrind,Parasoft C++ Test/Insure C++
FAQ:
1.如何使程式在出錯時產生core dump檔案
vi /etc/profile
# No core files by default
# ulimit -S -c 0 > /dev/null 2>&1
ulimit -S -c unlimited > /dev/null 2>&1
2.如果從core檔案中得出所需的資訊
gdb program core.xxx
bt
info locals
frame x
Links:
http://www.eventhelix.com/RealtimeMantra/Basics/debugging_software_crashes.htm
http://www.eventhelix.com/RealtimeMantra/Basics/debugging_software_crashes_2.htm