Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print the contents of stack in C program?

I want to, as the title says, print the contents of the stack in my C program.

Here are the steps I took:

  • I made a simple assembly (helper.s) file that included a function to return the address of my ebp register and a function to return the address of my esp register

    .globl get_esp
    
    get_esp:
        movl %esp, %eax
        ret
    # get_ebp is defined similarly, and included in the .globl section
    
  • I called the get_esp () and get_ebp () functions from my C program ( fpC = get_esp (); where fpC is an int)
  • I (successfully, I think) printed the address of my esp and ebp registers ( fprintf (stderr, "%x", fcP); )
  • I tried, and failed to, print out the contents of my esp register. (I tried fprintf (sderr, "%d", *fcP); and fprintf (sderr, "%x", *((int *)fcP));, among other methods). My program hits a segmentation fault at runtime when this line is processed.

What am I doing wrong?

EDIT: This must be accomplished by calling these assembly functions to get the stack pointers. EDIT2: This is a homework assignment.

like image 290
Nate Avatar asked Feb 02 '12 19:02

Nate


People also ask

How do you print from the bottom of the stack?

Approach 1 (Recursion): The idea is to pop the element of the stack and call the recursive function PrintStack. Once the stack becomes empty start printing the element which was popped last and the last element that was popped was the bottom-most element. Thus, elements will be printed from bottom to top.

How to create a stack in C++?

In order to create a stack in C++, we first need to include the stack header file. Once we import this file, we can create a stack using the following syntax: Here, type indicates the data type we want to store in the stack. For instance, In C++, the stack class provides various methods to perform different operations on a stack.

How to print the bottom-most element of a stack in Python?

Approach 1 (Recursion): The idea is to pop the element of the stack and call the recursive function PrintStack. Once the stack becomes empty start printing the element which was popped last and the last element that was popped was the bottom-most element.

What is the use of stack pointer in C++?

Stack pointer always points to the top element in the stack. The last node of a stack is set to NULL indicating the bottom of the stack. Therefore, we always check the stack pointer for NULL value while programming. Push function is used to insert data at the top of the stack while pop removes topmost data from the stack.


2 Answers

If your utilising a GNU system, you may be able to use GNU's extension to the C library for dealing backtraces, see here.

#include <execinfo.h>

int main(void)
{
     //call-a-lot-of-functions
}

void someReallyDeepFunction(void)
{
    int count;
    void *stack[50]; // can hold 50, adjust appropriately
    char **symbols;

    count = backtrace(stack, 50);
    symbols = backtrace_symbols(stack, count);

    for (int i = 0; i < count; i++)
        puts(symbols[i]);

    free(symbols);
}
like image 69
dreamlax Avatar answered Oct 07 '22 14:10

dreamlax


get_esp returns esp as it is within the function. But this isn't the same as esp in the calling function, because the call operation changes esp.

I recommend replacing the function with a piece of inline assembly. This way esp won't change as you try to read it.

Also, printing to sderr wouldn't help. From my experience, stderr works much better.

like image 32
ugoren Avatar answered Oct 07 '22 14:10

ugoren