|
Home > Archive > Unix Programming > December 2006 > finding function name from the function pointer?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
finding function name from the function pointer?
|
|
| Prasad 2006-12-15, 1:20 pm |
| Hi All,
I have a code something similar to this ==>
action = getaction () /* action is pointer to struct*/
(action->do_action)() ; /* calling a function from the pointer */
Now, I need to add a function which will get pointer to function and
it's purpose will be to generate the function name from pointer. Basic,
purpose of this is to log the function calls that have been maid.
So, I need to add following code
log ( action->do_action );
Now, the problem is how could i find out the name of the function from
it's pointer.
The expectation is this code should atleast work on RedHat and SUSE
Enterprise Editions.
Please help?
Thanks and regards,
Prasad.
| |
| Maurizio Loreti 2006-12-15, 1:20 pm |
| answered on gnu.gcc.help. if you REALLY have to post in several
groups, PLEASE CROSS-POST and NOT MULTI-POST. I will read your
message only once.
--
Maurizio Loreti ROT13:ybergv@cq.vasa.vg | (@_
Un. of Padova, Dept. of Physics, Padova, Italy | //\
http://www.pd.infn.it/~loreti/mlo.html | V_/_
| |
| Prasad 2006-12-15, 1:20 pm |
| Hi,
Okay thanks that u have replied.
Basically, I am not getting any answere from last 2 days. So, thought
of posting it to more than one groups.
Thanks and regards,
Prasad.
Maurizio Loreti wrote:
> answered on gnu.gcc.help. if you REALLY have to post in several
> groups, PLEASE CROSS-POST and NOT MULTI-POST. I will read your
> message only once.
>
> --
> Maurizio Loreti ROT13:ybergv@cq.vasa.vg | (@_
> Un. of Padova, Dept. of Physics, Padova, Italy | //\
> http://www.pd.infn.it/~loreti/mlo.html | V_/_
| |
| Maxim Yegorushkin 2006-12-15, 7:25 pm |
| Prasad wrote:
> I have a code something similar to this ==>
>
> action = getaction () /* action is pointer to struct*/
> (action->do_action)() ; /* calling a function from the pointer */
>
> Now, I need to add a function which will get pointer to function and
> it's purpose will be to generate the function name from pointer. Basic,
> purpose of this is to log the function calls that have been maid.
>
> So, I need to add following code
>
> log ( action->do_action );
>
> Now, the problem is how could i find out the name of the function from
> it's pointer.
>
> The expectation is this code should atleast work on RedHat and SUSE
> Enterprise Editions.
Try this recipe:
[max@k-pax test]$ cat test.cc
#include <map>
#include <string>
#include <iostream>
#include <iterator>
#include <cstdlib>
class fun_map
{
private:
typedef std::map<void*, std::string> the_map;
the_map fun_;
struct fun_rec
{
void* p;
std::string name;
operator the_map::value_type() const { return
the_map::value_type(p, name); }
friend std::istream& operator>>(std::istream& s, fun_rec& r)
{
return getline(s >> r.p, r.name);
}
};
public:
fun_map(std::istream& s)
: fun_(std::istream_iterator<fun_rec>(s),
std::istream_iterator<fun_rec>())
{}
char const* find(void* p) const
{
the_map::const_iterator i(fun_.find(p));
return fun_.end() == i ? "<unknown>" : i->second.c_str();
}
template<class T>
char const* find(T* p) const
{
return this->find((void*)p);
}
};
void foo() {}
void bar() {}
int main(int ac, char**)
{
fun_map m(std::cin);
printf("%p: %s\n", foo, m.find(foo));
printf("%p: %s\n", bar, m.find(bar));
printf("%p: %s\n", main, m.find(main));
printf("%p: %s\n", std::exit, m.find(std::exit));
printf("%p: %s\n", &ac, m.find(&ac));
}
[max@k-pax test]$ g++ -Wall -Wextra -o test -Wl,-M test.cc > test.map
[max@k-pax test]$ awk 'BEGIN{FIELDWIDTHS="16 16 6 64"} $2~/0x/ && $4~/^
*[a-zA-Z_]/ {gsub(/^ +| +$/, "",$2);gsub(/^ +| +$/, "",$4);print
$2,$4}' test.map > test.simple.map
[max@k-pax test]$ ./test < test.simple.map
0x8048ab4: foo()
0x8048aba: bar()
0x8048b32: main
0x80489a0: exit@@GLIBC_2.0
0xbf9ccb90: <unknown>
|
|
|
|
|