From 2dda9580b29b1ab46a7ededa6f8561ebb629b6f3 Mon Sep 17 00:00:00 2001
From: "SND\\kernelnet_cp"
 <SND\kernelnet_cp@9b283d60-5439-405e-af05-b73fd8c4d996>
Date: Wed, 2 Nov 2011 08:01:11 +0000
Subject: [PATCH] [0.1.x] added : integer fields output for typedVar class

git-svn-id: https://pykd.svn.codeplex.com/svn@70965 9b283d60-5439-405e-af05-b73fd8c4d996
---
 pykd/typedvar.cpp        | 46 +++++++++++++++++++++++++++++++++++++---
 pykd/typedvar.h          | 29 +++++++++++++------------
 test/scripts/typedvar.py | 15 +++++++------
 3 files changed, 67 insertions(+), 23 deletions(-)

diff --git a/pykd/typedvar.cpp b/pykd/typedvar.cpp
index dd20761..4afc15e 100644
--- a/pykd/typedvar.cpp
+++ b/pykd/typedvar.cpp
@@ -1,23 +1,63 @@
 #include "stdafx.h"    
 
 #include "typedvar.h"
-
+#include "dbgclient.h"
 
 namespace pykd {
 
 ///////////////////////////////////////////////////////////////////////////////////
 
+TypedVar::TypedVar ( IDebugClient4 *client, const TypeInfo& typeInfo, ULONG64 offset ) :
+    DbgObject( client ),
+    m_typeInfo( typeInfo ),
+    m_offset( offset )
+{
+    m_size = m_typeInfo.getSize();
+}
+
+///////////////////////////////////////////////////////////////////////////////////
+
+TypedVar::TypedVar( const TypeInfo& typeInfo, ULONG64 offset ) :
+    DbgObject( g_dbgClient->client() ),
+    m_typeInfo( typeInfo ),
+    m_offset( offset )
+{
+    m_size = m_typeInfo.getSize();
+}
+
+///////////////////////////////////////////////////////////////////////////////////
+
 TypedVarPtr
 TypedVar::getField( const std::string &fieldName ) 
 {
     TypeInfo fieldType = m_typeInfo.getField( fieldName );
 
+    TypedVarPtr     tv;
+
     if ( fieldType.isBasicType() )
     {
-        return TypedVarPtr( new BasicTypedVar( fieldType, 0 ) );
+        tv.reset( new BasicTypedVar( m_client, fieldType, m_offset + fieldType.getOffset() ) );
     }
+    else
+        throw DbgException( "can not get field" );
 
-    throw DbgException( "can not get field" );
+    return tv;
+}
+
+///////////////////////////////////////////////////////////////////////////////////
+
+ULONG64  
+BasicTypedVar::getValue() const
+{
+    HRESULT     hres;
+    ULONG64     val = 0;
+
+    hres = m_dataSpaces->ReadVirtual( m_offset, &val, getSize(), NULL );
+
+    if ( FAILED( hres ) )
+        throw MemoryException( m_offset, false );
+
+    return val;
 }
 
 ///////////////////////////////////////////////////////////////////////////////////
diff --git a/pykd/typedvar.h b/pykd/typedvar.h
index 8e4e298..6e63950 100644
--- a/pykd/typedvar.h
+++ b/pykd/typedvar.h
@@ -2,6 +2,7 @@
 
 #include "typeinfo.h"
 #include "intbase.h"
+#include "dbgobj.h"
 
 namespace pykd {
 
@@ -12,21 +13,21 @@ typedef boost::shared_ptr<TypedVar>  TypedVarPtr;
 
 ///////////////////////////////////////////////////////////////////////////////////
 
-class TypedVar : public intBase {
+class TypedVar : public intBase, protected DbgObject {
 
 public:
 
-    TypedVar ( const TypeInfo& typeInfo, ULONG64 offset ) :
-      m_typeInfo( typeInfo ),
-      m_offset( offset )
-      {}
+    TypedVar ( const TypeInfo& typeInfo, ULONG64 offset );
+
+
+    TypedVar ( IDebugClient4 *client, const TypeInfo& typeInfo, ULONG64 offset );
 
     ULONG64 getAddress() const {
         return m_offset;
     }
 
-    ULONG getSize() {
-        return m_typeInfo.getSize();
+    ULONG getSize() const {
+        return m_size;
     }
 
     TypeInfo
@@ -47,14 +48,14 @@ protected:
     }
     
     virtual void setValue( ULONG64  value) {
-        m_offset = value;
+       throw DbgException("can not change");
     }
 
-private:
+    TypeInfo                m_typeInfo;
 
-    TypeInfo        m_typeInfo;
+    ULONG64                 m_offset;
 
-    ULONG64         m_offset;
+    ULONG                   m_size;
 };
 
 ///////////////////////////////////////////////////////////////////////////////////
@@ -63,7 +64,7 @@ class BasicTypedVar : public TypedVar {
 
 public:
 
-    BasicTypedVar ( const TypeInfo& typeInfo, ULONG64 offset ) : TypedVar(typeInfo, offset){}
+    BasicTypedVar ( IDebugClient4 *client, const TypeInfo& typeInfo, ULONG64 offset ) : TypedVar(client, typeInfo, offset){}
 
     TypedVarPtr
     virtual getField( const std::string &fieldName ) {
@@ -74,8 +75,10 @@ public:
         return "BasicTypedVar";
     }
 
+    virtual ULONG64  getValue() const;
+
 };
 
 ///////////////////////////////////////////////////////////////////////////////////
 
-}; // namespace pykd
+} // namespace pykd
diff --git a/test/scripts/typedvar.py b/test/scripts/typedvar.py
index 08b4d42..aaf6f52 100644
--- a/test/scripts/typedvar.py
+++ b/test/scripts/typedvar.py
@@ -9,12 +9,6 @@ import pykd
 class TypedVarTest( unittest.TestCase ):
 
     def testCtor( self ):
-        try: pykd.typedVar()
-        except RuntimeError: pass
-        
-        try: pykd.typedVar( "structTest", target.module.g_structTest )
-        except RuntimeError: pass        
-        
         tv = target.module.typedVar( "structTest", target.module.g_structTest )
         tv = target.module.typedVar( "g_structTest" )
         
@@ -25,5 +19,12 @@ class TypedVarTest( unittest.TestCase ):
     def testGetSize( self ):
         tv1 = target.module.typedVar( "structTest", target.module.g_structTest )
         self.assertEqual( 16, tv1.sizeof() )
-        #tv2 = target.module.typedVar( "structTest[]", target.module.g_testArray )
+        #tv2 = target.module.typedVar( "structTest[2]", target.module.g_testArray )
         #self.assertEqual( tv1.sizeof()*2, tv2.sizeof() )        
+        
+    def testStruct(self):
+        tv1 = target.module.typedVar( "structTest", target.module.g_structTest )
+        self.assertEqual( 0, tv1.m_field0 + 0 )
+        self.assertEqual( 500, tv1.m_field1 )
+        self.assertEqual( True, tv1.m_field2 )
+        self.assertEqual( 1, tv1.m_field3 )
\ No newline at end of file