Get register with ptrace PTRACE_GETREGS vs PTRACE_PEEKUSER performance

Viewed 1905

If I need to get the values of some registers with ptrace for example I need r0 , r1 ,pc registers what way will give me better performance :

int pc= ptrace(PTRACE_PEEKUSER,pid,60,0);

r0 = ptrace(PTRACE_PEEKUSER, pid, 0, NULL);

r1 = ptrace(PTRACE_PEEKUSER, pid, 4, NULL);

Or get all registers with PTRACE_GETREGS and then use only the values I need?

How is that implement? Is PTRACE_GETREGS implement as a loop on PTRACE_PEEKUSER so if I don't need all the registers the best way is to use PTRACE_PEEKUSER as number of registers I need?

Is the performance depend on architecture (x86/Arm)?

1 Answers

How is that implement?

ptrace us arch dependent.

for arm in linux kernel ptrace implementation is (arch/arm/kernel/ptrace.c)[https://elixir.bootlin.com/linux/latest/source/arch/arm/kernel/ptrace.c#L784]

if I don't need all the registers the best way is to use PTRACE_PEEKUSER as number of registers I need?

as per code above. PTRACE_GETREGS is not iteration over PTRACE_PEEKUSER. so PTRACE_GETREGS seems a better option as per @Nate Eldredge 's comment. multiple syscall has higher cost than getting registers.

Is the performance depend on architecture (x86/Arm)?

yes, ptrace code is different so it will have impact also number of registers will also change and assembly instructions to read registers will be different.


these are the answer best of knowledge. if you have some more questions/doubts/clarifications feel free to comment I will try my best to answer them.

lastly my 2 cents about performance

performance is dependent on various factors so along with optimally coding , practical test comparisons are better way ahead. use code to create multiple tests and execute them to verify your hypothesis about it. so many things happen in hardware and compiler optimisations so

test your code on arm and x86 to know right answer about performance in your specific case.

Related