This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to implement a function which will give the address of the calling function during runtime.

I am working on a complex project written in C for 8051 arhitecture with Keil C. And unfortunately no real debugger. There are many functions called from various c files. Some times hundereds of different c file. I want to implement a function which will give me the address of the calling function during runtime. Is there a way to read the call stack to exctract the callig function address? If a can get the adress a can check it from .m51 file and can understand which function call is active.

func1(void)
{ func_test();
};

func2(void)
{ func_test();
};

func3(void)
{ func_test();
};

func_test(void)
{ int get_call_func_address;

get_call_func_address = ???????; printf("Calling function adress is %d", get_call_func_address);

//Do other things...

}

main()
{ func1(); func2(); func3();
}

Best regards
Saltuk Alakus

Parents
  • Printing a string for each enter/leave is too expensive for most applications.

    A slightly cheaper solution that is sometimes possible is to add a software stack with pointers to function names. This stack of function names can then be printed when needed. The bad part is that a number of functions may have multiple exit points making it very easy to create leaks, where the function name stack doesn't gets popped at all times.

    One partial solution is to use a ring-buffer instead of a stack and dump a limited number of entries - unless recursive calls are used, any duplicated function names in the dump would represent a function that sometimes fails to pop its information before returning.

    For max flexibility, the ring buffer could store one or two integers together with the string pointer, allowing important state information to be queued also.

    This solution consumes resonable amounts of processing power, but for a C51 processor, the stack/ring buffer may consume too much RAM.

Reply
  • Printing a string for each enter/leave is too expensive for most applications.

    A slightly cheaper solution that is sometimes possible is to add a software stack with pointers to function names. This stack of function names can then be printed when needed. The bad part is that a number of functions may have multiple exit points making it very easy to create leaks, where the function name stack doesn't gets popped at all times.

    One partial solution is to use a ring-buffer instead of a stack and dump a limited number of entries - unless recursive calls are used, any duplicated function names in the dump would represent a function that sometimes fails to pop its information before returning.

    For max flexibility, the ring buffer could store one or two integers together with the string pointer, allowing important state information to be queued also.

    This solution consumes resonable amounts of processing power, but for a C51 processor, the stack/ring buffer may consume too much RAM.

Children
  • Per,
    Why "13Per Westermark" and not "Per Westermark" ?

  • Because I sometime start to write before the page has loaded fully, and guess which field that always have the focus on load?

    All it takes is the phone to call or something and I will not notice this. All following posts will then use an incorrect name until I notice it and corrects it.

    It is a bit funny actually: Most people posts multiple times, but this forum thinks the most prioritized field is the first name. A little bit of javascript to move the focus to the 'Message' in case the name and email can be auto-filled using a cookie would be practival. Or the MCU field in case the user is creating a new thread.

    A little bit of javascript could also detect most unformatted source code and require the poster to correct.