Tracing stack and heap overflow errors - Mbed
文章推薦指數: 80 %
To monitor and debug memory overflow issues, mbed OS 5 provides runtime statistics for stack and heap usage. To make it easier to dynamically ...
Overview
Operatingsystem
MbedOS
TheopensourceOSforCortex-Mdevices
Developmenttools
MbedStudio
DownloadthedesktopIDEforMbedOS
KeilStudio
Arm'sIDEforIoT,embeddedandMbeddevelopment
MbedCLI
CommandlineaccesstoMbedtoolsandservices
SecurityandConnectivity
MbedTLSandMbedCrypto
IndustrystandardTLSstackandcryptolibrary
Connectivity
BLE,WiFi,Cellular,LoRaWANandmore
Ecosystem
Ourpartners
DozensofleadingcompaniestrustMbedOS
Hardware
Boards
BuildyourMbedprojectswithdevelopmentboardsforArmCortexprocessorsandMCUs
Modules
ModulesincludeaMCU,connectivityandonboardmemory,makingthemidealfordesigningIoTproductsformassproduction
Components
Thecomponentdatabasehostslibrariesfordifferentsensors,actuators,radios,inputs,middlewareandIoTservices
Hardwareoverview&MbedEnabled
LearnabouthardwaresupportforMbed,aswellastheMbedEnabledprogram,whichidentifiesMbedcompatibleproducts
MbedHDK
Referencedesigns,schematicsandboardlayoutstodevelopproductionhardwareandMbed-compatibledevelopmentboards
Code
Documentation
MbedOS
MbedStudio
KeilStudio
Casestudies
Community
Forum
Contribute
MbedOSTechForum
Reportaproblem
Blog
KeilStudioCloud
Amodern,browser-basedIDEforIoT,MLandembeddeddevelopmentwithMbedandCMSIS.
ClassicCompilerforMbedprojects.
LoginorSignup
LoginorSignup
Overview
Operatingsystem
MbedOS
Developmenttools
MbedStudio
KeilStudio
MbedCLI
Securityandconnectivity
MbedTLSandMbedCrypto
Connectivity
Ecosystem
Ourpartners
Hardware
Boards
Modules
Components
Hardwareoverview&MbedEnabled
MbedHDK
Code
Documentation
MbedOS
MbedStudio
KeilStudio
Casestudies
Community
Forum
Contribute
MbedOSTechForum
Reportaproblem
Blog
UpcomingmaintenanceonMbedsites:Tuesdays1000-1100UTC|Learnhowthismightaffectyou
Blog
»Tracingstackandheapoverflowerrors
Tracingstackandheapoverflowerrors
Lastupdated14Aug2018,byJanJongboom.
mbedOS5,
memory,
outofmemory,
RTX
WarningThisarticleisoutdated.Forthelatestversionofthisarticle,seeTrackingmemoryusagewithMbedOS.
Ifyouhaveeverseenthelightsofdeadonyourdevelopmentboard,accompaniedbyanRTXerrorcode:0x00000001orOperatornewoutofmemorymessageontheserialport,youhavehitamemoryoverflowbug.Memorymanagementremainsadifficultproblemonmicrocontrollers.Notonlyismemorylimited,butalsomicrocontrollersdonothaveanMMUandthereforecannotmovememoryblocksaroundwithoutchangingaddresses.Thislackofvirtualmemorymeansyouhavetohavefixedstacksizes,soyoucanrunintoastackoverflowerrorevenwhenthereisstillRAMavailable.
Tomonitoranddebugmemoryoverflowissues,mbedOS5providesruntimestatisticsforstackandheapusage.Tomakeiteasiertodynamicallyusetheseruntimestatistics,MaxVilimpoc-embeddedsoftwareleadatunuGmbHinGermany-releasedthembed-memory-statuslibrary,whichthisblogpostusestoanalyseanddebugbothastackoverflowandaheapallocationerror.
Inaddition,thispostshowshowyouuseMax'slibrarytotrackusageoftheISRstack,whichthembedOS5runtimestatisticsdonotprovide.
Addingmbed-memory-statustoyourproject
Ifyou'reusingtheonlinecompiler:
Right-clickonyourproject.
ChooseImportLibrary>FromURL.
Under'SourceURL',enter:https://github.com/nuket/mbed-memory-status
ClickImport.
Ifyou'reusingmbedCLI,run:
$mbedaddhttps://github.com/nuket/mbed-memory-status
Enablingruntimestatistics
Openorcreatethembed_app.jsonfile,andfillitwith:
{
"macros":["DEBUG_ISR_STACK_USAGE=1","MBED_HEAP_STATS_ENABLED=1","MBED_STACK_STATS_ENABLED=1"]
}
Thisenablesstack,heapandISRstackstatistics.
Note:Ifyou'reusingtheonlinecompiler,removetheDEBUG_ISR_STACK_USAGE=1macro.
ISRstatistics
TheISRstackisnotcoveredbythembedOSruntimestatisticsbutisanextensionmadebythembed-memory-statuslibrary.Trackingworkslikethis:
Thelibraryregistersafunctioninthestartup(*.S)scriptfortheapplication.
ThisfunctionfillstheISRstackwithplaceholdervalues(0xAFFEC7ED)butwithoutaffectingthestackpointer.
WhenrequestingtheavailablespaceontheISRstack,thecodeinspectsthememoryreservedfortheISRstackandseeswhichpartofthememoryhasnotchanged.
Toenablethesestatistics,you'llneedtomodifythestartupscriptforyourdevelopmentboard.Thisonlyworksifyou'recompilinglocallywithGCC,notintheonlinecompiler.Instructionsarehere.
Note:Thisapproachunfortunatelydoesnottrackfreedmemory.
Viewingmemoryregions
Nowthatyouhaveenabledruntimestatistics,youcanviewthememoryregions.Thisisusefulbecauseitgivesyouinsightinthethreadsthatarerunning,thestacksizesofthesethreadsandtheavailableheapmemory.ThisisespeciallyusefulwhendebuggingstackoverflowerrorsbecauseitshowsthememoryregionsandthreadIDs.
ToviewtherunningthreadsandtheheapandISRstacksizes,addtheselinestoyourmain()function:
#include"mbed_memory_status.h"
intmain(){
print_all_thread_info();
print_heap_and_isr_stack_info();
Formbed-os-example-blinkycompiledwithGCC_ARM,runningonaFRDM-K64Fthisresultsinthefollowingoutput:
stack(start:20002690end:200029B0size:00000320used:00000070)thread(id:200029F8entry:00002E8D)
stack(start:20000B7Cend:20001B7Csize:00001000used:00000098)thread(id:20002A38entry:000025F1)
stack(start:20002DC8end:20002FC8size:00000200used:00000040)thread(id:2000301Centry:00002639)
heap(start:20003130end:2002F000size:0002BED0used:00000000)alloc(ok:00000000fail:00000000)
isr_stack(start:2002F000end:20030000size:00001000used:00000400)
Youseethreethreadsrunning,theirmemoryregionsandtheamountofstackspaceused.Toseewhatthesethreadsare,youcaninspectthe.elffile(onadebugbuild)via:
arm-none-eabi-nmmbed-os-example-blinky.elf
Lookingforentrieswithoffsetentryminus1(Whyminus1?):
00002e8cTosTimerThread
000025f0Tpre_main
00002638Tos_idle_demon
Debuggingastackoverflowerror
Nowthatyouknowwhichthreadsarerunningandthememoryspacethattheyareusing,youcantriggerastackoverflowerror.Thefollowingprogramoverflowsthestacksizeofthemainthread(0x1000bytesontheK64F)afterasecond:
#include"mbed.h"
#include"mbed_memory_status.h"
intmain(){
uint8_tbig_arr[1024];
for(size_tix=0;ix
延伸文章資訊
- 1Stack And Heap In C | C Tutorial For Beginners - YouTube
- 2What is heap and stack? - Maxi-Pedia
- 3Binary Exploitation: Buffer Overflows | by Vickie Li - Medium
- 4Why are there only stack overflows, not heap overflows? - Quora
Heap overflow is a type of a buffer overflow that takes place on the heap, where another type is ...
- 5Heap vs. Stack for Delphi Developers - ThoughtCo
When you start programming in Delphi you might get errors like "stack overflow". Here's helpful i...