Created
August 28, 2018 03:35
-
-
Save ladyrick/c101ae0acce89b27f457f4fdd51224f9 to your computer and use it in GitHub Desktop.
print the virtual function table of an object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstring> | |
#include <iomanip> | |
#include <iostream> | |
using namespace std; | |
class Base1 { | |
public: | |
virtual void f() { cout << "Base1::f" << endl; } | |
virtual void g() { cout << "Base1::g" << endl; } | |
virtual void h() { cout << "Base1::h" << endl; } | |
}; | |
class Base2 { | |
public: | |
virtual void f() { cout << "Base2::f" << endl; } | |
virtual void g() { cout << "Base2::g" << endl; } | |
virtual void h() { cout << "Base2::h" << endl; } | |
}; | |
class Base3 { | |
public: | |
virtual void f() { cout << "Base3::f" << endl; } | |
virtual void g() { cout << "Base3::g" << endl; } | |
virtual void h() { cout << "Base3::h" << endl; } | |
}; | |
class Derive : public Base1, public Base2, public Base3 { | |
public: | |
virtual void ff() { cout << "Derive::ff" << endl; } | |
virtual void gg() { cout << "Derive::gg" << endl; } | |
virtual void hh() { cout << "Derive::hh" << endl; } | |
}; | |
typedef void (*Fun)(void); | |
void dumpmemory(void *memstart, size_t bytes) { | |
unsigned char t; | |
char *mem = (char *)memstart; | |
size_t j = 0; | |
for (size_t i = 0; i < bytes; i++) { | |
memcpy(&t, mem + i, 1); | |
cout.fill('0'); | |
cout.width(2); | |
cout << hex << (int)t << ' '; | |
if (++j % 8 == 0) | |
cout << endl; | |
} | |
} | |
int main() { | |
Fun pFun = NULL; | |
Derive d, e; | |
Fun **pVtab = (Fun **)addressof(d); | |
for (int i = 0;; i++) { | |
Fun *vtable = pVtab[i]; | |
// dumpmemory(vtable, 256); | |
cout << "virtual table " << i << ":" << endl; | |
for (int j = 0;; j++) { | |
Fun vfunc = vtable[j]; | |
if (~((int64_t)vfunc | 0xff) == 0) | |
break; | |
cout << (void *)vfunc << " -> "; | |
vfunc(); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment