Author Topic: Strange GDB Behavior on Cortex-M4  (Read 1925 times)

0 Members and 1 Guest are viewing this topic.

Offline Sal AmmoniacTopic starter

  • Super Contributor
  • ***
  • Posts: 1772
  • Country: us
Strange GDB Behavior on Cortex-M4
« on: August 31, 2016, 05:19:04 am »
I was debugging some Cortex-M4 assembly code tonight when I noticed that my code would run fine when I single-stepped it using GDB, but crashed with a Usage Fault when I just let it run.

I traced the usage faults down to some "BX  r0" instructions. I had forgotten to set bit 0 (the Thumb bit) in r0, and the branch caused a usage fault for trying to change modes to "ARM" mode, which Cortex-M MCUs don't support. After I made sure all instances of "BX r0" followed code to set the Thumb bit, my code ran fine.

The question is: why did it run at all when single-stepping? Why didn't the first "BX r0" it executed when r0 bit 0 was set to 0 cause a usage fault?
"That's not even wrong" -- Wolfgang Pauli
 

Offline mubes

  • Regular Contributor
  • *
  • Posts: 238
  • Country: gb
  • Do Not Boil
Re: Strange GDB Behavior on Cortex-M4
« Reply #1 on: August 31, 2016, 03:26:38 pm »
Running in RAM or Flash? GDB, by default, inserts soft breakpoints for RAM based code and may be giving you some special love when it finds a BX with bit 0 clear....that might be a red herring but in general there's nothing that says that GDB might not be processing the BX internally and setting bit0...I think it needs to perform _some_ processing of the BX destination to know where to put the next BKPT and it's quite possible it doesn't actually *perform* the BX, just updates LR etc; Its source code perusal time if that kind of thing keeps you awake at night :-) Let us know what you find though, 'cos that is the kind of knowledge its useful to keep in your back pocket.

DAVE
 

Offline Sal AmmoniacTopic starter

  • Super Contributor
  • ***
  • Posts: 1772
  • Country: us
Re: Strange GDB Behavior on Cortex-M4
« Reply #2 on: August 31, 2016, 03:56:01 pm »
Running in RAM or Flash? GDB, by default, inserts soft breakpoints for RAM based code and may be giving you some special love when it finds a BX with bit 0 clear....that might be a red herring but in general there's nothing that says that GDB might not be processing the BX internally and setting bit0...I think it needs to perform _some_ processing of the BX destination to know where to put the next BKPT and it's quite possible it doesn't actually *perform* the BX, just updates LR etc; Its source code perusal time if that kind of thing keeps you awake at night :-) Let us know what you find though, 'cos that is the kind of knowledge its useful to keep in your back pocket.

Code is running from Flash. I don't know why GDB does this, but it's a bad thing because it can mask errors in code that it shouldn't mask. If code is incorrect and will generate a fault, then I want to know about it. I don't want GDB "fixing" it for me.  :palm:
"That's not even wrong" -- Wolfgang Pauli
 

Offline mubes

  • Regular Contributor
  • *
  • Posts: 238
  • Country: gb
  • Do Not Boil
Re: Strange GDB Behavior on Cortex-M4
« Reply #3 on: August 31, 2016, 05:55:04 pm »
Yup. Not nessesarily defending it...but I suspect it's just updating LR rather than executing the BX.  You have no idea how many times I've got burned by that bit0....
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf