-
Notifications
You must be signed in to change notification settings - Fork 586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When PDB reading is enabled, MethodDef.Body cannot be read in other threads #24
Comments
Is your application multi-threaded and you try to use the PDB feature in multiple threads at the same time or does it only occur if you use it in one thread and that thread is not the main thread? Do you have simple test code to trigger it (otherwise I'll write some code myself)? |
[STAThread]
static void Main() {
var module = ModuleDefMD.Load(
"ConfuserEx.exe",
new ModuleCreationOptions() { TryToLoadPdbFromDisk = true }
);
var task = Task.Factory.StartNew(() => module.ResolveMethod(1).Body);
task.Wait();
Debug.Assert(task.Result != null);
} Note that STAThread is needed, or it will just fail to read the state without throwing exception. |
I tested this code. Without STAThread or with MTAThread, the code will work. Are you sure this code fails when STAThread isn't present? It worked for me.
and
When you use STAThread it seems like all COM calls must be done in the main thread. See this http://stackoverflow.com/questions/2175419/cannot-call-com-object-created-from-stathread-from-oher-sta-threads |
I suppose I should change that assert to When I tried without STAThread or with MTAThread, reader.GetMethod returns E_FAIL so no exception is thrown, but the PDB symbols are not read for the method body. When I tried with STAThread, reader.GetMethod throws the following exception:
|
Symbols are loaded in my test. Could you try my code? It's almost identical to yours. Create a new project: Update the path in the code to point to the exe: using System;
using System.Diagnostics;
using System.Threading.Tasks;
using dnlib.DotNet;
namespace dnpdbtest {
class Program {
//[STAThread]
//[MTAThread]
static void Main() {
var module = ModuleDefMD.Load(
@"C:\work\dnpdbtest\dnpdbtest\bin\Debug\dnpdbtest.exe",
new ModuleCreationOptions() { TryToLoadPdbFromDisk = true }
);
var task = Task.Factory.StartNew(() => module.ResolveMethod(1).Body);
task.Wait();
Debug.Assert(task.Result.HasScope);
var m = task.Result;
}
}
} |
BTW, I used VS2010 to compile the code and Windows 7 SP1 with latest updates. The project was compiled as a .NET 4.0 app. |
One reason your code might fail without STAThread is that the method with RID=1 does not have any debug info in the PDB file (eg. it's compiler generated). |
Your code works here. I suppose that is the reason. However, it still crashes when I put STAThread on the method. WinForms requires the main thread to be STAThread, so it would crash when I created the ModuleDefMD and use it in other threads. |
I'll see if there's a solution. EDIT: see below |
I've added a new option to ModuleCreatorOptions. Set PreLoadAllPdbData to true and all PDB data get loaded in the ModuleDefMD constructor. Alternatively, you could call ModuleDefMD.PreLoadAllPdbData() yourself in the same thread that creates it. |
Thanks for the fix, but as a side effect, the method bodies are loaded with PDB, which mean whatever the CIL code references would be loaded as well. I found this project and it seems it has a suitable license along with format documentation. If you're OK with it, I could write a managed reader using the documentation there. |
Which references are loaded when a CIL body is loaded? Nothing is resolved. Sure if you want to write a managed reader, go for it. I assume you'll add it to dnlib, and if so, the COM code is in Pdb/Dss, so you could put your code in Pdb/Managed or whatever name you want. If you want the code just to work with existing dnlib code, you'll need to implement various ifaces, eg. ISymbolMethod etc. dnlib only uses a few of the available methods/properties so most methods could throw a NotImplementedException. Let me know if you need help with some of the PDB internals. I've written a PDB library myself so I know a few things about the file format. But this is a general PDB library so it's not suitable for dnlib. .NET PDB files only use a small subset of the PDB file format. |
Another solution (similar to the code I already wrote) could be to just load all PDB data (and not the CIL body) and cache it in some dictionary. Whenever the CIL body is loaded, the cached data is used (if it's been cached). |
While I'm looking for information for internals of PDB, I found some parts of PdbReader in Rosyln. Would it be suitables to use some part of that, since it seems they re-licensed the code under Apache License? |
Apache 2.0 and MIT seem compatible according to some posts found by google. |
dnlib uses COM interfaces to read PDB. When it is used in other threads, it will produce an error.
The text was updated successfully, but these errors were encountered: