Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How can i read the stack frames of running process?

Reply
Thread Tools

How can i read the stack frames of running process?

 
 
J. J. Farrell
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 2:15 pm, karthikbalaguru <(E-Mail Removed)>
wrote:
> On Oct 17, 3:56 pm, harshal <(E-Mail Removed)> wrote:
>
> > Can we read the stack frame's of the current process.

>
> kmalloc comes from linux.


Nonsense. It no more comes from Linux than from eCos.

 
Reply With Quote
 
 
 
 
karthikbalaguru
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 3:56 pm, harshal <(E-Mail Removed)> wrote:
> Hi all,
>
> Can we read the stack frame's of the current process.
> as we know that whenever a function call is made in c new functions
> stack frame
> is created and pushed on to the stack. and when the function returns
> it is popped out from the stack. i want to know the caller functions
> name.


It can be done, but you need to understand your complete system
architecture properly.
What is the architecture you are working on ?

You can also do that by parsing the assembly code generated by your
compiler for that program.
For example in your case, w.r.t M68K family, you need to find the
location for the presence of the following which would give you the
answer as function 'b'.
JSR _a

Karthik Balaguru


Karthik Balaguru

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-17-2007
Mark Bluemel <(E-Mail Removed)> writes:
[...]
> That's put me right... Neither __FUNCTION__ or __func__ are, strictly
> speaking, macros...


Correct.

__FUNCTION__ is non-standard, but is provided by gcc as an extension.
In older versions of gcc, it's a "magic identifier" which is replaced
by a string literal; it could concatenated as if it were a literal,
but it didn't work with #ifdef. In newer versions of gcc, it's simply
another name for __func__.

__func__ is standard in C99, but didn't exist in C90 (though of course
a C90 compiler could provide it as an extension). It acts as if it
were declared as a static const char[] immediately following the
opening brace of the function definition.

If you think about it, __FUNCTION__ or __func__ can't be a macro,
since the preprocessor (more precisely, translation phase 4, which
handles macro expansion) runs before function definitions have been
processed.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-17-2007
Chip Coldwell <(E-Mail Removed)> writes:
> harshal <(E-Mail Removed)> writes:
>> and for that purpose i was thinking to print the callers name in
>> kmalloc.

>
> Just insert a call dump_stack(); you'll get everything.


Presumably implementing dump_stack() is left as an exercise. (There's
no such function in standard C, and there's no portable way of
implementing it; for a given implementation, there may not even be a
non-portable way of implementing it.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-18-2007
Philip Potter wrote:
> Mark Bluemel wrote:
>> Philip Potter wrote:
>>> Mark Bluemel wrote:
>>>> harshal wrote:
>>>>
>>>>> but my question is that if a function can print its name with
>>>>> __FUNCTION__
>>>>> macro then why it can not print its callers name.
>>>>
>>>> The __FUNCTION__ macro is handled by the preprocessor and is
>>>> simply a textual replacement at compile time. It's not
>>>> (terribly) difficult for the preprocessor to track which
>>>> function it's processing at a time.
>>>
>>> I can't find any mention of __FUNCTION__ in n1256. I *can* find
>>> __func__. Is __FUNCTION__ at all standard?

>>
>> It doesn't matter - the point that needed to be made to the OP
>> was that macros are processed by the precompiler. The guy didn't
>> need (as far as I can see) to worry about whether the particular
>> macro he was referring to was part of the standard.

>
> Yes, that's why I replied to you and not to him. I wasn't
> nitpicking, I was asking a question, because I'm not certain
> enough of Acrobat's searching functionality to be sure that
> __FUNCTION__ isn't in there.


One more reason to use the text version of N869. There is no
__FUNCTION__. However, __func__ does exist.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Iwo Mergler
Guest
Posts: n/a
 
      10-18-2007
harshal wrote:

> Hi all,
>
> Can we read the stack frame's of the current process.
> as we know that whenever a function call is made in c new functions
> stack frame


As others pointed out, it's not a good idea, it's highly system
dependent. On many systems it is possible to read the address of
the call instruction (or the one after) from the stack. In order
to get the *name* of the calling function, you'll have to compare
that address with a address map of the application. This is what
debuggers do.

> is created and pushed on to the stack. and when the function returns
> it is popped out from the stack. i want to know the caller functions
> name.


If you have full control over the source of the calling and the
called function, there is no need to mess with the stack. The
trick is to use a macro which passes __FUNCTION__ into the called
function as an argument.

>
> i mean i want something like this
>
> int a()
> {
> printf("File = %s\n",__FILE__);
> /* i want to print the callers name over here. something like this
> printf("Caller function = %s\n",__CALLER_FUN__); it should print
> b*/
> return 0;
> }


Replace with this:

#define a(...) _a(__FUNCTION__)
int _a(const char *caller)
{
printf("Caller function = %s\n",caller);
return 0;
}

Kind regards,

Iwo

 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      10-19-2007
Philip Potter wrote:

<...>


> Yes, that's why I replied to you and not to him. I wasn't nitpicking, I
> was asking a question, because I'm not certain enough of Acrobat's
> searching functionality to be sure that __FUNCTION__ isn't in there.


__func__ is C99, __FUNCTION__ is *not*.

--
Tor <torust [at] online [dot] no>

"I have stopped reading Stephen King novels. Now I just read C code instead"
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      10-19-2007
harshal wrote:
> Hi all,
>
> Can we read the stack frame's of the current process.


This isn't an assembler programming group.

> as we know that whenever a function call is made in c new functions
> stack frame
> is created and pushed on to the stack. and when the function returns
> it is popped out from the stack.


You know more than me... the C standard says nothing about *stack* or
*frame*.


> i want to know the caller functions name.


If I needed this debug information, I would crate my own call three
tracker. First, two functions is needed:

void func_enter(const char *func_name);
void func_leave(void);

and to get the parent function, you could e.g. use a double-linked list
and call

const char *func_parent(void);

--
Tor <torust [at] online [dot] no>

"I have stopped reading Stephen King novels. Now I just read C code instead"
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-20-2007
On Wed, 17 Oct 2007 22:33:43 -0400, in comp.lang.c , CBFalconer
<(E-Mail Removed)> wrote:

>Philip Potter wrote:
>> I'm not certain
>> enough of Acrobat's searching functionality to be sure that
>> __FUNCTION__ isn't in there.

>
>One more reason to use the text version of N869.


I don't think thats an accurate comment. The text version is just as
capable of having __ FUNCTION __ as the PDF.

More of an issue is Philip's uncertainty as to how to use a search
function... Hint: search for word fragments eg UNCTIO

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
Reply With Quote
 
=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0Fk?=
Guest
Posts: n/a
 
      10-20-2007
On Sat, 20 Oct 2007 16:59:09 +0100, Mark McIntyre wrote:
> On Wed, 17 Oct 2007 22:33:43 -0400, in comp.lang.c , CBFalconer
> <(E-Mail Removed)> wrote:
>
>>Philip Potter wrote:
>>> I'm not certain
>>> enough of Acrobat's searching functionality to be sure that
>>> __FUNCTION__ isn't in there.

>>
>>One more reason to use the text version of N869.

>
> I don't think thats an accurate comment. The text version is just as
> capable of having __ FUNCTION __ as the PDF.


The text version of N869 does not include any spacing in any of the 20
identifiers starting with __. There's no reason to assume that if one
more were mentioned, that one would.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How can I hide my stack frames in a TestCase subclass? David Banks Python 5 10-05-2012 09:26 AM
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM



Advertisments