Data Watchpoints (DWT) on cortex-m4 to detect memory corruption

823 Views Asked by At

I am trying to detect memory corruption on a Cortex M4 (STM32F4) using the Data watchpoint and trace (DWT) feature of cortex-m4 boards. I am able to set the watchpoints on a variable but when I access the variable in the code the DebugMon_Handler is not triggerd.

I trying to run the code form the following posts

Can Cortex M4 data watchpoint trigger an interrupt without a debugger

Cortex-M – Debugging runtime memory corruption

I have also added the below lines to trigger DebugMon_Handler interrupt

CoreDebug->DEMCR = CoreDebug_DEMCR_TRCENA_Msk /*enable tracing*/ |
                   CoreDebug_DEMCR_MON_EN_Msk /*enable debug interrupt*/;

But still the interrupt is not triggered even after writing to test_variable What am I doing wrong??

COMPLETE CODE:

#include <stdio.h>
#include <stdlib.h>
#include "diag/Trace.h"
//#include "core_cm3.h"
#include "stm32f4xx.h"

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#pragma GCC diagnostic ignored "-Wreturn-type"

void watchpoint_enable()
{
    CoreDebug->DEMCR = CoreDebug_DEMCR_TRCENA_Msk /*enable tracing*/ |
                       CoreDebug_DEMCR_MON_EN_Msk /*enable debug interrupt*/;
    uint32_t test_variable;
trace_printf("enable watch points.... \n");
DWT->COMP1 = &test_variable;
DWT->MASK1 = 0;                         // match all comparator bits, don't ignore any
DWT->FUNCTION1 = (1 << 11)             /*DATAVSIZE 1 - match whole word*/
                 | (1 << 1) | (1 << 2) /*generate a watchpoint event on write*/;
trace_printf("watch points enabled....\n");
test_variable = 5; //                   <<----- CPU stops after this line

}

int main(int argc, char *argv[])
{
    watchpoint_enable();
}

void DebugMon_Handler(void)
{

    trace_printf("Debug handler in action...\n");
}

#pragma GCC diagnostic pop

OUTPUT

enable watch points....

watch points enabled....

EXPECTED OUTPUT

enable watch points....

watch points enabled....

Debug handler in action...

NOTE

I am using STM32F407VG board emulated with QEMU on eclipse.

0

There are 0 best solutions below