如何查找C++中的所有全局变量源代码、DLL 或 VC 创建的任何文件编译器?

发布于 2024-12-04 14:34:48 字数 152 浏览 0 评论 0原文

我正在使我的应用程序线程安全。步骤之一是同步访问或消除全局变量的使用。我正在使用 Visual Studio。我找不到任何好方法来查找代码库中的所有全局变量。创建一个好的文本搜索模式是不可能的,而且我找不到任何有用的工具。大家知道有什么好的办法吗?它可以是源代码分析工具或二进制文件分析器。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

惟欲睡 2024-12-11 14:34:48

这可能会有所帮助:

  1. 在 Visual Studio 中打开项目。
  2. 打开项目的“类视图”
  3. 在项目标题下,您将找到“全局函数和变量”。

我已经使用 Visual Studio 2010 及更高版本对此进行了检查。

编辑:根据Ajay在评论中的建议,您还可以将项目分组。对于分组项目:

  1. 在类视图中,右键单击项目标题
  2. 选择“按对象/成员类型分组”
  3. 选择所需的树,例如变量或结构或枚举等。

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.
鹊巢 2024-12-11 14:34:48

一种选择可能是让链接器生成映射文件(Visual Studio 中的 /MAP)。

您将获得每个二进制文件的 .map 文件,其中包含两个部分:

段表

 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
 ...

符号列表(函数和数据)

  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
 ...

您可以通过以下方式区分函数和变量:段列表中的“代码”/“数据”指定。

优点:您将获得链接器未删除的所有符号,甚至是库中的符号。

缺点:您将获得链接器未删除的所有符号,甚至是库中的符号。我不知道有什么工具可以自动进行代码/数据分离。

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.

电影里的梦 2024-12-11 14:34:48

我知道 http://code.google.com/p/data- race-test/wiki/ThreadSanitizer 程序(google 的产品),可以在 Windows 和编译代码上运行。它是动态检测程序(如 valgrind 或有点像 qemu/virtualbox),它为内存访问添加了一些检查。它将尝试发现一些线程问题。您可以在threadsanitizer的控制下运行您的程序。动态转换和检测代码将会导致速度减慢(速度减慢 20 倍至 50 倍)。但有些问题会被自动检测到。

它还允许您在源代码中注释一些自定义同步函数。

程序的 Wiki 有到其他线程竞争检测器的链接: http://code .google.com/p/data-race-test/wiki/RaceDetectionLinks

I know the http://code.google.com/p/data-race-test/wiki/ThreadSanitizer program (product of google) which can work in Windows and on compiled code. It is dynamic instrumentation program (like valgrind or bit like qemu/virtualbox), which add some checks to memory accesses. It will try to find some threading problems. You can just run your program under control of threadsanitizer. There will be slowdown from dynamic translation and from instrumentation code (up to 20x-50x times slower). But Some problems will be detected automatically.

It also allows you to annotate some custom synchronization functions in source code.

Wiki of program has links to other thread-race detectors: http://code.google.com/p/data-race-test/wiki/RaceDetectionLinks

撩心不撩汉 2024-12-11 14:34:48

cppclean 是一个可以帮助您的静态分析工具。来自文档:

cppclean finds global/static data that are potential problems when using threads.

下面是一个带有静态局部变量和全局变量的简单示例。

./example.h:

void foo();

./example.cpp:

#include "example.h"

int globalVar = 42;

void foo(){
    static int localStatic = 0;
    localStatic++;
}

打开终端并运行 cppclean,如下所示:

$ cppclean --include-path . example.cpp
example.cpp:3: static data 'globalVar'
example.cpp:6: static data 'localStatic'

不幸的是,cppclean 有一些 解析问题错误。然而,这些问题非常罕见,并且影响的比例低于我测试过的所有代码的百分比。

cppclean is a static analysis tool that can help you. From the documentation:

cppclean finds global/static data that are potential problems when using threads.

A simple example with a static local variable and a global variable follows.

./example.h:

void foo();

./example.cpp:

#include "example.h"

int globalVar = 42;

void foo(){
    static int localStatic = 0;
    localStatic++;
}

Open a terminal and run cppclean as follows:

$ cppclean --include-path . example.cpp
example.cpp:3: static data 'globalVar'
example.cpp:6: static data 'localStatic'

Unfortunately, cppclean has some parsing issues and bugs. However, these issues are pretty rare, and affected below a percent of all code I've tested.

心不设防 2024-12-11 14:34:48

也许 dumpbin 工具在这里会有所帮助。您可以使用 /SYMBOLS 键运行它来显示 COFF 符号表并查找外部符号 - 全局变量应该在此列表中。 转储/符号

Maybe dumpbin tool will help here. You can run it with /SYMBOLS key to display the COFF symbol table and look for External symbols - global variables should be in this list. DUMPBIN /SYMBOLS.

携君以终年 2024-12-11 14:34:48

clang-query 中,可以使用列出所有全局变量

match varDecl(hasGlobalStorage())

对于更复杂的示例,要查找指向 my_type 的指针的所有全局变量,

match varDecl(hasGlobalStorage(), hasType(pointerType(pointee(hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl(hasName("my_type")))))))))

这会查找所有

  • 传统全局变量
  • static 变量
  • static 类的成员变量

请参阅正在运行clang-query 仅对输入文件 了解如何限制结果。

请注意,如果全局变量出现在从未实例化的模板中,上面将列出它,即使编译的代码中实际上不存在这样的全局变量。

In clang-query all global variables can be listed using

match varDecl(hasGlobalStorage())

For a more complicated example, to find all global variables that are pointers to my_type,

match varDecl(hasGlobalStorage(), hasType(pointerType(pointee(hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl(hasName("my_type")))))))))

This finds all

  • Traditional global variables
  • static variables inside functions
  • static member variables of classes

See Running clang-query only on input files for how to limit the results.

Note that if a global variable occurs in a template that is never instantiated, the above will list it, even though there isn't actually such a global variable in the compiled code.

蓝眸 2024-12-11 14:34:48

你们都把这件事搞得太复杂了。

1.
将每个文件的代码(一次一个与其他文件分开)复制到字符串或宽字符串等,然后解析出从“{”到“}”不包含的所有内容。将结果保存到外部文件。第一次之后,然后追加到该文件。

2.
即使在所有解析之后剩下的内容总共有 1,000 行,其中也包含了所有全局变量(取决于您如何创建全局变量)。如果您通过名称空间等创建它们,则返回并解析它们。我怀疑大多数程序员是否会拥有 1,000 个全局变量,但对于某些应用程序来说,这可能就是他们所使用的。如果此时您没有太多结果,请手动编辑结果的文本文件。

我发现这个网站上 90% 以上的答案可能都过于复杂,只会占用 CPU 时间和内存空间。保持简单。

您可能会发现提前加载一个 globals.h 文件并将大部分或全部全局变量保留在那里会很方便。看来是时候做大量的清理工作了。

You all are making this too complicated.

1.
Copy the code of each of your files (one at a time separate from the others) to a string or a wide string or etc. and then parse out everything that is from "{" to "}" uninclusive. Save the result to an exterior file. After the first time, then append to that file.

2.
Even if you have 1,000 lines total of what is left after all that parsing, in that are all of your globals (depending upon how you created the globals). If you created them via a namespace, etc. then go back and parse for that. I doubt that most programmers will have 1,000 globals, but for some applications it might be what they use. If you do not have too many at that point then manually edit that text file of the results.

I have found that maybe 90+ % of the answers on this site are bloated with far too much complexity that just eats up cpu time and memory space. Keep it simple.

You might find it handy to have a globals.h file which you load early and keep most or all of you globals there. It looks like time to do a lot of clean up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文