Search This Blog

Saturday, April 30, 2011

WinDbg - Some Starting Tips...


WinDBG is a debugging tool from Microsoft for user and kernel mode debugging. WinDBG is a GUI interface along with some debugging extensions. 
Here I am explaining how we can make use of WinDbg using Adplus commands to debug the application hang and crash scenarios. 

Please refer to the below link to download WinDBG


Once you have installed Debugging Tools for Windows, you can instantiate WinDBG from Start->Programs->Debugging Tools for Windows(x86)->WinDbg


Memory Dumps
Memory dump is a snapshot of the memory and register at the time of crash. You can also collect a memory of a process to get the snapshot of a process’s memory at a particular instance.


Step1 :  Get the Memory Dump file which created because of program hang or crash.
For this  we have to attach  debugger  to the application which we want to analyze.  

We will use Adplus commands to collect the dump files. Adplus have 3 modes of operation "Hang", "Crash", "Quick"


ADPlus has 3 modes of operation: ‘Hang‘, ‘Quick‘ and ‘Crash‘ mode.

Hang Mode
                        In ‘Hang’ mode, ADPlus assumes that a process is hung and it will attach a debugger to the process(s) specified on the command line. After the debugger is attached to the process(s) ADPlus will dump the memory of each process to a .dmp file for later analysis with a debugger (such as WinDBG). In this mode, processes are paused briefly while their memory is being dumped to a file and then resumed.

Quick Mode
                       In ‘-quick’ mode ADPlus assumes that a process is hung and it will attach a debugger to the process(s) specified on the command line with either the ‘-p’ or ‘-pn’ or ‘-iis’ switches. After the debugger is attached to the process(s) ADPlus will create mini dumps for each process, containing commonly requested debug information, rather than dumping the entire memory for each process.’Quick’ mode is generally faster than ‘Hang’ mode, but requires symbols to be installed on the server where ADPlus is running.

Crash Mode
                     In ‘Crash’ mode, ADPlus assumes that a process will crash and it will attach a debugger to the process(s) specified on the command line with either the ‘-p’ or ‘-pn’ or ‘-iis’ switches. After the debugger is attached to the process(s)ADPlus will configure the debugger to log ‘first chance’ access violations (AV’s) to a text file. When a ’second chance’ access violation occurs, the processes memory is dumped to a .dmp file for analysis with a debugger such as WinDBG. In this mode, a debugger remains attached to the process(s) until the process exits or the debugger is exited by pressing CTRL-C in the minimized debugger window. When the process crashes, or CTRL-C is pressed, it will need to be re-started.

Collecting Dump using Adplus:
   Open command prompt and go to WinDbg folder, then create command with following elements

-hang/-crash/-quick => indicates for what scenario you want to create dump

-pi => specify the process ID

-pn => specify the process name

-iis => will collect iis dump also

-quiet  => To suppress the dialogs

-o => specify the output folder to which you want to create the dump. By default, the Adplus script will create subfolder similar to the following Hang_Mode__Date_mm-ddyyyy__Time_hh-mm-ssAM/PM. This subfolder will be created under C:\WinDBG\, folder will contain debugger logs and a memory dump.

-r =>  Enables repeated hang attachments.
   


Followings are the additional switches you should know to collect crash dump
-FullOnFirst => This will create a full dump on first chance exception. Remember, you can’t do managed heap analysis unless you have a full dump. Sometime, you do want to get a full dump in case you are noticing unexpected behavior after a first chance exception has occurred. You may want to analyze application state, handles, objects on heap or data structures. Since there could be several first chance exceptions this may really slow down your application since it has to dump the memory on every single first chance exception. The default behavior is to have minidumps created on first chance.You are better off using a configuration file if you already know the exception type.
-MiniOnSecond  => Creates minidumps on second chance for all defined exceptions. The default behavior is to have full dumps created on second chance.

-NoDumpOnFirst  => 
Creates no dumps on first chance for all defined exceptions. The default behavior is to have minidumps created on first chance.

-NoDumpOnSecond  => 
Creates no dumps on second chance for all defined exceptions. The default behavior is to have full dumps created on second chance.

The below is the screenshot

Step2 : As we have created dump as above, the next step is to open the dump in WinDbg and analyze it
Will be continued.....

Wednesday, April 27, 2011

Deleting Locked Files.

Here I am explaining one common scenario, I faced......
          There can be situations in which you are needed to delete files which are locked by other processes. This can be because the process still holds some handles or references corresponding to this code files.

You can use unlocker to check what are processes which hold lock on these files.



There is 2 ways you can make the file deletable:


1. Find the list of processes which holds the lock and kill the process => but it is not desirable.
2. You can use windows APIs like MoveFileEx to mark the files as deletable on system reboots. Here system will add a registry entry for the files and will be deleted on next system reboot.
Behind this API, it just adds a value to record the pending deleted files under the registry entry “[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations]”. Windows will detect this registry entry after reboot and delete all recorded files.
Below is the sample code :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace LockedFileDeletion
{
    class Program
    {
        [Flags]
        enum MoveFileFlags
        {
            MOVEFILE_REPLACE_EXISTING = 0x00000001,
            MOVEFILE_COPY_ALLOWED = 0x00000002,
            MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004,
            MOVEFILE_WRITE_THROUGH = 0x00000008,
            MOVEFILE_CREATE_HARDLINK = 0x00000010,
            MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x00000020
        }

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, MoveFileFlags dwFlags);

        static void Main(string[] args)
        {
            string fileName = @"c:\LockedFile.txt";
            MoveFileEx(fileName, null, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT);
        }
    } 
}

once you execute the above code, you can check your registry entry which will be updated as below:




Saturday, April 23, 2011

.Net Threading => Interlocked Operations...

The static Interlocked class provides methods that synchronize access to a variable that is shared by multiple threads. The threads of different processes can use this mechanism if the variable is in shared memory. Interlocked operations are atomic — that is, the entire operation is a unit that cannot be interrupted by another interlocked operation on the same variable. 

The Interlocked class provides the following static operations:
  • Add method adds an integer value to a variable and returns the new value of the variable.
  • The Increment and Decrement methods increment or decrement a variable and return the resulting value.
  • The Exchange method performs an atomic exchange of the value in a specified variable, returning that value and replacing it with a new value. In the .NET Framework version 2.0, a generic overload of this method can be used to perform this exchange on a variable of any reference type. 
  • The CompareExchange method also exchanges two values, but contingent on the result of a comparison. In the .NET Framework version 2.0, a generic overload of this method can be used to perform this exchange on a variable of any reference type.

    In all the above methods, we pass the reference of thread variable....
Add Method:
    Add value to  the thread variable in an atomic operation...
                Interlocked.Add( ref sum, 100);

Increment/Decrement:
      
     The thread variable will be incremented/decremented,...

                     Interlocked.Increment(ref sum);

Exchange/CompareExchange:

     Exchange :
Exchange the specified value to the thread variable and returns the original value in the thraed variable...
         int sum = 100;
         int n = Interlocked.Exchange(ref sum, 10);
  Here n will be 100 and sum will be 10.............

   CompareExchange : The spcific variable will be compared to a value and if it is equal, then only exchange will happen.

  int n = Interlocked.Exchange(ref sum, 10, value); => sum will be compared with value and if equal then 10 will be assigned to sum;Retrun value will be the original value of sum before exchange...

Saturday, April 16, 2011

Are you familiar with [ThreadStatic]?

ThreadStatic is a super-cool concept if you have a static variable. you can make it static "per-thread" by placing the attribute on top.  This is one easy way to get around the thread-safety issues of working with statics - as they are per-thread, you do not have to take a lock when updating them.

[ThreadStatic]
private static string Foo;

Here each thread accessing this field will have its own copy and is not visible to other threads. 

Initialization =>  The initial value of thread static field is only applicable to the first accessing thread. And for all the subsequent threads it will be initialized to null. 

    class Program
    {
        static void Main(string[] args)
        {            
            Thread th = new Thread(new ThreadStart( ThreadFun ));
            Console.WriteLine("writing from main " + Demo.foo);
            th.Start();
            th.Join();
            Console.WriteLine("writing from main " + Demo.foo);
            Console.Read();
        }

        public static void ThreadFun( )
        {
            Console.WriteLine("writing from thread " + Demo.foo);
            Demo.foo= 500;
            Console.WriteLine("writing from thread " + Demo.foo);
        }

    }

    public class Demo
    {    
        [ThreadStatic]
        public static int foo = 100;               
    }



Output looks like below: 


You can see that main thread will get foo value as 100, but the next thread will gets its own seperate static varibale and the initial value as zero..........

*Note : I just tried the same attribute for non static fields, I can see that compiler wont give you any warnings/errors, but no impact. 


.Net InterProcess Communication Part1 - Using .Net Remoting

Here we are going to see  ".Net Remoting"  and how it is possible for Remote Procedure Calls. It can be considered as the first version of webservices. .Net objects are used for communication between applications.


Remote Objects?
        Any object that should be able to access across domain boundaries.Any object can be changed into a remote object by deriving it from MarshalByRefObject, or by making it serializable either by adding the[Serializable] tag or by implementing the ISerializable interface. When a client activates a remote object, it receives a proxy to the remote object.

       Marshaling by value is analogous to having a copy of the server object at the client. Objects that are marshaled by value are created on the remote server, serialized into a stream, and transmitted to the client where an exact copy is reconstructed. Once copied to the caller's application domain (by the marshaling process), all method calls and property accesses are executed entirely within that domain.
      In .Net if you want to make any calls to a remote object, .net will provide you a proxy of the object and use it for further calls. Here the proxy is responsible for all kinds of data marshalling between domain/context boundaries 

Marshalling?
          Object Marshalling specifies how a remote object is exposed to the client application. It is the process of packaging an object access request in one application domain and passing that request to another domain. The .NET Remoting infrastructure manages the entire marshaling process. There are two methods by which a remote object can be made available to a local client object: Marshal by value, and Marshal by reference. 


          Marshaling by value => A copy of the server object at the client. Objects that are marshaled by value are created on the remote server, serialized into a stream, and transmitted to the client where an exact copy is reconstructed. Once copied to the caller's application domain (by the marshaling process), all method calls and property accesses are executed entirely within that domain.

Marshall by value has several implications; 

       1.  The entire remote object is transmitted on the network. 
       2. Some or the entire remote object may have no relevance outside of its local context. For example, the remote object may have a connection to a database, or a handle to a window, or a file handle etc. 
       3. Parts of the remote object may not be serialiazable. In addition, when the client invokes a method on an MBV object, the local machine does the execution, which means that the compiled code (remote class) has to be available to the client. Because the object exists entirely in the caller's application domain, no state changes to the object are communicated to the originating application domain, or from the originator back to the caller.


It is impliemented by the Serilaizable concept....


              Marshal by reference => Marshal by reference passes a reference to the remote object back to the client. This reference is an ObjRef class that contains all the information required to generate the proxy object that does the communication with the actual remote object. On the network, only parameters and return values are passed. A remote method invocation requires the remote object to call its method on the remote host (server).


Marshal by reference classes must inherit from System.MarshalByRefObject.


Object Activation?
                Object activation refers to the various ways in which a remote object can be instantiated. Marshal by value objects have a simple activation scheme, they are created when the client first requests them. Marshal by reference objects have two activation schemes:

         Server Activated Objects => Server Activated Objects(Activator.GetObjec(...)) are created only when the client makes the first call to the remote method. In other words, when the client asks for the creation of the remote object, only the local proxy is created on the client, the actual remote object on the server is instantiated on the first method call. There are 2 types of call:

             SingleCall => Each method call will create a new instance
             Sinleton => Each method call will share a single object and all the clients use the same object. 
             ClientActivatedObjects( new or Activator.CreateInstance(...) )




Implementation......


1. Create Remote Object

using System.Runtime.Remoting; 
public class MyRemotableObject : MarshalByRefObject
{ 
    public MyRemotableObject()
    { 
    }
    public void SetMessage(string message)
    {
       Cache.GetInstance().MessageString = message;
    } 
}


2. Create a host application and host the above remote objects , so clients can access...

 using System.Runtime.Remoting;
 using System.Runtime.Remoting.Channels;
 using System.Runtime.Remoting.Channels.Tcp;
 public class FormHost : System.Windows.Forms.Form
    {        
        private MyRemotableObject remotableObject;
        private System.ComponentModel.Container components = null;
        public FormHost()
        {
            InitializeComponent();
            remotableObject = new MyRemotableObject();
            // using TCP protocol

            TcpChannel channel = new TcpChannel(8080);
            ChannelServices.RegisterChannel(channel);
            RemotingConfiguration.RegisterWellKnownServiceType(
               typeof(MyRemotableObject),"HelloWorld",
               WellKnownObjectMode.Singleton);
            RemotableObjects.Cache.Attach(this);
        }
    }



3. Now see how to call remote object from client..
 using System.Runtime.Remoting;
 using System.Runtime.Remoting.Channels;
 using System.Runtime.Remoting.Channels.Tcp;  
 public class frmRCleint : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox textBox1; 
        MyRemotableObject remoteObject;
        private System.ComponentModel.Container components = null;
        public frmRCleint()
        {
            InitializeComponent();
            // using TCP protocol 

            // running both client and server on same machines

            TcpChannel chan = new TcpChannel();
            ChannelServices.RegisterChannel(chan// Create an instance of the remote object

            remoteObject = (MyRemotableObject) Activator.GetObject(
              typeof(MyRemotableObject),"tcp://localhost:8080/HelloWorld");
        }
        private void textBox1_TextChanged(object sender, System.EventArgs e)
        {
            remoteObject.SetMessage(textBox1.Text);
        }
    }

Friday, April 8, 2011

Some Tips on Loading and Unloading Workflow


WorkflowInstance.Unload:
As we know, WorkflowInstance.Unload is the method that the host application can call in order to passivate a WF program instance. WorkflowInstance.TryUnload differs from Unload in that it will succeed only if the instance is idle or suspended. If the instance is doing any work (the scheduler thread is active) when tryUnload is called, a value of false is returned and there is no effect on the instance.


Every WF program instance has a globally unique instance identifier associated with it. Thus, the host application can load a previously saved (passivated) WF program instance by calling WorkflowRuntime.GetWorkflow passing the required identifier. GetWorkflow causes the program instance to be loaded into memory from the store provided by the persistence service. If the host application already holds a WorkflowInstance for a passivated instance, the instance can be loaded by calling WorkflowInstance.Load, as shown here:

   1:  using (WorkflowRuntime runtime = new WorkflowRuntime())
   2:  {
   3:    runtime.StartRuntime();
   4:   
   5:    WorkflowInstance instance = runtime.CreateWorkflow(type);
   6:   
   7:    instance.Start();
   8:    ...
   9:    instance.Unload();
  10:    ...
  11:    instance.Load();
  12:    ...
  13:  }

There is no fundamental difference between WorkflowRuntime.GetWorkflow and
WorkflowInstance.Load , Typically, a passivated instance is loaded in order to deliver stimulus that has arrived from the external world:

   1:  Guid handle = ...
   2:  WorkflowInstance instance = runtime.GetWorkflow(handle);
   3:  instance.EnqueueItem(...);

Monday, April 4, 2011

The Disposable Design Pattern

Please read my previous post  Managed Vs Unmanaged Resources and Garbage Collector before satrt reading this one.


As I mentioned dispose pattern is used for explicitly cleaning managed/unmanaged resources. A Dispose method should call the GC.SuppressFinalize method for the object it is disposing. If the object is currently on the finalization queue,GC.SuppressFinalize prevents its Finalize method from being called. Remember that executing a Finalize method is costly to performance. If yourDispose method has already done the work to clean up the object, then it is not necessary for the garbage collector to call the object's Finalizemethod.


As per this pattern, the class needs to impliment the interface IDisposable and the method Dispose() will impliment the clean up code. It should also release all resources owned by its base types by calling its parent type's Dispose method. The below code shows the code structure for the same.



   1:  class MyClass : IDisposable {
   2:   
   3:  /// <summary>Finalizer that will be called by the GC.</summary>
   4:  ~MyClass() {
   5:  Dispose(false); // false == called by the GC => IMPLICITE CALL for unmanaged clean up..
   6:  }
   7:   
   8:  /// <summary>Immediately releases all resources owned by the object.</summary>
   9:  public void Dispose() {
  10:  Dispose(true); // true == called explicitly by user code
  11:    
  12:  // We have released our resources already. Tell the GC that it
  13:  // doesn't need to call the finalizer anymore.
  14:  GC.SuppressFinalize(this);
  15:  }
  16:   
  17:  /// <summary>Immediately releases all resources owned by the object.</summary>
  18:  /// <param name="managed">
  19:  ///   If true, the object is being disposed explicitly and user wants to remove managed                ///    resources
  20:  ///   other managed objects it is referencing. If false, other managed objects
  21:  ///   maybe have been destroyed already and destroy only unmanaged.
  22:  /// </param>
  23:  protected virtual void Dispose(bool managed) {
  24:  if(managed) {
  25:  // Perform finalization of managed objects here
  26:  }
  27:   
  28:  // Perform finalization of unmanaged objects here
  29:  }
  30:   
  31:  }


Managed Vs Unmanaged Resources and Garbage Collector

Managed Vs UnManaged Resources:
  The Resourses which CLR handles directly are called managed ones and these items will be disposed after usage by the CLR. But in .net we can also use resources like File streams, windows handles, database connections, COM  objects( generally the resource which wraps OS types)...which are actually handled by win32 itself and can be termed as Unmanaged resources which we need to handle explicitley in code.


CLR Garbage Collector for Managed Resources:
Actually CLR Garbage collector will  free the managed resources memory implicitely.  Here GC will examins the managed heap and all the non referenced objects in managed heap will be destroyed and free up the memory. For the unmanaged objects, GC dont have any idea and we need to explicitly ask GC to clean up the same.  For this we impliment the Object.Finalize() method and will write code for releasing unmanaged objects . GC will call the object.Finalize() method during clean up operation and will perform the operation mentioned in the finalize method. In C# Finalize() methods are nothing but destrucotrs. So Finalize() will automatically call the destructor of the class.
The garbage collection GC class provides the GC.Collect method, which you can use to give your application some direct control over the garbage collector. In general, you should avoid calling any of the collect methods and allow the garbage collector to run independently.


Limitation of Finalize() => Implementing Finalize methods or destructors can have a negative impact on performance and you should avoid using them unnecessarily.The garbage collector keeps track of objects that have Finalize methods, using an internal structure called the finalization queue. Each time your application creates an object that has a Finalize method, the garbage collector places an entry in the finalization queue that points to that object. The finalization queue contains entries for all the objects in the managed heap that need to have their finalization code called before the garbage collector can reclaim their memory. So Reclaiming the memory used by objects with Finalize methods requires at least two garbage collections.
When the garbage collector performs a collection, 

   step1: It reclaims the memory for inaccessible objects without finalizers. At this time, it cannot collect the inaccessible objects that do have finalizers. Instead, it removes the entries for these objects from the finalization queue and places them in a list of objects marked as ready for finalization. Entries in this list point to the objects in the managed heap that are ready to have their finalization code called. The garbage collector calls the Finalize methods for the objects in this list and then removes the entries from the list. 
   step2 : A future garbage collection will determine that the finalized objects are truly garbage because they are no longer pointed to by entries in the list of objects marked as ready for finalization. In this future garbage collection, the objects' memory is actually reclaimed.


If We dont want to wait for GC and Need to free the memory (Recommended):
 Programers can use patterns like disposable patterns to explicitly free up the managed/unmanaged resources without waiting for the GC . Please find my post The Disposable Pattern for more details.