Terminate the simulation on exit() from firmware#380
Terminate the simulation on exit() from firmware#380edgar-bonet wants to merge 1 commit intobuserror:masterfrom
Conversation
When the firmware calls exit() or returns from main(), it ends up running _exit(), which disables interrupts and hangs the CPU in an infinite, empty loop. run_avr now terminates the simulation if the CPU-hanging instruction (rjmp .-2) is executed with interrupts disabled.
|
I checked that, with this pull request, cli();
_exit(1); |
|
+1 for this PR, i wondered why cli();while(1); does not stop the simulation. |
|
Oh cool does |
|
@mofosyne: No. The value passed to
|
|
@mofosyne: Here is a quick and dirty way to get simavr return the exit status of the firmware. Apply this pull request, then: diff --git a/simavr/sim/run_avr.c b/simavr/sim/run_avr.c
index 863fc8e..0b4fd50 100644
--- a/simavr/sim/run_avr.c
+++ b/simavr/sim/run_avr.c
@@ -334,5 +334,7 @@ main(
break;
}
+ int exit_status = avr->data[24]; // r24 at program termination
avr_terminate(avr);
+ return exit_status;
}There is a caveat: if the firmware terminates by crashing, or by sleeping with interrupts off, then the returned value is meaningless. In such a case, it would be wiser for simavr to return 0, or maybe 1 on a crash. For this reason, I am not submitting this as a pull request, but you can use this solution if you are not concerned about this caveat. |
The firmware can end the simulation by sleeping with interrupts disabled. While this way of hanging the CPU makes a lot of sense, it is not the most intuitive.
The standard way to terminate a C program is by either calling
exit()or returning frommain(). It turns out the AVR C runtime supports stopping the firmware in both these standard ways. Returning frommain()jumps toexit(), which disables interrupts and hangs the CPU in an infinite loop. The implementation is roughly like this:This pull request makes run_avr gracefully quit if the firmware executes the CPU-hanging instruction
rjmp .-2with interrupts disabled.The actual condition used in the code is:
Some of those tests may be redundant but, as I am not sure to understand all the subtleties of the simulation, I opted for defensive programming.
The feature has been tested with the following program: