Content of C++ vtbl

来源:互联网 发布:棋牌室收费软件 编辑:程序博客网 时间:2024/05/22 17:00

 

{/u/hui}:g++ t.C

{/u/hui}:./a.out
this[0x0]=0x11358       :
this[0x4]=0x41  :
this[0x8]=0x42  :

vtbl[0x0]=0x111f4       : B::foo1()
vtbl[0x4]=0x111d0       : B::foo2()
vtbl[0x8]=0x111ac       : B::foo3()
vtbl[0xc]=0x10eec       : B::~B()
vtbl[0x10]=0x11158      : B::~B()
vtbl[0x14]=0x11134      : B::foo4()

 

---------------------------------------------------------------

 

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

class A
{
public:
       virtual void foo1() { printf("in A::foo1()/n"); };
       virtual void foo2() { printf("in A::foo2()/n"); }
       virtual void foo3() { printf("in A::foo2()/n"); }
      
public:
                virtual ~A() {}
public:
        A():a1('A') {};
        A(int p):a1(p) {};
        int a1;
};

class B: public A
{
public:
       virtual void foo1() { printf("in B::foo1()/n"); };
       virtual void foo2() { printf("in B::foo2()/n"); }
       virtual void foo3() { printf("in B::foo2()/n"); }
       virtual void foo4() { printf("in B::foo2()/n"); }
      
public:
                virtual ~B() {}
public:
        B():b1('B') {};
        B(int p):b1(p) {};
        int b1;
};

/**
 * This function is not thread-safe.
 */
char * getSymbol(char * file, int address)
{
char *cmd = (char *)malloc(100);
static char buf[100];
FILE *ptr;

memset(buf,'/0',sizeof (buf));buf[0]='/n';
sprintf(cmd,"nm -C -x %s | grep /"%x|0x/" | awk -F//| '{print $8}'",file, address);
if ((ptr = popen(cmd, "r")) != NULL)
        fgets(buf, BUFSIZ, ptr);

free(cmd);
(void) pclose(ptr);

return buf;
}


int main(int argc, char * argv[])
{
#define X B

X d;

int * p = (int *)&d;
int size = sizeof (X)/4;

//printf("this=0x%x,sizeof=%d*4 bytes/n",p,size);
for (int i=0;i<size;i++)
{
        printf("this[0x%x]=0x%x/t: %s",i*4,*(p+i),getSymbol(argv[0],*(p+i)));
}
printf("/n");

int * vtbl = (int *)(*p);
for (int i=0;(((*(vtbl+i)) != 0));i++)
{
        printf("vtbl[0x%x]=0x%x/t: %s", i*4,(*(vtbl+i)),getSymbol(argv[0],*(vtbl+i)));
}

//sleep(100);

}

 

 

原创粉丝点击