Connecting Tech Pros Worldwide Help | Site Map

How get list of functions in current C program

  #1  
Old November 17th, 2008, 07:05 PM
Ali Shirvani
Guest
 
Posts: n/a
Hi all,
I want to get list of all functions that are defined in the current C
code, for example if I have the code below:

int f(int c, int d){
int a = 10;
int b = 12;
return c+a;
}

int g(void){
...
...
}

int main(){
}


I need a function that give me *f* and *g*.

thanks,
--
Shirvani Ali
  #2  
Old November 17th, 2008, 07:15 PM
Eric Sosman
Guest
 
Posts: n/a

re: How get list of functions in current C program


Ali Shirvani wrote:
Quote:
Hi all,
I want to get list of all functions that are defined in the current C
code, for example if I have the code below:
>
int f(int c, int d){
int a = 10;
int b = 12;
return c+a;
}
>
int g(void){
...
...
}
>
int main(){
}
>
>
I need a function that give me *f* and *g*.
... but not *main*?

The C language itself has no way to do this. Perhaps some
platform-specific utilities on your system might be able to do
what you require. On a Unixoid system, you might begin with the
"nm" utility and see what you can build atop it.

--
Eric.Sosman@sun.com

  #3  
Old November 17th, 2008, 07:15 PM
s0suk3@gmail.com
Guest
 
Posts: n/a

re: How get list of functions in current C program


On Nov 17, 2:05*pm, Ali Shirvani <aj.shirvani@gmail.comwrote:
Quote:
Hi all,
I want to get list of all functions that are defined in the current C
code, for example if I have the code below:
>
int f(int c, int d){
* *int a = 10;
* *int b = 12;
* *return c+a;
>
}
>
int g(void){
* *...
* *...
>
}
>
int main(){
>
}
>
I need a function that give me *f* and *g*.
>
thanks,
void get_f_and_g(int (**f_result)(int c, int d),
int (**g_result)(void))
{
*f_result = f; // f and g need to be in scope, of course
*g_result = g;
}

Sebastian

  #4  
Old November 17th, 2008, 07:35 PM
Gordon Burditt
Guest
 
Posts: n/a

re: How get list of functions in current C program


>I want to get list of all functions that are defined in the current C
Quote:
>code, for example if I have the code below:
There are a number of tools that operate on source code and will
give you information like this. "cproto", for example, will give
you a list of prototypes for the external functions. (and, of
course, those include the function names). These depend on being
able to read C source code as a text file, and are fairly portable.
Some of them invoke the C preprocessor, which isn't portable. Some
of those that don't invoke the C preprocessor can get confused by
macros used in defining functions.

An executable or object file may also contain the function names
(if not stripped) mangled appropriately (some implementations add
a leading underscore or make other changes to the name to avoid
assembler keywords. In C++ the function name may be horribly mangled
to indicate argument and return types.). Tools such as "nm" or
"objdump" may be available. It is common that there is a symbol
"type" that allows you to distinguish between functions and variables,
and to identify static vs. non-static names. This is all very
system-dependent.

There is no guarantee that a running executable can find its own
name accurately, and if it does, that it can open up the executable
to look at the symbols. And, of course, looking at the symbols is
very system-dependent.
Quote:
>int f(int c, int d){
int a = 10;
int b = 12;
return c+a;
>}
>
>int g(void){
...
...
>}
>
>int main(){
>}
>
>
>I need a function that give me *f* and *g*.
Is there any particular reason why this function should *NOT* also
give you "main", "printf", "exit", "getchar", etc. ?

  #5  
Old November 17th, 2008, 08:15 PM
Ali Shirvani
Guest
 
Posts: n/a

re: How get list of functions in current C program


On Nov 17, 10:30*pm, gordonb.pq...@burditt.org (Gordon Burditt) wrote:
Quote:
Quote:
I want to get list of all functions that are defined in the current C
code, for example if I have the code below:
>
There are a number of tools that operate on source code and will
give you information like this. *"cproto", for example, will give
you a list of prototypes for the external functions. *(and, of
course, those include the function names). *These depend on being
able to read C source code as a text file, and are fairly portable.
Some of them invoke the C preprocessor, which isn't portable. *Some
of those that don't invoke the C preprocessor can get confused by
macros used in defining functions.
>
An executable or object file may also contain the function names
(if not stripped) mangled appropriately (some implementations add
a leading underscore or make other changes to the name to avoid
assembler keywords. *In C++ the function name may be horribly mangled
to indicate argument and return types.). *Tools such as "nm" or
"objdump" may be available. *It is common that there is a symbol
"type" that allows you to distinguish between functions and variables,
and to identify static vs. non-static names. *This is all very
system-dependent.
>
There is no guarantee that a running executable can find its own
name accurately, and if it does, that it can open up the executable
to look at the symbols. *And, of course, looking at the symbols is
very system-dependent.
>
>
>
Quote:
int f(int c, int d){
* int a = 10;
* int b = 12;
* return c+a;
}
>
Quote:
int g(void){
* ...
* ...
}
>
Quote:
int main(){
}
>
Quote:
I need a function that give me *f* and *g*.
>
Is there any particular reason why this function should *NOT* also
give you "main", "printf", "exit", "getchar", etc. ?
Thanks for your reply.
I'm on Linux, and I want to get only the functions that I wrote them,
there is no problem, if any thing give me list of all function, I can
ignore some of them. Is there any library that work like *nm*?

Thanks
  #6  
Old November 17th, 2008, 08:35 PM
Keith Thompson
Guest
 
Posts: n/a

re: How get list of functions in current C program


Ali Shirvani <aj.shirvani@gmail.comwrites:
[...]
Quote:
Thanks for your reply.
I'm on Linux, and I want to get only the functions that I wrote them,
there is no problem, if any thing give me list of all function, I can
ignore some of them. Is there any library that work like *nm*?
Maybe, but that's a Linux or Unix question, not a C question. Try
comp.unix.programmer or one of the Linux newsgroups.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  #7  
Old November 17th, 2008, 10:15 PM
CBFalconer
Guest
 
Posts: n/a

re: How get list of functions in current C program


Ali Shirvani wrote:
Quote:
>
I want to get list of all functions that are defined in the
current C code, for example if I have the code below:
>
int f(int c, int d){
int a = 10;
int b = 12;
return c+a;
}
>
int g(void){
...
}
>
int main(){
}
>
I need a function that give me *f* and *g*.
Simple. You parse the source code, and whenever you find a
definition for a function you extract and spit out that function
name. You might find it simpler to just read the source.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
  #8  
Old November 18th, 2008, 01:05 AM
Gordon Burditt
Guest
 
Posts: n/a

re: How get list of functions in current C program


>I need a function that give me *f* and *g*.
Quote:
Quote:
>>
>Is there any particular reason why this function should *NOT* also
>give you "main", "printf", "exit", "getchar", etc. ?
>
>Thanks for your reply.
>I'm on Linux, and I want to get only the functions that I wrote them,
So you didn't write main()?
Quote:
>there is no problem, if any thing give me list of all function, I can
>ignore some of them. Is there any library that work like *nm*?
You have the source code to nm, so go ahead and modify a copy of
it so instead of outputting a list of symbols, it selects appropriate
ones and returns a list of them to the caller.

  #9  
Old November 18th, 2008, 05:15 AM
Ali Shirvani
Guest
 
Posts: n/a

re: How get list of functions in current C program


On Nov 18, 3:58*am, gor...@hammy.burditt.org (Gordon Burditt) wrote:
Quote:
Quote:
Quote:
I need a function that give me *f* and *g*.
>
Quote:
Quote:
Is there any particular reason why this function should *NOT* also
give you "main", "printf", "exit", "getchar", etc. ?
>
Quote:
Thanks for your reply.
I'm on Linux, and I want to get only the functions that I wrote them,
>
So you didn't write main()?
>
Quote:
there is no problem, if any thing give me list of all function, I can
ignore some of them. Is there any library that work like *nm*?
>
You have the source code to nm, so go ahead and modify a copy of
it so instead of outputting a list of symbols, it selects appropriate
ones and returns a list of them to the caller.
I want do this on process, but nm do this on program, not process!
  #10  
Old November 18th, 2008, 06:15 AM
Nate Eldredge
Guest
 
Posts: n/a

re: How get list of functions in current C program


This is more appropriate for comp.unix.programmer; crossposted and
followups to there.

Ali Shirvani <aj.shirvani@gmail.comwrites:
Quote:
On Nov 18, 3:58*am, gor...@hammy.burditt.org (Gordon Burditt) wrote:
Quote:
Quote:
>I need a function that give me *f* and *g*.
>>
Quote:
>Is there any particular reason why this function should *NOT* also
>give you "main", "printf", "exit", "getchar", etc. ?
>>
Quote:
>Thanks for your reply.
>I'm on Linux, and I want to get only the functions that I wrote them,
>>
>So you didn't write main()?
>>
Quote:
>there is no problem, if any thing give me list of all function, I can
>ignore some of them. Is there any library that work like *nm*?
>>
>You have the source code to nm, so go ahead and modify a copy of
>it so instead of outputting a list of symbols, it selects appropriate
>ones and returns a list of them to the caller.
>
I want do this on process, but nm do this on program, not process!
As far as I know, this is impossible. The names of your functions might
be stored in your program's binary, if it hasn't been stripped; but they
are certainly not loaded into your process's memory. By that time,
everything's been converted to raw addresses and the names are no longer
needed. So there is no way to get to them at runtime short of reading
the binary (which you could do with nm) or some other file where they
might be stored.

Why do you want to do this, anyway? Maybe there is a better way to
solve your underlying problem.
  #11  
Old November 18th, 2008, 11:25 AM
James Harris
Guest
 
Posts: n/a

re: How get list of functions in current C program


On 18 Nov, 05:12, Ali Shirvani <aj.shirv...@gmail.comwrote:
Quote:
On Nov 18, 3:58 am, gor...@hammy.burditt.org (Gordon Burditt) wrote:
>
>
>
Quote:
Quote:
>I need a function that give me *f* and *g*.
>
Quote:
Quote:
>Is there any particular reason why this function should *NOT* also
>give you "main", "printf", "exit", "getchar", etc. ?
>
Quote:
Quote:
>Thanks for your reply.
>I'm on Linux, and I want to get only the functions that I wrote them,
>
Quote:
So you didn't write main()?
>
Quote:
Quote:
>there is no problem, if any thing give me list of all function, I can
>ignore some of them. Is there any library that work like *nm*?
>
Quote:
You have the source code to nm, so go ahead and modify a copy of
it so instead of outputting a list of symbols, it selects appropriate
ones and returns a list of them to the caller.
>
I want do this on process, but nm do this on program, not process!
I've looked at your three messages and am not clear: Do you want to
get these names from the source code - i.e. by scanning the C source -
or from the running process - i.e. to find out while executing what
functions are present?

--
James
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to query a function and get a list of expected parameters? Matthew Wilson answers 2 September 29th, 2006 08:05 PM
How do you get a list of files currently in a directory?? Rob_S answers 2 March 1st, 2006 01:15 PM
List of all Instatiated Objects Don Stewart answers 6 November 16th, 2005 08:26 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM