Author Topic: Boost assertions closes the debugger  (Read 15664 times)

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Boost assertions closes the debugger
« on: November 01, 2011, 05:08:24 pm »
Hi together!

I've searched for this topic in the forum but I have found nothing related.

I'm using Boost libraries to test my projects in CB, but I've found that when I use BOOST_ASSERT macro, the application closes and the developer has no information about what happened.
Why doesn't CB stop at the line the assertion fails?
Is it the normal behaviour?
Should I configure something?
Must I execute assertions in another way?

Thanks in advance to all of you.

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #1 on: November 01, 2011, 05:15:37 pm »
Have you tried the debugger branch?
What is your OS/C::B version/GCC/boost/GDB?

Simple code to reproduce the problem?
Does normal assert(false) works?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #2 on: November 01, 2011, 05:17:34 pm »
> Why doesn't CB stop at the line the assertion fails?

Why should it? The assertion mechanism knows nothing about the debugger, and the debugger knows nothing about the assertion mechanism.

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #3 on: November 01, 2011, 05:24:33 pm »
Because the assertion mechanism most of the times causes the application to crash, at least the assert(false) macro, does so.
If the app crashes the debugger should catch it and the user should be able to inspect the reason for the crash.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #4 on: November 01, 2011, 05:26:23 pm »
If you want to write your own code to process the the BOOST_ASSERT, this is how to do it:

#include <iostream>
#define BOOST_ENABLE_ASSERT_HANDLER
#include <boost/assert.hpp>
using namespace std;

namespace boost
{
    void assertion_failed(char const * expr, char const * function, char const * file, long line) {
      cout << "BOOST assertion failed\n";
   }
}

int main()
{
   BOOST_ASSERT( 1 == 0 );
   cout << "Hello world!" << endl;
   return 0;
}


Replace:

     cout << "BOOST assertion failed\n";

with whatever you need to do to enter the debugger, though your stack frame will probably be off.

See also http://www.boost.org/doc/libs/1_36_0/libs/utility/assert.html. But at the end of the day, assertions are not great debugging tools.

« Last Edit: November 01, 2011, 05:28:38 pm by Neil Butterworth »

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #5 on: November 01, 2011, 05:43:11 pm »
Neil, please lets wait for the ThundThund to provide details about his problem.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #6 on: November 01, 2011, 05:50:45 pm »
Why? Surely the code I posted is useful and of interest to anyone using BOOST_ASSERT?

Further information, it appears that BOOST_ASSERT calls std::abort() on an assertion failure. Although I can see a debugger catching the abort, I can't see how it can catch the line at which the abort occurred, which is what the OP was asking about.


Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #7 on: November 01, 2011, 06:47:43 pm »
Thanks for all your answers.

Windows7/CB 10.05/MinGW (GCC 4.4.1)/ Boost 1.46.1

Well, the problem is that I'm coming from Visual Studio, and there the assertions break the execution at the line they fail, giving you the chance to review variable values and so, like a breakpoint. Even more, you can press F5 again and continue debugging (the program continues normally).
I expected CB has the same feature or a way to emulate it.

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #8 on: November 01, 2011, 07:06:09 pm »
ThundThund:
1. can you provide a minimal code sample.
2. try the debuggers branch nightly build

Neil: Stop the off topic, please, ThundThund has a concrete problem, so lets concentrate on it. And yes, C::B can put the current position marker on the line of the ASSERT, if the option 'When stopping, auto-switch to the first valid frame...' is enabled.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #9 on: November 01, 2011, 07:09:54 pm »
> Stop the off topic, please

I certainly may be wrong, but I am definitely not off-topic.

> ThundThund has a concrete problem

Which I am addressing. My approach may be different from yours, but that doesn't make it "wrong" (though it might be)  or "off-topic".
« Last Edit: November 01, 2011, 07:11:30 pm by Neil Butterworth »

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #10 on: November 01, 2011, 07:15:01 pm »
Well, the scenario is simple: I have one library, with many classes. In the method of one of the classes, I put BOOST_ASSERT(var). I link that library to my application, instance the class and then call the method. The debugger stops and I can't know what happened.


Note: Another thing I have to say is that breakpoints don't pause the debugger when I put them anywhere outside my "main.cpp file", don't know if it's related, maybe I'll post this in another thread.

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #11 on: November 01, 2011, 07:39:42 pm »
For me this code

#include <iostream>
#include <boost/assert.hpp>
using namespace std;


void f() {
  BOOST_ASSERT( 1 == 0 );
  // **** here ***
}

void g() {
   f();
}

int main()
{
   g();
   cout << "Hello world!" << endl;
   return 0;
}


When compiled with -g, and run in the debugger causes a break in the debugger. If I then go to the "Call Stack" debugger window and click on the the last entry there, I get the cursor moved to the line just after the BOOST_ASSERT. This is nowhere near as pretty as the MS debugger, I know, but we are talking gdb here!

This is with Win7, latest TDM, and a fairly recent CB nightly - SVN 7385.


Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #12 on: November 01, 2011, 07:56:15 pm »
I've copied and pasted that code, and the debugger doesn't break for me, it stops at all. :(
I don't know what I'm doing wrong. I have the option "When stopping..." and the -g flag activated.

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #13 on: November 01, 2011, 08:01:13 pm »
Are you running in it in the debugger - i.e. from the Debug|Start menu option? And have you got the "Call Stack" window open? And if you don't get a break, what does  happen?

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #14 on: November 01, 2011, 08:05:26 pm »
Yes, Debug->Start. I've the Call stack window open. Imagine that you press the Stop debugging button at the moment the program starts, that happens, tha debugging finishes at the moment the assert is executed.

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #15 on: November 01, 2011, 08:07:28 pm »
Actually, cancel that. I do get a break in the debugger, but the cursor is not accurately positioned after the assert - it seems like in the previous code it got located there by chance.

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #16 on: November 01, 2011, 08:13:51 pm »
ThundThund: Have you tried the debugger's branch nightly? Also have you read this http://wiki.codeblocks.org/index.php?title=Debugging_with_Code::Blocks ?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #17 on: November 01, 2011, 08:18:47 pm »
I've not tried that branch. I'm going to read that just in case I forgot something.

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #18 on: November 01, 2011, 08:25:26 pm »
I achieve all the requirements of the article.

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #19 on: November 01, 2011, 08:27:50 pm »
@oBFusCATed

Did you try the small code sample I posted? Does it work for you? For me it doesn't even stop after the assert using command-line gdb; does it for you?

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #20 on: November 01, 2011, 08:29:25 pm »
Code: [Select]
int main() {
 BOOST_ASSERT(false);
 ...
}
Works flawlessly.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #21 on: November 01, 2011, 08:30:42 pm »
Yes, but does the code I posted work flawlessly?

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #22 on: November 01, 2011, 09:01:13 pm »
Yes, the same as with my code...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #23 on: November 01, 2011, 09:10:16 pm »
Could you post a screenshot? I suspect that what you class as working and what I and the OP class as working may be different.

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #24 on: November 01, 2011, 09:33:06 pm »
What exactly do you need I show in the screenshot?

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #25 on: November 01, 2011, 09:35:47 pm »
Sorry I meant:

@oBFusCATed

Could you post a screenshot? I suspect that what you class as working and what I and the OP class as working may be different.


You (i.e. ThundThund) are the OP == Original Poster
« Last Edit: November 01, 2011, 09:39:26 pm by Neil Butterworth »

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #26 on: November 01, 2011, 10:46:19 pm »
Hm, why do you need a screen shot? It is the same as if the debugger is stopped on a breakpoint...

And keep in mind that I'm on linux and I'm running the latest debugger branch code. I don't know what is the behaviour of the 10.05 or trunk.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #27 on: November 01, 2011, 11:14:29 pm »
@oBFusCATed

I'd like to see  screenshot so that I can see what you think the correct appearance of the system should be after the boost assert fails. For me it does seem to break in the debugger, but I don't know how you or the OP think this break should correctly be displayed.  My understanding of WX/scintilla is that the appearance should be more or less platform independent, so a screenshot from a working CB implementation should resolve this. And it will take you, what? - two minutes to do?

To be explicit, I'd like to see the debugger call stack window and the editor window.

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #28 on: November 01, 2011, 11:28:40 pm »
I agree, it takes a few seconds to do and could clarify some things  :D.

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #30 on: November 02, 2011, 12:05:02 am »
That seems to be what I want to get  :)

EDIT: See image in previous page.

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #31 on: November 02, 2011, 12:21:51 pm »
I have just tried this with the latest debugger nightly, on Windows 7, and even simple  stuff like this (no Boost):

    assert( 1 == 0 );

or the even simpler:

    abort();

is not being caught in the debugger.

OTOH, stuff like this;

    int * p = 0;
    * p = 42;

is caught, and the correct stack-frame and location is displayed. Using this fact, one can write this fix/hack:

    #define BOOST_ENABLE_ASSERT_HANDLER
    #include <boost/assert.hpp>
     using namespace std;

    namespace boost
    {
        void assertion_failed(char const * expr, char const * function, char const * file, long line) {
            *(int *)0 = 42;
        }
    }


    void f() {
            BOOST_ASSERT( 1 == 0 );
    }

    void g() {
       f();
    }

    int main() {
        g();
    }

which will give you the call stack for assert that failed.




« Last Edit: November 02, 2011, 12:30:26 pm by Neil Butterworth »

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #32 on: November 02, 2011, 12:48:30 pm »
Neil please use code tags!
OK, so abort detection is not working on windows and this is a bug.
Can you provide the full debugger's debug log?
See the debugger options how to enable this log.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #33 on: November 02, 2011, 10:45:13 pm »
Do you mean this?

Starting debugger:
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb 6.8
Child process PID: 2732
Program exited with code 03.
Debugger finished with status 0

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #34 on: November 02, 2011, 10:55:05 pm »
No, this is the normal log, the debugger's debug log is not visible by default and should be explicitly enabled.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #35 on: November 02, 2011, 11:15:23 pm »
Attached is the debugger log for this program:

Code: [Select]
#include <cstdlib>
using namespace std;

void f() {
   abort();
}

void g() {
f();
}

int main() {
g();
}

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #36 on: November 02, 2011, 11:19:56 pm »
Hm, it looks like the debugger is not catching the call to abort. Pretty strange.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #37 on: November 03, 2011, 04:54:54 pm »
So are you going to send it as a Bug? If so, please paste here the reference to it, so I can track its progress.

Many thanks to you two for your interest, your time and your wisdom :)

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #38 on: November 03, 2011, 07:22:33 pm »
I don't know if i'm going to report it to gdb/mingw guys, first I need to check it.
Unfortunately, this won't happen soon.
But I've added it to my todo, so it won't be forgotten.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #39 on: November 03, 2011, 07:28:31 pm »
Ok, that's reasonable. The point is that I would like to be notified when this is solved (whenever).
Well, I guess I will have to ask in the forum in the future.

Thanks again.

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #40 on: November 03, 2011, 08:00:15 pm »
@ThundThund

It's a "feature" of the Windows implementation of the GDB debugger, and nothing to do with CB, so I wouldn't wait around here for a fix. The Windows version of GDB seems incapable of handling raise(SIGABRT), which is what is happening underneath all this. And I wouldn't expect this to be fixed any time soon, if ever.

Basically, GDB is a piss-poor debugger on Windows, and nothing the CB devs can do will alter that. Personally, I very, very rarely use a debugger of any sort, but if I did I would use Visual C++, which has an outstanding debugger.

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #41 on: November 03, 2011, 08:03:59 pm »
I'll post a message in this thread, if I can fix it.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #42 on: November 03, 2011, 08:05:17 pm »
It's a "feature" of the Windows implementation of the GDB debugger...
Are you 100% sure, could it be that the mingw standard lib is broken. Or there is a setting which should be enabled.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #43 on: November 03, 2011, 08:14:17 pm »
No, I'm not 100% sure. I have run some tests using command line GDB,  and I have done some googling which failed to come up with any magic settings. SIGABRT is caught by default on Linux, and is definitely not caught by default on Windows. I've tried using the "handle" command to change how the signal is handled in GDB, but that seems to make no difference. This really wants input from a GDB-on-Windows expert.

I am 100% sure that GDB is a poor Windows debugger, however. Particularly compared with the one that comes with VC++.

Online oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12129
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #44 on: November 03, 2011, 08:29:10 pm »
I am 100% sure that GDB is a poor Windows debugger, however. Particularly compared with the one that comes with VC++.
This can be said for almost any other debugger, because VStudio's debugger is close to the best debugger.
And on windows it is the native one and gdb doesn't use the MS's symbols, but the GNU symbols format, so most of the time GDB is working without symbols.
BTW there are no signals on windows, as far as I know.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #45 on: November 03, 2011, 08:33:43 pm »
> BTW there are no signals on windows, as far as I know.

http://msdn.microsoft.com/en-us/library/xdkz3x12%28v=vs.71%29.aspx

Despite what the "Visual Studio .NET 2003" header implies, the text makes it clear that signals have always been a part of Windows.

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #46 on: November 04, 2011, 09:14:57 am »
What about this?

http://stackoverflow.com/questions/1721543/continue-to-debug-after-failed-assertion-on-linux-c-c

They use SIGTRAP or Int 3.
I'm now at work, I'll try to use it this afternoon.

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #47 on: November 04, 2011, 09:47:10 am »
Interesting. This code:

Code: [Select]
#include <iostream>
using namespace std;

void f() {
   asm( "int $3" );
}

void g() {
f();
}

int main() {
cout << "hello world\n";
g();
cout << "hello again\n";
}

Does indeed cause a  break in the debugger and displays the correct stack frames.  It also allows continuation after the interrupt. Windows does not support SIGTRAP, so you would be limited to using the non-portable asm(). You would also have to modify the BOOST_ASSERT code too.
« Last Edit: November 04, 2011, 10:02:11 am by Neil Butterworth »