{"id":1496,"date":"2016-03-07T12:23:19","date_gmt":"2016-03-07T20:23:19","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=1496"},"modified":"2022-11-28T09:02:30","modified_gmt":"2022-11-28T17:02:30","slug":"visualizing-complex-data-structures-with-natvis-and-visualgdb","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/custom\/natvis\/","title":{"rendered":"Visualizing complex data structures with Natvis and VisualGDB"},"content":{"rendered":"<p>This tutorial shows how to visualize complex data structures using the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/jj620914.aspx\">Microsoft Natvis files<\/a> and VisualGDB. Before you begin, install VisualGDB 5.1 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open any of the VisualGDB Project Wizards. In this example we will create a Linux project, however the same technique will also work for Embedded, Windows and Android projects.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-natvistest.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1497\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-natvistest.png\" alt=\"01-natvistest\" width=\"874\" height=\"512\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-natvistest.png 874w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-natvistest-300x176.png 300w\" sizes=\"(max-width: 874px) 100vw, 874px\" \/><\/a><\/li>\n<li>As Natvis files do not require any special steps in the project wizard, you can simply proceed with the default ones. In this example we will create a basic Linux project: <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-prjtype.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1498\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-prjtype.png\" alt=\"02-prjtype\" width=\"682\" height=\"641\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-prjtype.png 682w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-prjtype-300x282.png 300w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/a><\/li>\n<li>If you are creating a Linux project, select your target computer and press &#8220;Next&#8221;. Otherwise, proceed with the default settings of the wizard you are using:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-computer.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1499\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-computer.png\" alt=\"03-computer\" width=\"682\" height=\"641\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-computer.png 682w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-computer-300x282.png 300w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/a><\/li>\n<li>On the last page of the wizard press &#8220;Finish&#8221; to generate a basic project:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-fileloc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1500\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-fileloc.png\" alt=\"04-fileloc\" width=\"682\" height=\"641\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-fileloc.png 682w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-fileloc-300x282.png 300w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/a><\/li>\n<li>Press Ctrl-Shift-B to build it. Ensure that there are no build errors:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1501\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-build.png\" alt=\"05-build\" width=\"799\" height=\"615\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-build.png 799w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-build-300x231.png 300w\" sizes=\"(max-width: 799px) 100vw, 799px\" \/><\/a><\/li>\n<li>Now we will add a very basic class template for linked lists. Simply replace the contents of your main file with the following:\n<pre class=\"\">template &lt;typename _Ty&gt; class LinkedList\r\n{\r\npublic:\r\n\u00a0\u00a0 \u00a0struct Node\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0_Ty Value;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Node *pNext;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Node(const _Ty &amp;val, Node *next = NULL)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Value = val;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0pNext = next;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0};\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0void Add(const _Ty &amp;value)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Node *pNode = new Node(value);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (!m_pFirst)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0m_pFirst = pNode;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (m_pLast)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0m_pLast-&gt;pNext = pNode;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0m_pLast = pNode;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0m_Size++;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0LinkedList()\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0: m_pFirst(NULL)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0, m_pLast(NULL)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0, m_Size(0)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0~LinkedList()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0for (Node *pNode = m_pFirst, *pNextNode; pNode; pNode = pNextNode)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0pNextNode = pNode-&gt;pNext;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0delete pNode;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\nprivate:\r\n\u00a0\u00a0 \u00a0Node *m_pFirst;\r\n\u00a0\u00a0 \u00a0Node *m_pLast;\r\n\u00a0\u00a0 \u00a0int m_Size;\r\n};\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n\u00a0\u00a0 \u00a0LinkedList&lt;int&gt; list;\r\n\u00a0\u00a0 \u00a0for (int i = 0; i &lt; 5; i++)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0list.Add(i * i);\r\n\u00a0\u00a0 \u00a0return 0;\r\n}<\/pre>\n<\/li>\n<li>Then press F5 to build and debug your project:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-list.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1502\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-list.png\" alt=\"06-list\" width=\"799\" height=\"617\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-list.png 799w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-list-300x232.png 300w\" sizes=\"(max-width: 799px) 100vw, 799px\" \/><\/a>Hover the mouse over the instance of your list. Notice that by default you won&#8217;t easily see the contents of the entire list, as only the first and the last elements are directly referenced from the list object.<\/li>\n<li>We will now create a .natvis file that will tell VisualGDB how to display the contents of your list. Right-click in the Solution Explorer, select <strong>Add-&gt;New Item<\/strong> and pick the Debugger visualization file from Visual C++-&gt;Utility:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-newnatvis.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1503\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-newnatvis.png\" alt=\"07-newnatvis\" width=\"786\" height=\"487\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-newnatvis.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-newnatvis-300x186.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a>\u00a0If this item type does not appear, simply create a .txt file and change its extension to .natvis later.<\/li>\n<li>If you are using Advanced CMake, please make sure you add the file under the project-wide &#8220;<strong>Special Files<\/strong>&#8221; node and not under target-wide &#8220;<strong>Source Files<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/natvis.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7999\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/natvis.png\" alt=\"\" width=\"1208\" height=\"867\" \/><\/a><\/li>\n<li>Replace the contents of the .natvis file with the following:\n<pre class=\"\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt; \r\n&lt;AutoVisualizer xmlns=\"http:\/\/schemas.microsoft.com\/vstudio\/debugger\/natvis\/2010\"&gt; \r\n\u00a0 &lt;Type Name=\"LinkedList&amp;lt;*&amp;gt;\"&gt;\r\n\u00a0\u00a0\u00a0 &lt;DisplayString&gt;List of {m_Size} elements&lt;\/DisplayString&gt;\r\n\u00a0\u00a0\u00a0 &lt;Expand HideRawView=\"0\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;LinkedListItems&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Size&gt;m_Size&lt;\/Size&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;HeadPointer&gt;m_pFirst&lt;\/HeadPointer&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;NextPointer&gt;pNext&lt;\/NextPointer&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;ValueNode&gt;Value&lt;\/ValueNode&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/LinkedListItems&gt;\r\n\u00a0\u00a0\u00a0 &lt;\/Expand&gt;\r\n\u00a0 &lt;\/Type&gt;\r\n&lt;\/AutoVisualizer&gt;<\/pre>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-natvisxml.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1504\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-natvisxml.png\" alt=\"08-natvisxml\" width=\"799\" height=\"617\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-natvisxml.png 799w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-natvisxml-300x232.png 300w\" sizes=\"(max-width: 799px) 100vw, 799px\" \/><\/a>This will tell VisualGDB to:<\/p>\n<ul>\n<li>Apply special displaying rules to all types matching the &#8220;LinkedList&lt;*&gt;&#8221; pattern<\/li>\n<li>Replace the string value of the expression with &#8220;List of {m_Size} elements&#8221; where {m_Size} is replaced with the value of the corresponding field<\/li>\n<li>Treat the object as a linked list where <strong>m_Size<\/strong> contains the list size, <strong>m_pFirst<\/strong> points to the first node and <strong>&lt;node&gt;.pNext<\/strong> points to the next node<\/li>\n<\/ul>\n<\/li>\n<li>Now start debugging again by pressing F5 (no rebuild is required) and hover the mouse over &#8216;list&#8217;. VisualGDB will display the actual contents of the list according to the rules you specified in the .natvis file:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-visualized.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1505\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-visualized.png\" alt=\"09-visualized\" width=\"799\" height=\"617\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-visualized.png 799w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-visualized-300x232.png 300w\" sizes=\"(max-width: 799px) 100vw, 799px\" \/><\/a>You can use other Natvis constructs described <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/jj620914.aspx\">here<\/a> to describe displaying rules for strings, arrays, trees and other structures. Note that as of v5.1 VisualGDB does not support the <strong>CustomListItems<\/strong> rule.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to visualize complex data structures using the Microsoft Natvis files and VisualGDB. Before you begin, install<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[38,118],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1496"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=1496"}],"version-history":[{"count":4,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1496\/revisions"}],"predecessor-version":[{"id":8001,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1496\/revisions\/8001"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=1496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=1496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=1496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}