{"id":7632,"date":"2021-12-23T10:23:05","date_gmt":"2021-12-23T18:23:05","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=7632"},"modified":"2021-12-23T10:23:05","modified_gmt":"2021-12-23T18:23:05","slug":"understanding-high-level-code-structure-with-codeexplorer","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/intellisense\/codeexplorer\/structure\/","title":{"rendered":"Understanding High-Level Code Structure with CodeExplorer"},"content":{"rendered":"<p>This tutorial shows how to use CodeExplorer to quickly analyze high-level structure of large projects, and to find relevant pieces of code. We will load the Sysprogs port of <a href=\"https:\/\/github.com\/sysprogs\/openocd\/\">OpenOCD<\/a> &#8211; an open-source tool that works with JTAG\/SWD debuggers, and will use Code Explorer to quickly understand what functions are related to certain types and scenarios.<\/p>\n<p>Before you begin, install VisualGDB 5.6 or later.<\/p>\n<ol>\n<li>Follow <a href=\"https:\/\/visualgdb.com\/tutorials\/arm\/openocd\/build\/\">this tutorial<\/a> to clone our OpenOCD repository and open it in Visual Studio. You can also use the techniques shown below to analyze your own code base &#8211; all functionality shown in this tutorial will work for any project type that is using Clang IntelliSense.<\/li>\n<li>We will begin with looking up the code that handles the &#8220;reset&#8221; command, and will use Code Explorer to find other command-related types and functions. Search the entire solution for <strong>&#8220;reset&#8221;<\/strong> (with quotes) and double-click on the last entry in the <strong>target.c<\/strong> file:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/00-reset.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7633\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/00-reset.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a><\/li>\n<li>Then, click on <strong>handle_reset_command<\/strong> and press F12. This will navigate to the <strong>handle_reset_command()<\/strong> function highlighting it in the Code Explorer outline. If the Code Explorer window is not active, you can open it via View-&gt;VisualGDB Code Explorer:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/01-reset-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7635\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/01-reset-1.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a>Note that the <strong>handle_reset_command()<\/strong> function is defined via a preprocessor macro, so its argument types are not clearly visible in the source code. However, the Outline view in Code Explorer does show them correctly. Take a note of the &#8220;<strong>struct command_invocation<\/strong>&#8221; shown in the Outline, and switch Code Explorer into the globals view via a button in the top left corner of Code Explorer.<\/li>\n<li>If this is the first time you open the Globals view for this project, VisualGDB will build a global symbol cache. It typically takes a few minutes, depending on the size of the project. For OpenOCD, it should take around 1-2 minutes:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/02-cache.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7636\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/02-cache.png\" alt=\"\" width=\"1355\" height=\"822\" \/><\/a><\/li>\n<li>Locate <strong>handle_reset_command<\/strong> in the globals view (or use the button in the top right corner of the editor) and go to the Code Explorer Details view:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/type.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7637\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/type.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a>See how the &#8220;Referenced Types&#8221; node shows all types referenced by the function (i.e. used by its arguments and variables), including the &#8220;<strong>command_invocation<\/strong>&#8221; struct. Right-click on <strong>command_invocation<\/strong> and select &#8220;Locate in Symbol List&#8221;.<\/li>\n<li>This will show all information about <strong>command_invocation<\/strong>, including all functions referencing it, all placed in the code where it is instantiated, and any other structures it uses as fields:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/03-refs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7638\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/03-refs.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a><\/li>\n<li>As navigating all the information at once could be distracting, use the relations filter to restrict it to just allocating\/deleting functions and incoming calls:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/04-restrict.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7639\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/04-restrict.png\" alt=\"\" width=\"1355\" height=\"821\" \/><\/a>Hint: hold Shift while clicking on a checkbox in the Code Explorer filters to automatically uncheck all other checkboxes.<\/li>\n<li>Now you can see that <strong>command_invocation<\/strong> structure used by all command handlers is only instantiated in the <strong>run_command()<\/strong> function and is allocated on stack (the &#8220;{&#8221; icon). You can further see that <strong>run_command()<\/strong> is only called by <strong>script_command_run()<\/strong>, that is in turn called by <strong>command_unknown()<\/strong> and <strong>script_command()<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/05-tree.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7640\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/05-tree.png\" alt=\"\" width=\"1355\" height=\"822\" \/><\/a>This gives a clear picture of the functions involved in processing of the OpenOCD commands.<\/li>\n<li>Now we will explore some other command implementations. Reset the relation type filter to show all relations and look through the references of <strong>command_invocation<\/strong>. It will show all functions and structs that have a parameter, variable or field of type <strong>command_invocation<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/07-refs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7643\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/07-refs.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a>Note how most of them are additionally flagged with the &#8220;reference&#8221; symbol. If you hover the mouse pointer over it, VisualGDB will show a tooltip explaining that the corresponding references are pointers. Note that each subtype flag (such as &#8220;reference&#8221;) has a corresponding filter button in the details toolbar.<\/li>\n<li>Click on the &#8220;pointers&#8221; filter in the toolbar. This will hide all pointers to <strong>command_invocation<\/strong>, leaving out only instances:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/08-inst.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7644\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/08-inst.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a>This means that no other struct includes an instance of <strong>command_invocation<\/strong> as a field, and no other function instantiates it.<\/li>\n<li>Turn the reference filter back on and enter &#8220;reset&#8221; in the filter. This will show all functions accepting &#8220;<strong>command_invocation *<\/strong>&#8221; as an argument that have &#8220;reset&#8221; in their name. Expand the <strong>handle_soft_reset_halt_command()<\/strong> function and look for the types it references:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/09-target.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7645\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/09-target.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a>This shows the types of all parameters and local variables of the function. The &#8220;accessed data&#8221; section below shows all fields and global variables referenced by the function. Right-click on the &#8220;target&#8221; type and select &#8220;locate in the Symbol List&#8221;.<\/li>\n<li>Code Explorer will now show the detailed information about the <strong>target<\/strong> struct. Note that the <strong>references <\/strong>list shows other types and functions that have a field\/variable of this type, and the <strong>instances<\/strong> list shows the exact fields\/variables:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/10-inst.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7646\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/10-inst.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a><\/li>\n<li>Normally, Code Explorer will only index globally visible entities (i.e. not specific local variables), however you can change this behavior by setting the &#8220;index types of local variables and parameters&#8221; checkbox:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/locals-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7648\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/locals-1.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a><\/li>\n<li>Scroll the Details view to &#8220;referenced types&#8221; list. It shows all types referenced by fields of the <strong>target<\/strong> structure. Note how pointers and references are shown with different tags:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/12-type.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7649\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/12-type.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a>Locate the <strong>target_type <\/strong>field and select &#8220;Locate in the Symbol List&#8221; from the context menu.<\/li>\n<li>This will show all fields of <strong>struct target_type<\/strong>. Select the &#8220;add_breakpoint&#8221; field that contains a pointer to the target-specific breakpoint setting function to view all information about it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/13-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7650\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/13-bkpt.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a>See how it shows all functions &#8220;implementing&#8221; this field (i.e. being assigned to it anywhere in the code), all functions reading\/writing it, and the only function calling it.<\/li>\n<li>We will now use Code Explorer to locate other breakpoint-related functions. Find &#8220;struct breakpoint&#8221; among the types referenced by <strong>target_add_breakpoint<\/strong> and select it in the symbol list. Then scroll the list of functions referencing it to the ones starting with &#8220;target&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/14-favs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7651\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/14-favs.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a>Flag the 4 functions shown above as &#8220;favorite&#8221; by clicking in the &#8220;favorite&#8221; column.<\/li>\n<li>You can use the &#8220;favorite&#8221; flag to quickly flag functions for further review, and then instantly locate them in the symbol list. Enable the &#8220;favorites only&#8221; view in Symbol List, limit the displayed relations to <strong>Outgoing Calls<\/strong>, and select the &#8220;<strong>target_add_context_breakpoint()<\/strong>&#8221; and &#8220;<strong>target_add_hybrid_breakpoint()<\/strong>&#8221; functions:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/15-tree.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7652\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/15-tree.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a>Code Explorer will find and display the common root &#8211; function that has all the selected function among its children on the call tree. If multiple roots are found, all of them will be displayed.<\/li>\n<li>In large projects, the call tree can quickly grow huge even if inspecting a handful of functions. Use the &#8220;favorites only&#8221; filter in the Detail view to restrict it to favorite functions only:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/16-inv.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7653\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/16-inv.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a>This instantly shows that both <strong>target_add_context_breakpoint()<\/strong> and <strong>target_add_hybrid_breakpoint()<\/strong> can only be reached through a call to <strong>handle_bp_command_set()<\/strong> function.<\/li>\n<li>You can manage multiple sets of favorite functions by saving the favorite lists to files. This allows revisiting certain combinations of functions (e.g. breakpoint-related functions) once the code base changes, and quickly getting up-to-date information about them:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/save.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7655\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/save.png\" alt=\"\" width=\"1271\" height=\"861\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use CodeExplorer to quickly analyze high-level structure of large projects, and to find relevant pieces<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[134],"tags":[236,52],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/7632"}],"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=7632"}],"version-history":[{"count":3,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/7632\/revisions"}],"predecessor-version":[{"id":7656,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/7632\/revisions\/7656"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=7632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=7632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=7632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}