none
how to get function call stack

    Question

  • Hello everyone,


    I am using C/C++ on Windows. I am wondering how to get the function call stack from my application.

    For example,

    if function foo call function goo, then function goo call function zoo, in function goo, I want to output the function call stack (foo --> goo --> zoo), and in goo, output the function call stack (foo --> goo).

    Are there any samples of how to implement that? Does Windows runtime support this feature?


    thanks in advance,
    George
    Friday, November 03, 2006 5:56 AM

Answers

  • If it's for managed code you can use the classes  in System.Diagnostics. If it's for unmanaged code, this article should be of some help
     http://www.codeproject.com/tools/minidump.asp


    Regards
      Sahir

    Friday, November 03, 2006 9:00 AM
  • Are you looking for code in C++ or searching for how to using call stack window while debugging?


    You can make use of __FUNCTION__ macro for getting undecorated name name of function.

    Put it inside the function with cout or printf

    Check it here
    Friday, November 03, 2006 9:12 AM
  •  George2 wrote:
    Hello everyone,

    if function foo call function goo, then function goo call function zoo, in function goo, I want to output the function call stack (foo --> goo --> zoo), and in goo, output the function call stack (foo --> goo).

    There's no "quick" way to do this. You will have to walk the stack, and output symbol information along the way. This can be done by use of StackWalk64 function of the debug helper api. There's a codeguru article involving StackWalk64 at http://www.codeguru.com/cpp/w-p/system/threading/article.php/c10317/, which may prove helpful.

    Note that you are likely to run into portability issues no matter how you go about this. While the stack walking may be especially hard to include in an app meant to be portable, using the _ReturnAddress intrinsic, combined with symbol lookup from the debug helper api, may be easier. See http://einaros.livejournal.com/#einaros923 for an example of the intrinsic, but note that I've not included any symbol lookup there. SymGetSymFromAddr64 makes it relatively easy to resolve the symbol name aswell, though.

    Sunday, November 05, 2006 12:02 PM
    Moderator

All replies

  • If it's for managed code you can use the classes  in System.Diagnostics. If it's for unmanaged code, this article should be of some help
     http://www.codeproject.com/tools/minidump.asp


    Regards
      Sahir

    Friday, November 03, 2006 9:00 AM
  • Are you looking for code in C++ or searching for how to using call stack window while debugging?


    You can make use of __FUNCTION__ macro for getting undecorated name name of function.

    Put it inside the function with cout or printf

    Check it here
    Friday, November 03, 2006 9:12 AM
  • hI Sahir,

     

     

     Sahir Shah wrote:
    If it's for managed code you can use the classes  in System.Diagnostics. If it's for unmanaged code, this article should be of some help
     http://www.codeproject.com/tools/minidump.asp


    Regards
      Sahir

     

    The tutorial seems what I am looking for. But I am using pure C in Visual Studio, I am wondering whether the techniques in this toturial could be applied in my C program?

     

     

    regards,

    George

    Sunday, November 05, 2006 8:47 AM
  • Hi Sarath,

     

     

     Sarath. wrote:
    Are you looking for code in C++ or searching for how to using call stack window while debugging?


    You can make use of __FUNCTION__ macro for getting undecorated name name of function.

    Put it inside the function with cout or printf

    Check it here

     

    Other than knowing the current function name, I want to know which function calls current function in some certain situations. For example, when function a calls functions b, and function b calls function c, in function c, I want to get the information like, a-->b-->c -- other than only the function name c.

    Any ideas?

     

     

    regards,

    George

    Sunday, November 05, 2006 8:53 AM
  •  George2 wrote:
    Hello everyone,

    if function foo call function goo, then function goo call function zoo, in function goo, I want to output the function call stack (foo --> goo --> zoo), and in goo, output the function call stack (foo --> goo).

    There's no "quick" way to do this. You will have to walk the stack, and output symbol information along the way. This can be done by use of StackWalk64 function of the debug helper api. There's a codeguru article involving StackWalk64 at http://www.codeguru.com/cpp/w-p/system/threading/article.php/c10317/, which may prove helpful.

    Note that you are likely to run into portability issues no matter how you go about this. While the stack walking may be especially hard to include in an app meant to be portable, using the _ReturnAddress intrinsic, combined with symbol lookup from the debug helper api, may be easier. See http://einaros.livejournal.com/#einaros923 for an example of the intrinsic, but note that I've not included any symbol lookup there. SymGetSymFromAddr64 makes it relatively easy to resolve the symbol name aswell, though.

    Sunday, November 05, 2006 12:02 PM
    Moderator
  •  George2 wrote:
      But I am using pure C in Visual Studio, I am wondering whether the techniques in this toturial could be applied in my C program ?

    Yes it can be used in a pure C program.

    Regards

      Sahir

    Sunday, November 05, 2006 2:08 PM
  • Hi Sahir,

     

     

     Sahir Shah wrote:

     George2 wrote:
      But I am using pure C in Visual Studio, I am wondering whether the techniques in this toturial could be applied in my C program ?

    Yes it can be used in a pure C program.

    Regards

      Sahir

     

    The issue is not whether it could be used in C program, but I need to modify every function in my project to manually record the function name. Are there any automatic ways?

     

     

    regards,

    George

    Tuesday, November 07, 2006 1:17 PM
  •  einaros wrote:

     George2 wrote:
    Hello everyone,

    if function foo call function goo, then function goo call function zoo, in function goo, I want to output the function call stack (foo --> goo --> zoo), and in goo, output the function call stack (foo --> goo).

    There's no "quick" way to do this. You will have to walk the stack, and output symbol information along the way. This can be done by use of StackWalk64 function of the debug helper api. There's a codeguru article involving StackWalk64 at http://www.codeguru.com/cpp/w-p/system/threading/article.php/c10317/, which may prove helpful.

    Note that you are likely to run into portability issues no matter how you go about this. While the stack walking may be especially hard to include in an app meant to be portable, using the _ReturnAddress intrinsic, combined with symbol lookup from the debug helper api, may be easier. See http://einaros.livejournal.com/#einaros923 for an example of the intrinsic, but note that I've not included any symbol lookup there. SymGetSymFromAddr64 makes it relatively easy to resolve the symbol name aswell, though.

     

    They are very good reference. Thank you einaros!

     

     

    regards,

    George

    Tuesday, November 07, 2006 1:20 PM
  • I've also made a couple of related additions to my blog, if you should wish to consider other options. Combining the two posts I'm about to link would allow you to at least see which function called the function you are currently "residing" in.

    See http://einaros.livejournal.com/#einaros923 and http://einaros.livejournal.com/#einaros3287. If you've got any questions, comments, or the posts simply make no sense to you; be sure to give me a tell.

    Tuesday, November 07, 2006 1:28 PM
    Moderator