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.
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.
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.
You can go to the quick-watch window - Ctrl + Alt + Q , enter the variable name there, and press Add Watch .
This could help:
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With