473,668 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting call stack (like dbx where cmd)

Is there a way (understandably non-portable) to get the
call stack from within a function? That is, assuming the
application has been compiled with symbols, get the list
of calling function names (similar to dbx "where" command").

I am working with IBM compiler and AIX but any other
OS/compiler solution would be interesting as well.

To answer the inevitable question why I want this,
I am writing a simple profiler for my library. However,
within the library I want to distinguish which part of
the application I was called from....

Thanks for any ideas or pointers,
Adrian
Nov 14 '05 #1
5 3171
Adrian <no******@null. net> writes:
Is there a way (understandably non-portable) to get the
call stack from within a function?
Sure: if dbx can do it, so can you.
To answer the inevitable question why I want this,
I am writing a simple profiler for my library. However,
within the library I want to distinguish which part of
the application I was called from....
Why not pass a parameter?
Thanks for any ideas or pointers,


Look for POWERPC parts here:
http://cvs.sourceforge.net/viewcvs.p...ol/src/stack.c

This is off-topic in comp.lang.c (I think).
Followup-to comp.unix.aix

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Nov 14 '05 #2
Adrian wrote:

Is there a way (understandably non-portable) to get the
call stack from within a function? That is, assuming the
application has been compiled with symbols, get the list
of calling function names (similar to dbx "where" command").

I am working with IBM compiler and AIX but any other
OS/compiler solution would be interesting as well.

To answer the inevitable question why I want this,
I am writing a simple profiler for my library. However,
within the library I want to distinguish which part of
the application I was called from....


Since you know the CPU things are running on, it should not be hard
to write a couple of callable assembly language routines to return
those values. Something like:

void *getbp(void);
void *getsp(void);

They might even be implemented as inline, since they should be very
short, and that will avoid complications of digging into the
stack. getbp would probable resolve to "mov ax,bp".

Implementation is OT here, however how they should be integrated
into a C program is not.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #3

"Adrian" <no******@null. net> wrote in message
news:10******** *****@news.supe rnews.com...
I am working with IBM compiler and AIX but any other
OS/compiler solution would be interesting as well.


Well, since this completely OS/Architecture specific you'll generally have
to search newsgroups for solutions for the specific platforms you're looking
for. Anyways, for AIX it'll look something like this:

void FaultHandler(in t iSignal, siginfo_t *ptSigInfo, void *pvSomething)
{
DumpCallStack(1 , (ucontext_t *) pvSomething);
}

void DumpCallStack(i nt iFd, ucontext_t *ptContext)

void *pvInstructionP ointer;
void **ppvStackFrame ;
int iFrame = 0;

/* The standard stack layout for a process on the PowerPC architecture
(AIX ABI) when a() has called b() which called c() looks
like the following:

--------------------------------
-- | Saved Stack Pointer | <-- Stack pointer (%R1/SP)
| | Saved Control Register |
| | Saved Location Register | Not used until call out of
C()
| --------------------------------
| | |
| | Local Variables for c() |
| | |
| --------------------------------
-> | Saved Stack Pointer | --
| Saved Control Register | |
| Saved Location Register | | Saved by c() containing
location
-------------------------------- | or call from b()
| | |
| Local Variables for b() | |
| | |
-------------------------------- |
| Saved Stack Pointer | <-
| Saved Control Register |
| Saved Location Register | Saved by b() containing
location
-------------------------------- or call from a()
| |
| Local Variables for a() |
| |
--------------------------------

Refer to the PowerPC Compiler Writer's Guide or the AIX Assembly
Language
Reference for more information */
sprintf(sString , "Faulted while executing instruction at 0x%08x\n"
"\n"
"Traceback: \n",
ptContext->uc_mcontext.jm p_context.iar);
write(iFd, sString, strlen(sString) );

/* Roll down the stack frames */
pvInstructionPo inter = (void *) ptContext->uc_mcontext.jm p_context.iar;
ppvStackFrame = (void **) ptContext->uc_mcontext.jm p_context.gpr[1];
do
{
sprintf(sString , "(%2d) 0x%p\n", iFrame, pvInstructionPo inter);
write(iFd, sString, strlen(sString) );

/* Have we hit the bottom of the stack? */
if (!ppvStackFrame[0])
break;

/* Validate the stack frame before using any portion of it */
if (!ValidAddress( (void *) ppvStackFrame) ||
!ValidAddress(( void *) ppvStackFrame[0]) ||
!ValidAddress(( void *) ((void **) ppvStackFrame[0] + 2)))
{
sprintf(sString , "Invalid frame at %p\n", (void *) ppvStackFrame);
write(iFd, sString, strlen(sString) );
break;
}

ppvStackFrame = (void **) ppvStackFrame[0];
pvInstructionPo inter = ppvStackFrame[2];
iFrame++;
} while(ppvStackF rame);

Cheers,
Shaun
Nov 14 '05 #4
"Shaun Clowes" <de****@no.spam .for.me.progsoc .org> wrote in message news:<ET******* *******@nnrp1.o zemail.com.au>. ..
"Adrian" <no******@null. net> wrote in message
news:10******** *****@news.supe rnews.com...
I am working with IBM compiler and AIX but any other
OS/compiler solution would be interesting as well.


Well, since this completely OS/Architecture specific you'll generally have
to search newsgroups for solutions for the specific platforms you're looking
for. Anyways, for AIX it'll look something like this:

void FaultHandler(in t iSignal, siginfo_t *ptSigInfo, void *pvSomething)
{
DumpCallStack(1 , (ucontext_t *) pvSomething);
}

void DumpCallStack(i nt iFd, ucontext_t *ptContext)

void *pvInstructionP ointer;
void **ppvStackFrame ;
int iFrame = 0;

/* The standard stack layout for a process on the PowerPC architecture
(AIX ABI) when a() has called b() which called c() looks
like the following:

--------------------------------
-- | Saved Stack Pointer | <-- Stack pointer (%R1/SP)
| | Saved Control Register |
| | Saved Location Register | Not used until call out of
C()
| --------------------------------
| | |
| | Local Variables for c() |
| | |
| --------------------------------
-> | Saved Stack Pointer | --
| Saved Control Register | |
| Saved Location Register | | Saved by c() containing
location
-------------------------------- | or call from b()
| | |
| Local Variables for b() | |
| | |
-------------------------------- |
| Saved Stack Pointer | <-
| Saved Control Register |
| Saved Location Register | Saved by b() containing
location
-------------------------------- or call from a()
| |
| Local Variables for a() |
| |
--------------------------------

Refer to the PowerPC Compiler Writer's Guide or the AIX Assembly
Language
Reference for more information */
sprintf(sString , "Faulted while executing instruction at 0x%08x\n"
"\n"
"Traceback: \n",
ptContext->uc_mcontext.jm p_context.iar);
write(iFd, sString, strlen(sString) );

/* Roll down the stack frames */
pvInstructionPo inter = (void *) ptContext->uc_mcontext.jm p_context.iar;
ppvStackFrame = (void **) ptContext->uc_mcontext.jm p_context.gpr[1];
do
{
sprintf(sString , "(%2d) 0x%p\n", iFrame, pvInstructionPo inter);
write(iFd, sString, strlen(sString) );

/* Have we hit the bottom of the stack? */
if (!ppvStackFrame[0])
break;

/* Validate the stack frame before using any portion of it */
if (!ValidAddress( (void *) ppvStackFrame) ||
!ValidAddress(( void *) ppvStackFrame[0]) ||
!ValidAddress(( void *) ((void **) ppvStackFrame[0] + 2)))
{
sprintf(sString , "Invalid frame at %p\n", (void *) ppvStackFrame);
write(iFd, sString, strlen(sString) );
break;
}

ppvStackFrame = (void **) ppvStackFrame[0];
pvInstructionPo inter = ppvStackFrame[2];
iFrame++;
} while(ppvStackF rame);

Cheers,
Shaun

Thanks for the tips Shaun, however you didn't define the
function "ValidAddress() ".

-tony
Nov 14 '05 #5

"T.R.Bennet t" <be**********@c nf.com> wrote in message
news:20******** *************** ***@posting.goo gle.com...
"Shaun Clowes" <de****@no.spam .for.me.progsoc .org> wrote in message news:<ET******* *******@nnrp1.o zemail.com.au>. ..
"Adrian" <no******@null. net> wrote in message
news:10******** *****@news.supe rnews.com...
I am working with IBM compiler and AIX but any other
OS/compiler solution would be interesting as well.


Well, since this completely OS/Architecture specific you'll generally have to search newsgroups for solutions for the specific platforms you're looking for. Anyways, for AIX it'll look something like this:

.... Thanks for the tips Shaun, however you didn't define the
function "ValidAddress() ".


True, here goes:

int ValidAddress(vo id *pvAddr)
{
int iRet = 1;

/* We could also use lchown() and probably a number of others,
* we just need a system call which takes in a userland pointer
* and doesn't change any important process context */
if (access((char *) pvAddr, F_OK) && (errno == EFAULT))
iRet = 0;

return(iRet);
}

Some people prefer mincore(2).

Cheers,
Shaun
Nov 14 '05 #6

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

Similar topics

5
2694
by: Peter Steele | last post by:
We have an application that when it runs in the IDE in debug mode an unhandled exception is occurring in a system header file associated with STL stirngs. The actual statement that crashes is return ::memcmp(_First1, _First2, _Count); On inspecting these variables, the strings are in fact equal when the exception occurs and _Count is the right size. As a test I replaced this code in the system include file with a for loop to do the...
9
3255
by: Microsoft News Server | last post by:
Hi, I am currently having a problem with random, intermittent lock ups in my ASP.net application on our production server (99% CPU usage by 3 threads, indefinately). I currently use IIS Debug Tools to do a memory dump of the app when the lock up occurs, however the stack information is not very useful. I have just put a new build of our system onto production, and this build is a "Debug" build as opposed to a "Release" build. I am...
2
3791
by: partybob99 | last post by:
I am trying to call SP_Password from some vb.net code. This should be very straight forward but no matter what I do, I keep getting errors. Here is the code strConnectString = "Data Source=" + strServer + ";Initial Catalog=master;user id=" + strID + ";password=" + strOldPass + ";" Conn.ConnectionString = strConnectString Conn.Open()
1
4965
by: thangchan | last post by:
Hi all, i am getting SQL update problem. as below ======================error messages ======================= Server Error in '/CMS' Application. -------------------------------------------------------------------------------- 無值提供給一或多個必要參數。 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more
3
1558
by: CSUIDL PROGRAMMEr | last post by:
hi folks I am new to python. I have a module does call a os.command(cmd) where cmd is a rpm command. Instead of using os.command and getting the results on command line , i would like to dump the output in a file. Is os.command(cmd > filename) the most efficient command?? thanks
1
1285
by: Warren Stringer | last post by:
Here is what I would like to do: #------------------------------------------------------------ a = Tr3() # implements domain specific language a.b = 1 # this works, Tr3 overrides __getattr__ a.__dict__ = 2 # just so you know that b is local a = 3 # I want to resolve locally, but get: Traceback (most recent call last): ... exec cmd in globals, locals ...
9
6998
by: Allen | last post by:
The arguments of function is variable. Given function address, argument type and data, how to dynamically call the function? The following is pseudo code. int count = 0; int offset = 0; char buffer; count = getcount(buffer, offset); void* pfun = getmethod(buffer, offset);
1
3965
by: jweiss | last post by:
I am trying to run cmd.exe so that I can ftp a file to a remote server. I've been doing this for a long time, but something broke. I did some windows updates last week?? I am getting a 'permission denied' error at this line 48... 48 Call oScript.Run ("C:\inetpub\wwwroot\test\cmd.exe /c " & strCMD & " > " & strTempFile, 0, True) I am stumbling with permissions here. Here is where my cmd.exe lives (i copied it from system32)...
4
2212
by: Mick Walker | last post by:
Hi Everyone, I am stumped here. I have the following stored proceedure:P CREATE PROCEDURE . @SupplierSKU varchar(50), @RetVal int AS Select @Retval = count(*) from dbo.ImportLines Where = @SupplierSKU
0
8459
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8374
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6206
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2784
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
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.