Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:D-Programming-Language/druntime
Browse files Browse the repository at this point in the history
  • Loading branch information
complexmath committed Sep 26, 2011
2 parents 0c2ab0a + 7f695ce commit 0493e81
Show file tree
Hide file tree
Showing 4 changed files with 365 additions and 259 deletions.
57 changes: 57 additions & 0 deletions src/core/exception.d
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,47 @@ unittest
}


/**
* Thrown on an invalid memory operation.
*
* An invalid memory operation error occurs in circumstances when the garbage
* collector has detected an operation it cannot reliably handle. The default
* D GC is not re-entrant, so this can happen due to allocations done from
* within finalizers called during a garbage collection cycle.
*/
class InvalidMemoryOperationError : Error
{
this(string file = __FILE__, size_t line = __LINE__, Throwable next = null )
{
super( "Invalid memory operation", file, line, next );
}

override string toString()
{
return msg ? super.toString() : "Invalid memory operation";
}
}

unittest
{
{
auto oome = new InvalidMemoryOperationError();
assert(oome.file == __FILE__);
assert(oome.line == __LINE__ - 2);
assert(oome.next is null);
assert(oome.msg == "Invalid memory operation");
}

{
auto oome = new InvalidMemoryOperationError("hello", 42, new Exception("It's an Exception!"));
assert(oome.file == "hello");
assert(oome.line == 42);
assert(oome.next !is null);
assert(oome.msg == "Invalid memory operation");
}
}


/**
* Thrown on a switch error.
*/
Expand Down Expand Up @@ -462,6 +503,22 @@ extern (C) void onOutOfMemoryError()
}


/**
* A callback for invalid memory operations in D. An
* InvalidMemoryOperationError will be thrown.
*
* Throws:
* InvalidMemoryOperationError.
*/
extern (C) void onInvalidMemoryOperationError()
{
// The same restriction applies as for onOutOfMemoryError. The GC is in an
// undefined state, thus no allocation must occur while generating this object.
throw cast(InvalidMemoryOperationError)
cast(void*) InvalidMemoryOperationError.classinfo.init;
}


/**
* A callback for switch errors in D. A SwitchError will be thrown.
*
Expand Down
37 changes: 34 additions & 3 deletions src/core/runtime.d
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ private
extern (C) void* rt_loadLibrary( in char[] name );
extern (C) bool rt_unloadLibrary( void* ptr );

extern (C) void* rt_stackBottom();

extern (C) string[] rt_args();

version( linux )
Expand Down Expand Up @@ -395,8 +397,33 @@ Throwable.TraceInfo defaultTraceHandler( void* ptr = null )
{
static enum MAXFRAMES = 128;
void*[MAXFRAMES] callstack;

numframes = backtrace( callstack, MAXFRAMES );
if (numframes < 2) // backtrace() failed, do it ourselves
{
static void** getBasePtr()
{
version( D_InlineAsm_X86 )
asm { naked; mov EAX, EBP; ret; }
else
version( D_InlineAsm_X86_64 )
asm { naked; mov RAX, RBP; ret; }
else
return null;
}

void** stackTop = getBasePtr(), stackBottom = cast(void**)rt_stackBottom();
void* dummy;
if (stackTop && &dummy < stackTop && stackTop < stackBottom)
{
void** stackPtr = stackTop;
numframes = 0;
while (stackTop <= stackPtr && stackPtr < stackBottom && numframes < MAXFRAMES)
{
callstack[numframes++] = *(stackPtr+1);
stackPtr = cast(void**)*stackPtr;
}
}
}
framelist = backtrace_symbols( callstack, numframes );
}

Expand Down Expand Up @@ -490,10 +517,14 @@ Throwable.TraceInfo defaultTraceHandler( void* ptr = null )
}
else version( linux )
{
// format is:
// module(_D6module4funcAFZv) [0x00000000]
// format is: module(_D6module4funcAFZv) [0x00000000]
// or: module(_D6module4funcAFZv+0x78) [0x00000000]
auto bptr = cast(char*) memchr( buf.ptr, '(', buf.length );
auto eptr = cast(char*) memchr( buf.ptr, ')', buf.length );
auto pptr = cast(char*) memchr( buf.ptr, '+', buf.length );

if (pptr && pptr < eptr)
eptr = pptr;

if( bptr++ && eptr )
{
Expand Down
Loading

0 comments on commit 0493e81

Please sign in to comment.