locked
How do i serialize MdbgValue or CorValue RRS feed

  • Question

  • I wanted to dump either the MDbgValue or Cor Value onto the disk. How do i do it?????

    Another is how do i get the actually object from the above two.
    Tuesday, February 2, 2010 7:21 PM

Answers

  • Sorry for the delay Abhishek.
    The short answer is that what you want to do is not possible in general - objects can't necessarily be copied correctly from one process to another.  For example, what if the object has a field that points to native memory or an OS resource - that sort of data is strictly process-specific so it doesn't really make sense to try to use it from another process.

    But it's certainly true that for simple cases you could build something that would work - just read the fields one at a time using Mdbg APIs and use reflection to create an object with the same state in your debugger process.  But, like it said, there's a lot involved to get this right (eg. how you handle static fields, assembly loading, deciding where to prune large object graphs, reliably detecting cases in the data and code that you can't support, etc.).  In fact I had an intern work for me for 4 months experimenting with something like this - it's certainly a lot of work to handle anything beyond the most simple cases.

    That said, perhaps there's another way to accomplish what you're trying to do.  In particular, VS 2010 avoids the need to do things like this by instead injecting code into the target process to run against the data there ("funceval").  Eg., rather than try to copy 'oClass' over to the debugger, use funcEval to run code in the target process that manipulates oClass in the way you want.

    Rick
    • Proposed as answer by Rick Byers Tuesday, February 23, 2010 7:45 PM
    • Marked as answer by Rick Byers Saturday, March 20, 2010 9:57 PM
    Tuesday, February 23, 2010 7:45 PM

All replies

  • 1) How to store object value on disk? ... I don't think the type is serializable, but you can maybe store its string value (MDbgValue.GetStringValue()).
    2) How to get/set value: http://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/5142ca19-b994-44ce-93be-461ec853da88/

    -Karel
    Tuesday, February 2, 2010 9:06 PM
    Moderator
  • Yes both the types are not serializable. What i want to do is that save the object on disk and then load that object again in a different instance.
    MDbgValue.GetStringValue()  works well with primitive data types but for complex types it does not. For complex  i again need to do getFileds() in a recurrsive fashion.

    I wanted some kind of simpler way to do it ex: if a have a Person Object i want to save the state of  the whole object so that it can be used later.

    Another question was that

    MDbgValue  contains Person object

    MDbgValue.TypeName = Person
    MDbgValue.Name = MyPersonObj


    What i want is to retrive Person object and strored it in a Person  Type variable from MDbgValue which contains Person object.

    Wednesday, February 3, 2010 8:47 AM
  • Are you saying you want to recreate an MDbgValue object in a different instance of MDbg that is debugging the exact same instance?  For example, for reference types you could just save the heap address, and then create a new MDbgValue for that address in a new instance of MDbg.

    Or are you talking about something less debugging-specific such as using MDbg to serialize out a 'Person' object and then some other tool to read it back into a process (not MDbg) as a new instance of 'Person'?  This isn't possible in general - eg. an object (or field it references) could have an OS handle value in it which you couldn't necessarily recreate in a new process (this is why types must opt-into supporting serialization in .NET).  But for simple cases you could make this work by traversing the object graph (getFields as you describe, etc.), and then recreating the object using reflection.  But there's not any code in MDbg to do this for you, and getting it to be relatively complete would be a non-trivial amount of work.

    Or mabye if you're just talking about serializing objects from the debugger that already support serialization (eg. have the [Serializable] attribute or implement ISerializable), then you could use the debugger to func-eval the appropriate serialization code in the target process.

    I hope this helps,
        Rick

    Wednesday, February 3, 2010 6:53 PM
  • Thanks Rick and Karel for you answers.

    The problem that i have is that i cannot change the object (put the serialize attribute on it).

    Another thing that i am trying to solve is as following

    public

     

    class MyClass
    {
        public int ClassLevel = 10;
        public string MyProperty { get; set; }
    }


    void ExceptionFunction(MyClass oClass)
    {
       throw new Exception()
    }

    Now when the debugger will stop and in the stackFrame i will get the name of the function(ExceptionFunction) and its function parameter(oClass).

    Now my question is that the function parameter - oClass will be wrapped in the Mdbgvalue how do achieve the following functionality

    MyClass oCls = SomeGetFunction(oMdbgvalue) where oMdbgvalue will containg the object oClass.

    In short i want to have the object that it is holding. 

    Thursday, February 4, 2010 2:30 PM
  • Sorry for the delay Abhishek.
    The short answer is that what you want to do is not possible in general - objects can't necessarily be copied correctly from one process to another.  For example, what if the object has a field that points to native memory or an OS resource - that sort of data is strictly process-specific so it doesn't really make sense to try to use it from another process.

    But it's certainly true that for simple cases you could build something that would work - just read the fields one at a time using Mdbg APIs and use reflection to create an object with the same state in your debugger process.  But, like it said, there's a lot involved to get this right (eg. how you handle static fields, assembly loading, deciding where to prune large object graphs, reliably detecting cases in the data and code that you can't support, etc.).  In fact I had an intern work for me for 4 months experimenting with something like this - it's certainly a lot of work to handle anything beyond the most simple cases.

    That said, perhaps there's another way to accomplish what you're trying to do.  In particular, VS 2010 avoids the need to do things like this by instead injecting code into the target process to run against the data there ("funceval").  Eg., rather than try to copy 'oClass' over to the debugger, use funcEval to run code in the target process that manipulates oClass in the way you want.

    Rick
    • Proposed as answer by Rick Byers Tuesday, February 23, 2010 7:45 PM
    • Marked as answer by Rick Byers Saturday, March 20, 2010 9:57 PM
    Tuesday, February 23, 2010 7:45 PM
  • Thanks Rick for your help, same here sorry for the delay.

    Why i wanted is simply to have MyObject.Value from a client machine back out to the developers machine to see what happened at that time.

    Right now did a simple stuff to loop through and create the graph with of course limitation of depth.

     

    Saturday, March 20, 2010 8:11 AM
  • Cool, well watch out of the cases I mentioned (unmarshallable fields like handles and native pointers), but with some work it could certainly be quite useful!  Let us know if you have any more questions...

    Rick

    Saturday, March 20, 2010 9:57 PM