<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/23762/how-to-implement-a-function-which-will-give-the-address-of-the-calling-function-during-runtime</link><description> 
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</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/thread/124653?ContentTypeID=1</link><pubDate>Fri, 09 May 2008 10:51:22 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:bf33bd63-6aba-4763-91d3-b9a48c43ec31</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;I think this macro will inform when the function is
called...&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Yes, that&amp;#39;s right!&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;&amp;quot;...but who calls the function is still not known.&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Yes it is - by examining the preceding trace!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/thread/142004?ContentTypeID=1</link><pubDate>Fri, 09 May 2008 04:29:52 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6812efe9-b8a7-4b5a-81e6-07c77c3f9635</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
Because I sometime start to write before the page has loaded
fully, and guess which field that always have the focus on load?&lt;/p&gt;

&lt;p&gt;
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.&lt;/p&gt;

&lt;p&gt;
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 &amp;#39;Message&amp;#39; 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.&lt;/p&gt;

&lt;p&gt;
A little bit of javascript could also detect most unformatted
source code and require the poster to correct.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/thread/138767?ContentTypeID=1</link><pubDate>Fri, 09 May 2008 03:37:50 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:446b5870-bf35-44b2-b9e6-da73f9dc4b60</guid><dc:creator>Catcus Blip</dc:creator><description>&lt;p&gt;&lt;p&gt;
Per,&lt;br /&gt;
Why &amp;quot;13Per Westermark&amp;quot; and not &amp;quot;Per Westermark&amp;quot; ?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/thread/135370?ContentTypeID=1</link><pubDate>Fri, 09 May 2008 02:16:30 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c00d80de-2164-429c-9597-51e6880cfd24</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
Printing a string for each enter/leave is too expensive for most
applications.&lt;/p&gt;

&lt;p&gt;
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&amp;#39;t gets
popped at all times.&lt;/p&gt;

&lt;p&gt;
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.&lt;/p&gt;

&lt;p&gt;
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.&lt;/p&gt;

&lt;p&gt;
This solution consumes resonable amounts of processing power, but
for a C51 processor, the stack/ring buffer may consume too much
RAM.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/thread/124655?ContentTypeID=1</link><pubDate>Fri, 09 May 2008 00:52:28 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:87b3a380-701e-4eac-8b0a-9e336fd489e1</guid><dc:creator>Catcus Blip</dc:creator><description>&lt;p&gt;&lt;p&gt;
one more comment: I also used to work with such huge projects. I
never needed this kind of logging due to:&lt;br /&gt;
* proper documentation&lt;br /&gt;
* encapsulation&lt;br /&gt;
* clear protocols between modules&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/thread/124656?ContentTypeID=1</link><pubDate>Fri, 09 May 2008 00:48:06 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:085c2b56-585b-4f34-9203-d918bd6a874e</guid><dc:creator>Catcus Blip</dc:creator><description>&lt;p&gt;&lt;p&gt;
Saltuk,&lt;br /&gt;
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!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/thread/113923?ContentTypeID=1</link><pubDate>Fri, 09 May 2008 00:29:40 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:19772940-5207-4cf9-b750-bac2cd40ce46</guid><dc:creator>S. Alakus</dc:creator><description>&lt;p&gt;&lt;p&gt;
#define ENTER(fn) printf( fn )&lt;/p&gt;

&lt;p&gt;
I think this macro will inform when the function is called but who
calls the function is still not known.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/thread/100639?ContentTypeID=1</link><pubDate>Fri, 09 May 2008 00:23:25 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:127edfe3-8458-4baf-89ea-9af60b5fa7ee</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
Bear in mind that this is likely to have a &lt;i&gt;&lt;b&gt;serious&lt;/b&gt;&lt;/i&gt;
impact on performance!!&lt;/p&gt;

&lt;p&gt;
You could try a couple of debug trace macros; eg,&lt;/p&gt;

&lt;pre&gt;
#define ENTER(fn) printf( fn )
&lt;/pre&gt;

&lt;p&gt;
and&lt;/p&gt;

&lt;pre&gt;
#define EXIT(fn) printf( fn )
&lt;/pre&gt;

&lt;p&gt;
thus:&lt;/p&gt;

&lt;pre&gt;
whatever my_func( whatever )
{
   ENTER( &amp;quot;my_func&amp;quot; );
   :
   :
   // your code here
   :
   :
   EXIT( &amp;quot;my_func&amp;quot; );
}
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/thread/76270?ContentTypeID=1</link><pubDate>Fri, 09 May 2008 00:13:46 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:105ba83c-4b92-475c-bdec-c143ce0a945a</guid><dc:creator>S. Alakus</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hello,&lt;/p&gt;

&lt;p&gt;
Simply bacause I have to debug the code on the running hardware.
But there is no hardware debugger for my platform.I can only debug by
simple printf or putstring to display some simple debug message.&lt;/p&gt;

&lt;p&gt;
Running code on PC is not appropriate. Bacause there are harware
dependent code areas and simulating on pc is not possible.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/thread/51750?ContentTypeID=1</link><pubDate>Thu, 08 May 2008 11:30:40 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6cc8c203-f7eb-41d3-8522-f5f77c7eafaf</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
I think you need to explain &lt;i&gt;&lt;b&gt;why&lt;/b&gt;&lt;/i&gt; you think you need
to do this?&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;&amp;quot;If I can get the adress I can check it from .m51 file and can
understand which function call is active.&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
So you&amp;#39;re talking about a debugging aid?&lt;/p&gt;

&lt;p&gt;
So you should probably be looking into a uVision
&lt;i&gt;&lt;b&gt;debug&lt;/b&gt;&lt;/i&gt; function, rather than trying to do this in your
embedded code?&lt;br /&gt;
See: &lt;a href="http://www.keil.com/support/man/docs/uv3/uv3_debug_functions.htm"&gt;http://www.keil.com/support/man/docs/uv3/uv3_debug_functions.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
Again, if you explained what you&amp;#39;re actually trying to achieve,
then people would be able to provide appropriate suggestions...&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a function which will give the address of the calling function during runtime.</title><link>https://community.arm.com/thread/49863?ContentTypeID=1</link><pubDate>Thu, 08 May 2008 07:42:06 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c33a7b4b-8073-4019-a5da-6f103dc4cf0c</guid><dc:creator>stephen phillips</dc:creator><description>&lt;p&gt;&lt;p&gt;
Some things to consider.&lt;br /&gt;
First consider this: &lt;b&gt;what happens when a function is
called?&lt;/b&gt;&lt;br /&gt;
Second how can that be used to identify the calling function?&lt;br /&gt;
Third consider what else you need to display.&lt;/p&gt;

&lt;p&gt;
I believe a good understanding of the underlying architecture is
necessary to do what you want.&lt;/p&gt;

&lt;p&gt;
Here are some useful hints&lt;br /&gt;
The stack (IE call stack) and everything about it and how to find
it&amp;#39;s &lt;b&gt;bottom&lt;/b&gt; are needed.&lt;br /&gt;
Your program model will also affects how the stack is used.&lt;br /&gt;
You need to understand the format of the .M51 file and what for and
where to look in it.&lt;/p&gt;

&lt;p&gt;
I modified my STARTUP.A51 adding this information to it:&lt;/p&gt;

&lt;pre&gt;
                RSEG    ?STACK
                                PUBLIC  __stack
__stack:         DS      1
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
The label __stack and PUBLIC __stack are the only additions
needed.&lt;br /&gt;
__stack should now be a constant available to your program.&lt;/p&gt;

&lt;p&gt;
I choose __stack because it&amp;#39;s odd enough looking someone won&amp;#39;t
assume that it is a variable. (and commented the heck out of where it
was used).&lt;/p&gt;

&lt;p&gt;
Next You need the REAL stack in your called function (func_test)
and you need to be very careful how you use the stack (be sure you
don&amp;#39;t assign anything to the stack in other words). Be sure to have
your processors &amp;lt;regXXX.h&amp;gt; file included for this to work (or
it won&amp;#39;t compile). The data on the stack is in little endian format.
IE the MSB is at the top and LSB is beneath it.&lt;br /&gt;
Also you should NOT use decimal numbers since the M51 file uses HEX
numbers for the addresses. You&amp;#39;ll be quite tired converting if you
use decimals.&lt;/p&gt;

&lt;pre&gt;
unsigned char data *call_stack;

call_stack = SP; // get the stack pointer
printf(&amp;quot;Call Stack addresses\n&amp;quot;);
do
{
  printf(&amp;quot;%02bX&amp;quot;, *call_stack);
  call_stack--;
  printf(&amp;quot;%02bX\n&amp;quot;, *call_stack);
  call_stack--;
}
// if we haven&amp;#39;t reached the bottom of the __stack
// __stack is the constant start of the stack loaded
// in the startup code see STARTUP.A51
while(call_stack &amp;gt; __stack);
printf(&amp;quot;end of call stack\n&amp;quot;);
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
This will give you a complete list of the nesting of the function
calls all the way to main from top to bottom (IE last function call
to main).&lt;br /&gt;
There is a reason you need this. Namely sometimes the Keil compiler
changes a CALL to a JMP instruction. This is something you will miss
completely if you just look at the prior address.&lt;/p&gt;

&lt;p&gt;
Next this &lt;b&gt;does not give you the calling address&lt;/b&gt; it gives
you the address of the next instruction. So you can&amp;#39;t assume the
address you see is the line of code, it is the NEXT instruction. You
will have to examine your addresses carefully in the M51 file (and
sometimes this is quite tricky). Also the Keil compiler on ocassion
does use the stack for pushing temp variables in certain situations,
there is no easy way around this situation unfortunately.&lt;/p&gt;

&lt;p&gt;
This method works as I&amp;#39;ve recently used it. I make no claim as to
the fitness or safety the code is. IE it&amp;#39;s as is and for your
aggrandizement only. It is extremely dangerous to do anything with
the stack, I don&amp;#39;t recommend you do anything with SP, other than to
get a copy (as the above code does).&lt;/p&gt;

&lt;p&gt;
Also note this is not identical to the original code for various
reasons not the least of which is clarity.&lt;/p&gt;

&lt;p&gt;
Stephen&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>