From 80eaf476cd5807996a92c908467580c3394cf247 Mon Sep 17 00:00:00 2001
From: "SND\\kernelnet_cp"
 <SND\kernelnet_cp@9b283d60-5439-405e-af05-b73fd8c4d996>
Date: Tue, 7 Jun 2011 16:13:05 +0000
Subject: [PATCH] [pykd] updated : move all code for input/output operation to
 dbgio.h/cpp

git-svn-id: https://pykd.svn.codeplex.com/svn@66396 9b283d60-5439-405e-af05-b73fd8c4d996
---
 pykd/dbgcmd.cpp                  |  2 +-
 pykd/dbgevent.cpp                |  2 +-
 pykd/dbgext.cpp                  | 77 +++++++++++++++++---------------
 pykd/dbginput.h                  | 40 -----------------
 pykd/{dbgprint.cpp => dbgio.cpp} | 12 +++--
 pykd/{dbgcallback.h => dbgio.h}  | 56 +++++++++++++++++++++--
 pykd/dbgmem.cpp                  |  2 +-
 pykd/dbgmodule.cpp               |  2 +-
 pykd/dbgprint.h                  | 17 -------
 pykd/dbgsym.cpp                  |  1 -
 pykd/dbgsystem.cpp               |  2 +-
 pykd/pykd.vcproj                 | 28 ++++++------
 12 files changed, 121 insertions(+), 120 deletions(-)
 delete mode 100644 pykd/dbginput.h
 rename pykd/{dbgprint.cpp => dbgio.cpp} (58%)
 rename pykd/{dbgcallback.h => dbgio.h} (70%)
 delete mode 100644 pykd/dbgprint.h

diff --git a/pykd/dbgcmd.cpp b/pykd/dbgcmd.cpp
index e6f4337..2d480b1 100644
--- a/pykd/dbgcmd.cpp
+++ b/pykd/dbgcmd.cpp
@@ -5,7 +5,7 @@
 #include "dbgext.h"
 #include "dbgcmd.h"
 #include "dbgexcept.h"
-#include "dbgcallback.h"
+#include "dbgio.h"
 #include "dbgsystem.h"
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/pykd/dbgevent.cpp b/pykd/dbgevent.cpp
index 79eeb10..c84558c 100644
--- a/pykd/dbgevent.cpp
+++ b/pykd/dbgevent.cpp
@@ -7,7 +7,7 @@
 #include <dbgeng.h>
 
 #include "dbgmodule.h"
-#include "dbgcallback.h"
+#include "dbgio.h"
 #include "dbgevent.h"
 
 /////////////////////////////////////////////////////////////////////////////////
diff --git a/pykd/dbgext.cpp b/pykd/dbgext.cpp
index d7e6947..4b60e86 100644
--- a/pykd/dbgext.cpp
+++ b/pykd/dbgext.cpp
@@ -11,7 +11,6 @@
 #include <boost/python/overloads.hpp>
 
 #include "dbgext.h"
-#include "dbgprint.h"
 #include "dbgreg.h"
 #include "dbgtype.h"
 #include "dbgmodule.h"
@@ -24,12 +23,12 @@
 #include "dbgeventcb.h"
 #include "dbgcallback.h"
 #include "dbgpath.h"
-#include "dbginput.h"
 #include "dbgprocess.h"
 #include "dbgsynsym.h"
 #include "dbgclient.h"
 #include "dbgevent.h"
 #include "dbgbreak.h"
+#include "dbgio.h"
 
 //////////////////////////////////////////////////////////////////////////////
 
@@ -41,8 +40,8 @@ dbgClient    g_dbgClient;
 
 //////////////////////////////////////////////////////////////////////////////
 
-BOOST_PYTHON_FUNCTION_OVERLOADS( dprint, DbgPrint::dprint, 1, 2 )
-BOOST_PYTHON_FUNCTION_OVERLOADS( dprintln, DbgPrint::dprintln, 1, 2 )
+BOOST_PYTHON_FUNCTION_OVERLOADS( dprint, dbgPrint::dprint, 1, 2 )
+BOOST_PYTHON_FUNCTION_OVERLOADS( dprintln, dbgPrint::dprintln, 1, 2 )
 
 BOOST_PYTHON_FUNCTION_OVERLOADS( loadCharsOv, loadChars, 2, 3 )
 BOOST_PYTHON_FUNCTION_OVERLOADS( loadWCharsOv, loadWChars, 2, 3 )
@@ -82,9 +81,9 @@ BOOST_PYTHON_MODULE( pykd )
         "Check if script works in windbg context" );
     boost::python::def( "symbolsPath", &dbgSymPath, 
         "Return symbol path" );
-    boost::python::def( "dprint", &DbgPrint::dprint, dprint( boost::python::args( "str", "dml" ), 
+    boost::python::def( "dprint", &dbgPrint::dprint, dprint( boost::python::args( "str", "dml" ), 
         "Print out string. If dml = True string is printed with dml highlighting ( only for windbg )") );
-    boost::python::def( "dprintln", &DbgPrint::dprintln, dprintln( boost::python::args( "str", "dml" ), 
+    boost::python::def( "dprintln", &dbgPrint::dprintln, dprintln( boost::python::args( "str", "dml" ), 
         "Print out string and insert end of line symbol. If dml = True string is printed with dml highlighting ( only for windbg )"  ) );
     boost::python::def( "loadDump", &dbgLoadDump,
         "Load crash dump (only for console)");
@@ -523,15 +522,15 @@ private:
             main_namespace[ key ] = pykd_namespace[ key ];
         }            
         
-        // ��������������� ����������� ������� ��
-        boost::python::object       sys = boost::python::import( "sys");
-        
-        dbgOut                      dout;
-        sys.attr("stdout") = boost::python::object( dout );
+        //// ��������������� ����������� ������� ��
+        //boost::python::object       sys = boost::python::import( "sys");
+        //
+        //dbgOut                      dout;
+        //sys.attr("stdout") = boost::python::object( dout );
 
-        dbgIn                       din;
-        sys.attr("stdin") = boost::python::object( din );
-        
+        //dbgIn                       din;
+        //sys.attr("stdin") = boost::python::object( din );
+        //
         g_dbgClient.startEventsMgr();
     }
     
@@ -769,7 +768,8 @@ py( PDEBUG_CLIENT4 client, PCSTR args)
     }
    
     catch(...)
-    {           
+    {      
+        dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "unexpected error" );         
     }     
     
     Py_EndInterpreter( localInterpreter ); 
@@ -784,25 +784,30 @@ HRESULT
 CALLBACK
 pycmd( PDEBUG_CLIENT4 client, PCSTR args )
 {
-    try {
+    DbgExt      ext( client );
 
-        DbgExt      ext( client );
-  
+    try {
+        
         if ( !std::string( args ).empty() )
         {
-            try
-            {
-                OutputReader        outputReader( dbgExt->client );
-            
-                boost::python::exec( args, WindbgGlobalSession::global(), WindbgGlobalSession::global() );
+            try {
+        
+                boost::python::object  retObj = boost::python::eval( args, WindbgGlobalSession::global(), WindbgGlobalSession::global() );    
+   
+                if ( retObj.ptr() != NULL )
+                {   
+                    PyObject  *s = PyObject_Str( retObj.ptr() );
+                    dbgExt->control->Output( DEBUG_OUTPUT_NORMAL, "%s\n", PyString_AS_STRING( s )  );    
+                    Py_DECREF( s );                   
+                }                    
             }
             catch( boost::python::error_already_set const & )
             {
                 // ������ � �������
                 PyObject    *errtype = NULL, *errvalue = NULL, *traceback = NULL;
-                
+
                 PyErr_Fetch( &errtype, &errvalue, &traceback );
-                
+
                 if(errvalue != NULL) 
                 {
                     PyObject *s = PyObject_Str(errvalue);
@@ -814,9 +819,9 @@ pycmd( PDEBUG_CLIENT4 client, PCSTR args )
 
                 Py_XDECREF(errvalue);
                 Py_XDECREF(errtype);
-                Py_XDECREF(traceback);        
+                Py_XDECREF(traceback);    
             }  
-        }
+        }     
         else
         {
             char        str[100];
@@ -831,7 +836,7 @@ pycmd( PDEBUG_CLIENT4 client, PCSTR args )
                     
                 do {    
                                 
-                    OutputReader        outputReader( (IDebugClient*)client );                    
+                    OutputReader     outputReader( (IDebugClient*)client );                    
                     
                     HRESULT   hres = dbgExt->control->Input( str, sizeof(str), &inputSize );
                 
@@ -871,15 +876,15 @@ pycmd( PDEBUG_CLIENT4 client, PCSTR args )
                     }  
                     
             } while( !stopInput );                                
-        }
-    }
-  
-    catch(...)
-    {           
-    }     
+        }              
+    } 
     
-    return S_OK;          
-            
+    catch(...)    
+    {
+        dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "unexpected error" );
+    }
+    
+    return S_OK;    
 }
 
 ///////////////////////////////////////////////////////////////////////////////// 
diff --git a/pykd/dbginput.h b/pykd/dbginput.h
deleted file mode 100644
index 0612685..0000000
--- a/pykd/dbginput.h
+++ /dev/null
@@ -1,40 +0,0 @@
-#pragma once
-
-#include "dbgprint.h"
-
-/////////////////////////////////////////////////////////////////////////////////
-
-class dbgOut {
-
-public:
-
-    void
-    write( const boost::python::object  &str ) {
-        DbgPrint::dprint( str );
-    }         
-    
-};
-
-/////////////////////////////////////////////////////////////////////////////////
-
-class dbgIn {
-
-public:
-
-    std::string
-    readline() {
-    
-        char        str[100];
-        ULONG       inputSize;
-        
-        OutputReader        outputReader( dbgExt->client );
-    
-        dbgExt->control->Input( str, sizeof(str), &inputSize );
-    
-        return std::string( str );
-    }    
-
-};
-
-/////////////////////////////////////////////////////////////////////////////////
-
diff --git a/pykd/dbgprint.cpp b/pykd/dbgio.cpp
similarity index 58%
rename from pykd/dbgprint.cpp
rename to pykd/dbgio.cpp
index b2bc2a9..f49bb38 100644
--- a/pykd/dbgprint.cpp
+++ b/pykd/dbgio.cpp
@@ -1,13 +1,16 @@
 #include "stdafx.h"
 
 #include <iostream>
+#include <Fcntl.h>
 
-#include "dbgprint.h"
+#include "dbgio.h"
 #include "dbgext.h"
 
 using namespace std;
 
-void DbgPrint::dprint( const boost::python::object& obj, bool dml )
+/////////////////////////////////////////////////////////////////////////////////
+
+void dbgPrint::dprint( const boost::python::object& obj, bool dml )
 {
     std::wstring   str = boost::python::extract<std::wstring>( obj );
 
@@ -16,7 +19,9 @@ void DbgPrint::dprint( const boost::python::object& obj, bool dml )
     std::wcout << str;
 }
 
-void DbgPrint::dprintln( const boost::python::object& obj, bool dml  )
+/////////////////////////////////////////////////////////////////////////////////
+
+void dbgPrint::dprintln( const boost::python::object& obj, bool dml  )
 {
     std::wstring   str = boost::python::extract<std::wstring>( obj );
     str += L"\r\n";
@@ -26,3 +31,4 @@ void DbgPrint::dprintln( const boost::python::object& obj, bool dml  )
     std::wcout << str;    
 }
 
+/////////////////////////////////////////////////////////////////////////////////
diff --git a/pykd/dbgcallback.h b/pykd/dbgio.h
similarity index 70%
rename from pykd/dbgcallback.h
rename to pykd/dbgio.h
index b8c4b17..5294afb 100644
--- a/pykd/dbgcallback.h
+++ b/pykd/dbgio.h
@@ -1,9 +1,21 @@
 #pragma once
 
-#include "dbgext.h"
-#include "dbgexcept.h"
+#include <string>
+#include <dbgeng.h>
 
-#include <wdbgexts.h>
+#include "dbgext.h"
+
+/////////////////////////////////////////////////////////////////////////////////
+
+class dbgPrint {
+
+public:
+
+	static void dprint( const boost::python::object& obj, bool dml = false );
+	
+	static void dprintln( const boost::python::object& obj, bool dml = false );
+
+};
 
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -173,4 +185,40 @@ private:
 
 };
 
-///////////////////////////////////////////////////////////////////////////////
\ No newline at end of file
+
+/////////////////////////////////////////////////////////////////////////////////
+
+class dbgOut {
+
+public:
+
+    void
+    write( const boost::python::object  &str ) {
+        dbgPrint::dprint( str );
+    }         
+    
+};
+
+/////////////////////////////////////////////////////////////////////////////////
+
+class dbgIn {
+
+public:
+
+    std::string
+    readline() {
+    
+        char        str[100];
+        ULONG       inputSize;
+        
+        OutputReader        outputReader( dbgExt->client );
+    
+        dbgExt->control->Input( str, sizeof(str), &inputSize );
+    
+        return std::string( str );
+    }    
+
+};
+
+/////////////////////////////////////////////////////////////////////////////////
+
diff --git a/pykd/dbgmem.cpp b/pykd/dbgmem.cpp
index 5f0548a..6ed805d 100644
--- a/pykd/dbgmem.cpp
+++ b/pykd/dbgmem.cpp
@@ -7,7 +7,7 @@
 #include "dbgexcept.h"
 #include "dbgmem.h"
 #include "dbgsystem.h"
-#include "dbgcallback.h"
+#include "dbgio.h"
 
 using namespace std;
 
diff --git a/pykd/dbgmodule.cpp b/pykd/dbgmodule.cpp
index 3d04876..34127f1 100644
--- a/pykd/dbgmodule.cpp
+++ b/pykd/dbgmodule.cpp
@@ -8,7 +8,7 @@
 #include "dbgmodule.h"
 #include "dbgexcept.h"
 #include "dbgsym.h"
-#include "dbgcallback.h"
+#include "dbgio.h"
 #include "dbgsynsym.h"
 
 /////////////////////////////////////////////////////////////////////////////////
diff --git a/pykd/dbgprint.h b/pykd/dbgprint.h
deleted file mode 100644
index 3f380a4..0000000
--- a/pykd/dbgprint.h
+++ /dev/null
@@ -1,17 +0,0 @@
-// ����� ���������� � ��������
-
-#pragma once
-
-#include <string>
-
-class DbgPrint {
-
-public:
-
-	static void dprint( const boost::python::object& obj, bool dml = false );
-	
-	static void dprintln( const boost::python::object& obj, bool dml = false );
-	
-
-};
-
diff --git a/pykd/dbgsym.cpp b/pykd/dbgsym.cpp
index 9cf51b0..3b33fee 100644
--- a/pykd/dbgsym.cpp
+++ b/pykd/dbgsym.cpp
@@ -3,7 +3,6 @@
 #include "dbgext.h"
 #include "dbgsym.h"
 #include "dbgexcept.h"
-#include "dbgprint.h"
 #include "dbgmem.h"
 
 /////////////////////////////////////////////////////////////////////////////////
diff --git a/pykd/dbgsystem.cpp b/pykd/dbgsystem.cpp
index d8c3a13..d7e4d93 100644
--- a/pykd/dbgsystem.cpp
+++ b/pykd/dbgsystem.cpp
@@ -6,7 +6,7 @@
 #include "dbgext.h"
 #include "dbgexcept.h"
 #include "dbgsystem.h"
-#include "dbgcallback.h"
+#include "dbgio.h"
 
 ///////////////////////////////////////////////////////////////////////////////////
 
diff --git a/pykd/pykd.vcproj b/pykd/pykd.vcproj
index 95ab0c4..c9435ae 100644
--- a/pykd/pykd.vcproj
+++ b/pykd/pykd.vcproj
@@ -368,6 +368,10 @@
 				RelativePath=".\dbgdump.cpp"
 				>
 			</File>
+			<File
+				RelativePath=".\dbgevent.cpp"
+				>
+			</File>
 			<File
 				RelativePath=".\dbgeventcb.cpp"
 				>
@@ -381,11 +385,11 @@
 				>
 			</File>
 			<File
-				RelativePath=".\dbgmem.cpp"
+				RelativePath=".\dbgio.cpp"
 				>
 			</File>
-  		<File
-				RelativePath=".\dbgevent.cpp"
+			<File
+				RelativePath=".\dbgmem.cpp"
 				>
 			</File>
 			<File
@@ -396,10 +400,6 @@
 				RelativePath=".\dbgpath.cpp"
 				>
 			</File>
-			<File
-				RelativePath=".\dbgprint.cpp"
-				>
-			</File>
 			<File
 				RelativePath=".\dbgprocess.cpp"
 				>
@@ -474,10 +474,6 @@
 				RelativePath=".\dbgbreak.h"
 				>
 			</File>
-			<File
-				RelativePath=".\dbgcallback.h"
-				>
-			</File>
 			<File
 				RelativePath=".\dbgclient.h"
 				>
@@ -490,6 +486,10 @@
 				RelativePath=".\dbgdump.h"
 				>
 			</File>
+			<File
+				RelativePath=".\dbgevent.h"
+				>
+			</File>
 			<File
 				RelativePath=".\dbgeventcb.h"
 				>
@@ -507,11 +507,11 @@
 				>
 			</File>
 			<File
-				RelativePath=".\dbgmem.h"
+				RelativePath=".\dbgio.h"
 				>
 			</File>
-  		<File
-				RelativePath=".\dbgevent.h"
+			<File
+				RelativePath=".\dbgmem.h"
 				>
 			</File>
 			<File