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

Commit

Permalink
Don't trap exceptions if a debugger is detected on Windows.
Browse files Browse the repository at this point in the history
Having to "step into" the program every time you debug it to set
rt_trapExceptions to false is a nuisance (perhaps more so with Windows
debuggers than GDB). Fortunately, we can easily detect when our D program
is run under a debugger, and disable the default exception handler, so
that an uncaught exception will be handled by the debugger.

I believe other Windows runtimes (C++/Delphi) do this as well.
  • Loading branch information
CyberShadow committed Feb 24, 2012
1 parent 54ee857 commit 1e48655
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/rt/dmain2.d
Expand Up @@ -36,6 +36,7 @@ version (Windows)
extern (Windows) wchar_t* GetCommandLineW();
extern (Windows) wchar_t** CommandLineToArgvW(wchar_t*, int*);
extern (Windows) export int WideCharToMultiByte(uint, uint, wchar_t*, int, char*, int, char*, int);
extern (Windows) int IsDebuggerPresent();
pragma(lib, "shell32.lib"); // needed for CommandLineToArgvW
}

Expand Down Expand Up @@ -418,6 +419,12 @@ extern (C) int main(int argc, char** argv)

bool trapExceptions = rt_trapExceptions;

version (Windows)
{
if (IsDebuggerPresent())
trapExceptions = false;
}

void tryExec(scope void delegate() dg)
{
void printLocLine(Throwable t)
Expand Down

5 comments on commit 1e48655

@jkm
Copy link

@jkm jkm commented on 1e48655 Sep 13, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you "step into" the program?
I'm trying to debug my program but I cannot stop the runtime from handling the Throwable. My minimal example is:

extern extern(C) __gshared bool rt_trapExceptions;                              
static this()                                                                   
{                                                                               
    rt_trapExceptions = false;                                                  
}                                                                               

void main()                                                                     
{                                                                               
    throw new Exception("test");                                                
}

But this still prints the backtrace.
I'm using the rt_trapExceptions because I'm running Linux.

@alexrp
Copy link
Member

@alexrp alexrp commented on 1e48655 Sep 13, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux, just break on _d_throwc.

@jkm
Copy link

@jkm jkm commented on 1e48655 Sep 13, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks. I would have never found this myself. I'll give it a try.

@CyberShadow
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't set rt_trapExceptions from your program, because it is read before any of your code runs (incl. static constructors). It must be set from a debugger.

@jkm
Copy link

@jkm jkm commented on 1e48655 Sep 13, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meaning the debugger sets the variable and then calls main?
But anyway I do not understand the commit message then.

Please sign in to comment.