flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Win api exception crashes.

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 2079
Roman 08 Aug 2026, 06:04
Just put this code to winCrashReport.txt and include in your program in Start: include "winCrashReport.txt"
Code:
       invoke  SetUnhandledExceptionFilter,ExceptionFilter
        jmp done@@Exeption@FilterProc

zbuf dd 16 dup(0) 

EXCEPTION_MAXIMUM_PARAMETERS = 15
SIZE_OF_80387_REGISTERS      = 80
MAXIMUM_SUPPORTED_EXTENSION  = 512
EXCEPTION_CONTINUE_EXECUTION = -1

struct FLOATING_SAVE_AREA
  ControlWord          dd ?
  StatusWord           dd ?
  TagWord              dd ?
  ErrorOffset          dd ?
  ErrorSelector        dd ?
  DataOffset           dd ?
  DataSelector         dd ?
  RegisterArea         rb SIZE_OF_80387_REGISTERS
  Cr0NpxState          dd ?
ends

struct CONTEXT
  ContextFlags         dd ?
  iDr0                 dd ?
  iDr1                 dd ?
  iDr2                 dd ?
  iDr3                 dd ?
  iDr6                 dd ?
  iDr7                 dd ?
  FloatSave            FLOATING_SAVE_AREA
  regGs                dd ?
  regFs                dd ?
  regEs                dd ?
  regDs                dd ?
  regEdi               dd ?
  regEsi               dd ?
  regEbx               dd ?
  regEdx               dd ?
  regEcx               dd ?
  regEax               dd ?
  regEbp               dd ?
  regEip               dd ?
  regCs                dd ?
  regFlag              dd ?
  regEsp               dd ?
  regSs                dd ?
  ExtendedRegisters    rb MAXIMUM_SUPPORTED_EXTENSION
ends

struct EXCEPTION_RECORD
  ExceptionCode        dd ?
  ExceptionFlags       dd ?
  pExceptionRecord     dd ?
  ExceptionAddress     dd ?
  NumberParameters     dd ?
  ExceptionInformation rd EXCEPTION_MAXIMUM_PARAMETERS
ends
struct EXCEPTION_POINTERS
  pExceptionRecord     dd ?
  pContextRecord       dd ?
ends
proc  ExceptionFilter lpExcept:DWORD
        locals
            szFile   rb MAX_PATH
            szBuffer rb 1000h
        endl

        push    edi
        mov     eax,[lpExcept]
        ; EDI -> EXCEPTION_RECORD
        mov     edi,[eax+EXCEPTION_POINTERS.pExceptionRecord]
        ; DBG_PRINTEXCEPTION_WIDE_C
        cmp     [edi+EXCEPTION_RECORD.ExceptionCode],4001000Ah
        je      .loc_skip_exept
        ; DBG_PRINTEXCEPTION_C
        cmp     [edi+EXCEPTION_RECORD.ExceptionCode],40010006h
        je      .loc_skip_exept
        jmp     @f

.loc_skip_exept:
        pop     edi
        mov     eax,-1
        ret
@@:
        pop     edi

        ; EDI -> CONTEXT
        mov     edi,[eax+EXCEPTION_POINTERS.pContextRecord]
        push    [edi+CONTEXT.regEdi]
        push    [edi+CONTEXT.regEsi]
        push    [edi+CONTEXT.regEbp]
        push    [edi+CONTEXT.regEsp]
        push    [edi+CONTEXT.regEdx]
        push    [edi+CONTEXT.regEcx]
        push    [edi+CONTEXT.regEbx]
        push    [edi+CONTEXT.regEax]

        ; EDI -> EXCEPTION_RECORD
        mov     edi,[eax+EXCEPTION_POINTERS.pExceptionRecord]

        ; ReadWrite
        mov     eax,[edi+EXCEPTION_RECORD.ExceptionInformation]
        cmp     eax,2
        jb      @f
        mov     eax,2
@@:
        push    [.szOperation+eax*4]

        ; NumberParameters
        push    [edi+EXCEPTION_RECORD.NumberParameters]

        ; Continuable
        mov     eax,[edi+EXCEPTION_RECORD.ExceptionFlags]
        push    [.szLogical+eax*4]

        ; Type
        mov     esi,.szType
@@:
        lodsd
        or      eax,eax
        jz      @f
        cmp     eax,[edi+EXCEPTION_RECORD.ExceptionCode]
        je      @f
        lodsd
        jmp     @b
@@:
        push    dword [esi]

        ; ExceptionCode
        push    [edi+EXCEPTION_RECORD.ExceptionCode]

        ; ExceptionAddress
        push    [edi+EXCEPTION_RECORD.ExceptionAddress]

        
        lea     ebx,[szBuffer]
        invoke  wsprintf,ebx,.szMask
        add     esp,40h
        add ebx,eax
        ;get asm code
        mov ecx,[edi+EXCEPTION_RECORD.ExceptionAddress]
        
        xor eax,eax
     rept 16 n:0 { ;get asm code from crash address
                  mov al,[ecx+n]
                  mov dword [zbuf+n*4],eax
                  }

     cinvoke  wsprintf,ebx,.AsmTxt,[zbuf],[zbuf+4],[zbuf+8],[zbuf+12],\
                     [zbuf+16],[zbuf+20],[zbuf+24],[zbuf+28],[zbuf+32]
     
;get dlls adresa
        add ebx,eax
        push esi ebp edi
        mov esi,.Names@@AllMyDLLs
.up:    movups  xmm0,xword [.dllClearTxt]
        movups xword [.dllAdrrr],xmm0
        invoke  GetModuleHandle,dword [esi]
        mov ecx,[esi]
        mov edi,.dllAdrrr
.n0:    mov dl,[ecx]
        test dl,dl
        jz   .nn
        mov  [edi],dl
        inc  edi
        inc  ecx
        jmp  .n0
.nn:
        cinvoke  wsprintf,ebx, .dllAdrrr,eax
        add ebx,eax
        add esi,4
        cmp esi,.Names@@AllMyDLLs.end
        jb  .up
        pop edi ebp esi


        lea     ebx,[szFile]
        invoke  GetModuleHandle,NULL
        invoke  GetModuleFileName,eax,ebx,MAX_PATH
        invoke  lstrcat,ebx,.szTail

        
        invoke  CreateFile,ebx,GENERIC_WRITE,FILE_SHARE_READ,\
                NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        cmp     eax,-1
        je      @f
        mov     ebx,eax

        
        invoke  SetFilePointer,ebx,0,0,FILE_END
        or      eax,eax
        jz      .loc_write_data

        invoke  lstrlen,.divider
        invoke  _lwrite,ebx,.divider,eax

.loc_write_data:
        
        lea     esi,[szBuffer]
        invoke  lstrlen,esi
        invoke  _lwrite,ebx,esi,eax
        invoke  CloseHandle,ebx
@@:
        
        invoke  MessageBox,0,esi,.szTitle,\
                MB_OK+MB_ICONHAND+MB_APPLMODAL+MB_TOPMOST

        invoke  ExitProcess,0

.Names@@AllMyDLLs dd .pDLLBox3D,.pDLLDirctx11,.pDLLMFPlay,.kernel32,.user32,.d3dx11_43,.msvcrt,.winmm
.Names@@AllMyDLLs.end:
.pDLLBox3D      db 'box3D.DLL',0
.pDLLDirctx11   db 'D3D11.DLL',0
.pDLLMFPlay     db 'MFPlay.DLL',0
.kernel32       db 'KERNEL32.DLL',0
.user32         db 'USER32.DLL',0
.d3dx11_43      db 'D3DX11_43.DLL',0
.msvcrt         db 'MSVCRT.DLL',0
.winmm          db 'WINMM.DLL',0
.dllAdrrr       db '                = %X',13,10,0
.dllClearTxt    db 16 dup(32)


.szTail      db '_errors.log',0

.divider     db '------------------------------------------------------------',13,10,13,10,0

.szLogical   dd .szFalse,.szTrue
.szFalse     db 'false',0
.szTrue      db 'true',0

.szOperation dd .szRead,.szWrite,.szOther
.szRead      db 'read',0
.szWrite     db 'write',0
.szOther     db 'other',0

.szType      dd 0xC0000005,.stat1
             dd 0xC000008C,.stat2
             dd 0x80000003,.stat3
             dd 0x80000002,.stat4
             dd 0xC000008D,.stat5
             dd 0xC000008E,.stat6
             dd 0xC000008F,.stat7
             dd 0xC0000090,.stat8
             dd 0xC0000091,.stat9
             dd 0xC0000092,.stat10
             dd 0xC0000093,.stat11
             dd 0x80000001,.stat12
             dd 0xC000001D,.stat13
             dd 0xC0000006,.stat14
             dd 0xC0000094,.stat15
             dd 0xC0000095,.stat16
             dd 0xC0000026,.stat17
             dd 0xC0000008,.stat18
             dd 0xC0000025,.stat19
             dd 0xC0000096,.stat20
             dd 0x80000004,.stat21
             dd 0xC00000FD,.stat22
             dd 0x80000029,.stat23
             dd 0xC000009A,.stat24
             dd 0xC000009B,.stat25
             dd 0,.sUnk

.stat1       db 'ACCESS VIOLATION (NULL)',0
.stat2       db 'ARRAY BOUNDS EXCEEDED',0
.stat3       db 'BREAKPOINT',0
.stat4       db 'DATATYPE MISALIGNMENT',0
.stat5       db 'FLOAT DENORMAL OPERAND',0
.stat6       db 'FLOAT DIVIDE BY ZERO',0
.stat7       db 'FLOAT INEXACT RESULT',0
.stat8       db 'FLOAT INVALID OPERATION',0
.stat9       db 'FLOAT OVERFLOW',0
.stat10      db 'FLOAT STACK CHECK',0
.stat11      db 'FLOAT UNDERFLOW',0
.stat12      db 'GUARD PAGE VIOLATION',0
.stat13      db 'ILLEGAL INSTRUCTION',0
.stat14      db 'IN PAGE ERROR',0
.stat15      db 'INTEGER DIVIDE BY ZERO',0
.stat16      db 'INTEGER OVERFLOW',0
.stat17      db 'INVALID DISPOSITION',0
.stat18      db 'INVALID HANDLE',0
.stat19      db 'NONCONTINUABLE EXCEPTION',0
.stat20      db 'PRIVILEGED_INSTRUCTION',0
.stat21      db 'SINGLE STEP',0
.stat22      db 'STACK OVERFLOW',0
.stat23      db 'UNWIND CONSOLIDATE',0
.stat24      db 'FLOAT_MULTIPLE_FAULTS',0
.stat25      db 'FLOAT_MULTIPLE_TRAPS',0
.sUnk        db 'UNKNOWN',0

.szTitle     db 'Critical error',0
.szMask      db 'Exception addr: %08Xh',13,10,'Exception code: %08Xh = %s',13,10,13,10
             db 'Information:',13,10
             db 'Continuable = %s, NumberParameters = %u, ReadWrite = %s',13,10,13,10
             db 'Registers:',13,10
             db 'EAX=%08Xh, EBX=%08Xh, ECX=%08Xh, EDX=%08Xh',13,10
             db 'ESP=%08Xh, EBP=%08Xh, ESI=%08Xh, EDI=%08Xh',13,10,13,10
             db 0
.AsmTxt      db 'asm op code: %X , %X , %X , %X , %X , %X , %X , %X , %X',13,10,13,10,0
endp             

done@@Exeption@FilterProc:           
    


Last edited by Roman on 08 Aug 2026, 09:20; edited 5 times in total
Post 08 Aug 2026, 06:04
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 2079
Roman 08 Aug 2026, 06:08
There is not always available debager. This code help fast get info about crash in your program.
Tested in Windows 10 and Windows 11.

Saved to file crash error. And not erased in file previous errors !
Saves errors history in file !


Asm commands op codes
http://ref.x86asm.net/coder32.html
Post 08 Aug 2026, 06:08
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 2079
Roman 08 Aug 2026, 09:10
Post 08 Aug 2026, 09:10
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.