Visual Studio Extension for C++ struct memory layout visualization

Overview

StructLayout

Visual Studio Extension for C++ struct memory layout visualization

MarketPlace Donate

Download latest from the Visual Studio Marketplace

Motivation

In C++, the structure layout can be affected by different factors. In order to produce performant data cache oriented code or reduce the structure memory footprint, it is important to be aware of the class layouts at the same spot where code is created, updated, removed or debugged. This extension allows programmers to visualize their structures within Visual Studio with just 1 click.

Features

Right click on top of any C++ struct definition and select Show Struct Layout (or press Alt+L) in order to visualize the memory layout.

Interaction

Stack Mode

In this visualization mode the types with children are stacked in order to allow navigation in and out and get a better sense of overall structure.

Stack screenshot

Flat Mode

The flat mode skips all groups and only shows one layer, producing a more compact view.

Flat screenshot

How it works

Struct Layout uses Clang LibTooling internally to parse the C++ files and extract the memory layout information.

When a Layout request is made the extension does the following:

  • Retrieve the active document and cursor position.
  • Extract the relevant file and project properties (cl or nmake).
    1. Include directories
    2. Force includes
    3. Preprocessor definitions
    4. Exclude directories
  • Add the extra parameters from the extension options.
  • Trigger the LayoutParser (Clang libtooling application) with all the arguments gathered.
  • Visualize the results or print any issues found in the StructLayout Output Pane.

Documentation

References

Contributing

This project is open to code contributions.

If you found this extension useful you can always buy me a cup coffee.

paypal

Comments
  • Immediate VS crash on button press

    Immediate VS crash on button press

    In one (but not all) of my projects, the entire Studio immediately crashes half a second or so after I click the "show struct layout" Button. No error messages or anything.

    When I run the studio with devenv /log, the last two log entries are:

      <entry>
        <record>728</record>
        <time>2020/10/31 06:30:09.126</time>
        <type>Warning</type>
        <source>GlobalBrokeredServiceContainer</source>
        <description>Request for &quot;Microsoft.VisualStudio.Shell.Internal.SettingsManager (1.0)&quot; from Process denied because the service is only exposed RemoteExclusiveClient, LiveShareGuest.</description>
      </entry>
      <entry>
        <record>729</record>
        <time>2020/10/31 06:30:09.126</time>
        <type>Warning</type>
        <source>GlobalBrokeredServiceContainer</source>
        <description>Request for proxy to &quot;Microsoft.VisualStudio.Shell.Internal.SettingsManager (1.0)&quot; is declined: ServiceAudienceMismatch.</description>
      </entry>
    </activity>
    

    Any other kind of log I could post to help?

    bug 
    opened by s9w 18
  • [ERROR] Unable to scan the given location.

    [ERROR] Unable to scan the given location.

    running 0.2.1 (0.2.0 also didn't work):

    struct.h:13:10: fatal error: 'Base Project'\NA-Project Constants.h' file not found #include "Base Project\NA-Project Constants.h" ^~~~~~~~~~~~~~~~~~~~~~~ 1 error generated. Error while processing C:\Users\Eric\source\Proj\NA-Project\VS\VerXXXXXX\MsgHdrs\struct.h. [ERROR] Unable to scan the given location. (394.0328 ms)

    no configuration file was generated so I can't attempt to include additional paths. My Additional Include Directories is set as "$(ProjectDir);$(SolutionDir);%(AdditionalIncludeDirectories)"

    bug 
    opened by ericthobbs 9
  • Not working with cmake projects

    Not working with cmake projects

    Hi. This is a really useful tool. Will save me a lot of time and it's convenient.

    I tested it out with a few cmake projects opened with VS. Get this error in my output:

    [14:22:23] [ERROR] Unable to retrieve the project configuration
    

    Regenerating the cmake cache and restarting VS don't fix this so I assume it relies on data from VS projects. Also tried creating a new cmake project, same error. It works on the normal VS projects I tested it on. I could paste my structs/classes into a normal VS project and check them there but that'd remove some of the convenience.

    enhancement 
    opened by Moneyl 8
  • Visual Studio 17.4 Support

    Visual Studio 17.4 Support

    Hi, so the latest VS update has unfortunately broken this plugin due to them inserting a minimum required clang version in yvals_core.h

    #if __clang_major__ < 14 _EMIT_STL_ERROR(STL1000, "Unexpected compiler version, expected Clang 14.0.0 or newer."); #endif // ^^^ old Clang ^^^

    I use libclang internally and had to upgrade that due to this as well :(

    bug 
    opened by BobbyAnguelov 6
  • Error dialog -

    Error dialog - "Unable to retrieve the project configuration"

    I just installed this VisualStudio extension, and consistently get the dialog box error, making the extension unusable. This is a standard C++ VS project, and it definitely has a valid project configuration.

    opened by Pnyxor 6
  • Not working with std::string_view

    Not working with std::string_view

    code: #include <string_view>

    void f() { std::string_view name, prefix, suffix; }

    struct MyStruct { int x, y, z; };

    output: [19:23:57] Looking for structures at D:\C++\Testing\TestCode\TestCode\src\struct_layout.cpp:8:11... [19:23:57] COMMAND LINE: D:\C++\Testing\TestCode\TestCode\src\struct_layout.cpp -- -x c++ -m64 -w -DFORCE_STATIC_VCRT -D_MBCS -ID:\C++\Testing\TestCode\TestCode\SDL\ -ID:\C++\Testing\TestCode\TestCode\include [19:23:57] Execution Log: D:\C++\Testing\TestCode\TestCode\src\struct_layout.cpp:5:7: error: no type named 'string_view' in namespace 'std' std::string_view name, prefix, suffix; ~~~~~^ 1 error generated. Error while processing D:\C++\Testing\TestCode\TestCode\src\struct_layout.cpp.

    [19:23:57] [ERROR] Unable to scan the given location. (37.0765 ms)

    bug 
    opened by RisingLiberty 6
  • Error parsing intrin.h, included by other standard headers

    Error parsing intrin.h, included by other standard headers

    I've recently started getting this issue on any data structure that I try seeing the layout of:

    [12:02:59] Capturing configuration from VS projects...
    [12:02:59] Looking for structures at C:\dev\halley\src\engine\utils\include\halley\data_structures\maybe.h:17:14...
    [12:02:59] COMMAND LINE: C:\dev\halley\src\engine\utils\include\halley\data_structures\maybe.h -- -x c++ -m64 -std=c++17 -w -DWIN32 -D_WINDOWS -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DGSL_THROW_ON_CONTRACT_VIOLATION -DDEV_BUILD -D_WIN32_WINNT=0x0601 -DWINVER=0x0601 -D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS -DWITH_SDL2 -DBOOST_ALL_NO_LIB -DBOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE -DWITH_OPENGL -DWITH_ASIO -DWITH_DX11 -DWITH_MEDIA_FOUNDATION -DCMAKE_INTDIR=\"Debug\" -IC:\dev\halley\src\contrib -IC:\dev\halley\src\engine\utils\include -IC:\dev\halley\src\engine\utils\..\core\include -IC:\dev\halley\src\engine\utils\..\..\contrib\yaml-cpp\include -IC:\dev\halley_deps\Boost\include\boost-1_72 -working-directory=C:\dev\build\halley\src\engine\utils -std=c++17
    [12:02:59] Execution Log:
    In file included from C:\dev\halley\src\engine\utils\include\halley\data_structures\maybe.h:3:
    In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\optional:18:
    In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\xmemory:13:
    In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\limits:14:
    In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\intrin0.h:18:
    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\intrin.h:56:1: error: expected function body after function declarator
    __MACHINE(void * _AddressOfReturnAddress(void))
    ^
    1 error generated.
    Error while processing C:\dev\halley\src\engine\utils\include\halley\data_structures\maybe.h.
    
    [12:02:59] [ERROR] Unable to scan the given location. (241.0200 ms)
    

    It seems that pretty much any standard header eventually includes intrin.h, which causes that issue.

    The extension used to work fine on this project, so it was either a recent update or updating Visual Studio to 16.8.2 (sorry, those two things happened at the same time, not sure who's at fault).

    opened by amzeratul 5
  • No structure found at the given location.

    No structure found at the given location.

    In VS 2019 16.2.0

    I get the following error at Last l{};:

    [10:56:29] Looking for structures at C:\Users\Source\Repos\StructLayout\StructLayout\StructLayout.cpp:24:7... [10:56:29] COMMAND LINE: C:\Users\Source\Repos\StructLayout\StructLayout\StructLayout.cpp -- -x c++ -m32 -w -DWIN32 -D_DEBUG -D_CONSOLE -working-directory=C:\Users\Source\Repos\StructLayout\StructLayout [10:56:31] No structure found at the given location. (1.0630 s)

    Note: This message is printed in Output. It would be nice to have some other popup or way to report that nothing happened. As I didn't notice the Output I thought that the extension failed.

    Is VS 2019 16.2.0 adequate for this extension?

    #include <iostream>
    #include <string>
    
    struct Base {
    	int i;
    };
    
    struct Derived: Base {
    
    	int i;
    	bool c;
    	Base b;
    };
    
    struct Last : Derived {
    	std::string c;
    };
    
    int main()
    {
    	Last l{};
        std::cout << "Hello World!\n" << l.c << std::endl;
    }
    
    enhancement 
    opened by Zingam 5
  • Strange errors about missing include files that aren't missing

    Strange errors about missing include files that aren't missing

    As a simple example I am trying your Visual Studio TestProject sample. I can layout Base from TestHeader.h without errors, but if I try TestStruct from TestProject.cpp I get error:

    [04:57:01] Using manual configuration... [04:57:01] Looking for structures at H:\temp\StructLayout\TestProjects\Regular\TestProject.cpp:7:16... [04:57:01] COMMAND LINE: H:\temp\StructLayout\TestProjects\Regular\TestProject.cpp -- -x c++ -m64 [04:57:01] Execution Log: H:\temp\StructLayout\TestProjects\Regular\TestProject.cpp:1:10: fatal error: 'cstdio' file not found #include ^~~~~~~~ 1 error generated. Error while processing H:\temp\StructLayout\TestProjects\Regular\TestProject.cpp.

    [04:57:01] [ERROR] Unable to scan the given location. (313.0954 ms)

    but when I compile the project there are no errors and I can open cstdio without a problem.

    If I force include cstdio then it complains about another header and if that one is forced then another and another ad infinitum. Has anybody ran into a similar situation? The truth is that I am getting a similar error in a more complex project but the symptoms are the same.

    I have tried other options, and visual studio settings but nothing helps. None of the current issues seem similar (at least that's the impression I get and i have read them all). Does anybody have any suggestions or things I may try to find the source of my problem? Anything would be greatly appreciated as this extension can be very useful but I am totally stumped.

    I realize that the problem is with my system as nobody else seems to talk about it but other than this extension I have no problems with visual studio (2019). Thank you.

    Paxcut

    opened by paxcut 4
  • Not working with relative include paths and full include paths that contain whitespaces

    Not working with relative include paths and full include paths that contain whitespaces

    Repro 1- Create an empty project with a source file 2- Add a relative path in C/C++ -> Additional Include Directories 3- Include a file from this directory in your source file 4- Define a random struct after 5- Ensure it compiles 6- Run Show Struct Layout on the struct

    Result fatal error: 'included_file.h' file not found

    The same result arises when there's a whitespace in a full include path. That's because include paths passed to Clang with -I don't seem to be delimited with "".

    bug 
    opened by jpanneton 4
  • MS-style inline assembly is not available. Unable to find target for this triple (no targets are registered).

    MS-style inline assembly is not available. Unable to find target for this triple (no targets are registered).

    When I right click on class name and press Show Struct Layout I get this error: MS-style inline assembly is not available. Unable to find target for this triple (no targets are registered). https://imgur.com/Of08Imx Can you please fix this?

    bug 
    opened by pavledev 3
  • "The filename or extension is too long" - Unable to scan the given location

    Windows has a 32KiB (including null terminator) command line length limit with CreateProcess (https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx)

    When StructLayout tries to invoke ClangLayout with a command line that exceeds this limit it will fail. A response file should be used to pass args to clang if this limit is reached or exceeded.

    opened by nedwardsnae 0
  • PDB mode pretty instable

    PDB mode pretty instable

    The Clang mode wasn't viable to my usecases, so all of this only refers to the PDB mode.

    I'm still having trouble getting it to work reliably. For example right now, this program leads to the usual "no structure found at the given position" dialog:

    #include <string>
    #include <vector>
    
    struct str
    {
       char a;
       int b;
       char c;
    };
    
    int main()
    {
       str t{};
       return 0;
    }
    

    If I remove the vector include or change the struct layout, things work. For example if I change the struct to

    struct str{
       int a;
    }
    

    it works. If I then change back to the original struct, I get this:

    image

    So he somehow reads back the old code. All of this is pretty unreliable and I can often get a code to work which did not prior and the other way round. In particular I found a rebuild fix the "wrong struct" errors.

    So there are really two main problems:

    1. Displaying old layouts (fixable by rebuild)
    2. Things not working at all (Some fixable by rebuild, but some never)

    I'm using VS2022, debug mode and have things compiled and cursor on the first line, just as instructed.

    The overall pattern what works and what not isn't clear. The unfixable errors certainly happen more often in complex types from "real" projects. They might have things like members of library types, precompiled headers, functions, forced includes etc. Not sure if any of that makes things harder on the parsing side. Those complexities make it harder to isolate the problem unfortunately.

    bug 
    opened by s9w 4
  • Troublesome unions with PDB extraction

    Troublesome unions with PDB extraction

    [Visual Studio Community 2022 v17.1.5 / Struct Layout v0.6.0, PDB extraction]

    I don't think this is correct. image

    It is impossible to interact with struct C when an array is introduced. image

    It works fine with Clang extraction, but I'm running into some troubles with it not parsing my #includes properly.

    bug 
    opened by PersonMedBrukernavn 2
  • support MSVC toolset option for side-by-side toolset installations

    support MSVC toolset option for side-by-side toolset installations

    Struct Layout compilation uses incorrect version of MSVC++ toolchain when the system has multiple VS installations, and/or multiple toolchain installed side-by-side on a single VS instance, and a given project is configured to select a specific version.

    Here is the project configuration snapshot where this setting is made:

    image

    To reproduce, install a secondary (older) version of C++ toolchain using the VS Installer (locate one under individual component install). Configure a sample project to use this older version. Struct Layout will insist on using the latest version available, instead of the one specified by the project.

    On non-trivial codes, this causes compiler errors because the code may not be compatible with the new toolchain versions (hence the official support for such per-project selection and side-by-side paradigm by VS). To reproduce this scenario artificially, add the following preprocessor condition in code:

    For example,

    #if _MSC_VER > 1927
    #error "unsupported compiler
    #endif
    

    where 19.27 corresponds to v14.27 of the MSVC toolchain:

    image

    bug 
    opened by slymz 1
Releases(v0.6.1)
Owner
Ramon Viladomat
A game programmer trying to make the 'Hello World!' a better place.
Ramon Viladomat
The Hoard Memory Allocator: A Fast, Scalable, and Memory-efficient Malloc for Linux, Windows, and Mac.

The Hoard Memory Allocator Copyright (C) 1998-2020 by Emery Berger The Hoard memory allocator is a fast, scalable, and memory-efficient memory allocat

Emery Berger 927 Jan 2, 2023
Custom memory allocators in C++ to improve the performance of dynamic memory allocation

Table of Contents Introduction Build instructions What's wrong with Malloc? Custom allocators Linear Allocator Stack Allocator Pool Allocator Free lis

Mariano Trebino 1.4k Jan 2, 2023
MMCTX (Memory Management ConTeXualizer), is a tiny (< 300 lines), single header C99 library that allows for easier memory management by implementing contexts that remember allocations for you and provide freeall()-like functionality.

MMCTX (Memory Management ConTeXualizer), is a tiny (< 300 lines), single header C99 library that allows for easier memory management by implementing contexts that remember allocations for you and provide freeall()-like functionality.

A.P. Jo. 4 Oct 2, 2021
Memory-dumper - A tool for dumping files from processes memory

What is memory-dumper memory-dumper is a tool for dumping files from process's memory. The main purpose is to find patterns inside the process's memor

Alexander Nestorov 31 Nov 9, 2022
Mesh - A memory allocator that automatically reduces the memory footprint of C/C++ applications.

Mesh: Compacting Memory Management for C/C++ Mesh is a drop in replacement for malloc(3) that can transparently recover from memory fragmentation with

PLASMA @ UMass 1.5k Dec 30, 2022
STL compatible C++ memory allocator library using a new RawAllocator concept that is similar to an Allocator but easier to use and write.

memory The C++ STL allocator model has various flaws. For example, they are fixed to a certain type, because they are almost necessarily required to b

Jonathan Müller 1.2k Dec 26, 2022
Public domain cross platform lock free thread caching 16-byte aligned memory allocator implemented in C

rpmalloc - General Purpose Memory Allocator This library provides a public domain cross platform lock free thread caching 16-byte aligned memory alloc

Mattias Jansson 1.7k Dec 28, 2022
OpenXenium JTAG and Flash Memory programmer

OpenXenium JTAG and Flash Memory programmer * Read: "Home Brew" on ORIGINAL XBOX - a detailed article on why and how * The tools in this repo will all

Koos du Preez 29 Oct 23, 2022
manually map driver for a signed driver memory space

smap manually map driver for a signed driver memory space credits https://github.com/btbd/umap tested system Windows 10 Education 20H2 UEFI installati

ekknod 89 Dec 17, 2022
Memory instrumentation tool for android app&game developers.

Overview LoliProfiler is a C/C++ memory profiling tool for Android games and applications. LoliProfiler supports profiling debuggable applications out

Tencent 491 Jan 6, 2023
A single file drop-in memory leak tracking solution for C++ on Windows

MemLeakTracker A single file drop-in memory leak tracking solution for C++ on Windows This small piece of code allows for global memory leak tracking

null 22 Jul 18, 2022
Dump the memory of a PPL with a userland exploit

PPLdump This tool implements a userland exploit that was initially discussed by James Forshaw (a.k.a. @tiraniddo) - in this blog post - for dumping th

Clément Labro 707 Dec 29, 2022
Implementation of System V shared memory (a type of inter process communication) in xv6 operating system.

NOTE: we have stopped maintaining the x86 version of xv6, and switched our efforts to the RISC-V version (https://github.com/mit-pdos/xv6-riscv.git)

Viraj Jadhav 5 Feb 21, 2022
An In-memory Embedding of CPython

An In-memory Embedding of CPython This repository contains all the build artifacts necessary to build an embedding of CPython 3.8.2 that can be run en

null 105 Dec 5, 2022
Execute MachO binaries in memory using CGo

Execute Thin Mach-O Binaries in Memory This is a CGo implementation of the initial technique put forward by Stephanie Archibald in her blog, Running E

Dwight Hohnstein 68 Dec 2, 2022
Initialize the 8-bit computer memory with a program to be executed automatically on powering.

Initialize the 8-bit computer memory with a program to be executed automatically on powering. This project is small extension of Ben Eater's computer

Dmytro Striletskyi 64 Aug 27, 2022
Artifacts of that Memory Management Tsoding Session

Artifacts of those Memory Management Tsoding Sessions Quick Start $ make $ ./heap Limitations The pointers to the heap can only be located in the heap

Tsoding 73 Dec 26, 2022
PoC for CVE-2021-32537: an out-of-bounds memory access that leads to pool corruption in the Windows kernel.

CVE-2021-32537: Out-of-bounds access in RTKVHD64 leading to pool corruption. This is a bug that I reported to Realtek beginning of April 2021. The aff

Axel Souchet 55 Aug 23, 2022
A simple windows driver that can read and write to process memory from kernel mode

ReadWriteProcessMemoryDriver A simple windows driver that can read and write to process memory from kernel mode This was just a small project for me t

Hypervisor 8 Dec 7, 2022