From 44fd2e17cb968862457fdb829066440b02c629cd Mon Sep 17 00:00:00 2001
From: "SND\\kernelnet_cp"
 <SND\kernelnet_cp@9b283d60-5439-405e-af05-b73fd8c4d996>
Date: Tue, 20 Nov 2012 05:36:48 +0000
Subject: [PATCH] [0.2.x] added : setProcessorMode routine

git-svn-id: https://pykd.svn.codeplex.com/svn@81227 9b283d60-5439-405e-af05-b73fd8c4d996
---
 pykd/cpureg.h       |  8 --------
 pykd/dbgengine.h    |  6 ++++++
 pykd/pymod.cpp      |  2 ++
 pykd/win/dbgeng.cpp | 23 +++++++++++++++++++++++
 4 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/pykd/cpureg.h b/pykd/cpureg.h
index fa62bfb..2269a46 100644
--- a/pykd/cpureg.h
+++ b/pykd/cpureg.h
@@ -38,14 +38,6 @@ CpuReg getRegByName( const std::string &regName );
 
 CpuReg getRegByIndex( ULONG index );
 
-ULONG64 loadMSR( ULONG  msr );
-
-void setMSR( ULONG msr, ULONG64 value);
-
-std::string getProcessorMode();
-
-std::string getProcessorType();
-
 ///////////////////////////////////////////////////////////////////////////////////
 
 }; // end pykd namespace
diff --git a/pykd/dbgengine.h b/pykd/dbgengine.h
index 8ac16ae..535ce71 100644
--- a/pykd/dbgengine.h
+++ b/pykd/dbgengine.h
@@ -52,6 +52,12 @@ std::string getRegNameByIndex( ULONG index );
 BaseTypeVariant getRegVariantValue( ULONG index );
 ULONG64 getRegInstructionPointer();
 
+ULONG64 loadMSR( ULONG  msr );
+void setMSR( ULONG msr, ULONG64 value);
+std::string getProcessorMode();
+std::string getProcessorType();
+void setProcessorMode( const std::string &mode );
+
 // Stack and local variables
 struct STACK_FRAME_DESC {
     ULONG number;
diff --git a/pykd/pymod.cpp b/pykd/pymod.cpp
index 1bcb720..bc14370 100644
--- a/pykd/pymod.cpp
+++ b/pykd/pymod.cpp
@@ -240,6 +240,8 @@ BOOST_PYTHON_MODULE( pykd )
         "Return current processor mode as string: X86, ARM, IA64 or X64" );
     python::def( "getProcessorType", &getProcessorType,
         "Return type of physical processor: X86, ARM, IA64 or X64" );
+    python::def( "setProcessorMode", &setProcessorMode,
+        "Set current processor mode by string (X86, ARM, IA64 or X64)" );
 
     // stack and local variables
     python::def( "getCurrentStack", &getCurrentStack,
diff --git a/pykd/win/dbgeng.cpp b/pykd/win/dbgeng.cpp
index 87b8716..b4381e5 100644
--- a/pykd/win/dbgeng.cpp
+++ b/pykd/win/dbgeng.cpp
@@ -787,6 +787,29 @@ std::string getProcessorMode()
 
 ///////////////////////////////////////////////////////////////////////////////
 
+void setProcessorMode( const std::string &mode )
+{
+    HRESULT         hres;  
+    ULONG           processorMode;
+
+    if ( mode == "X86" )
+        processorMode = IMAGE_FILE_MACHINE_I386;
+    else if ( mode == "ARM" )
+        processorMode = IMAGE_FILE_MACHINE_ARM;
+    else if ( mode == "IA64" )
+        processorMode = IMAGE_FILE_MACHINE_IA64;
+    else if ( mode == "X64" )
+        processorMode = IMAGE_FILE_MACHINE_AMD64;
+    else
+        throw DbgException( "Unknown processor type" );
+
+    hres =  g_dbgEng->control->SetEffectiveProcessorType( processorMode );
+    if ( FAILED( hres ) )
+        throw DbgException( "IDebugControl::SetEffectiveProcessorType  failed" );
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
 std::string getProcessorType()
 {
     PyThread_StateRestore pyThreadRestore( g_dbgEng->pystate );