name of the calling function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vsachar
    New Member
    • Mar 2009
    • 15

    #1

    name of the calling function

    When executing a C++ program, How can I find out the name of the function that is currently being executed. If assume my program main is calling a function f1() and that in turn is calling f2(). Then at any given time how can I find out which function has completed execution.
    i am creating an automatic garbage collector and I need to know where the program control is during collection phase so that the memory of the functions that have finished execution is collected.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    When a function has finished execution its stack frame is lost and possibly reused by other functions, i.e. you are too late then; you have to invoke your garbage collector just before that stack frame is lost. The layout of those frames is highly implementation dependent. Check Hans Boems garbage collector and see through what hoops it has to go in order to find pointers to reclaimable memory.

    kind regards,

    Jos

    Comment

    • vainstah
      New Member
      • Mar 2008
      • 8

      #3
      Theoritically :
      What you are talking about is the functionality of code a profiler. Basically a compiler should have the ability to instrument your functions so that some code is executed at the entry and exit of your function.

      In practise :
      The above paragraph talks about what is available in dynamic languages. From experience In the systems languages world such support is obtuse and hard to find (intel compilers do it I think, MS compilers can dump out timing information).

      Method I use(and have seen in complex software):
      I stick function prolog and epilogue macros in my code. These macros execute code which (a) captures timing information (b) inserts a blanket try catch block for every function in order to locate exception sources in code.

      Now you could take a similar approach. At the start of every function write the function name to a std C++ stack object (as a global of course) and pop it at the end. Also write the stack out at each function entry.

      Conclusion:
      There are a hundred different ways of doing this and serious software will include instrumentation like this.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by vainstah
        Conclusion:
        There are a hundred different ways of doing this and serious software will include instrumentation like this.
        This is a simple trace log usually implemented using a circular file holding enough entries for a crash autopsy. This is done all the time in production code.

        Trace entries also track values of the incoming and outgoing variables plus any other pertinant information (like the calling function name). Just stick the name where the tracer can find it.

        Trace functions can have the ability to read a parameter file so that incoming values can be altered arbitrarily to assist in a debug.

        The trace functions are equipped with a monitor that checks if the trace is on. If not, the trace function does nothing. This avoids slowdowns when crashes are not being autopsied.

        Comment

        • vsachar
          New Member
          • Mar 2009
          • 15

          #5
          Actually what i also want to know is how can a function know which function called it?
          void main()
          {
          add();
          }
          in the above example how will add know, who called it?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You would need to set this up in your own code.

            Maybe:
            Code:
            enum Function {main, add, none,etc...)
            
            int main()
            {
                Function = main;
                add();
            }
            
            void add()
            {
               if (Function == main)
               {
                  // it was main 
              }
            }
            I don't know why you would want to do this unless you are debugging and if that's the case you should just use your debugger to look at the call stack.

            Comment

            Working...