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, 11h56   #1
harshal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How can i read the stack frames of running process?

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.

Thanks and Regards
Harshal Shete

  Réponse avec citation
Vieux 17/10/2007, 12h09   #2
Richard Bos
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:

> 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.
  Réponse avec citation
Vieux 17/10/2007, 13h06   #3
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 need to understand the architecture of your system, assembly,
compiler very well.
Write a logic in such a way that, you
try parsing throught the generated assembly file and get to know the
function that is calling it.
In the case of M68K there will be something like
JSR _a inside 'b' function.

Karthik Balaguru

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

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 ?

actually i thought of this thing because i want to resolve some memory
leak issues.
and for that purpose i was thinking to print the callers name in
kmalloc.
by the way i am using eCos as operating system.

Thanks and Regards
Harshal


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


"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message
news:4715ebfc.158034270@news.xs4all.nl...
> harshal <harshalshete@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.
>


now, this is curious:
I personally do not know of any arch requiring "pre-arranged permission to
put one's grubby mitts in the program's data".

there are a lot of things generally prevented by the OS and hardware, but I
have not heard of this one...


unless of course, by 'pre-arranged permission' you mean 'have to write stuff
in assembler...'.
well, this is true. now, the fun part, is getting the name for the
address...


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


yes, plausible. there is little requiring C compilers, for example, to not
perform inlining, tail-call optimization, and other such tricks...


>> i want to know the caller functions name.

>
> Pass it. Don't rely on dangerous, unportable, and dirty hackery.
>


unless you want to be like me and write your own dynamic compiler framework,
where one comes face to face with this kind of ugly and unportable
hackerry...


now, it is the case, that there is probably no simple answer here either, as
nearly any attempt at an answer would likely also require that the reader
understand what all is going on here.



  Réponse avec citation
Vieux 17/10/2007, 13h46   #6
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 write some methods based on the MAP file created by Linker and
also using the assembly listing.

Karthik Balaguru

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

karthikbalaguru wrote:
> On Oct 17, 3:56 pm, harshal <harshalsh...@gmail.com> wrote:
>> Hi all,
>>
>> Can we read the stack frame's of the current process.

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


I think it's unlikely that anyone capable of doing this would need to
post the question...
  Réponse avec citation
Vieux 17/10/2007, 13h56   #8
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can i read the stack frames of running process?

harshal wrote:
> On Oct 17, 4:09 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:


> Can you please explain this in simple words.

OK - simple words follow:-

It is very difficult to do in any particular implementation.

It is impossible to produce a general solution.

> from what you are saying it looks like it is nearly impossible.


That's about right.

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

> actually i thought of this thing because i want to resolve some memory
> leak issues.


It can be a useful approach. But unless someone else has already
implemented it, it won't be at all easy...

> and for that purpose i was thinking to print the callers name in
> kmalloc.


I've never heard of kmalloc...

> by the way i am using eCos as operating system.


Then perhaps you could ask in a newsgroup or forum related to that
operating system.
  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 06h07.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14855 seconds with 16 queries