Code Monkey home page Code Monkey logo

Comments (15)

 avatar commented on July 25, 2024

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)?

from dnlib.

yck1509 avatar yck1509 commented on July 25, 2024
[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.

from dnlib.

 avatar commented on July 25, 2024

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.

//DISABLED: [STAThread]
static void Main() {

and

[MTAThread]
static void Main() {

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

from dnlib.

yck1509 avatar yck1509 commented on July 25, 2024

I suppose I should change that assert to task.Result.HasScope.

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:

Unable to cast COM object of type 'System.__ComObject' to interface type 'dnlib.DotNet.Pdb.Dss.ISymUnmanagedReader'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B4CE6286-2A6B-3712-A3B7-1EE1DAD467B5}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

from dnlib.

 avatar commented on July 25, 2024

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;
        }
    }
}

from dnlib.

 avatar commented on July 25, 2024

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.

from dnlib.

 avatar commented on July 25, 2024

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

from dnlib.

yck1509 avatar yck1509 commented on July 25, 2024

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.

from dnlib.

 avatar commented on July 25, 2024

I'll see if there's a solution. EDIT: see below

from dnlib.

 avatar commented on July 25, 2024

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.

from dnlib.

yck1509 avatar yck1509 commented on July 25, 2024

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.

from dnlib.

 avatar commented on July 25, 2024

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.

from dnlib.

 avatar commented on July 25, 2024

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

from dnlib.

yck1509 avatar yck1509 commented on July 25, 2024

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?

from dnlib.

 avatar commented on July 25, 2024

Apache 2.0 and MIT seem compatible according to some posts found by google.

from dnlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.