473,573 Members | 3,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 programmaticall y.

Apr 25 '06 #1
11 2490
se**********@ya hoo.com wrote:

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

No, not in standard C++.

--
Ian Collins.
Apr 25 '06 #2
* se**********@ya hoo.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 programmaticall y.


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 theLevelVariabl e = 0;
return theLevelVariabl e;
}

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::lev el()
<< 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**********@ya hoo.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 programmaticall y.


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.g r/codequality/stack.gif?clcpp >. This technique
is not portable, but will work on most computer architectures.
Apr 25 '06 #4
Diomidis Spinellis wrote:
se**********@ya hoo.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.g r/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**********@ya hoo.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.g r/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.g r/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**********@ya hoo.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.g r/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**********@ya hoo.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 programmaticall y.

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.g r/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

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

Similar topics

0
1300
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 FileInfo(ImageFilePath.PostedFile.FileName).Length is also quit easy. However, obtaining the Height and Width in Pixels is difficult. I need this information to...
1
5940
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 structure has callstack information but I thought there must be a way to directly retrieve it. Thanks!
2
7260
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 source line number for each frame. This works fine for unmanaged code, but when we have a call stack that goes into or through some managed code, we...
6
6786
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. Nick.
11
4047
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 binary format. But what "server()" and "issuer()" return are strings, with the pairs separated by "/". The documentation at...
8
1728
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 find any documentation online with regards to it's methods. Also, as I am learning, it would be nice to be able to know what methods are available...
3
2785
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 themselves at a certain point in time. This code works fine for normal objects. However, my issue occurs if at any point, an event object is...
3
1581
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? I assume there are some framework classes, that can help me on this task but I have no idea which ones that are. Thanks for hints and help on this...
5
2148
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 called the function currently being executed. Is this thread-safe, or are there other alternatives (I'm pretty new to reflection; can it do that)? ...
0
7741
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7661
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7977
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6347
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3692
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2163
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1256
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
984
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.