Pages: 1 [2]   Go Down
Author Topic: What does abort() do?  (Read 4969 times)
0 Members and 1 Guest are viewing this topic.
Palermo
Offline Offline
Full Member
***
Karma: 0
Posts: 174
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you do nothing special...

Code:
#include <assert.h>

void setup( void )
{
  assert( false );
}

void loop( void )
{
}

abort is called if the assertion fails.  stderr does not come into the picture.

That's all clear to me. That's how I already used it after you suggested it to me.
But ... that way, it does no more than calling abort(), right? Why should I even bother with calling assert() then? What's the point if it does'nt add anything new to the picture?
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1478
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can disable it with the #define.  Just a few posts up you said that was a good thing. 8^)
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But now that it has been brought up, *is* there any way to redirect stderr to Serial, and then enable with #define __ASSERT_USE_STDERR , like int std c++ you could do

 std::cerr.rdbuf(myOStream.rdbuf());

?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But now that it has been brought up, *is* there any way to redirect stderr to Serial,

Yes, there is. You can associate stdin, stdout and stderr with the hardware Serial if you want (or any other device) by writing put and get functions for the device and then associating these functions with each stream by calls to fdevopen().
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3080
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's the most interesting piece of information I have seen all week.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13072
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why should I even bother with calling assert() then?

Welcome to "Design by Contract" ... http://en.wikipedia.org/wiki/Design_by_contract ... I highly recommend Bertrand Meyer's book.
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What does actually abort() do in Arduino?

From stdlib.h:

Code:
/** The abort() function causes abnormal program termination to occur.
    This realization disables interrupts and jumps to _exit() function
    with argument equal to 1. In the limited AVR environment, execution is
    effectively halted by entering an infinite loop. */
extern void abort(void) __ATTR_NORETURN__;

A simple error handler could be:

Code:
if (somethingBadHappens)
  {
  Serial.println (F("Bad thing XYZ happened."));
  Serial.flush ();
  exit (1);
  }

The flush call will ensure the message appears before you go into the infinite loop. The F macro saves wasting valuable RAM on the message.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13072
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


To ensure there is no ambiguity, I strongly agree with @PeterH...
http://arduino.cc/forum/index.php/topic,144756.msg1087549.html#msg1087549

It is never appropriate to use the AVR Libc abort.
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually I got bitten in one of my libraries because exit() isn't always implemented. This might be neater:

Code:
if (somethingBadHappens)
  {
  Serial.println (F("Bad thing XYZ happened."));
  Serial.flush ();
  noInterrupts ();
  while (true) { }   // give up
  }
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13072
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


This should get you started...

Code:
#define __ASSERT_USE_STDERR
#include <assert.h>

extern void __assert(
    const char *__func,
    const char *__file,
    int __lineno,
    const char *__sexp )
{
  // Put your stuff here.
  // __func is the function name (setup)
  // __file is the module name (the name of your sketch)
  // __lineno is the source code line number of the assert
  // __sexp is the assert expression (as a C string)

  Serial.print( F( "ASSERT FAILURE: " ) );
  Serial.print( __file );
  Serial.print( F( ": " ) );
  Serial.print( __func );
  Serial.print( F( ": #" ) );
  Serial.print( __lineno );
  Serial.println();
  Serial.flush ();
  noInterrupts ();
  while (true) { }   // give up
}

void setup( void )
{
  assert( false );
}

void loop( void )
{
}

Bear in mind that each assert puts up to three string constants in SRAM.  A better version would keep the string constants in Flash.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4836
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Silly me, I thought I'd need an underlying OS to abort to!
But then when I write code, I expect users to make the occasional error... since 1980.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Look on the bright side. Users expect programmers to make the occasional error, too. smiley
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 652
Posts: 50861
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Users expect programmers to make the occasional error, too.
And I never disappoint them.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4836
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From the OLD (it was old in 85) Programmer's Joke Sheet:

Users should not complain about the code they get. They are lucky to get anything at all.

1980 is when I started writing programs for engineers. Error handling became a must!
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Palermo
Offline Offline
Full Member
***
Karma: 0
Posts: 174
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Users should not complain about the code they get. They are lucky to get anything at all.

Priceless!!! smiley-grin
Logged

Pages: 1 [2]   Go Up
Jump to: