How to print exact value of the program counter in C

后端 未结 3 1389
有刺的猬
有刺的猬 2020-12-10 06:29

I want to write a C program which would print the contents of the program counter PC. Can this be done from user space, or assembly, or some specific kernel rou

3条回答
  •  春和景丽
    2020-12-10 07:04

    Well I think you can get the information by inserting assembly blocks inside your C code. This will totally depend on your compiler and the register set of your platform. I did it like this:

    int get_counter1()
    
    {
    
        __asm__ ("lea (%rip), %eax ") ;
    }
    
    int get_counter2()
    
    {
    
        int x = 0;
        __asm__ ("lea (%rip), %eax") ;
    }
    
    int main()
    
    {
    
        printf("%x\n",get_counter1());
        printf("%x\n",get_counter2());
        return 0;
    }
    

    4004ce

    4004e1

提交回复
热议问题