Article View: pl.comp.lang.c
Article #307944Re: Jak są zaimplementowane wyjątki ?
From: Wiktor S.
Date: Fri, 11 Dec 2015 19:51
Date: Fri, 11 Dec 2015 19:51
125 lines
3669 bytes
3669 bytes
> Ale uwaga - gdy w .c nie było throw, wtedy nie ma obsługi wyjątku mimo
> dzielenia. Czyżby gcc nie potrafił obsługiwać tego typu wyjątków? Co
> się stanie, gdy jednak będzie błąd dzielenia?
Nie wiem jak GCC.
Wiem za to, że Visual C++ ma obsługę wyjątków SEH (Structured Exception
Handling), która pozwala na łapanie wyjątków systemowych i sprzętowych.
-------------------
/* to jest C */
#include <stdio.h>
#include <windows.h>
int dziel(int a, int b)
{
__try
{
return a/b;
}
__except(GetExceptionCode() == EXCEPTION_INT_DIVIDE_BY_ZERO)
{
return 0;
}
}
int main()
{
printf("%d\n", dziel(5,0));
}
-------------------
__sehtable$_dziel DD 0fffffffeH
DD 00H
DD 0ffffffd8H
DD 00H
DD 0fffffffeH
DD FLAT:$LN5@dziel
DD FLAT:$LN6@dziel
...
__$SEHRec$ = -24 ; size = 24
_a$ = 8 ; size = 4
_b$ = 12 ; size = 4
_dziel PROC
; Line 5
push ebp
mov ebp, esp
push -2 ; fffffffeH
push OFFSET __sehtable$_dziel
push OFFSET __except_handler4
mov eax, DWORD PTR fs:0
push eax
sub esp, 8
push ebx
push esi
push edi
mov eax, DWORD PTR ___security_cookie
xor DWORD PTR __$SEHRec$[ebp+16], eax
xor eax, ebp
push eax
lea eax, DWORD PTR __$SEHRec$[ebp+8]
mov DWORD PTR fs:0, eax
mov DWORD PTR __$SEHRec$[ebp], esp
; Line 6
mov DWORD PTR __$SEHRec$[ebp+20], 0
; Line 8
mov eax, DWORD PTR _a$[ebp]
cdq
idiv DWORD PTR _b$[ebp]
mov DWORD PTR __$SEHRec$[ebp+20], -2 ; fffffffeH
; Line 14
mov ecx, DWORD PTR __$SEHRec$[ebp+8]
mov DWORD PTR fs:0, ecx
pop ecx
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret 0
$LN5@dziel:
; Line 10
mov eax, DWORD PTR __$SEHRec$[ebp+4]
mov ecx, DWORD PTR [eax]
mov eax, DWORD PTR [ecx]
xor edx, edx
cmp eax, -1073741676 ; c0000094H
sete dl
mov eax, edx
$LN7@dziel:
ret 0
$LN6@dziel:
mov esp, DWORD PTR __$SEHRec$[ebp]
; Line 12
mov DWORD PTR __$SEHRec$[ebp+20], -2 ; fffffffeH
xor eax, eax
; Line 14
mov ecx, DWORD PTR __$SEHRec$[ebp+8]
mov DWORD PTR fs:0, ecx
pop ecx
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret 0
_dziel ENDP
-------------------
Uch... dużo tego. Nie analizowałem dokładnie. Wygląda na to że pod
"magicznym" adresem fs:0 umieszczany jest wskaźnik na strukturę zawierającą
m.in. adresy callbacków - funkcji sprawdzającej czy wyjątek ma być obsłużony
i samego handlera.
Callbacki wywoływane są przez system operacyjny.
--
Wiktor S.
Technical Details
Message-ID:
<n4f9g6$2ro$1@usenet.news.interia.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.interia.pl!not-for-mailReferences:
<n4durn$q3e$1@node1.news.atman.pl>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.interia.pl!not-for-mail From: "Wiktor S." <wswiktorSP@Mpoczta.fm> Newsgroups: pl.comp.lang.c Subject: Re: Jak są zaimplementowane wyjątki ? Date: Fri, 11 Dec 2015 20:51:33 +0100 Organization: INTERIA.PL S.A. Lines: 126 Message-ID: <n4f9g6$2ro$1@usenet.news.interia.pl> References: <n4durn$q3e$1@node1.news.atman.pl> NNTP-Posting-Host: user-109-243-176-92.play-internet.pl Mime-Version: 1.0 X-Trace: usenet.news.interia.pl 1449863495 2936 109.243.176.92 (11 Dec 2015 19:51:35 GMT) X-Complaints-To: usenet@firma.interia.pl NNTP-Posting-Date: Fri, 11 Dec 2015 19:51:35 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit