Dominique
Dominique

Reputation: 17493

Is it possible to find the stack depth of a C function, using Visual Studio 2013 Express for Windows?

Good afternoon,
I am developping a program in C, where (for debugging purposes) I am regularly adding lines like:

printf("[%s]\n", __FUNCTION__);

I would like to preceed these comment lines by an amount of spaces (or dots), being the stack depth of my function, so that I can easily determine which function is calling which other function.

I am working with Visual Studio 2013 Express for Windows, on a Windows-7 system.
As far as I know, I'm not working with a gcc compiler.

Is this even possible?
Thanks

Upvotes: 2

Views: 82

Answers (2)

Dominique
Dominique

Reputation: 17493

Thanks a lot, @SergeyA, I have followed the guidelines in your URL (https://msdn.microsoft.com/en-us/library/windows/desktop/bb204633(v=vs.85).aspx) and I have now come up with following solution (edited after Armali's comment):

#include "DbgHelp.h"

void          *stack[ 100 ];
unsigned short frames;
HANDLE         process;

process = GetCurrentProcess();
SymInitialize( process, NULL, TRUE );
frames = CaptureStackBackTrace( 0, 100, stack, NULL );

printf("%*c[%s]\n", frames+1, ' ', __FUNCTION__);

(In case somebody finds a quicker/easier way for adding leading blanks, don't hesitate to react.)

Upvotes: 1

Armali
Armali

Reputation: 19375

In case somebody finds a quicker/easier way for adding trailing blanks, don't hesitate to react.

You mean leading blanks. You can use a field width argument with *:

printf("%*c[%s]\n", frames+1, ' ', __FUNCTION__);

Upvotes: 1

Related Questions