RIP is a register that stores the address of the current instruction. I am writing a system call to get the program counter of a user level process. Taking the following code for an example, the sys_getrip() is the system call that I am working on. How shall I implement it?
The platform that I am working on is Ubuntu 14.04 with Linux kernel 3.14.4.
/*A system call in the kernel level*/
void sys_getrip(){
char *pc =//What's the magic here?
printk(KERN_INFO, "The current program counter of %d is %p", current->pid, pc);
}
/*A test function running in user level*/
void main(void){
...
sys_getrip();
...
...
sys_getrip();
...
}
A similar problem has been posted here: How to print exact value of the program counter in C. I tried the approach in this post by implementing the sys_getrip() as following
void sys_getrip(void){
printk(KERN_INFO "RIP = %p\n", __builtin_return_address(0));
}
In this case, however, different invokes of sys_getrip() in main() gave the same RIP value.