static protected uint ReadAndUnpackBits(_DBG.BitStream stream)
        {
            const int SHIFT_PER_NIBBLE = 2; //2^2 = 4 bits per nibble
            uint      nibbles          = stream.ReadBits(Packets.Commands.Bits.NibbleCount) + 1;

            return(stream.ReadBits((int)nibbles << SHIFT_PER_NIBBLE));
        }
        public TimestampPacket(_DBG.BitStream stream) : base(Commands.c_Profiling_Timestamp)
        {
            ulong l = (ulong)ReadAndUnpackBits(stream) << Packets.Commands.Bits.TimestampShift;

            m_time = l / TimeSpan.TicksPerMillisecond;
            Tracing.PacketTrace("time {0}", m_time);
        }
        static protected uint ReadMethodDefIndex(_DBG.BitStream stream)
        {
            uint assembly = ReadAndUnpackBits(stream);
            uint method   = ReadAndUnpackBits(stream);

            return(assembly << ASSEMBLY_BIT_OFFSET | method);
        }
        static protected uint ReadTypeDefIndex(_DBG.BitStream stream)
        {
            uint assembly = ReadAndUnpackBits(stream);
            uint type     = ReadAndUnpackBits(stream);

            return(assembly << ASSEMBLY_BIT_OFFSET | type);
        }
        public HeapDumpObjectPacket(_DBG.BitStream stream)
            : base(Commands.c_Profiling_HeapDump_Object)
        {
            m_address = ReadAndUnpackBits(stream);
            m_size    = ReadAndUnpackBits(stream) * ProfilerSession.HeapBlockSize;
            m_dt      = (_DBG.RuntimeDataType)stream.ReadBits(Commands.Bits.DataType);
            if (m_dt == _DBG.RuntimeDataType.DATATYPE_CLASS || m_dt == _DBG.RuntimeDataType.DATATYPE_VALUETYPE)
            {
                m_typedef = ReadTypeDefIndex(stream);
            }
            else if (m_dt == _DBG.RuntimeDataType.DATATYPE_SZARRAY)
            {
                m_arrayElementType = ReadTypeDefIndex(stream);
                m_arrayLevels      = (ushort)ReadAndUnpackBits(stream);
            }

            m_refs = new List <uint>();

            bool moreRefs;

            while (moreRefs = ReadBoolean(stream))
            {
                m_refs.Add(ReadAndUnpackBits(stream));
            }
        }
        internal static Packets.ProfilerPacket Decode(_DBG.BitStream stream)
        {
            uint type = stream.ReadBits(Packets.Commands.Bits.CommandHeader);

            Tracing.PacketTrace("New Packet {0}", type);
            switch (type)
            {
            case Packets.Commands.c_Profiling_Timestamp:
                return(new Packets.TimestampPacket(stream));

            case Packets.Commands.c_Profiling_Memory_Layout:
                return(new Packets.MemoryLayoutPacket(stream));

            case Packets.Commands.c_Profiling_HeapDump_Start:
                return(new Packets.HeapDumpStartPacket(stream));

            case Packets.Commands.c_Profiling_HeapDump_Stop:
                return(new Packets.HeapDumpStopPacket(stream));

            case Packets.Commands.c_Profiling_HeapDump_Root:
                return(new Packets.HeapDumpRootPacket(stream));

            case Packets.Commands.c_Profiling_HeapDump_Object:
                return(new Packets.HeapDumpObjectPacket(stream));

            case Packets.Commands.c_Profiling_Calls_Call:
                return(new Packets.CallPacket(stream));

            case Packets.Commands.c_Profiling_Calls_Return:
                return(new Packets.ReturnPacket(stream));

            case Packets.Commands.c_Profiling_Calls_CtxSwitch:
                return(new Packets.ContextSwitchPacket(stream));

            case Packets.Commands.c_Profiling_Allocs_Alloc:
                return(new Packets.ObjectAllocationPacket(stream));

            case Packets.Commands.c_Profiling_Allocs_Relloc:
                return(new Packets.ObjectRelocationPacket(stream));

            case Packets.Commands.c_Profiling_Allocs_Delete:
                return(new Packets.ObjectDeletionPacket(stream));

            case Packets.Commands.c_Profiling_GarbageCollect_Begin:
                return(new Packets.GarbageCollectionBeginPacket(stream));

            case Packets.Commands.c_Profiling_GarbageCollect_End:
                return(new Packets.GarbageCollectionEndPacket(stream));

            case Packets.Commands.c_Profiling_HeapCompact_Begin:
                return(new Packets.HeapCompactionBeginPacket(stream));

            case Packets.Commands.c_Profiling_HeapCompact_End:
                return(new Packets.HeapCompactionEndPacket(stream));

            default:
                throw new ApplicationException("Unable to decode packet.");
            }
        }
 public CallPacket(_DBG.BitStream stream)
     : base(Commands.c_Profiling_Calls_Call)
 {
     if (ReadBoolean(stream))
     {
         m_assembly = ReadAndUnpackBits(stream);
         Debug.Assert(m_assembly != 0);
     }
     m_method = ReadAndUnpackBits(stream);
 }
 public HeapDumpRootPacket(_DBG.BitStream stream)
     : base(Commands.c_Profiling_HeapDump_Root)
 {
     m_address = ReadAndUnpackBits(stream);
     m_source  = stream.ReadBits(Packets.Commands.Bits.RootTypes);
     switch (m_source)
     {
     case Packets.Commands.RootTypes.Root_Stack:
         m_method = ReadMethodDefIndex(stream);
         break;
     }
     Tracing.PacketTrace("root type:{0} at {1}", m_source, m_address);
 }
        public ObjectRelocationPacket(_DBG.BitStream stream)
            : base(Commands.c_Profiling_Allocs_Relloc)
        {
            uint count = ReadAndUnpackBits(stream);

            reloc = new ObjectRelocation.RelocationRegion[count];

            for (uint i = 0; i < count; i++)
            {
                reloc[i]          = new ObjectRelocation.RelocationRegion();
                reloc[i].m_start  = ReadAndUnpackBits(stream);
                reloc[i].m_end    = ReadAndUnpackBits(stream);
                reloc[i].m_offset = ReadAndUnpackBits(stream);
            }
        }
 public ObjectAllocationPacket(_DBG.BitStream stream)
     : base(Commands.c_Profiling_Allocs_Alloc)
 {
     m_address = ReadAndUnpackBits(stream);
     m_size    = ReadAndUnpackBits(stream) * ProfilerSession.HeapBlockSize;
     m_dt      = (_DBG.RuntimeDataType)stream.ReadBits(Commands.Bits.DataType);
     if (m_dt == _DBG.RuntimeDataType.DATATYPE_CLASS || m_dt == _DBG.RuntimeDataType.DATATYPE_VALUETYPE ||
         m_dt == _DBG.RuntimeDataType.DATATYPE_SZARRAY)
     {
         m_type = ReadTypeDefIndex(stream);
         if (m_dt == _DBG.RuntimeDataType.DATATYPE_SZARRAY)
         {
             m_rank = (ushort)ReadAndUnpackBits(stream);
         }
     }
     else
     {
         m_type = (uint)m_dt;
     }
 }
        public ProfilerSession(_DBG.Engine engine)
        {
            if (engine == null)
            {
                throw new ArgumentNullException();
            }

            m_connected = true;
            m_engine = engine;
            m_engine.OnCommand += new _DBG.CommandEventHandler(OnDeviceCommand);
            m_incomingStream = new _DBG.BitStream(true);

            m_startTime = 0;
            m_lastKnownTime = 0;
            m_currentHeapDump = null;
            m_threadCallStacks = new Dictionary<uint, Stack<uint>>();

            m_liveObjectTable = new List<uint>();

            m_firstPacket = true;

            m_receiverThread = new Thread(WorkerThread);
            m_receiverThread.Start();
        }
        public ProfilerSession(_DBG.Engine engine)
        {
            if (engine == null)
            {
                throw new ArgumentNullException();
            }

            m_connected         = true;
            m_engine            = engine;
            m_engine.OnCommand += new _DBG.CommandEventHandler(OnDeviceCommand);
            m_incomingStream    = new _DBG.BitStream(true);

            m_startTime        = 0;
            m_lastKnownTime    = 0;
            m_currentHeapDump  = null;
            m_threadCallStacks = new Dictionary <uint, Stack <uint> >();

            m_liveObjectTable = new List <uint>();

            m_firstPacket = true;

            m_receiverThread = new Thread(WorkerThread);
            m_receiverThread.Start();
        }
		private void InitializeForDeserialization(Type t, byte[] data, int pos, int len)
		{
			m_stream = new BitStream(data, pos, len);            
			m_value = null;
			Initialize(t, true);
		}
		private void InitializeForSerialization(Type t, object o)
		{
			m_stream = new BitStream();
			m_value = o;
			Initialize(t, false);
		}
 public GarbageCollectionEndPacket(_DBG.BitStream stream)
     : base(Commands.c_Profiling_GarbageCollect_End)
 {
     m_freeBytes = ReadAndUnpackBits(stream);
 }
 public HeapCompactionEndPacket(_DBG.BitStream stream)
     : base(Commands.c_Profiling_HeapCompact_End)
 {
     m_freeBytes = ReadAndUnpackBits(stream);
 }
 static protected bool ReadBoolean(_DBG.BitStream stream)
 {
     return(stream.ReadBits(1) == 1);
 }
 public ObjectDeletionPacket(_DBG.BitStream stream)
     : base(Commands.c_Profiling_Allocs_Delete)
 {
     m_address = ReadAndUnpackBits(stream);
 }
 public MemoryLayoutPacket(_DBG.BitStream stream) : base(Commands.c_Profiling_Memory_Layout)
 {
     m_heapAddress = ReadAndUnpackBits(stream);
     m_heapLength  = ReadAndUnpackBits(stream);
     Tracing.PacketTrace("layout {0}:{1}", m_heapAddress, m_heapLength);
 }
 public ContextSwitchPacket(_DBG.BitStream stream)
     : base(Commands.c_Profiling_Calls_Call)
 {
     m_thread = ReadAndUnpackBits(stream);
 }
 public ReturnPacket(_DBG.BitStream stream)
     : base(Commands.c_Profiling_Calls_Call)
 {
     m_duration = (ulong)ReadAndUnpackBits(stream) << Packets.Commands.Bits.CallTimingShift;
 }
 public HeapDumpStartPacket(_DBG.BitStream stream)
     : base(Commands.c_Profiling_HeapDump_Start)
 {
 }
 public HeapDumpStopPacket(_DBG.BitStream stream)
     : base(Commands.c_Profiling_HeapDump_Stop)
 {
     m_heapBytesUsed = ReadAndUnpackBits(stream);
 }