简体   繁体   English

从C#调试C ++ dll

[英]Debug C++ dll from C#

I will briefly tell you the situation. 我将简要地告诉你情况。 I got a C# project which uses some DLL created in C++. 我有一个C#项目,它使用了一些用C ++创建的DLL。

Now, separately, I also have a C++ project, which was used to create that DLL some time ago. 现在,另外,我还有一个C ++项目,它曾用于创建该DLL。 Now, I wanted to debug the C++ DLL during running the C# project. 现在,我想在运行C#项目期间调试C ++ DLL。

I enabled "Enable Unmanaged Code Debugging" in my C# project. 我在我的C#项目中启用了“启用非托管代码调试”。

I started debugging C# project and stepping into some functions alongside. 我开始调试C#项目并同时进入一些功能。 All seemed to be ok. 一切似乎都没问题。 When I reached a function which belonged to C++ DLL, it asked for the source of the C++ file, I had to browse to my C++ project. 当我到达一个属于C ++ DLL的函数时,它询问了C ++文件的来源,我不得不浏览到我的C ++项目。 (Before I think it complained about some .pdb files). (在我认为它抱怨一些.pdb文件之前)。

Now, I managed to step into the C++ function also, but as I step over and over, some of the data structures in that function don't seem to be populated with the data, eg, please see screenshot below 现在,我也设法进入了C ++函数,但是当我一遍又一遍时,该函数中的一些数据结构似乎没有填充数据,例如,请参见下面的截图

在此输入图像描述

You can see the blob data structure is empty, and same happened with DataParser (it was showing it had 0 items inside, whereas in code above you can see there are multiple items being added to it). 您可以看到blob数据结构为空, DataParser (它显示内部有0个项目,而在上面的代码中,您可以看到有多个项目被添加到其中)。

I would really appreciate some help, what is going wrong here? 我真的很感激一些帮助,这里出了什么问题? And where I could have done mistake. 而我可能在哪里做错了。 How can I debug this C++ DLL so that I also see what values are assigned to its variables currently? 如何调试此C ++ DLL,以便我还可以看到当前为其变量分配的值?

Maybe my way of debugging this C++ DLL is wrong? 也许我调试这个C ++ DLL的方法是错误的? The fact that the C# project is using an already created DLL, and I have this C++ project which was used to create this DLL some time ago - the fact that they are separate, maybe that has to do something with it also? 事实上,C#项目正在使用已经创建的DLL,并且我有一个用于创建此DLL的C ++项目 - 事实上它们是分开的,也许它也必须用它做什么?

PS Before I had to make changes like this to C++ project and lower toolset because I use VS2012 (strange if project was created using VS2013 though because I think it is old project). PS之前,我不得不做出样改变这种以C ++项目和更低的工具集,因为我使用VS2012(才怪项目是使用VS2013虽然创建,因为我认为这是旧的项目)。 Also the project uses lot of manually written other C++ classes. 该项目还使用了大量手动编写的其他C ++类。 Maybe that is the problem also and somehow the compiler can't retrieve their values and definitions? 也许这也是问题而且不知何故编译器无法检索它们的值和定义?

What are the steps in general to debug a C++ DLL file in a setup like I have? 在像我这样的设置中调试C ++ DLL文件的一般步骤是什么?

EDIT : PPS. 编辑 :PPS。 Also some other interesting facts I have seen. 还有一些我见过的有趣事实。 If I click F11(Step into) on the DataParser.Add function for example, not necessarily I am taken to the body of that function, it shows me body of other function (which might be somehow related to it). 例如,如果我在DataParser.Add函数上单击F11(Step into),不一定我被带到该函数的主体,它向我显示了其他函数的主体(可能与它有某种关系)。

Also if I press F10 say after first time Request.Add is called, it jumps over multiple Request.Add lines, and moves to the fifth one for example. 此外,如果我在第一次调用Request.Add后按F10说,它会跳过多个Request.Add行,例如移到第五行。

EDIT2 : Also before I step into C++ code it is showing me warning that "the source is different version than the one that was used to create a DLL". 编辑2在我进入C ++代码之前,它向我显示警告“源的版本与用于创建DLL的版本不同”。 Is this a problem? 这是一个问题吗?

Module and PDB 模块和PDB

There is a link between a module ( .dll / .exe ) and the debug database ( .pdb ). 模块( .dll / .exe )和调试数据库( .pdb )之间存在链接。 This link is established via a timestamp and a checksum that is present in both files. 此链接是通过两个文件中都存在的时间戳和校验和建立的。 Visual Studio checks the correctness of those, otherwise it will complain and not stop at breakpoints at all. Visual Studio会检查这些内容的正确性,否则它会抱怨并且根本不会在断点处停止。

While other debuggers such as WinDbg have commands to turn that feature off, Visual Studio doesn't have such a feature and requires active manipulation (such as Chkmatch ) to turn off the checmsum verification. 虽然其他调试器(如WinDbg)具有关闭该功能的命令,但Visual Studio没有这样的功能,需要主动操作(如Chkmatch )才能关闭checmsum验证。 As long as you didn't use such a tool, your debugging symbols are fine. 只要你没有使用这样的工具,你的调试符号就可以了。

PDB and source PDB和来源

There is also a link between the debug database ( .pdb ) and the source. 调试数据库( .pdb )和源之间也有一个链接。 This link is established by file name and line numbers. 此链接由文件名和行号建立。 As you can guess, your source code will not modified during compilation, so the source code does not contain any checksum or timestamp that could be verified. 您可以猜测,您的源代码在编译期间不会被修改,因此源代码不包含任何可以验证的校验和或时间戳。

Therefore, the source may have changed and the line numbers may not even match roughly any more. 因此,源可能已经改变,并且行号甚至可能不再大致匹配。 There are several reasons for line numbers to get broken. 线号被破坏有几个原因。 I have answered a similar question before and listed the following reasons for line number changes although the code itself did not change: 我之前回答了类似的问题,并列出了行号更改的以下原因,尽管代码本身没有改变:

  • code reformat, which eg sorts the methods by visibility, so complete methods are moved 代码重新格式化,例如,通过可见性对方法进行排序,从而移动完整的方法
  • code reformat, which eg breaks long lines at 80 characters, usually this moves things down 代码重新格式化,例如将长行以80个字符分隔,通常这会使事情变得迟钝
  • optimize usings (R#) which removes 30 lines of unneeded imports, so things move up 优化使用(R#),删除30行不需要的进口,因此事情向上移动
  • insertion of comments or newlines 插入评论或换行符

How to debug 如何调试

  1. Restore the exact source code of that version, if you can. 如果可以,请恢复该版本的确切源代码。
  2. Debug completely without source, just by PDB information. 完全无需源代码调试,仅通过PDB信息进行调试。 This way you can keep the binary components, if that's important (eg if a bug can only be reproduced with that version) 通过这种方式,您可以保留二进制组件,如果这很重要(例如,如果只能使用该版本重现错误)
  3. Rebuild all modules to make the code match the modules again. 重建所有模块以使代码再次与模块匹配。 That way you lose the binary and the problem may not reproduce any more. 这样你就失去了二进制文件,问题可能无法再复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM