Hi there,
We are using cortex M0 uC. We need to recognize at our code whether or not the debugger is attached. We have found after a lot of search and effort that we cannot use CoreDebug->DHCSR with cortex M0.
We have found a different approach using the HardFault exception and software breakpoint that indirect recognize if the debugger is attached.
In this approach using assembly if a hardware exception occurs due to a breakpoint then no debugger is attach and the HardFault exception return to the program skipping the breakpoint instruction.
Below is the assembly code we have found and slightly changed:
__asm("MOVS R0, #4"); __asm("MOV R1, __return_address()"); __asm("TST R0, R1"); __asm("BEQ _MSP"); __asm("MRS R0, PSP"); __asm("B _checkFaultInstruction"); __asm("_MSP: "); __asm("MRS R0, MSP"); __asm("_checkFaultInstruction: "); __asm("LDR R1, [R0,#24]"); __asm("LDRH R2, [R1]"); __asm("LDR R3,=0xBEAB"); __asm("CMP R2, R3"); __asm("BNE HardFault_HandlerC "); __asm("ADDS R1, #2"); __asm("STR R1, [R0,#24]"); __asm("BX __return_address()");
The compiler is throwing the follows errors:
1) __asm("LDR R3,=0xBEAB"); -> error #29: expected an expression We have replaced this instruction with: __asm("MOV R3, 0xBEAB"); Does this instruction has the same effect?
2) __asm("BX __return_address()"); -> error #1084: This instruction not permitted in inline assembler. How can we replace this unsupported instruction?
3) __asm("BNE HardFault_HandlerC "); -> error #114:label "HardFault_HandlerC" was reference but not defined HardFault_HandlerC is a function name 4) __asm("BX __return_address()"); -> error #114:label "..." was reference but not defined
What is wrong with 3 and 4?
Thanks in advance.
It uses an immediate form where it fits the 8-bit+shift form, and where it doesn't it loads the constant from a literal pool it creates. This usually resides after the subroutine return code, or after a unconditional branch.
The bigger question is why?
If you're just trying it for the purposes of your own testing, dig into the DWT/ITM units, or DBGMCU type stuff, it should be easy enough to find something to key off, without using assembler or handlers.
If it's for software protection purposes, consider most hackers can actually code in assembler fluently, and read code without the source. It helps to have a skill level above that of your perceived threat.
According to the following link our replacement to the first error is ok. infocenter.arm.com/.../index.jsp
any ideas for the rest ones?