[英]How do I interpret GetExceptionCode results when using SEH?
我一直在嘗試編寫一些錯誤保護子句,以識別由第三方提供給我們的dll中的問題。 該dll中可能存在問題(內存異常,浮點錯誤等),並且能夠在不訪問源代碼的情況下識別這些錯誤是有利的。
我從各種SEH錯誤處理例程中收集了一些信息,但是盡管可以正常工作,但還是有一些...不一致之處。 我正試圖隔離每個人,並且我將分別對每個人提出一個問題。
這與在SEH __try / __ except子句中用於識別錯誤的GetExceptionCode有關。 它似乎不能可靠地做到這一點。
這是一個很明顯的被零除的情況:
#include <float.h> // defines of _EM_OVERFLOW, etc.
#include <string.h> // strncpy_s & strncat_s
#include <stdlib.h> // malloc
#include <excpt.h> // EXCEPTION_EXECUTE_HANDLER
#include <iostream> // cout
#include <bitset> // bitset
#include <conio.h> // _kbhit
#pragma fenv_access (on)
const unsigned int SERIOUS_FP_EXCEPTIONS = _EM_DENORMAL | _EM_ZERODIVIDE | _EM_INVALID;
const unsigned int MINOR_FP_EXCEPTIONS = _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT;
int main(int argc, char[])
{
double numerator = 1.0;
double denominator = 0.0;
double result = 0.0;
unsigned int _previous_floating_point_control;
_controlfp_s(&_previous_floating_point_control, 0, 0);
_controlfp_s(nullptr, MINOR_FP_EXCEPTIONS, _MCW_EM);
__try {
result = numerator / denominator;
_controlfp_s(NULL, _previous_floating_point_control, _MCW_EM);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
std::cout << "_EM_INEXACT = " << std::bitset<32>(_EM_INEXACT) << std::endl;
std::cout << "_EM_UNDERFLOW = " << std::bitset<32>(_EM_UNDERFLOW) << std::endl;
std::cout << "_EM_OVERFLOW = " << std::bitset<32>(_EM_OVERFLOW) << std::endl;
std::cout << "_EM_ZERODIVIDE = " << std::bitset<32>(_EM_ZERODIVIDE) << std::endl;
std::cout << "_EM_INVALID = " << std::bitset<32>(_EM_INVALID) << std::endl;
std::cout << "_EM_DENORMAL = " << std::bitset<32>(_EM_DENORMAL) << std::endl;
std::cout << "_EM_AMBIGUOUS = " << std::bitset<32>(_EM_AMBIGUOUS) << std::endl;
std::cout << std::endl;
std::cout << " divide-by-zero" << std::endl;
std::cout << " |" << std::endl;
std::cout << " ambiguous code? underflow" << std::endl;
std::cout << " | : |" << std::endl;
std::cout << " v v v" << std::endl;
std::cout << "Exception code = " << std::bitset<32>(GetExceptionCode()) << std::endl;
std::cout << " ^ ^ ^ ^" << std::endl;
std::cout << " | : : |" << std::endl;
std::cout << " denormal number inexact number" << std::endl;
std::cout << " : |" << std::endl;
std::cout << " overflow" << std::endl;
std::cout << " |" << std::endl;
std::cout << " invalid number" << std::endl;
if (GetExceptionCode() & _EM_ZERODIVIDE)
std::cout << "ERROR! Divide By Zero!" << std::endl;
else
std::cout << "No divide by zero found here!" << std::endl;
_controlfp_s(NULL, _previous_floating_point_control, _MCW_EM);
}
std::cout << "result = " << result << std::endl;
while (!_kbhit()) // Wait until a key is pressed to close console.
{ }
}
這將打印以下內容:
_EM_INEXACT = 00000000000000000000000000000001
_EM_UNDERFLOW = 00000000000000000000000000000010
_EM_OVERFLOW = 00000000000000000000000000000100
_EM_ZERODIVIDE = 00000000000000000000000000001000
_EM_INVALID = 00000000000000000000000000010000
_EM_DENORMAL = 00000000000010000000000000000000
_EM_AMBIGUOUS = 10000000000000000000000000000000
divide-by-zero
|
ambiguous code? underflow
| : |
v v v
Exception code = 11000000000000000000001010110101
^ ^ ^ ^
| : : |
denormal number inexact number
: |
overflow
|
invalid number
No divide by zero found here!
result = 0
它已識別出一個問題(嚴重),但尚未完全正確診斷。
更糟糕的是,當子句被缺少依賴項的dll調用所替代時,我得到:
f.p. exceptions
denormal number |
| _|_
v / \
11000000011011010000000001111110
^^ ^ ^ ^^
|| | | ||
\________________/
unknown codes
如果發生SIGSEV錯誤(分段錯誤),則返回相似的結果。 這意味着我們將其他問題誤診斷為浮點異常。
所以我的問題是:
PS:請不要發表評論或回答我應該檢查分母是否為0-我知道,我在我可以控制的所有代碼中都這樣做。
您將需要一些類似的東西
DWORD exception_filter(DWORD dwExceptionCode)
{
// use dwExceptionCode to handle only the types of exceptions you want
// if you want to use it inside your handler, you'll need to save it.
return EXCEPTION_EXECUTE_HANDLER; // or other value depending on ExceptionCode
}
Your exception handler...
__try
{
something();
}
__except (exception_filter(GetExceptionCode())
{
// DO NOT CALL GetExceptionCode() or GetExceptionInfo() here. If you need
// Exception Info as well, pass it to the filter, and save the values you need.
switch (dwSavedExceptionCode)
{
case EXCEPTION_FLT_OVERFLOW:
ItWasAFloatingPointOverflow();
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
ItWasAFloatingDivideByZero();
break;
case ***Other Exception Types You Want handled (==the ones you returned execute_handler for in the filter) ***
break;
}
}
異常代碼= 11000000000000000000001010110101
該值為0xC00002B5
,又名STATUS_FLOAT_MULTIPLE_TRAPS
。
請參閱為什么在啟用浮點異常后出現多個陷阱錯誤 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.