1

When I use gdb to debug process in arm linux I can use call like call write(123,"abc",3)

How does gdb inject that call into process and recover all?

3
  • Consider how breakpoints work on Linux ARM devices. The debugger has already broken the code execution at the break-point, so it's just a matter of marshaling the parameters in accordance with ARM ABI and calling the specified entry point. The return value is usually in a register, gets converted for display and the debugger waits for you next instruction. Commented Jul 15, 2020 at 18:34
  • 1
    Way too broad of a question. See: github.com/eklitzke/ptrace-call-userspace Commented Jul 15, 2020 at 20:11
  • WIth GDB being an open source utility, Suggest reading the source, starting with: https://www.gnu.org/software/gdb/current/ Then retrieve the source via: git clone git://sourceware.org/git/binutils-gdb.git and can install git via: sudo apt install git Commented Jul 17, 2020 at 16:16

1 Answer 1

2

How does gdb inject that call into process and recover all?

GDB can read and write the inferior (being debugged) process memory using ptrace system call.

So it reads and saves in its own memory some chunk of instructions from inferior (say 100 bytes).

Then it overwrites this chunk with new instructions, which look something like:

r0 = 123
r1 = pointer to "abc"
r2 = 3
BLR write
BKPT

Now GDB saves the current inferior registers, sets ip to point to the chunk of instructions it just wrote, and resumes the inferior.

Inferior executes instructions until it reaches the breakpoint, at which point GDB regains control. It can now look at the return register to know what write returned and print it. GDB now restores the original instructions and original register values, and we are back as if nothing happened.

P.S. This is a general description of how "call function in inferior" works; I do not claim that this is exactly how it works.

There are also complications: if write calls back into the code that GDB overwrote, it wouldn't work. So in reality GDB uses some other mechanism to obtain suitable "scratch area" in the inferior. Also, the "abc" string requires scratch area as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.