使用 c++调试器作为反射替代品

发布于 2024-11-28 18:45:22 字数 835 浏览 3 评论 0原文

这个问题与我本周早些时候问的这个问题有些相关< /a>.我仍在处理 C++ 无法正确反映其自身代码的问题。现在,虽然我遇到了几个以某种方式尝试添加此功能的库,但我想问一下,是否无法使用更容易获得和广泛使用(即更容易实现)的方法来实现我需要做的事情。

我需要的基本上是某个时间点的对象实例的快照。它们可以有子对象(即一个对象“拥有”一个或多个其他对象的某种结构),但基本上就是这样。现在,虽然反射似乎是一个显而易见的选择,但由于项目始终使用 C++ 进行编码,因此几乎已经排除了这种选择。然而,调试器似乎做了我需要的事情(在某个点停止程序并告诉我当前内存中的实例),所以我觉得我应该能够使用简单的调试技术来实现某种信息提取,如可以假定给定项目的代码可用。我的程序中的工作流程可能类似于

  1. 在调试模式下编译c++代码
  2. 创建并附加调试器
  3. 设置断点(据我所知,方法调用表明所有需要的实例都已创建)
  4. 运行并检查实例(和子实例)某些类(及其继承类型),提取此信息以进行进一步处理

虽然这基本上是我在调试时通常所做的事情,但我无法找到有关如何从另一个程序中自动执行此操作的任何信息。这样做的优点是无需任何进一步的库或代码更改即可实现有限的反射。那么,有什么方法 - 最好是用 Java、C# 或 C++,因为我最熟悉这些 - 只加载和编译 VS 解决方案或执行 makefile 并执行上面给出的步骤?我不断找到很多页面告诉我如何使用调试器,但这在这里真的没有用......

感谢您的帮助。

This question is somewhat related to this one I asked earlier this week. I'm still dealing with C++'s inability to properly reflect over its own code. Now, while I've come across several libraries that somehow try to add this functionality, I'd like to ask if what I need to do can't be achieved using more readily available and widely used (ie simpler to implement) methods.

What I need is basically a snapshot of object instances at a certain point in time. They can have sub-objects (ie some structure where an object "has" one or more other objects), but that's basically it. Now, while reflection seemed like an obvious choice, this has been pretty much ruled out due to the projects always being coded in C++. However, a debugger seems to do just what I need (stop the program at a certain point and tell me about the instances currently in memory), so I feel like I should be able to achieve some kind of information extraction using simple debugging techniques as the code of the given projects can be assumed to be available. The workflow within my program could look like

  1. compile c++ code in debug mode
  2. create and attach debugger
  3. set breakpoint (as I know the method call which indicates that all needed instances have been created)
  4. run and check for instances (and sub-instances) of a certain class (and its inherited types), extract this information for further processing

While this is basically what I usually do while debugging, I have been unable to find any information about how to do this automatically from within another program. This would have the advantage of achieving kind of a limited reflection without any further libraries or code changes. So, is there any way - preferrably in Java, C# or C++, as I'm most familiar with these - to just load and compile a VS solution or execute a makefile and perform the steps given above? I keep finding a lot of pages that tell me how to use a debugger, but that's really not useful here...

Thanks for any help.

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

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

发布评论

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

评论(4

半衾梦 2024-12-05 18:45:23

难道您不能简单地将代码转换为 .NET 应用程序并使用 .NET 反射吗?

Couldn't you simply convert the code to a .NET application and use .NET reflection?

岁月染过的梦 2024-12-05 18:45:23

如果您想要查看在程序执行的某个时刻分配了哪些内容,那么代码分析器可能会有所帮助。代码分析器可以向您显示代码在特定时间点正在执行的操作的快照。在 Linux 上,valgrind 是一个流行的选择。 Visual Studio 应该有一个分析器,尽管我从未使用过它。

If what you are trying to do is to see what has been allocated at a certain point in the execution of your program, a code profiler might help. A code profiler can show you snapshots of what your code is doing at particular points in time. On Linux, valgrind is a popular choice. Visual Studio is supposed to have a profiler, although I've never used it.

碍人泪离人颜 2024-12-05 18:45:22

如果 Visual Studio 不是必须的,您可以看看 GDB。该调试器可以通过编程方式进行控制,它实际上被一些调试器 GUI 使用,因此您应该能够使用它执行步骤 2-4。对于第一步,在这种情况下显而易见的选择是 gcc。

If Visual Studio is not a must, you could have a look at GDB. This debugger can be controlled programmatically, it is actually used by some debugger GUIs, so you should be able to perform the steps 2-4 with it. For the first step, the obvious choice in this context is gcc.

暖阳 2024-12-05 18:45:22

好吧,因为从您的其他问题看来,您的平台是 Windows,原则上您可以使用 Windows 调试工具或 Windows SDK 包中提供的 Windbg 和相关(仅限命令行)调试器来执行此操作。

有了它,您可以轻松地以自动化方式执行任务 2 到 4 - 尽管 4(自动检查实例)我认为对于本机(C++)代码不可能自动执行,因为正如您已经提到的,C++ 缺少类似.net 代码中的反射机制。

现在我不确定所有这些努力是否真的值得。如果您想要完成的只是调试/调整应用程序,您可以使用条件编译来调试/发布代码,并添加额外的代码以进行跟踪。

Well since it seems from your other question that your plattform is Windows you could do this in principle with windbg and related (command line only) debuggers available in the Debugging Tools for Windows or Windows SDK package.

With it you can easily perform tasks 2 to 4 in an automated way - although 4 (checking automatically for instances) would I think not be possible to do automatically for native (C++) code since, as you already mentioned, C++ lacks something like the reflection mechanismus in .net code.

Now I'm not sure if all this effort will be really worth it. If all you are trying to accomplish is to debug/tune your application you can use conditional compiling for debug/release code and add extra code for tracing purposes.

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