How to detect the entry point of a function?

J

Johnny

Hi,
Is there a way to detect the entry point of all the functions in a
project? If so, can I make a function that will be called at the every
entry point? Thanks for your consideration.

Regards,
Johnny
 
G

Gordon Burditt

Is there a way to detect the entry point of all the functions in a

Not portably, unless you're willing to construct a list of all
the functions from the source code.
If so, can I make a function that will be called at the every
entry point? Thanks for your consideration.

What is "the every entry point"? Some compilers have a profiling
setup, which might be what you are looking for.

Gordon L. Burditt
 
J

Jack Klein

Hi,
Is there a way to detect the entry point of all the functions in a
project? If so, can I make a function that will be called at the every
entry point? Thanks for your consideration.

Regards,
Johnny

C has no concept of "entry point". All that you can do it call
another function as the first executable statement of the function(s)
that you want to do this.

Unless you are using a compiler in C99 conforming mode, this must be
after the definitions of any objects defined in the function's top
level block scope. Depending on what auto objects you define, and how
you initialize them, the compiler might generate executable code
before the first executable statement that you write.

Example:

int function(int x, int y)
{
int z = x + y;
call_special_entry_function();
if (z == x * y)
{
/* ... */
}
return z;
}

This useless example shows that the compiler will generate code to add
x and y and store the result in z before calling your special
function. One way around this is to put the call at the top of the
function, then move everything else into a nested block:

int function(int x, int y)
{
call_special_entry_function();
{
int z = x + y;
if (z == x * y)
{
/* ... */
}
return z;
}
}

Now your function call will be executed before any initialization code
for locals happens. The compiler might still have lower level
function entry code (generally called prologue) that executes before
anything you can write.
 
J

Johnny

Jack said:
C has no concept of "entry point". All that you can do it call
another function as the first executable statement of the function(s)
that you want to do this.

Unless you are using a compiler in C99 conforming mode, this must be
after the definitions of any objects defined in the function's top
level block scope. Depending on what auto objects you define, and how
you initialize them, the compiler might generate executable code
before the first executable statement that you write.

Example:

int function(int x, int y)
{
int z = x + y;
call_special_entry_function();
if (z == x * y)
{
/* ... */
}
return z;
}

This useless example shows that the compiler will generate code to add
x and y and store the result in z before calling your special
function. One way around this is to put the call at the top of the
function, then move everything else into a nested block:

int function(int x, int y)
{
call_special_entry_function();
{
int z = x + y;
if (z == x * y)
{
/* ... */
}
return z;
}
}

Now your function call will be executed before any initialization code
for locals happens. The compiler might still have lower level
function entry code (generally called prologue) that executes before
anything you can write.

Thanks Jack, in fact, that's what i was thinking about. But if there
are millions of functions in a project, adding the calling of
call_special_entry_function() would be a great deal of workload and
also, is very difficult for maintainance. Is there a clean way to do
such kind of things?
 
O

octangle

Thanks Jack, in fact, that's what i was thinking about. But if there
are millions of functions in a project, adding the calling of
call_special_entry_function() would be a great deal of workload and
also, is very difficult for maintainance. Is there a clean way to do
such kind of things?

I would be curious to know what the precise purpose of the call to the
special entry function is...

Write a perl (or python) script to insert a
call_special_entry_function() reference into every function in your
project that does not already have such a call ... you could run this
script as a preprocessing step in every build to assure that newly
created functions have the call added.
 
K

kleuske

Johnny schreef:
Hi,
Is there a way to detect the entry point of all the functions in a
project? If so, can I make a function that will be called at the every
entry point? Thanks for your consideration.

This is entirely implementation depended and goes _far_ beyond the
scope of this n.g. You may have a better chance of getting any
practical answers in another group.

<Off Topic>
There is no portable way to do so.

Unportably, there are a number of ways you can do it

1. Inspect symtables produced by your toolchain (best option)
2. Inspect object code for CALL instructions and note adresses
3. Scan for function-prologue code.

</Off Topic>
 
J

Johnny

octangle said:
I would be curious to know what the precise purpose of the call to the
special entry function is...
I want to add a function to record the entering time of every function
to do some profilings. I don't know how can TrueTime to do so, maybe
also a little bit like this?
Write a perl (or python) script to insert a
call_special_entry_function() reference into every function in your
project that does not already have such a call ... you could run this
script as a preprocessing step in every build to assure that newly
created functions have the call added.
That's surely better than adding the messes by hand Octangle, but I
still want to know if there is a clean way which doesn't need the help
of the script?
 
J

Johnny

Johnny schreef:


This is entirely implementation depended and goes _far_ beyond the
scope of this n.g. You may have a better chance of getting any
practical answers in another group.
If this post bothers you, sorry for that...
<Off Topic>
There is no portable way to do so.

Unportably, there are a number of ways you can do it

1. Inspect symtables produced by your toolchain (best option)
2. Inspect object code for CALL instructions and note adresses
3. Scan for function-prologue code.

</Off Topic>
Thanks for your suggestion, I'll try to find something more about your
suggestions.

Regards,
Johnny
 
K

Keith Thompson

Johnny said:
I want to add a function to record the entering time of every function
to do some profilings. I don't know how can TrueTime to do so, maybe
also a little bit like this?

Um, have you tried using a profiler?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top