473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

obtaining callstack level

Is there a way to get the callstack level in c++? for example, take
the following code:

void call3() {
//callstack level would be 3
}

void call2() {
//callstack level would be 2
call3();
}

int main() {
//callstack level would be 1
call2();
return 0;
}

And I don't mean obtaining it from using a debugger and looking at the
callstack. I'm mean obtaining it programmatically.

Apr 25 '06 #1
11 2472
se**********@yahoo.com wrote:

And I don't mean obtaining it from using a debugger and looking at the
callstack. I'm mean obtaining it programmatically.

No, not in standard C++.

--
Ian Collins.
Apr 25 '06 #2
* se**********@yahoo.com:
Is there a way to get the callstack level in c++? for example, take
the following code:

void call3() {
//callstack level would be 3
}

void call2() {
//callstack level would be 2
call3();
}

int main() {
//callstack level would be 1
call2();
return 0;
}

And I don't mean obtaining it from using a debugger and looking at the
callstack. I'm mean obtaining it programmatically.


Instrument the functions, that is, add code that maintains a call stack
level count.

That can easily be done via the construction and destruction of a local
object in each function.

For example, off the cuff,

class CallTracer
{
private:
static std::size_t& theLevel()
{
static std::size_t theLevelVariable = 0;
return theLevelVariable;
}

public:
CallTracer(){ ++theLevel(); }
~CallTracer(){ --theLevel(); }
static std::size_t level() { return theLevel(); }
};

which you'd use like

void foo3()
{
CallTracer tracer;
std::cout
<< "foo3, at call level " << CallTracer::level()
<< std::endl;
}

This counts the calls of functions instrumented this way; if that's not
enough, use a decent debugger.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 25 '06 #3
se**********@yahoo.com wrote:
Is there a way to get the callstack level in c++? for example, take
the following code:
[...]
And I don't mean obtaining it from using a debugger and looking at the
callstack. I'm mean obtaining it programmatically.


If you're not interested in a precise number, but can accept a measure
that would indicate the stack depth in bytes you can obtain the address
of one of the function's plain local variables through the & operator
and cast it to a char * pointer. I've used this technique to track the
use of stack space during the runtime of a program; see the snapshots at
<http://www.spinellis.gr/codequality/stack.gif?clcpp>. This technique
is not portable, but will work on most computer architectures.
Apr 25 '06 #4
Diomidis Spinellis wrote:
se**********@yahoo.com wrote:
Is there a way to get the callstack level in c++? for example, take
the following code:
If you're not interested in a precise number, but can accept a measure
that would indicate the stack depth in bytes you can obtain the address
of one of the function's plain local variables through the & operator
and cast it to a char * pointer. I've used this technique to track the
use of stack space during the runtime of a program; see the snapshots at
<http://www.spinellis.gr/codequality/stack.gif?clcpp>. This technique
is not portable, but will work on most computer architectures.


The Itanium of course being one of those where it doesn't work; it has
two
stacks. C++ doesn't care; you can still implement Standard C++ on it.
Yet such features explain why Standard C++ doesn't have "a" callstack
level; it doesn't really make sense.

HTH,
Michiel Salters

Apr 25 '06 #5
* Mi*************@tomtom.com:
Diomidis Spinellis wrote:
se**********@yahoo.com wrote:
Is there a way to get the callstack level in c++? for example, take
the following code:

If you're not interested in a precise number, but can accept a measure
that would indicate the stack depth in bytes you can obtain the address
of one of the function's plain local variables through the & operator
and cast it to a char * pointer. I've used this technique to track the
use of stack space during the runtime of a program; see the snapshots at
<http://www.spinellis.gr/codequality/stack.gif?clcpp>. This technique
is not portable, but will work on most computer architectures.


The Itanium of course being one of those where it doesn't work; it has
two
stacks. C++ doesn't care; you can still implement Standard C++ on it.
Yet such features explain why Standard C++ doesn't have "a" callstack
level; it doesn't really make sense.


Although a bit off-topic, would you care to elaborate? It's a long time
since I did any assembly level programming, but the only case of /two/
stacks I'm familiar with is the normal call stack versus system
exception handling stack (as on e.g. MC68K). I can't see how ordinary
function execution could utilize two different stacks, except if the
Itanium has one stack for return addresses and another for arguments?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 25 '06 #6
Mi*************@tomtom.com wrote:
Diomidis Spinellis wrote:
se**********@yahoo.com wrote:
Is there a way to get the callstack level in c++? for example, take
the following code:


If you're not interested in a precise number, but can accept a measure
that would indicate the stack depth in bytes you can obtain the address
of one of the function's plain local variables through the & operator
and cast it to a char * pointer. I've used this technique to track the
use of stack space during the runtime of a program; see the snapshots at
<http://www.spinellis.gr/codequality/stack.gif?clcpp>. This technique
is not portable, but will work on most computer architectures.

The Itanium of course being one of those where it doesn't work; it has
two
stacks. C++ doesn't care; you can still implement Standard C++ on it.
Yet such features explain why Standard C++ doesn't have "a" callstack
level; it doesn't really make sense.

That's assuming the Itanium user is subscribed to this group :)

--
Ian Collins.
Apr 25 '06 #7
Alf P. Steinbach wrote:
* Mi*************@tomtom.com:
Diomidis Spinellis wrote:
se**********@yahoo.com wrote:
Is there a way to get the callstack level in c++? for example, take
the following code:

If you're not interested in a precise number, but can accept a measure
that would indicate the stack depth in bytes you can obtain the address
of one of the function's plain local variables through the & operator
and cast it to a char * pointer. I've used this technique to track the
use of stack space during the runtime of a program; see the snapshots at
<http://www.spinellis.gr/codequality/stack.gif?clcpp>. This technique
is not portable, but will work on most computer architectures.


The Itanium of course being one of those where it doesn't work; it has
two
stacks. C++ doesn't care; you can still implement Standard C++ on it.
Yet such features explain why Standard C++ doesn't have "a" callstack
level; it doesn't really make sense.


Although a bit off-topic, would you care to elaborate? It's a long time


My understanding is that the Itanium has SPARC-like register windows
backed by a hardware assisted so-called register stack engine (RSE).
When a function calls another arguments are placed into the general
purpose registers Gr32-Gr128. Through an alloc instruction the called
function specifies the layout of its stack frame (input registers, local
registers, and output registers). This has the effect of mapping the
calling function's output registers to the called function's input
registers, and thus passing the function arguments through the registers
without any overhead. The registers get renamed at the point of the
call, so that all functions will always see their registers starting at
Gr32. When the 96 general purpose registers used for implementing the
scheme run out, a spill will burst-write registers to the special
register stack; later when the stack is unwound a fill will read the
contents back from memory.

Why are two stacks needed? My guess is that it was simpler to optimize
the RSE hardware (for example caching or the burst mode alignment
requirements) if the return addresses and the normal stack push/pop
instructions didn't interfere with the register spill stack.

You can find a few more details in this article:
http://www.intel.com/cd/ids/develope...314.htm?page=1

--
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality?clcpp
Apr 25 '06 #8
Diomidis Spinellis wrote:
se**********@yahoo.com wrote:
Is there a way to get the callstack level in c++? for example, take
the following code:

[...]
And I don't mean obtaining it from using a debugger and looking at the
callstack. I'm mean obtaining it programmatically.

If you're not interested in a precise number, but can accept a measure
that would indicate the stack depth in bytes you can obtain the address
of one of the function's plain local variables through the & operator
and cast it to a char * pointer. I've used this technique to track the
use of stack space during the runtime of a program; see the snapshots at
<http://www.spinellis.gr/codequality/stack.gif?clcpp>. This technique
is not portable, but will work on most computer architectures.


Do you get artificially high values?

I was thinking of the case where the compiler could use registers for
local variables. Until you start taking their address which would
defeat that optimisation.

--
Ian Collins.
Apr 25 '06 #9
* Diomidis Spinellis:

You can find a few more details in this article:
http://www.intel.com/cd/ids/develope...314.htm?page=1


Thanks. It's, well, perplexing. They have cached the stack, but turned
the conceptual picture 180 degrees around so that it seems main memory
is used to relieve what actually is the cache, and instead of making
this transparent they require direct support from the machine code, and
therefore (one must presume) limited the mechanism to 64-bit code.

Hm.

Hopefully it won't influence ordinary C++ programming, but I saw nothing
in that article that discussed how the architecture meshed with common
C++ techniques such as using a pointer or reference to a local variable.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 25 '06 #10
Ian Collins wrote:
Diomidis Spinellis wrote:
se**********@yahoo.com wrote:
Is there a way to get the callstack level in c++? for example, take
the following code:


[...]
And I don't mean obtaining it from using a debugger and looking at the
callstack. I'm mean obtaining it programmatically.


If you're not interested in a precise number, but can accept a measure
that would indicate the stack depth in bytes you can obtain the address
of one of the function's plain local variables through the & operator
and cast it to a char * pointer. I've used this technique to track the
use of stack space during the runtime of a program; see the snapshots at
<http://www.spinellis.gr/codequality/stack.gif?clcpp>. This technique
is not portable, but will work on most computer architectures.


Do you get artificially high values?

I was thinking of the case where the compiler could use registers for
local variables. Until you start taking their address which would
defeat that optimisation.


I assume by "high values" you mean high memory addresses, i.e. low stack
usage values. At most the measure will be off by the number of the
processor's registers, because even when the compiler allocates local
variables to registers these have to be stored on the stack when another
function gets called. In the particular case I wanted to profile the
stack's growth across millions of function invocations; an offset of a
few tens of bytes would hardly matter. Function inlining is also
affecting the results (I used a modified call graph profiler function
prologue to write the values to a file, so inlined functions weren't
tracked at all.)

--
Diomidis
Apr 26 '06 #11
Diomidis Spinellis wrote:
Ian Collins wrote:
Diomidis Spinellis wrote:
se**********@yahoo.com wrote:

Is there a way to get the callstack level in c++? for example, take
the following code:
[...]

And I don't mean obtaining it from using a debugger and looking at the
callstack. I'm mean obtaining it programmatically.
If you're not interested in a precise number, but can accept a measure
that would indicate the stack depth in bytes you can obtain the address
of one of the function's plain local variables through the & operator
and cast it to a char * pointer. I've used this technique to track the
use of stack space during the runtime of a program; see the snapshots at
<http://www.spinellis.gr/codequality/stack.gif?clcpp>. This technique
is not portable, but will work on most computer architectures.

Do you get artificially high values?

I was thinking of the case where the compiler could use registers for
local variables. Until you start taking their address which would
defeat that optimisation.

I assume by "high values" you mean high memory addresses, i.e. low stack
usage values. At most the measure will be off by the number of the
processor's registers, because even when the compiler allocates local
variables to registers these have to be stored on the stack when another
function gets called. In the particular case I wanted to profile the
stack's growth across millions of function invocations; an offset of a
few tens of bytes would hardly matter. Function inlining is also
affecting the results (I used a modified call graph profiler function
prologue to write the values to a file, so inlined functions weren't
tracked at all.)

That's a good point, I forgot about calling other functions.

The only place this would be a problem would be on a Sparc like
processor that uses register wheels to pass parameters.

I've used similar techniques to this, along with high water marks.

--
Ian Collins.
Apr 26 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lennart Hoglund | last post by:
Uploading an Image to a Server using the method described in the Knowledge Base article Q315832, works fine and smoothly. Obtaining the Image Size using; ImageSize.Text = New...
1
by: Zeng | last post by:
Hello, In my code, when I run into an error, I would like to dump the callstack into error log, how do I do that w/o throwing an exception then catching it just to get the callstack? Exception...
2
by: asanford | last post by:
We use StackWalk(StackWalk64) from dbghelp.dll to walk our callstacksas needed, using the various Sym* methods (SymGetSymFromAddr, SymGetLineFromAddr) to resolve source file, function name, and...
6
by: Nak | last post by:
Hi there, Is it possible to retrieve the callstack at runtime in a VB.NET application? I wish the verify that specific methods have been called as a security measure. Thanks in advance. ...
11
by: John Nagle | last post by:
The Python SSL object offers two methods from obtaining the info from an SSL certificate, "server()" and "issuer()". The actual values in the certificate are a series of name/value pairs in ASN.1...
8
by: Daz | last post by:
Hi everyone. I just wanted to know if there was any way to use script to display a list of methods linked to a particular object? The object I have in mind is getBrowser(), and I can't seem to...
3
by: phocis | last post by:
I wrote a pair of functions to enable scoped or referenced setTimeout calls. I did this because I have an object factory that creates multiple objects and those objects need to delay a few calls on...
3
by: Rainer Queck | last post by:
Hi NG, currently I am working on a add-in which shell read the callstack and write it to a file. The question is, how can I read the current callstack of a debugged application with a add-in?...
5
by: Agrona | last post by:
I found this thread: http://www.thescripts.com/forum/thread233505.html only marginally helpful. It seems that going through the callstack is the only way to determine which function may have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.