MudBytes
» MUDBytes Community » Coding Discussions » Coding and Design » Iterating through all functio...
Pages: << prev 1 next >>
Iterating through all function pointers in C - Is it possible?
Lobotomy
Sorcerer






Group: Members
Posts: 378
Joined: Jul 18, 2008

Go to the bottom of the page Go to the top of the page
#1 id:12864 Posted Sep 27, 2008, 5:46 am

I'm trying to locate some sort of function or system accessible to C for iterating through all of the function pointers and whatnot in a program's executable (mine is already set up to use dlsym, dladdr, etc). I was hopeful that dl_iterate_phdr would be what I was looking for, but that appears to only work on shared library objects (such as libgcc_s.so, etc). If anyone knows a potential solution or can offer some help and/or insight on the matter, I'd appreciate it.

Although, I suspect that it might not be possible with C at all.  :sad:

Last edited Sep 27, 2008, 5:48 am by Lobotomy
David Haley
Wizard






Group: Members
Posts: 6,913
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#2 id:12878 Posted Sep 27, 2008, 1:08 pm

I'm not sure. What exactly are you going to do with them as you iterate over them? You won't/can't have the function types, so you won't really be able to call arbitrary functions. And you'll only have function names if the compiler generated debugging information.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Lobotomy
Sorcerer






Group: Members
Posts: 378
Joined: Jul 18, 2008

Go to the bottom of the page Go to the top of the page
#3 id:12882 Posted Sep 27, 2008, 2:01 pm

The function names (and pointers as a result) are all I need, actually. At the moment I was looking to use the information to build a function scanner for my codebases's dynamic command system that would, during loadup, search for function names prefixed with a particular string that designates them as commands which are not yet present in a particular command table and then load them into it automatically. I.e, say a function were to begin with "command_(etc)" and it were not found belonging to any command in a particular command table, then it would be added into it. It's not really that important, as it merely eliminates the single step of having to enter new commands manually with an command editor command of some sort, but it would be interesting and useful to have. :thinking:

David Haley
Wizard






Group: Members
Posts: 6,913
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#4 id:12884 Posted Sep 27, 2008, 2:21 pm

Well, if you figure out how, I'd be curious... Personally, though, I wouldn't spend too much time on this, because I think the step of making the command is a lot less time-expensive than figuring out function introspection in C. :wink:
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Lobotomy
Sorcerer






Group: Members
Posts: 378
Joined: Jul 18, 2008

Go to the bottom of the page Go to the top of the page
#5 id:12888 Posted Sep 27, 2008, 3:18 pm

Introspection, huh? Wasn't aware of there being a term to go along with it. If I do figure it out I will let you know, although I was somewhat hopeful that by your response you had an idea of how to do it. :sad:

I do have a slight idea, in that if the functions in question were accessible as shared objects they could be accessed directly (in theory, anyways) by the dl_iterate_phdr function. However, from the little bit I've gleaned regarding the creation of shared objects it seems like it would be entirely too cumbersome to be practical; unless I'm misunderstanding it, anyways. I'd like to find a method that's straightforward, much like the dlsym and dladdr functions (which I've turned into str_to_ptr() and ptr_to_str() functions, respectively).

While I'm at it, I do have another idea somewhat in that it might be possible to create a seperate program or sub-function of the main program that searches through the actual source files for function declarations, compiles a list of the results, and then feeds them into a function that runs the names one by one through dlsym() to try and find results, but aside from it possibly not working, it's not as elegant or as straightforward of a method as I would like. Thoughts?

Edit: The site I used as information regarding dl_iterate_phdr is here.

Last edited Sep 27, 2008, 3:28 pm by Lobotomy
David Haley
Wizard






Group: Members
Posts: 6,913
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#6 id:12889 Posted Sep 27, 2008, 3:31 pm

Yup, introspection is what this is called: looking at your available methods/functions/etc.

The problem is that only shared objects have any kind of export table. In normal objects, especially without debugging information turned on, the functions are just addresses in the object file or executable. In order to iterate over them, you need some kind of table of functions, which most (if not all?) compilers do not provide.

The idea of a separate program that gets function prototypes and then having dlsym try each one could work, if that's really what you want to do. It actually seems a lot more straightforward than some of the other ideas, but I agree that it's not terribly elegant.

Registering the functions one way or another  is a pretty good approach. It's what I normally do when I want to have a list of 'things' (objects, classes, functions, ...) without having to do it explicitly. It's not completely automatic, so I hadn't suggested it earlier.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Davion
Idle Hand






Group: Administrators
Posts: 1,441
Joined: May 14, 2006

Go to the bottom of the page Go to the top of the page
#7 id:12890 Posted Sep 27, 2008, 3:46 pm

Lobotomy said:

While I'm at it, I do have another idea somewhat in that it might be possible to create a seperate program or sub-function of the main program that searches through the actual source files for function declarations, compiles a list of the results, and then feeds them into a function that runs the names one by one through dlsym() to try and find results, but aside from it possibly not working, it's not as elegant or as straightforward of a method as I would like. Thoughts?


I've used ctags before to accomplish this. Run ctags on a file, grab appropriate information, parse, execute. You most likely wont even need a separate program to do it.
.........................
http://mudbytes.net/mudbytessignature-davion2.png

Lobotomy
Sorcerer






Group: Members
Posts: 378
Joined: Jul 18, 2008

Go to the bottom of the page Go to the top of the page
#8 id:12895 Posted Sep 27, 2008, 4:02 pm

Davion said:
I've used ctags before to accomplish this. Run ctags on a file, grab appropriate information, parse, execute. You most likely wont even need a separate program to do it.

Outstanding! Ctags will save me a considerable amount of time. Thank you. :smile:

Edit: Works like a charm.  :smile:

Last edited Sep 27, 2008, 7:24 pm by Lobotomy
Pages:<< prev 1 next >>
Tags
[+]

Valid XHTML 1.1! Valid CSS!