ptrace in 2.6.5
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 11 lines · Fabiano Ramos
Hi All.
Is ptrace(), in singlestep mode, required to stop after a int 0x80? When tracing a sequence like mov ... int 0x80 mov .... ptrace would notify the tracer after the two movs, but not after theint
0x80. I want to know if it is a bug or the expected behaviour.
What happens is that after the int 0x80 the CPU is in ring 0 (you don't get an trace event in that mode unless you use a kernel debugger). Then when the kernel returns the last instruction executed before it is an IRET. But the IRET is also executed still in ring 0 and you should not get an event for it (you can not even access its code from user space).
So it's expected behaviour.
-Andi
Quoted 14 lines · Andi Kleen
Quoted 11 lines · Fabiano Ramos
Hi All.
Is ptrace(), in singlestep mode, required to stop after a int 0x80? When tracing a sequence like mov ... int 0x80 mov .... ptrace would notify the tracer after the two movs, but not after theint
0x80. I want to know if it is a bug or the expected behaviour.
Quoted 7 lines
What happens is that after the int
0x80the CPU is in ring 0 (you don't get an trace event in that mode unless you use a kernel debugger). Then when the kernel returns the last instruction executed before it is an IRET. But the IRET is also executed still in ring 0 and you should not get an event for it (you can not even access its code from user space).So it's expected behaviour.
IIRC, it's the "int" instruction that automatically clears the TF bit from flags. The next "iret" will restore the caller flags and re-enable the TF bit.
- Davide
Quoted 20 lines · Andi Kleen
Quoted 11 lines · Fabiano Ramos
Hi All.
Is ptrace(), in singlestep mode, required to stop after a int 0x80? When tracing a sequence like mov ... int 0x80 mov .... ptrace would notify the tracer after the two movs, but not after theint
0x80. I want to know if it is a bug or the expected behaviour.What happens is that after the int
0x80the CPU is in ring 0 (you don't get an trace event in that mode unless you use a kernel debugger). Then when the kernel returns the last instruction executed before it is an IRET. But the IRET is also executed still in ring 0 and you should not get an event for it (you can not even access its code from user space).
Quoted 3 lines
So it's expected behaviour.
-Andi
I got it. But I need it to stop after the instruction. I am a newbie, so is it trivial to patch the kernel so that it STOPS after the int 0x80? Can you give me some light on it?
Quoted 6 lines
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 16 lines · Fabiano Ramos
Quoted 7 lines
Quoted 7 lines
Is ptrace(), in singlestep mode, required to stop after a int 0x80? When tracing a sequence like mov ... int 0x80 mov ....Quoted 8 lines
Quoted 2 lines
ptrace would notify the tracer after the two movs, but not after the
int0x80. I want to know if it is a bug or the expected behaviour.What happens is that after the int
0x80the CPU is in ring 0 (you don't get an trace event in that mode unless you use a kernel debugger). Then when the kernel returns the last instruction executed before it is an IRET. But the IRET is also executed still in ring 0 and you should not get an event for it (you can not even access its code from user space).
Quoted 3 lines
I got it. But I need it to stop after the instruction. I am a newbie, so is it trivial to patch the kernel so that it STOPS after the int
0x80? Can you give me some light on it?
This is the behavior of CPU, not kernel. "iret" after "int 0x80", it restores the eip to "mov ...".
int 0x80
does syscall
iret
mov ....
<---- exception here (eip = "next insn")
next insn
So single-step exception happen *after* executed the "mov ...". Probably you need to use the breakpoint instead of single-step.
Signature · 1 line
OGAWA Hirofumi <[email protected]>
Quoted 2 lines · OGAWA Hirofumi
So single-step exception happen *after* executed the "mov ...". Probably you need to use the breakpoint instead of single-step.
Ah, sorry. Just use PTRACE_SYSCALL instead of PTRACE_SINGLESTEP. It's will stop before/after does syscall.
Signature · 1 line
OGAWA Hirofumi <[email protected]>
Quoted 7 lines · OGAWA Hirofumi
Quoted 2 lines · OGAWA Hirofumi
So single-step exception happen *after* executed the "mov ...". Probably you need to use the breakpoint instead of single-step.
Ah, sorry. Just use
PTRACE_SYSCALLinstead ofPTRACE_SINGLESTEP. It's will stop before/after does syscall.
Doing it this way is pretty lousy - you have to inspect the code after every step to see if it's an int $0x80. Is there some reason not to report a trap on the syscall return path if single-stepping?
Signature · 1 line
Daniel Jacobowitz
Quoted 6 lines · Daniel Jacobowitz
Quoted 5 lines · OGAWA Hirofumi
Quoted 2 lines · OGAWA Hirofumi
So single-step exception happen *after* executed the "mov ...". Probably you need to use the breakpoint instead of single-step.
Quoted 6 lines
Quoted 2 lines
Ah, sorry. Just use
PTRACE_SYSCALLinstead ofPTRACE_SINGLESTEP. It's will stop before/after does syscall.Doing it this way is pretty lousy - you have to inspect the code after every step to see if it's an int $0x80. Is there some reason not to report a trap on the syscall return path if single-stepping?
Well, the "iret" restore TF, and Intel states that the TF flag must be set at the beginning of the instruction for the trap to be fired. The next insn has the TF set, and the tap is fired. But the EIP is the one following the trapped insn.
- Davide
Quoted 6 lines · Daniel Jacobowitz
Quoted 5 lines · OGAWA Hirofumi
Quoted 2 lines · OGAWA Hirofumi
So single-step exception happen *after* executed the "mov ...". Probably you need to use the breakpoint instead of single-step.
Quoted 6 lines
Quoted 2 lines
Ah, sorry. Just use
PTRACE_SYSCALLinstead ofPTRACE_SINGLESTEP. It's will stop before/after does syscall.Doing it this way is pretty lousy - you have to inspect the code after every step to see if it's an int $0x80. Is there some reason not to report a trap on the syscall return path if single-stepping?
Strange thing. When entering a syscall, the int 0x80 does clear the trap, but first it is saved into the stack. When the iret is executed, the eflags is restored from the stack, thus single stepping is re-enabled.
The question is: the int 0x80 can be seen as complex instructions that is only completed after the iret. This way, I do not see why a debug trap is not generated afer the int 0x80 and BEFORE the mov.
I reinvented the wheel and built a module that did the same thing as a singlestep ptrace, and a the trap WAS generated after the int 0x80 completed, before the mov.
So I think it has sth to do with the debug trap handler.
I DO NOT BELIEVE THIS BEAVIOUR is right, since if it is not stopping after the int 0x80, ptrace is not TRULLY singlestepping.
Quoted 4 lines · Fabiano Ramos
The question is: the int
0x80can be seen as complex instructions that is only completed after the iret. This way, I do not see why a debug trap is not generated afer the int0x80and BEFORE the mov.
Quoted 8 lines
I reinvented the wheel and built a module that did the same thing as a singlestep ptrace, and a the trap WAS generated after the int
0x80completed, before the mov.So I think it has sth to do with the debug trap handler.
I DO NOT BELIEVE THIS BEAVIOUR is right, since if it is not stopping after the int
0x80, ptrace is not TRULLY singlestepping.
If you look at the Intel manual 24319202 page 44 (TF bit), it clearly says that the trap is generated on the instruction that follows the IRET. In the same doc, at page 145, it also says that the return address seen by the trap handler is the one following the trapped instructuion (INT#1 is a trap). Ideally, I'm with you in expecting a full trace on all the instructions (out of INTs), but this doesn't seem to be what documented. On the kernel side, this would be pretty much solved by issuing a ptrace op, with a modified EIP (+2) on return from a syscall (if in single-step mode).
- Davide
Quoted 3 lines · Davide Libenzi
On the kernel side, this would be pretty much solved by issuing a ptrace op, with a modified EIP (+2) on return from a syscall (if in single-step mode).
Actaully, the EIP should not be changed (since it already points to the intruction following INT 0x80) and I believe it is sufficent to replace the test for _TIF_SYSCALL_TRACE with (_TIF_SYSCALL_TRACE | TIF_SINGLESTEP) in the system call return path. This should generate a ptrace trap with EIP pointing to the next instruction following INT 0x80.
- Davide
Quoted 11 lines · Davide Libenzi
Quoted 3 lines · Davide Libenzi
On the kernel side, this would be pretty much solved by issuing a ptrace op, with a modified EIP (+2) on return from a syscall (if in single-step mode).
Actaully, the EIP should not be changed (since it already points to the intruction following INT
0x80) and I believe it is sufficent to replace the test for_TIF_SYSCALL_TRACEwith (_TIF_SYSCALL_TRACE|TIF_SINGLESTEP) in the system call return path. This should generate a ptrace trap with EIP pointing to the next instruction following INT0x80.
The patch below (for i386) should work.
- Davide
Index: arch/i386/kernel/entry.S
===================================================================
RCS file: /usr/src/bkcvs/linux-2.5/arch/i386/kernel/entry.S,v retrieving revision 1.83
i386/kernel/entry.S +1 -1
@@ -354,7 +354,7 @@ # perform syscall exit tracing ALIGN syscall_exit_work: - testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT), %cl + testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl jz work_pending sti # could let do_syscall_trace() call # schedule() instead
Index: include/asm-i386/thread_info.h
===================================================================
RCS file: /usr/src/bkcvs/linux-2.5/include/asm-i386/thread_info.h,v retrieving revision 1.19
asm-i386/thread_info.h +1 -1
@@ -165,7 +165,7 @@/* work to do on interrupt/exception return */ #define _TIF_WORK_MASK \ - (0x0000FFFF & ~(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT)) + (0x0000FFFF & ~(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP))#define _TIF_ALLWORK_MASK 0x0000FFFF/* work to do on any return to u-space */ /*
Quoted 8 lines · Davide Libenzi
Quoted 6 lines · Davide Libenzi
Quoted 3 lines · Davide Libenzi
On the kernel side, this would be pretty much solved by issuing a ptrace op, with a modified EIP (+2) on return from a syscall (if in single-step mode).
Quoted 7 lines
Quoted 5 lines
Actaully, the EIP should not be changed (since it already points to the intruction following INT
0x80) and I believe it is sufficent to replace the test for_TIF_SYSCALL_TRACEwith (_TIF_SYSCALL_TRACE|TIF_SINGLESTEP) in the system call return path. This should generate a ptrace trap with EIP pointing to the next instruction following INT0x80.The patch below (for i386) should work.
Yeah, that's what I was suggesting. I think the patch is right.
Signature · 1 line
Daniel Jacobowitz