[net]如何将调试代码注入程序集?

发布于 2024-08-02 17:14:07 字数 584 浏览 21 评论 0原文

给定一个具有如下入口点的程序集:

int FooClass::doFoo(int x, double y)
{
   int ret;
   // Do some foo
   return ret;
}

是否可以使用另一个程序集来模拟类似的内容:

int FooClass::doFoo(int x, double y)
{
   int ret;
   TRACE_PARAM_INT(x)
   TRACE_PARAM_DOUBLE(y)
   // Do some foo
   TRACE_RETURN_INT(ret)
   return ret;
}

并且仅在存在 DEBUG 时启用此代码注入。如果有这样的方式,你如何加载“调试”程序集?

编辑1:#ifdef 不是一个选项。比如说,我不想修改代码库。

编辑2:我的主要问题是“如何将代码注入到已编译的程序集中”。我确实有基本代码,但我不想在该主代码中添加用于跟踪的 K 行,而是使用另一个程序集来执行此操作。我确实知道如何使用 VS 进行调试,我想要的是添加变量跟踪机制(除其他外)。

Given an assembly with an entry point like:

int FooClass::doFoo(int x, double y)
{
   int ret;
   // Do some foo
   return ret;
}

Is it possible to use yet another assembly to simulate something like:

int FooClass::doFoo(int x, double y)
{
   int ret;
   TRACE_PARAM_INT(x)
   TRACE_PARAM_DOUBLE(y)
   // Do some foo
   TRACE_RETURN_INT(ret)
   return ret;
}

And only enable this code injection when DEBUG is present. If there is such way, how do you load the "debugging" assembly?

EDIT 1: #ifdef is not an option. Say, I don't want to modify the code base.

EDIT 2: My main question is "How to INJECT code to an already compiled assembly". I do have the base code but I'd rather not add the K of lines for tracing in that main code but have another assembly that do such. I do know how to use VS to debug, what I want to is add tracing mechanism of variables (among other things).

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

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

发布评论

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

评论(6

倦话 2024-08-09 17:14:07

您可以尝试使用 AOP 后编译器,例如 PostSharp。它适用于所有 .net 语言,但我还没有尝试过使用 C++。

You could try an AOP post-compiler like PostSharp. It works with all .net languages, but I have not tried it with C++.

对风讲故事 2024-08-09 17:14:07

为了将代码注入到现有程序集中,我将使用 Cecil 库,它可以让您使用伊利诺伊州。如果您想要的话,这将允许您重写程序集。我必须警告你:这不是一件小事。

哦,还有一个名为 Reflexil 的 Reflector 插件,可以让您编辑程序集。

顺便说一句,基于 AOP 的跟踪不会将代码直接添加到程序集中。您可以将所有 AOP 内容保存在一个单独的程序集中(事实上,这是一个非常好的主意),然后将其与属性一起应用。 PostSharp 将为您硬连线代码,但其他 AOP 框架(例如 Spring 或 PIAB)使事情变得更加灵活,因为它们使用动态代理,因此您可以在不需要时有效地“关闭”方面。

For injecting code into an existing assembly, I would use the Cecil library, which lets you work with IL. This would let you rewrite the assembly if that's what you're after. I have to warn you: it's no small feat.

Oh, there's also an add-in for Reflector, called Reflexil, which lets you edit assemblies.

By the way, AOP-based tracing doesn't add code directly to your assembly. You can keep all the AOP stuff in a separate assembly (in fact, it's a very good idea), and then apply it with attributes. PostSharp will hard-wire code for you, but other AOP frameworks such as Spring or PIAB make things more flexible as they use dynamic proxies, so you can effectively 'turn off' your aspects when they are not needed.

友谊不毕业 2024-08-09 17:14:07

企业库策略注入应用程序块允许您在方法之间执行代码来电。它不会做您在问题中要求的复杂事情,其中​​涉及在方法内注入代码,但它可能足以满足您的需求,并且可以免费使用。

The Enterprise Library Policy Injection Application Block allows you to execute code between method calls. It wont do the complex things you've asked for in your question which involve injecting code inside methods, but it may be sufficient for your needs and it's freely availably.

给妤﹃绝世温柔 2024-08-09 17:14:07

您是否看过 CThru (http://cthru. codeplex.com/Wiki/View.aspx?title=使用%20the%20CThru%20Engine)?

它允许您跟踪方法调用、构造函数调用等,以便您可以从外部跟踪所有内容。它是模拟库的一部分,但它更有用。

Have you looked at CThru (http://cthru.codeplex.com/Wiki/View.aspx?title=Using%20the%20CThru%20Engine)?

It allows you trace method calls, constructor calls, etc so you could trace everything from the outside. It is part of a mocking library but it is so much more useful.

风为裳 2024-08-09 17:14:07

如果 doFoo 函数不是虚拟的,或者该类不是通过接口访问的,则无法执行此操作。

原因是,当您编译使用 doFoo 的类时,会编译为调用该确切类上的确切函数。因此,接收调用的目标类是在编译时评估的。
但是,如果该函数是虚拟函数,或者您通过接口访问该类,则将在运行时评估用于接收调用的目标类。

因此,为了使用任何 AOP(面向方面​​编程)或 DI(直接注入)框架来完成您想要的任务,目标类需要满足这些条件之一。通常,通过接口访问类是首选方式。

有许多不同的 AOP 和 DI 框架,但这篇文章并没有讨论使用哪一个,所以我不会这样做,但如果你不需要这样的框架,你可以使用 DynamicProxy 创建一个装饰器来添加日志记录功能,既可以添加到接口中,也可以添加到具有虚函数的类中。后者创建一个新类,它是您已有类的子类

If the doFoo function is not virtual, or if the class is not accessed through an interface, you cannot do it.

The reason is that when you are compiling the class that uses the doFoo is compiled to call the exact function on that exact class. So the target class for receiving the call is evaluated at compile time.
If the function is virtual however, or you access the class through an interface, the target class for receiving the call is evaluated at runtime.

So in order to use any AOP (aspect oriented programming) or DI (direct injection) frameworks for accomplishing what you want, the target class needs to fulfill either of these conditions. Normally accessing the class through an interface would be the preferred way.

There are many different AOP and DI frameworks out there, but the post was not about which one to use, so I'll keep myself from doing that, but if you don't need such one, you can use DynamicProxy to create a decorator to add logging functionality, both to an interface, and to a class with virtual functions. The latter creates a new class that is a subclass of the one that you already have

酒几许 2024-08-09 17:14:07

取决于您是否拥有程序集的源代码以及是否能够重新编译它以允许调试。

假设您有源代码,并且可以在启用调试的情况下对其进行编译,那么您应该能够使用开发工具(我猜是 Visual Studio)单步执行代码并查看 X、Y 的值然后边走边休息。

这不需要修改代码——只需要编译调试版本的能力。

Depends on whether you have the source code for the assembly and whether you're able to recompile it to allow debugging.

Assuming that you have the source code, and you can compile it with debugging enabled, then you should be able to use your developer tool (Visual Studio, I'm guessing) to single step through the code and see the values of X, Y and ret, as you go.

This would not requie modification of the code - just the ability to compile a debug version.

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