473,408 Members | 1,980 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,408 software developers and data experts.

Standard Method for Stack Trace In 'C'

Is there any portable method for identifying the caller of a function, its
caller, and so on, at runtime, without using a debugger?

Clearly, one can dissect the stack frame and trace it back. But this method
isn't very portable (it requires a knowledge of the machine).

Is there any method supported by the language or by standards? (Maybe,
similar to the way variable-length argument lists are handled?)
Jan 6 '07 #1
7 2064
"David T. Ashley" <dt*@e3ft.comwrites:
Is there any portable method for identifying the caller of a function, its
caller, and so on, at runtime, without using a debugger?
No.

If you're using GCC, then there's a GCC-specific extension to
help with this. See the section in the GCC manual titled
"Getting the Return or Frame Address of a Function" for more
information.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Jan 6 '07 #2
"Ben Pfaff" <bl*@cs.stanford.eduwrote in message
news:87************@blp.benpfaff.org...
>
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Ben, just out of curiousity, what practice was McConnell referring to?

Thanks.
Jan 6 '07 #3
"David T. Ashley" <dt*@e3ft.comwrites:
"Ben Pfaff" <bl*@cs.stanford.eduwrote in message
news:87************@blp.benpfaff.org...
>>
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell

Ben, just out of curiousity, what practice was McConnell referring to?
I couldn't say without rereading _Code Complete_ in its entirety
(not that that is a bad idea).
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Jan 6 '07 #4
Ben Pfaff wrote:
"David T. Ashley" <dt*@e3ft.comwrites:
>"Ben Pfaff" <bl*@cs.stanford.eduwrote in message
news:87************@blp.benpfaff.org...
>>"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Ben, just out of curiousity, what practice was McConnell referring to?

I couldn't say without rereading _Code Complete_ in its entirety
(not that that is a bad idea).
From Page 309 of the second edition:

Use Named Constants Consistently. It's dangerous to use a named
constant in one place and a literal in another to represent the same entity.
Jan 6 '07 #5
On Jan 6, 2:03 am, "David T. Ashley" <d...@e3ft.comwrote:
Is there any portable method for identifying the caller of a function, its
caller, and so on, at runtime, without using a debugger?
Unfortunately, no.
Clearly, one can dissect the stack frame and trace it back. But this method
isn't very portable (it requires a knowledge of the machine).
<OT>Not only that, it's not as clear as one might think on some
machines (the foo PA-RISC comes to mind). x86 and SPARC are pretty
straight forward though, you don't need a lot of machine specific
knowledge.</OT>
Is there any method supported by the language or by standards? (Maybe,
similar to the way variable-length argument lists are handled?)
<OT>As far as I know, there isn't even a POSIX way of doing this, so
consider yourself lucky if e.g. dladdr() is implemented on a specific
machine.</OT>
--
WYCIWYG - what you C is what you get

Jan 6 '07 #6
Groovy hepcat David T. Ashley was jivin' on Fri, 5 Jan 2007 20:03:21
-0500 in comp.lang.c.
Standard Method for Stack Trace In 'C''s a cool scene! Dig it!
>Is there any portable method for identifying the caller of a function, its
caller, and so on, at runtime, without using a debugger?

Clearly, one can dissect the stack frame and trace it back. But this method
isn't very portable (it requires a knowledge of the machine).
Implement your own stack containing the names of called functions.
When you enter a function, push its name on the stack. When you exit a
function, pop the name off the top of the stack. It could get messy if
your functions have many exit points, though.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Jan 8 '07 #7
David T. Ashley wrote:
Is there any portable method for identifying the caller of a function, its
caller, and so on, at runtime, without using a debugger?
Yes: pass the arguments in yourself.
Is there any method supported by the language or by standards? (Maybe,
similar to the way variable-length argument lists are handled?)
No.

(Thank the gods: it would kill one of the single most useful
optimisations in the world. IMAO.)

--
Chris "yes, I hate Java for that as well" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/

Jan 8 '07 #8

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

Similar topics

1
by: Mark Miller | last post by:
I just recently started getting the above error on a page I am posting MULTIPART/FORM-DATA. We have SoftArtisans FileUp component and Filter installed on the server in question and up until a day...
3
by: Jeremy | last post by:
While working with ASP.NET I've sometimes encountered errors in my applications, but I've always known exactly why I'm getting the error and have been able to fix it. So I've never needed to...
1
by: Phil Sandler | last post by:
Hello all, Thanks in advance for any help with this. I am trying to get sample code to run from Michael Russell's excellent article on removing whitespace:...
3
by: Nirbho | last post by:
Hi, I'm newish to C# .Net. I'm doing some error handling in my ASP.net pages. When handling the error in the catch block, I want to pass through the namespace of where the error occured.....
1
by: Modica82 | last post by:
I have created a contract first web service, and to test i have created a client (asp.net) to attempt to test that my web service is at least communication. But when i call a method i get the...
2
by: Lasse Vågsæther Karlsen | last post by:
If I got the following code: try { // something that might throw an exception } catch (Exception ex) { // Log contents of ex here throw;
3
by: barker7 | last post by:
I am calling an unmanaged C++ method from C#. When an exception occurs in the C++ code, it gets caught in our C# code. CSharpMethod() { try { NativeCPPMethod(); } catch (Exception e)
3
by: Fred | last post by:
Hello, I get this strange error in the code of a very simple aspx page just called to download a file. Do you have any idea about it ? (The same code works fine on another server) Server...
3
by: Paul McGuire | last post by:
Is there any way to hide portions of an exception stack trace? When users get exceptions when using pyparsing, there are usually many layers of pyparsing-internal stack messages that are not at...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.