5

when debugging ARMv7 binary with GDB, aside from looking the instruction length, is there a way to figure out which mode the CPU is currently in? (ARM, Thumb)

daehee
  • 5,047
  • 7
  • 44
  • 70

1 Answers1

12

I'm using this little gdb-script to determine the current state from the CPSR field, just put it inside your ~/.gdbinit file and call arm_isa when needed.

define arm_isa
  if ($cpsr & 0x20)
    printf "Using THUMB(2) ISA\n"
  else
    printf "Using ARM ISA\n"
  end
end

It checks bit 5 in cpsr, which indicates the current state and outputs the used ISA.

Nico Erfurth
  • 3,362
  • 22
  • 26
  • This works in my experiments on GDB 8.2, but I wonder where GDB gets the information from, since DDI0487 da says `CPSR[5]` is `res0`, and that you should read `PSTATE.P` from `SPSR[5]`, and SPSR does not seem accessible from user mode. – Ciro Santilli May 30 '19 at 15:25
  • Keep in mind that this answer is from 2014, referring to ARMv7, the document you linked to is for ARMv8. PSTATE was only introduced in ARMv8 and took some of the former CPSR bits. – Nico Erfurth May 30 '19 at 19:27
  • Yes, I understand. And this still works, which is great :-) – Ciro Santilli May 31 '19 at 06:45