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
Bear in mind that this is likely to have a serious impact on performance!!
You could try a couple of debug trace macros; eg,
#define ENTER(fn) printf( fn )
and
#define EXIT(fn) printf( fn )
thus:
whatever my_func( whatever ) { ENTER( "my_func" ); : : // your code here : : EXIT( "my_func" ); }
I think this macro will inform when the function is called but who calls the function is still not known.
Saltuk, what you are trying to do is something extraordinary: you want to be able to log just about any possible call tree in your program. The point is that this is not required if your software is organized properly: for example, if you have state machines in your program, you can simplify your requirement significantly because knowing the state of the state machine and the signal that it received, yields knowledge of the executed function! what you want to do is going to have, even if it works, such a huge impact on performance that it might distort the value of the data your acquire itself!
one more comment: I also used to work with such huge projects. I never needed this kind of logging due to: * proper documentation * encapsulation * clear protocols between modules
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.
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.
"I think this macro will inform when the function is called..."
Yes, that's right!
"...but who calls the function is still not known."
Yes it is - by examining the preceding trace!