如何在C中打印程序计数器的精确值

时间:2021-07-08 11:40:35

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 routines are used?

我想写一个C程序,它将打印程序计数器PC的内容。可以从用户空间或程序集中完成,还是使用某些特定的内核例程?

3 个解决方案

#1


11  

You should be able to determine the PC by using the __current_pc() intrinsic in the ARM compiler toolchain (the ARM compiler supports many of the same extensions as GCC).* This is particular to ARM:

您应该能够通过使用ARM编译器工具链中的__current_pc()内在函数来确定PC(ARM编译器支持许多与GCC相同的扩展)。*这是ARM特有的:

int main () {
    printf("%#x\n", __current_pc());
    printf("%#x\n", __current_pc());
    printf("%#x\n", __current_pc());
    return 0;
}

* Thanks to FrankH. for pointing out the presence of __current_pc()

*感谢FrankH。指出__current_pc()的存在

In general, the PC gets saved as the return address in a function call. On non-ARM systems with GCC, you can call __builtin_return_address(0) to obtain the return address of the current function call context. Obtaining the program counter in this way incurs the penalty of adding a function call, but it avoids inline assembly, so this technique is portable to any system supported by GCC.

通常,PC在函数调用中保存为返回地址。在具有GCC的非ARM Linux系统上,可以调用__builtin_return_address(0)来获取当前函数调用上下文的返回地址。以这种方式获得程序计数器会引起添加函数调用的惩罚,但它避免了内联汇编,因此这种技术可以移植到GCC支持的任何系统。

void * get_pc () { return __builtin_return_address(0); }
int main () {
    printf("%p\n", get_pc());
    printf("%p\n", get_pc());
    printf("%p\n", get_pc());
    return 0;
}

When I run the above program on my x86 system, it produces the output:

当我在x86系统上运行上面的程序时,它会产生输出:

0x8048432
0x8048447
0x804845c

When disassembled in gdb:

在gdb中反汇编时:

Dump of assembler code for function main:
   0x08048424 <+0>: push   %ebp
   0x08048425 <+1>: mov    %esp,%ebp
   0x08048427 <+3>: and    $0xfffffff0,%esp
   0x0804842a <+6>: sub    $0x10,%esp
   0x0804842d <+9>: call   0x804841c <get_pc>
   0x08048432 <+14>:    mov    %eax,0x4(%esp)
   0x08048436 <+18>:    movl   $0x8048510,(%esp)
   0x0804843d <+25>:    call   0x80482f0 <printf@plt>
   0x08048442 <+30>:    call   0x804841c <get_pc>
   0x08048447 <+35>:    mov    %eax,0x4(%esp)
   0x0804844b <+39>:    movl   $0x8048510,(%esp)
   0x08048452 <+46>:    call   0x80482f0 <printf@plt>
   0x08048457 <+51>:    call   0x804841c <get_pc>
   0x0804845c <+56>:    mov    %eax,0x4(%esp)
   0x08048460 <+60>:    movl   $0x8048510,(%esp)
   0x08048467 <+67>:    call   0x80482f0 <printf@plt>
   0x0804846c <+72>:    mov    $0x0,%eax
   0x08048471 <+77>:    leave  
   0x08048472 <+78>:    ret    
End of assembler dump.

#2


5  

On ARM, you can use:

在ARM上,您可以使用:

static __inline__ void * get_pc(void)  {
    void *pc;
    asm("mov %0, pc" : "=r"(pc));
    return pc;
}

Or this one should work as well:

或者这个应该也可以:

static __inline__ void * get_pc(void) {
    register void * pc __asm__("pc");
    __asm__("" : "=r"(pc));
    return pc;
}

The forced inlining is important here, because that ensures you retrieve PC as per the call site.

强制内联在这里很重要,因为这可以确保您根据呼叫站点检索PC。

Edit: just remembered, __current_pc() ARM intrinsic. GCC should have this as well.

编辑:刚才记得,__ current_pc()ARM内在。海湾合作委员会也应该这样做。

#3


2  

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:

我想你可以通过在C代码中插入汇编块来获取信息。这完全取决于您的编译器和平台的寄存器集。我是这样做的:

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

#1


11  

You should be able to determine the PC by using the __current_pc() intrinsic in the ARM compiler toolchain (the ARM compiler supports many of the same extensions as GCC).* This is particular to ARM:

您应该能够通过使用ARM编译器工具链中的__current_pc()内在函数来确定PC(ARM编译器支持许多与GCC相同的扩展)。*这是ARM特有的:

int main () {
    printf("%#x\n", __current_pc());
    printf("%#x\n", __current_pc());
    printf("%#x\n", __current_pc());
    return 0;
}

* Thanks to FrankH. for pointing out the presence of __current_pc()

*感谢FrankH。指出__current_pc()的存在

In general, the PC gets saved as the return address in a function call. On non-ARM systems with GCC, you can call __builtin_return_address(0) to obtain the return address of the current function call context. Obtaining the program counter in this way incurs the penalty of adding a function call, but it avoids inline assembly, so this technique is portable to any system supported by GCC.

通常,PC在函数调用中保存为返回地址。在具有GCC的非ARM Linux系统上,可以调用__builtin_return_address(0)来获取当前函数调用上下文的返回地址。以这种方式获得程序计数器会引起添加函数调用的惩罚,但它避免了内联汇编,因此这种技术可以移植到GCC支持的任何系统。

void * get_pc () { return __builtin_return_address(0); }
int main () {
    printf("%p\n", get_pc());
    printf("%p\n", get_pc());
    printf("%p\n", get_pc());
    return 0;
}

When I run the above program on my x86 system, it produces the output:

当我在x86系统上运行上面的程序时,它会产生输出:

0x8048432
0x8048447
0x804845c

When disassembled in gdb:

在gdb中反汇编时:

Dump of assembler code for function main:
   0x08048424 <+0>: push   %ebp
   0x08048425 <+1>: mov    %esp,%ebp
   0x08048427 <+3>: and    $0xfffffff0,%esp
   0x0804842a <+6>: sub    $0x10,%esp
   0x0804842d <+9>: call   0x804841c <get_pc>
   0x08048432 <+14>:    mov    %eax,0x4(%esp)
   0x08048436 <+18>:    movl   $0x8048510,(%esp)
   0x0804843d <+25>:    call   0x80482f0 <printf@plt>
   0x08048442 <+30>:    call   0x804841c <get_pc>
   0x08048447 <+35>:    mov    %eax,0x4(%esp)
   0x0804844b <+39>:    movl   $0x8048510,(%esp)
   0x08048452 <+46>:    call   0x80482f0 <printf@plt>
   0x08048457 <+51>:    call   0x804841c <get_pc>
   0x0804845c <+56>:    mov    %eax,0x4(%esp)
   0x08048460 <+60>:    movl   $0x8048510,(%esp)
   0x08048467 <+67>:    call   0x80482f0 <printf@plt>
   0x0804846c <+72>:    mov    $0x0,%eax
   0x08048471 <+77>:    leave  
   0x08048472 <+78>:    ret    
End of assembler dump.

#2


5  

On ARM, you can use:

在ARM上,您可以使用:

static __inline__ void * get_pc(void)  {
    void *pc;
    asm("mov %0, pc" : "=r"(pc));
    return pc;
}

Or this one should work as well:

或者这个应该也可以:

static __inline__ void * get_pc(void) {
    register void * pc __asm__("pc");
    __asm__("" : "=r"(pc));
    return pc;
}

The forced inlining is important here, because that ensures you retrieve PC as per the call site.

强制内联在这里很重要,因为这可以确保您根据呼叫站点检索PC。

Edit: just remembered, __current_pc() ARM intrinsic. GCC should have this as well.

编辑:刚才记得,__ current_pc()ARM内在。海湾合作委员会也应该这样做。

#3


2  

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:

我想你可以通过在C代码中插入汇编块来获取信息。这完全取决于您的编译器和平台的寄存器集。我是这样做的:

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