What is the Linux equivalent to MSVC++'s option /d1reportSingleClassLayout?

c++, linux

Solution

You need to compile the file with debugging information (`-g` option) then use `pahole` to display the struct layout. `pahole` is usually available from the `dwarves` package (source; on GitHub; in Ubuntu).

$ g++ -ggdb -c -o myfile.o myfile.cpp
$ pahole -C MyClass myfile.o
class MyClass {
public:

    int ()(void) * *           _vptr.MyClass;        /*     0     4 */
    int                        i;                    /*     4     4 */
    const char  *              c;                    /*     8     4 */
    void MyClass(class MyClass *, const class MyClass  &);

    void MyClass(class MyClass *);

    virtual void ~MyClass(class MyClass *, int);


    /* size: 12, cachelines: 1, members: 3 */
    /* last cacheline: 12 bytes */
};

The `-C` option lets you select which class/struct to examine.

If you don't have access to `pahole` you can get the same information in a much less readable form from `readelf -wi myfile.o` or `eu-readelf -winfo myfile.o`. The paper https://www.kernel.org/doc/ols/2007/ols2007v2-pages-35-44.pdf describes `pahole` alongside other `dwarves` tools.

Problem

I am moving development to Linux but I couldn't find how I can get an output similar to /d1reportSingleClassLayout from MSVC++ under g++ or clang++. If these compiler do not have such a feature, is there an external tool that provides similar visualization?

Original source

Related problems