Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all global variables in C++ source code, DLL or any file created by the VC++ compiler?

I'm making my application thread-safe. One of the steps is to synchronize access or eliminate usages of global variables. I'm using Visual Studio. I can't find any good way to find all global variables in my codebase. It's impossible to create a good text search pattern and I can't find any helpful tool. Do you guys know any good way to do that? It could be a source code analysis tool or a binary file analyzer.

like image 763
Michał Fronczyk Avatar asked Sep 15 '11 11:09

Michał Fronczyk


People also ask

How do you access global variables?

Global Variable: The variable that exists outside of all functions. It is the variable that is visible from all other scopes. We can access global variable if there is a local variable with same name in C and C++ through Extern and Scope resolution operator respectively.

Where are global variables stored in assembly?

Global variables are stored in the data section. Unlike the stack, the data region does not grow or shrink — storage space for globals persists for the entire run of the program.

How do I view global variables in Visual Studio?

You can go to the quick-watch window - Ctrl + Alt + Q , enter the variable name there, and press Add Watch .


2 Answers

This could help:

  1. Open the project in visual studio.
  2. Open 'Class View' of the project
  3. Under the project title, you will find 'Global Functions and Variable'.

I have checked this with Visual Studio 2010 and above.

Edit: As suggested by Ajay in comments, you could also categorize items in groups. For grouping items:

  1. In class view, right click on project title
  2. Select `Group By Object/Member Type'
  3. Select the required tree like variables or structures or enums etc.
like image 88
Amber Beriwal Avatar answered Sep 21 '22 03:09

Amber Beriwal


One option might be letting the linker generate a map file (/MAP in Visual Studio).

You will get a .map file for each binary with two sections:

A table of segments

 Start         Length     Name                   Class  0001:00000000 00010000H .textbss                DATA  0002:00000000 000034b4H .text                   CODE  0003:00000000 00000104H .CRT$XCA                DATA  0003:00000104 00000104H .CRT$XCAA               DATA  0003:00000208 00000104H .CRT$XCZ                DATA  0003:0000030c 00000104H .CRT$XIA                DATA  ... 

A list of symbols (functions and data)

  Address         Publics by Value              Rva+Base       Lib:Object   0000:00000000       ___safe_se_handler_count   00000000     <absolute>  0000:00000000       ___safe_se_handler_table   00000000     <absolute>  0000:00000000       ___ImageBase               00400000     <linker-defined>  0001:00000000       __enc$textbss$begin        00401000     <linker-defined>  0001:00010000       __enc$textbss$end          00411000     <linker-defined>  0002:000003a0       _wmain                     004113a0 f   console4.obj  ... 

You can tell apart the functions from variables by the "CODE" / "DATA" designaiton in the segment list.

Advantage: You will get all symbols, even those in libraries, that were not removed by the Linker.

Disadvanatge: You will get all symbols, even those in libraries, that were not removed by the Linker. I don't know of any tool that does the code/data separation automatically.

like image 26
peterchen Avatar answered Sep 23 '22 03:09

peterchen