PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > How can i read the stack frames of running process?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How can i read the stack frames of running process?

Réponse
 
LinkBack Outils de la discussion
Vieux 17/10/2007, 14h03   #9
karthikbalaguru
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can i read the stack frames of running process?

On Oct 17, 3:56 pm, harshal <harshalsh...@gmail.com> 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.
>
> 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;
>
> }
>
> int b()
> {
> a();
> return 0;
>
> }
>
> int main()
> {
> b();
>
> }
>
> if there is any way please tell me.
>


You can try something as below :
( This is just a rough idea for you . )

You need to know the register that has the address of the current
frame info.
and that address is normally saved to the frame by the called
function.

Create a LIFO list with this info
a) address of the calling function frame
b) return address
c) parameter list

Every function call should add a frame to the list.
Every return from function will delete the frame.

So, if you know the register that has the address of the current frame
info and the
value at that register, then you can make it up on your own.

Also , Many ways are dependent on parsing through the assembly files
and MAP files .

Karthik Balaguru

  Réponse avec citation
Vieux 17/10/2007, 14h08   #10
karthikbalaguru
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can i read the stack frames of running process?

On Oct 17, 3:56 pm, harshal <harshalsh...@gmail.com> 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.
>
> 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;
>
> }
>
> int b()
> {
> a();
> return 0;
>
> }
>
> int main()
> {
> b();
>
> }
>
> if there is any way please tell me.
>




You can try something as below :
( This is just a rough idea for you . )

You need to know the system architecture very well for this.

You need to know the register that has the address of the current
frame info.
and that address is normally saved to the frame by the called
function.

Create a LIFO list with info something as below such as for Every
function call that will add a frame to the list.
and for every return from function, it will delete the frame.
a) address of the calling function frame
b) return address
c) parameter list

So, if you know the register that has the address of the current frame
info and the
value at that register, then you can make it up on your own.

Also , Many ways are dependent on parsing through the assembly files
and MAP files .

Karthik Balaguru

  Réponse avec citation
Vieux 17/10/2007, 14h11   #11
Erik Trulsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can i read the stack frames of running process?

harshal <harshalshete@gmail.com> wrote:
> On Oct 17, 4:09 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>> harshal <harshalsh...@gmail.com> wrote:
>> > Can we read the stack frame's of the current process.

>>
>> In reliable ISO C, you don't even know that you have something called a
>> "current process"[1], nor a "stack frame", let alone anything that is in
>> this stack frame's possession. All these details are system-specific,
>> all functions to read them are necessarily also system-specific, and in
>> all probability, old systems make doing so a brittle, unreliable process
>> while more modern systems (wisely) forbid you to put your grubby mitts
>> inside the running program's data without pre-arranged permission.
>>
>> > as we know that whenever a function call is made in c new functions
>> > stack frame is created and pushed on to the stack.

>>
>> You may think you know that; I think you merely suspect it.
>>
>> > i want to know the caller functions name.

>>
>> Pass it. Don't rely on dangerous, unportable, and dirty hackery.
>>
>> (BTW: in English, the first person singular personal pronoun is
>> capitalised, except by pretentious bankers like E.E. Cummings.)
>>
>> > if there is any way please tell me.

>>
>> There is no way that I would trust with my computer's mental health.
>>
>> Richard
>>
>> [1] Unless you touch a live wire.

>
> Can you please explain this in simple words.
> from what you are saying it looks like it is nearly impossible.
>
> but my question is that if a function can print its name with
> __FUNCTION__
> macro then why it can not print its callers name.
> then what harm is there with printing its callers name ?


Because the compiler knows at compile time which function it is compiling
and can replace __FUNCTION__ with a string containing that name.

At run-time there is no portable way of finding out even which function
you are in, much less which one you were called from.


Often all information about the names of functions and variables are
discarded by the compiler or linker when that information is no longer needed.

Sometimes a function do not have a stack frame of their own (even in systems
that use an ordinary stack.) Examples of this are functions that have been
inlined or a leaf-function (a function which does not call any other
function) that gets all its parameters (including the return address) passed
in registers.

And even if you do have a stackframe, and even if the information about the
names of functions is retained by the compiler, then the format of this stack frame
will be entirely system-dependent.

In short there is no way that is even half-way portable for a function to
find out which function it was called from.



If you really want to know the code-flow you will have to modify the source
code and insert a printf() (or equivalent) before each call to the function
you are interested in.





--
<Insert your favourite quote here.>
Erik Trulsson
ertr1013@student.uu.se
  Réponse avec citation
Vieux 17/10/2007, 14h15   #12
karthikbalaguru
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can i read the stack frames of running process?

On Oct 17, 3:56 pm, harshal <harshalsh...@gmail.com> 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.
>
> 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;
>
> }
>
> int b()
> {
> a();
> return 0;
>
> }
>
> int main()
> {
> b();
>
> }
>
> if there is any way please tell me.
>


kmalloc comes from linux. I think, gdb has good provisions w.r.t stack
frame .
There are many ways to print information about the selected stack
frame using gdb.

Karthik Balaguru

  Réponse avec citation
Vieux 17/10/2007, 16h02   #13
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can i read the stack frames of running process?

karthikbalaguru wrote:
>

.... snip ...
>
> You can write some methods based on the MAP file created by Linker
> and also using the assembly listing.


Please tell us exactly where the C standard discusses the MAP file,
or the action of the Linker. Also where it discusses 'the assembly
listing'.

--
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

  Réponse avec citation
Vieux 17/10/2007, 16h07   #14
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can i read the stack frames of running process?

karthikbalaguru wrote:
>

.... snip ...
>
> kmalloc comes from linux. I think, gdb has good provisions w.r.t
> stack frame . There are many ways to print information about the
> selected stack frame using gdb.


The subject of this newsgroup is the C language, as specified in
the various (current and past) C standards. None of kmalloc,
linux, gdb, stack frame, have ever appeared or been specified in
any C standard, and are all off-topic and system dependent here.
Note the 'system dependent'. That means such discussions are not
general to all C systems, and thus are not suitable for this
newsgroup.

--
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

  Réponse avec citation
Vieux 17/10/2007, 16h24   #15
Chip Coldwell
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can i read the stack frames of running process?

harshal <harshalshete@gmail.com> 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.

Chip

--
Charles M. "Chip" Coldwell
"Turn on, log in, tune out"
Somerville, Massachusetts, New England
  Réponse avec citation
Vieux 17/10/2007, 16h33   #16
Philip Potter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can i read the stack frames of running process?

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?

--
Philip Potter pgp <at> doc.ic.ac.uk
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h50.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15695 seconds with 16 queries