Skip to content
This repository was archived by the owner on Nov 15, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main/OpenCover.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace OpenCover.Console
{
class Program
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(Bootstrapper));
private static readonly ILog Logger = LogManager.GetLogger("OpenCover");

/// <summary>
/// This is the initial console harness - it may become the full thing
Expand Down
16 changes: 8 additions & 8 deletions main/OpenCover.Framework/Communication/CommunicationManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using log4net.Repository.Hierarchy;
using OpenCover.Framework.Manager;
using OpenCover.Framework.Service;

Expand Down Expand Up @@ -68,20 +70,18 @@ private static void SendChunkAndWaitForConfirmation(int writeSize, IManagedCommu
mcb.InformationReadByProfiler.Reset();
}

private byte[] _data = null;
public byte[] HandleMemoryBlock(IManagedMemoryBlock mmb)
{
_data = _data ?? new byte[mmb.BufferSize];
mmb.ProfilerHasResults.Reset();
do
{
mmb.StreamAccessorResults.Seek(0, SeekOrigin.Begin);
} while (mmb.StreamAccessorResults.Read(mmb.Buffer, 0, mmb.BufferSize) != mmb.BufferSize);

mmb.StreamAccessorResults.Seek(0, SeekOrigin.Begin);
mmb.StreamAccessorResults.Read(_data, 0, mmb.BufferSize);

var nCount = (int)BitConverter.ToUInt32(_data, 0);
var nCount = (int)BitConverter.ToUInt32(mmb.Buffer, 0);
var dataSize = (nCount + 1)*sizeof (UInt32);
var newData = new byte[dataSize];
Buffer.BlockCopy(_data, 0, newData, 0, dataSize);

Buffer.BlockCopy(mmb.Buffer, 0, newData, 0, dataSize);
mmb.ResultsHaveBeenReceived.Set();

return newData;
Expand Down
145 changes: 84 additions & 61 deletions main/OpenCover.Framework/Communication/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using log4net;
using OpenCover.Framework.Manager;
using OpenCover.Framework.Model;
using OpenCover.Framework.Service;
Expand All @@ -30,6 +31,8 @@ public class MessageHandler : IMessageHandler
private readonly IMarshalWrapper _marshalWrapper;
private readonly IMemoryManager _memoryManager;

private static readonly ILog Logger = LogManager.GetLogger("OpenCover");

public MessageHandler(IProfilerCommunication profilerCommunication, IMarshalWrapper marshalWrapper, IMemoryManager memoryManager)
{
_profilerCommunication = profilerCommunication;
Expand All @@ -56,76 +59,96 @@ public int StandardMessage(MSG_Type msgType, IManagedCommunicationBlock mcb, Act

case MSG_Type.MSG_GetSequencePoints:
{
var msgGSP = _marshalWrapper.PtrToStructure<MSG_GetSequencePoints_Request>(pinnedMemory);
InstrumentationPoint[] origPoints;
var responseCSP = new MSG_GetSequencePoints_Response();
_profilerCommunication.GetSequencePoints(msgGSP.modulePath, msgGSP.assemblyName,
msgGSP.functionToken, out origPoints);
var num = origPoints == null ? 0 : origPoints.Length;

var index = 0;
var chunk = Marshal.SizeOf(typeof (MSG_SequencePoint));
do
var responseGSP = new MSG_GetSequencePoints_Response();
try
{
writeSize = Marshal.SizeOf(typeof (MSG_GetSequencePoints_Response));
responseCSP.more = num > GSP_BufSize;
responseCSP.count = num > GSP_BufSize ? GSP_BufSize : num;
_marshalWrapper.StructureToPtr(responseCSP, pinnedMemory, false);
for (var i = 0; i < responseCSP.count; i++)
var msgGSP = _marshalWrapper.PtrToStructure<MSG_GetSequencePoints_Request>(pinnedMemory);
InstrumentationPoint[] origPoints;
_profilerCommunication.GetSequencePoints(msgGSP.modulePath, msgGSP.assemblyName,
msgGSP.functionToken, out origPoints);
var num = origPoints == null ? 0 : origPoints.Length;

var index = 0;
var chunk = Marshal.SizeOf(typeof (MSG_SequencePoint));
do
{
var point = new MSG_SequencePoint();
point.offset = origPoints[index].Offset;
point.uniqueId = origPoints[index].UniqueSequencePoint;

_marshalWrapper.StructureToPtr(point, pinnedMemory + writeSize, false);
writeSize += chunk;
index++;
}

if (responseCSP.more)
{
chunkReady(writeSize, mcb);
num -= GSP_BufSize;
}
} while (responseCSP.more);
writeSize = Marshal.SizeOf(typeof (MSG_GetSequencePoints_Response));
responseGSP.more = num > GSP_BufSize;
responseGSP.count = num > GSP_BufSize ? GSP_BufSize : num;
_marshalWrapper.StructureToPtr(responseGSP, pinnedMemory, false);
for (var i = 0; i < responseGSP.count; i++)
{
var point = new MSG_SequencePoint();
point.offset = origPoints[index].Offset;
point.uniqueId = origPoints[index].UniqueSequencePoint;

_marshalWrapper.StructureToPtr(point, pinnedMemory + writeSize, false);
writeSize += chunk;
index++;
}

if (responseGSP.more)
{
chunkReady(writeSize, mcb);
num -= GSP_BufSize;
}
} while (responseGSP.more);
}
catch (Exception ex)
{
Logger.DebugFormat("{0}:{1}", ex.GetType(), ex.Message);
responseGSP.more = false;
responseGSP.count = 0;
_marshalWrapper.StructureToPtr(responseGSP, pinnedMemory, false);
}
}
break;

case MSG_Type.MSG_GetBranchPoints:
{
var msgGBP = _marshalWrapper.PtrToStructure<MSG_GetBranchPoints_Request>(pinnedMemory);
BranchPoint[] origPoints;
var responseCSP = new MSG_GetBranchPoints_Response();
_profilerCommunication.GetBranchPoints(msgGBP.modulePath, msgGBP.assemblyName,
msgGBP.functionToken, out origPoints);
var num = origPoints == null ? 0 : origPoints.Length;

var index = 0;
var chunk = Marshal.SizeOf(typeof (MSG_BranchPoint));
do
var responseGBP = new MSG_GetBranchPoints_Response();
try
{
writeSize = Marshal.SizeOf(typeof (MSG_GetBranchPoints_Response));
responseCSP.more = num > GBP_BufSize;
responseCSP.count = num > GBP_BufSize ? GBP_BufSize : num;
_marshalWrapper.StructureToPtr(responseCSP, pinnedMemory, false);
for (var i = 0; i < responseCSP.count; i++)
var msgGBP = _marshalWrapper.PtrToStructure<MSG_GetBranchPoints_Request>(pinnedMemory);
BranchPoint[] origPoints;
_profilerCommunication.GetBranchPoints(msgGBP.modulePath, msgGBP.assemblyName,
msgGBP.functionToken, out origPoints);
var num = origPoints == null ? 0 : origPoints.Length;

var index = 0;
var chunk = Marshal.SizeOf(typeof (MSG_BranchPoint));
do
{
var point = new MSG_BranchPoint();
point.offset = origPoints[index].Offset;
point.uniqueId = origPoints[index].UniqueSequencePoint;
point.path = origPoints[index].Path;

_marshalWrapper.StructureToPtr(point, pinnedMemory + writeSize, false);
writeSize += chunk;
index++;
}

if (responseCSP.more)
{
chunkReady(writeSize, mcb);
num -= GBP_BufSize;
}
} while (responseCSP.more);
writeSize = Marshal.SizeOf(typeof (MSG_GetBranchPoints_Response));
responseGBP.more = num > GBP_BufSize;
responseGBP.count = num > GBP_BufSize ? GBP_BufSize : num;
_marshalWrapper.StructureToPtr(responseGBP, pinnedMemory, false);
for (var i = 0; i < responseGBP.count; i++)
{
var point = new MSG_BranchPoint();
point.offset = origPoints[index].Offset;
point.uniqueId = origPoints[index].UniqueSequencePoint;
point.path = origPoints[index].Path;

_marshalWrapper.StructureToPtr(point, pinnedMemory + writeSize, false);
writeSize += chunk;
index++;
}

if (responseGBP.more)
{
chunkReady(writeSize, mcb);
num -= GBP_BufSize;
}
} while (responseGBP.more);
}
catch (Exception ex)
{
Logger.DebugFormat("{0}:{1}", ex.GetType(), ex.Message);
responseGBP.more = false;
responseGBP.count = 0;
_marshalWrapper.StructureToPtr(responseGBP, pinnedMemory, false);
}
}
break;

Expand Down
1 change: 1 addition & 0 deletions main/OpenCover.Framework/Manager/IManagedMemoryBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public interface IManagedMemoryBlock : IDisposable
EventWaitHandle ResultsHaveBeenReceived { get; }
MemoryMappedViewStream StreamAccessorResults { get; }
int BufferSize { get; }
byte[] Buffer { get; }
}
}
2 changes: 2 additions & 0 deletions main/OpenCover.Framework/Manager/MemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ internal class ManagedMemoryBlock : ManagedBlock, IManagedMemoryBlock
private readonly MemoryMappedFile _mmfResults;
public MemoryMappedViewStream StreamAccessorResults { get; private set; }
public int BufferSize { get; private set; }
public byte[] Buffer { get; private set; }

/// <summary>
/// Gets an ACL for unit test purposes
Expand Down Expand Up @@ -112,6 +113,7 @@ internal ManagedMemoryBlock(string @namespace, string key, int bufferSize, int b
transparent,
HandleInheritability.Inheritable);

Buffer = new byte[bufferSize];
StreamAccessorResults = _mmfResults.CreateViewStream(0, bufferSize, MemoryMappedFileAccess.ReadWrite);
StreamAccessorResults.Write(BitConverter.GetBytes(0), 0, 4);
BufferSize = bufferSize;
Expand Down
47 changes: 28 additions & 19 deletions main/OpenCover.Framework/Symbols/CecilSymbolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ private static SymbolFolder FindSymbolsFolder(string fileName, string targetfold
return null;
}

private void LoadSourceAssembly()
{
try
{
var symbolFolder = FindSymbolsFolder();
if (symbolFolder == null) return;
var folder = symbolFolder.TargetFolder ?? Environment.CurrentDirectory;

var parameters = new ReaderParameters
{
SymbolReaderProvider = symbolFolder.SymbolReaderProvider ?? new PdbReaderProvider(),
ReadingMode = ReadingMode.Deferred,
ReadSymbols = true
};
var fileName = Path.GetFileName(ModulePath) ?? string.Empty;
_sourceAssembly = AssemblyDefinition.ReadAssembly(Path.Combine(folder, fileName), parameters);

if (_sourceAssembly != null)
_sourceAssembly.MainModule.ReadSymbols(parameters.SymbolReaderProvider.GetSymbolReader(_sourceAssembly.MainModule, _sourceAssembly.MainModule.FullyQualifiedName));
}
catch (Exception)
{
// failure to here is quite normal for DLL's with no PDBs => no instrumentation
_sourceAssembly = null;
}
}

public AssemblyDefinition SourceAssembly
{
get
Expand All @@ -109,25 +136,7 @@ public AssemblyDefinition SourceAssembly
var currentPath = Environment.CurrentDirectory;
try
{
var symbolFolder = FindSymbolsFolder();
var folder = symbolFolder.Maybe(_ => _.TargetFolder) ?? Environment.CurrentDirectory;

var parameters = new ReaderParameters
{
SymbolReaderProvider = symbolFolder.SymbolReaderProvider ?? new PdbReaderProvider(),
ReadingMode = ReadingMode.Deferred,
ReadSymbols = true
};
var fileName = Path.GetFileName(ModulePath) ?? string.Empty;
_sourceAssembly = AssemblyDefinition.ReadAssembly(Path.Combine(folder, fileName), parameters);

if (_sourceAssembly != null)
_sourceAssembly.MainModule.ReadSymbols(parameters.SymbolReaderProvider.GetSymbolReader(_sourceAssembly.MainModule, _sourceAssembly.MainModule.FullyQualifiedName));
}
catch (Exception)
{
// failure to here is quite normal for DLL's with no PDBs => no instrumentation
_sourceAssembly = null;
LoadSourceAssembly();
}
finally
{
Expand Down
10 changes: 10 additions & 0 deletions main/OpenCover.Framework/log4net.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@
<conversionPattern value="%message%newline" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="DEBUG" />
</filter>
</appender>

<root>
<appender-ref ref="ColoredConsoleAppender" />
<appender-ref ref="DebugAppender" />
</root>

</log4net>
Expand Down
Loading