输出堆栈指针的值。

时间:2021-04-16 22:02:24

How can I print out the current value at the stack pointer in C in Linux (Debian and Ubuntu)?

在Linux (Debian和Ubuntu)中,我如何在C的堆栈指针中打印当前值?

I tried google but found no results.

我试了谷歌,但没有结果。

5 个解决方案

#1


19  

One trick, which is not portable or really even guaranteed to work, is to simple print out the address of a local as a pointer.

一种技巧是简单地将本地地址作为指针打印出来,这种技巧既不能移植,也不能保证有效。

void print_stack_pointer() {
  void* p = NULL;
  printf("%p", (void*)&p);
}

This will essentially print out the address of p which is a good approximation of the current stack pointer

这将打印出p的地址,这是当前堆栈指针的一个很好的近似

#2


16  

There is no portable way to do that.

没有可移植的方法。

If you want a solution for gcc/x86, you could use this:

如果您想要gcc/x86的解决方案,可以使用以下方法:

register int sp asm ("sp");
printf("%x", sp);

#3


6  

In addition to duedl0r's answer with specifically GCC you could use __builtin_frame_address(0) which is GCC specific (but not x86 specific).

除了duedl0r使用特定GCC的答案之外,您还可以使用__builtin_frame_address(0),它是特定于GCC的(但不是特定于x86)。

This should also work on Clang (but there are some bugs about it).

这应该也适用于Clang(但是有一些bug)。

Taking the address of a local (as JaredPar answered) is also a solution.

获取本地地址(JaredPar回答)也是一种解决方案。

Notice that AFAIK the C standard does not require any call stack in theory.

注意,AFAIK C标准在理论上不需要任何调用堆栈。

Remember Appel's paper: garbage collection can be faster than stack allocation; A very weird C implementation could use such a technique! But AFAIK it has never been used for C.

记住Appel的文件:垃圾收集可以比堆栈分配更快;一个非常奇怪的C实现可以使用这种技术!但AFAIK从未用于C。

One could dream of a other techniques. And you could have split stacks (at least on recent GCC), in which case the very notion of stack pointer has much less sense (because then the stack is not contiguous, and could be made of many segments of a few call frames each).

你可以想象其他的技术。您可以使用分割堆栈(至少在最近的GCC上是这样),在这种情况下,堆栈指针的概念就没有那么重要了(因为堆栈不是连续的,每个调用帧都可以由许多段组成)。

#4


2  

On Linuxyou can use the proc pseudo-filesystem to print the stack pointer.

在linux上,您可以使用proc伪文件系统来打印堆栈指针。

Have a look here, at the /proc/your-pid/stat pseudo-file, at the fields 28, 29.

请看这里的/proc/your-pid/stat伪文件,在字段28,29。

startstack %lu The address of the start (i.e., bottom) of the stack.

startstack %lu起始地址(即,底部)的堆栈。

kstkesp %lu The current value of ESP (stack pointer), as found in the kernel stack page for the process.

kstkesp %lu ESP(堆栈指针)的当前值,在进程的内核堆栈页面中可以找到。

You just have to parse these two values!

您只需解析这两个值!

#5


1  

u can also use an extended assembler instruction for example

例如,您还可以使用扩展的汇编程序指令。

#include <stdint.h>

uint64_t getsp( void )
{
    uint64_t sp;
    asm( "mov %%rsp, %0" : "=rm" ( sp ));
    return sp;
}

for 32 bit system 64 has to be replaced with 32 and rsp with esp

32位系统64必须用esp替换为32和rsp。

#1


19  

One trick, which is not portable or really even guaranteed to work, is to simple print out the address of a local as a pointer.

一种技巧是简单地将本地地址作为指针打印出来,这种技巧既不能移植,也不能保证有效。

void print_stack_pointer() {
  void* p = NULL;
  printf("%p", (void*)&p);
}

This will essentially print out the address of p which is a good approximation of the current stack pointer

这将打印出p的地址,这是当前堆栈指针的一个很好的近似

#2


16  

There is no portable way to do that.

没有可移植的方法。

If you want a solution for gcc/x86, you could use this:

如果您想要gcc/x86的解决方案,可以使用以下方法:

register int sp asm ("sp");
printf("%x", sp);

#3


6  

In addition to duedl0r's answer with specifically GCC you could use __builtin_frame_address(0) which is GCC specific (but not x86 specific).

除了duedl0r使用特定GCC的答案之外,您还可以使用__builtin_frame_address(0),它是特定于GCC的(但不是特定于x86)。

This should also work on Clang (but there are some bugs about it).

这应该也适用于Clang(但是有一些bug)。

Taking the address of a local (as JaredPar answered) is also a solution.

获取本地地址(JaredPar回答)也是一种解决方案。

Notice that AFAIK the C standard does not require any call stack in theory.

注意,AFAIK C标准在理论上不需要任何调用堆栈。

Remember Appel's paper: garbage collection can be faster than stack allocation; A very weird C implementation could use such a technique! But AFAIK it has never been used for C.

记住Appel的文件:垃圾收集可以比堆栈分配更快;一个非常奇怪的C实现可以使用这种技术!但AFAIK从未用于C。

One could dream of a other techniques. And you could have split stacks (at least on recent GCC), in which case the very notion of stack pointer has much less sense (because then the stack is not contiguous, and could be made of many segments of a few call frames each).

你可以想象其他的技术。您可以使用分割堆栈(至少在最近的GCC上是这样),在这种情况下,堆栈指针的概念就没有那么重要了(因为堆栈不是连续的,每个调用帧都可以由许多段组成)。

#4


2  

On Linuxyou can use the proc pseudo-filesystem to print the stack pointer.

在linux上,您可以使用proc伪文件系统来打印堆栈指针。

Have a look here, at the /proc/your-pid/stat pseudo-file, at the fields 28, 29.

请看这里的/proc/your-pid/stat伪文件,在字段28,29。

startstack %lu The address of the start (i.e., bottom) of the stack.

startstack %lu起始地址(即,底部)的堆栈。

kstkesp %lu The current value of ESP (stack pointer), as found in the kernel stack page for the process.

kstkesp %lu ESP(堆栈指针)的当前值,在进程的内核堆栈页面中可以找到。

You just have to parse these two values!

您只需解析这两个值!

#5


1  

u can also use an extended assembler instruction for example

例如,您还可以使用扩展的汇编程序指令。

#include <stdint.h>

uint64_t getsp( void )
{
    uint64_t sp;
    asm( "mov %%rsp, %0" : "=rm" ( sp ));
    return sp;
}

for 32 bit system 64 has to be replaced with 32 and rsp with esp

32位系统64必须用esp替换为32和rsp。