linux程序计数器,如何在C中打印程序计数器的确切值
小編典典
您應該能夠使用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;
}
*感謝FrankH。指出存在__current_pc()
通常,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;
}
當我在x86系統上運行上述程序時,它將產生輸出:
0x8048432
0x8048447
0x804845c
在拆卸時gdb:
Dump of assembler code for function main:
0x08048424 : push %ebp
0x08048425 : mov %esp,%ebp
0x08048427 : and $0xfffffff0,%esp
0x0804842a : sub $0x10,%esp
0x0804842d : call 0x804841c
0x08048432 : mov %eax,0x4(%esp)
0x08048436 : movl $0x8048510,(%esp)
0x0804843d : call 0x80482f0
0x08048442 : call 0x804841c
0x08048447 : mov %eax,0x4(%esp)
0x0804844b : movl $0x8048510,(%esp)
0x08048452 : call 0x80482f0
0x08048457 : call 0x804841c
0x0804845c : mov %eax,0x4(%esp)
0x08048460 : movl $0x8048510,(%esp)
0x08048467 : call 0x80482f0
0x0804846c : mov $0x0,%eax
0x08048471 : leave
0x08048472 : ret
End of assembler dump.
2020-06-03
總結
以上是生活随笔為你收集整理的linux程序计数器,如何在C中打印程序计数器的确切值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux pps 包 网卡,linux
- 下一篇: 红旗linux yum,更换红旗Linu