summary refs log tree commit diff stats
Commit message (Expand)AuthorAgeFilesLines
* c2nim: new features & bugfixesAndreas Rumpf2010-08-1112-166/+1257
* added missing filesAndreas Rumpf2010-08-084-0/+190
* inlining of the write barrier for dllsAndreas Rumpf2010-08-081-17/+17
* inlining of the write barrier for dllsAndreas Rumpf2010-08-0825-854/+860
* DLL generation of the stdlib for unixAndreas Rumpf2010-08-049-111/+101
* before stack init changeAndreas Rumpf2010-08-0113-58/+94
* handling of compiler procs improved for DLL generationAndreas Rumpf2010-07-2933-671/+701
* implemented user-defined pragmasAndreas Rumpf2010-07-2311-151/+237
* standalone structs; function pointersAndreas Rumpf2010-07-235-86/+270
* standalone structs still not implementedAndreas Rumpf2010-07-231-0/+2
* c2nim: cast-bug fixes (sort of)Andreas Rumpf2010-07-232-112/+149
* c2nim: rewritten symbol exporting codeAndreas Rumpf2010-07-224-90/+44
* c2nim: code cleanupAndreas Rumpf2010-07-221-45/+0
* c2nim: better parsing of #ifdef C2NIM; #def supportAndreas Rumpf2010-07-228-98/+404
* bugfix: c2nim: typedef unsigned charAndreas Rumpf2010-07-224-7/+5
* bugfix: c2nim: typedef a b;Andreas Rumpf2010-07-214-4/+10
* added system.appType magicAndreas Rumpf2010-07-2111-14/+49
* test file for c2nimAndreas Rumpf2010-07-213-8/+456
* cleanup of todo.txtAndreas Rumpf2010-07-211-37/+15
* c2nim tool addedAndreas Rumpf2010-07-2128-66/+3498
* bugfix: exception handling (still not correct)Andreas Rumpf2010-06-0418-39/+145
* revert to old behavior of getStartMilsecs; getStartMilsecs deprecatedAndreas Rumpf2010-05-302-8/+8
* unicode.nim compiles againAndreas Rumpf2010-05-291-2/+2
* system.each makes more sense with var?Andreas Rumpf2010-05-291-1/+1
* deleted tests/tenuminh.nimAndreas Rumpf2010-05-291-10/+0
* psapi cleanupAndreas Rumpf2010-05-291-40/+45
* resolved system.nim conflictsAndreas Rumpf2010-05-2937-28481/+28956
|\
| * Integrating my changes, mostly minor/cosmetic fixes; plus a big Windows lib u...PhiLho2010-05-2137-30077/+30552
* | added testsAndreas Rumpf2010-05-287-0/+172
* | explicit types for generic routinesAndreas Rumpf2010-05-2825-343/+411
* | crc check for external files to compile; bugfix: os.parseCmdLineAndreas Rumpf2010-04-057-54/+145
* | bugfix: complex.nim compilesAndreas Rumpf2010-04-047-79/+55
* | tiny C backend for a much faster REPLAndreas Rumpf2010-04-0212-53/+208
|/
* bugfix: duplicate gtk2.set_tab_posAndreas Rumpf2010-03-291-2/+7
* bugfix: duplicate gtk2.set_tab_posAndreas Rumpf2010-03-294-18/+17
* Bugfix: code generation bug that affected dialogs.nimAndreas Rumpf2010-03-254-2/+13
* merged dom's changesAndreas Rumpf2010-03-221-5/+5
|\
| * bugfix: / not part of the path in parseurlDominik Picheta2010-03-221-5/+5
* | versioning for GTK wrappersAndreas Rumpf2010-03-203-7/+6
|/
* Bugfix: strutils.deleteAndreas Rumpf2010-03-203-8/+19
* Bugfix: empty echo statementAndreas Rumpf2010-03-205-23/+19
* BUGFIX: mEqRef for evalOpAndreas Rumpf2010-03-172-1/+3
* bugfix: return type of gtk2.check_menu_item_new*Andreas Rumpf2010-03-176-10/+359
* version 0.8.8Andreas Rumpf2010-03-152-0/+0
* version 0.8.8Andreas Rumpf2010-03-1452-6196/+15735
* improvements graphics moduleAndreas Rumpf2010-03-103-32/+45
* bugfix: len openarrayAndreas Rumpf2010-03-108-13/+74
* bugfix: tseqtupleAndreas Rumpf2010-03-104-40/+214
* bugfix: wrong error: not all cases covered with enums with holesAndreas Rumpf2010-03-093-3/+25
* bugfix:hexadecimal ranges 0x00..0x01Andreas Rumpf2010-03-092-1/+9
re>
         









                                                         








                                                         
                                                                          












                                        
#
#
#            Nim's Runtime Library
#        (c) Copyright 2017 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This modules registers a signal handler that turns access violations /
## segfaults into a ``NilAccessDefect`` exception. To be able to catch
## a NilAccessDefect all you have to do is to import this module.
##
## Tested on these OSes: Linux, Windows, OSX

{.used.}

# do allocate memory upfront:
var se: ref NilAccessDefect
new(se)
se.name = "NilAccessDefect"
se.msg = "Could not access value because it is nil."

when defined(windows):
  include "../system/ansi_c"

  import winlean

  const
    EXCEPTION_ACCESS_VIOLATION = DWORD(0xc0000005'i32)
    EXCEPTION_CONTINUE_SEARCH = Long(0)

  type
    PEXCEPTION_RECORD = ptr object
      exceptionCode: DWORD # other fields left out

    PEXCEPTION_POINTERS = ptr object
      exceptionRecord: PEXCEPTION_RECORD
      contextRecord: pointer

    VectoredHandler = proc (p: PEXCEPTION_POINTERS): LONG {.stdcall.}
  proc addVectoredExceptionHandler(firstHandler: ULONG,
                                   handler: VectoredHandler): pointer {.
    importc: "AddVectoredExceptionHandler", stdcall, dynlib: "kernel32.dll".}

  {.push stackTrace: off.}
  proc segfaultHandler(p: PEXCEPTION_POINTERS): LONG {.stdcall.} =
    if p.exceptionRecord.exceptionCode == EXCEPTION_ACCESS_VIOLATION:
      {.gcsafe.}:
        raise se
    else:
      result = EXCEPTION_CONTINUE_SEARCH
  {.pop.}

  discard addVectoredExceptionHandler(0, segfaultHandler)

  when false:
    {.push stackTrace: off.}
    proc segfaultHandler(sig: cint) {.noconv.} =
      {.gcsafe.}:
        rawRaise se
    {.pop.}
    c_signal(SIGSEGV, segfaultHandler)

else:
  import posix

  var sa: Sigaction

  var SEGV_MAPERR {.importc, header: "<signal.h>".}: cint

  {.push stackTrace: off.}
  proc segfaultHandler(sig: cint, y: ptr SigInfo, z: pointer) {.noconv.} =
    if y.si_code == SEGV_MAPERR:
      {.gcsafe.}:
        raise se
    else:
      quit(1)
  {.pop.}

  discard sigemptyset(sa.sa_mask)

  sa.sa_sigaction = segfaultHandler
  sa.sa_flags = SA_SIGINFO or SA_NODEFER

  discard sigaction(SIGSEGV, sa)