I have a simple C program
int main() {
int var;
for(int i=0;i<3;i++) {
var=1;
}
return 0;
}
In kernel module, I hook (using ftrace) [do_user_addr_fault][1] function which get struct pt_regs *regs as one of the parameters
Now using regs->cs and regs->ip, I could uniquely identify an instruction, but in case of loop both values for var=1 would be same. I thought regs->cx would help as I read that it stores the loop counter, but its values seems ad-hoc. I use pr_info("cs %lu ip %lu cx %lu\n", regs->cs, regs->ip, regs->cx); to print the values.
So how could I differentiate between the same instruction in a loop?
this is the assembly code I got from g++ -S prog.cpp
.file "prog.cpp"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, -8(%rbp)
.L3:
cmpl $2, -8(%rbp)
jg .L2
movl $1, -4(%rbp)
addl $1, -8(%rbp)
jmp .L3
.L2:
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0"
.section .note.GNU-stack,"",@progbits
.section .note.gnu.property,"a"
.align 8
.long 1f - 0f
.long 4f - 1f
.long 5
0:
.string "GNU"
1:
.align 8
.long 0xc0000002
.long 3f - 2f
2:
.long 0x3
3:
.align 8
4:
ref [1]: https://elixir.bootlin.com/linux/latest/source/arch/x86/mm/fault.c#L1220