Re: Stack Overflow Detection
- From: Ben Bradley <ben_u_bradley@xxxxxxxxxxx>
- Date: Tue, 03 Jun 2008 23:02:12 -0400
On Fri, 30 May 2008 10:37:20 -0500, Vladimir Vassilevsky
<antispam_bogus@xxxxxxxxxxx> wrote:
Richard Phillips wrote:
Hello all,
I'm thinking about implementing the above in a system I'm working on, does
anyone have a good strategy for doing this?
Look into the map file. A sensible linker will provide the call tree
with the corresponding stack usage. Depending on the system, you may or
may not need to account for the stack usage in the interrupts. Avoid the
unpredictable stack usage such as recursions and the dynamic allocations
from stack.
One possibility is having an area of RAM (beyond the stack "area") filled
with known data, if any of that data gets trashed then assume the stack has
overflowed. What are the pros and cons of this?
This is one of the common ways. The main problem with it is that you
never know for sure what is the maximum stack usage.
You CAN know the max stack usage if you can trace out every
possible call and interrupt (the call tree, as you mentioned). If it's
THAT important (in a "high reliability" application such as vehicle
control), the program can/should be planned and written from the start
with this in mind (doing as you said above, avoiding unpredictable
stack usage).
But this method, while rigorous, may not be easily applicable to "a
system I'm working on."
Another idea is to actually watch the SP, but one drawback is if I check
this at a predefined point in the code, then it's possible the worst case
scenario will occur between checks?
Check the SP in the interrupt which happens at random time instants.
This also doesn't guarantee the detection of the max. usage.
Make a repetitive interrupt that does nothing but check the stack.
If it happens often enough, it will have the largest size the stack
reaches. It would have to run quite often and may take a substantial
percentage of CPU cycles to catch the stack value as used in short
subroutines and interrupts, but it would reasonably find the max the
stack has reached (not neccesarily the theoretical max the stack could
get).
There's an old-fashioned device called a logic analyzer that when
properly set up can do wonders for tracing things such as stack usage
in real time without interfering with normal operation, but they don't
work with embedded parts that don't have address and data lines
brought out to pins. Some manufacturers have made versions of their
microcontrollers with all the pertinent lines bonded out, but I don't
know if anyone still does that.
Any better ideas?
Use the CPU with MMU. Limit the stack segment size.
Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com
.
- Prev by Date: Re: Microcontroller-related engineering jobs
- Next by Date: NFL Sports Jersey, NBA NHL Soccer Jersey Supplires
- Previous by thread: Re: Stack Overflow Detection
- Next by thread: Material Choice for Portable Sound Player
- Index(es):
Relevant Pages
|
|