Pages: [1] 2   Go Down
Author Topic: What does abort() do?  (Read 2922 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

I don't get it, please help me out with this.

What does actually abort() do in Arduino? I don't find any related documentation.

If I upload this code into Arduino:
Code:
void setup() {
  Serial.begin(19200);
  Serial.println("Start");
}
void loop() {
  Serial.println("Start Loop");
  abort();
}>
I only get two dashes on the serial monitor, no "Start Loop" and even no "Start". It looks like not even setup() is running. Well, actually that's impossible, since something is "moving" on the serial line after all.

What am I missing?

Logged

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

What does actually abort() do in Arduino?

It clearly has undesirable side-effects.  Why would you want to use it?
« Last Edit: January 24, 2013, 03:35:39 pm by Coding Badly » Logged

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

Quote
I only get two dashes on the serial monitor, no "Start Loop" and even no "Start". It looks like not even setup() is running. Well, actually that's impossible, since something is "moving" on the serial line after all.
setup() is running, and buffering serial data. Then, loop() starts and buffers some more. Before anything can get shifted out, you call abort(), which does what it's name suggests. It stops the program that is running, including interrupts (which are how the serial data gets shifted out).

So, the question is why are you calling abort()?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


There is more documentation about the libs taht can be used @ nongnu.org

abort() is described (short) here - http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga63e28bec3592384b44606f011634c5a8
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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

Btw, I was going to use abort() as a measure for halting the software in case totally unacceptable data were used as input.
I understand that's somehow drastic as an error handling method. I wonder how/if/when abort() maybe used such as that.

I'm not going to write some crazy complex error handling routine for this particular purpose anyway, enough being to simply stop everything.
Logged

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


Code:
while ( true );
Logged

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

It clearly has undesirable side-effects.  Why would want to use it?

I understand you care for others ... that's nice :-)
Logged

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


Code:
while ( true );

I see now!!! I've found it in stdlib.h :-)
Thank you anyway, I appreciate that. It's nice to know there's people like you out there :-)
Logged

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

setup() is running, and buffering serial data. Then, loop() starts and buffers some more. Before anything can get shifted out, you call abort(), which does what it's name suggests. It stops the program that is running, including interrupts (which are how the serial data gets shifted out).
That was quite precise and complete. Respect!

Logged

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

Quote
I'm not going to write some crazy complex error handling routine for this particular purpose anyway, enough being to simply stop everything.
In CATIA, we have, occasionally, dialog boxes that pop up that say "Click OK to terminate", like someone would really want to.

The cause is almost always a result of failing to handle an exception. That sounds like what you are planning to implement.

I would strongly encourage you not to do that. Restarting the Arduino because of a type, or noise on the serial line, or some other strange input arriving is not a best practice. It's far better to simply ignore out-of-range data.
Logged

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

Quote
I'm not going to write some crazy complex error handling routine for this particular purpose anyway, enough being to simply stop everything.
In CATIA, we have, occasionally, dialog boxes that pop up that say "Click OK to terminate", like someone would really want to.

The cause is almost always a result of failing to handle an exception. That sounds like what you are planning to implement.

I would strongly encourage you not to do that. Restarting the Arduino because of a type, or noise on the serial line, or some other strange input arriving is not a best practice. It's far better to simply ignore out-of-range data.

I completely agree with you, andI usually tend to spend quite some time on error handling. Especially because I'm not a professional programmer, so I'm going to use the code for myself, not for customers ;-)

In this case I'm going to use abort() in a preliminary stage of the coding. I'm not going to bother much about providing valid alternative default values in case the input is going to be unacceptable. It's ok to me if it runs an infinite loop. It doesn't break anything at the moment. But it's good to know what it does precisely :-)
Logged

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


In which case, this may be helpful...
http://www.nongnu.org/avr-libc/user-manual/group__avr__assert.html
Logged

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


It could ... but I don't have the slightest idea of how to direct stderr to the serial ... so why bother adding this layer of complexity?
No, seriously, how may I define __ASSERT_USE_STDERR ? Do I get to redirect it to the serial channel? How?

Anyway, I find usefull that I can disable the assert() when I'm finished debugging, by simply defining NDEBUG once and for all.
That's cool to know :-)
Logged

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

In this case I'm going to use abort() in a preliminary stage of the coding.

It's already been explained to you that calling abort() under any circumstances in an Arduino sketch is a bad idea. If you want the sketch to chop itself off at the ankles, you could use the while(1); that Coding Badly showed you. Personally I think doing that just because of duff input is probably a cop-out, but if you can't be bothered to do anything more user-friendly then it's a legitimate way to halt your sketch. Calling abort() is not.
Logged

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

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

Quote
...but I don't have the slightest idea of how to direct stderr to the serial...

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.
Logged

Pages: [1] 2   Go Up
Jump to: