Click here to Skip to main content
11,641,971 members (62,089 online)
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads
Add your own
alternative version

Building .NET Coverage Tool

, 25 Aug 2009 MIT 46.8K 1.8K 107
This article is a walkthrough for building a .NET coverage tool
Coverage.NET_demo.zip
TestApp
TestApp.exe
TestApp.pdb
Tool
Coverage.Counter.dll
Coverage.exe
Mono.Cecil.dll
Mono.Tools.Pdb2Mdb.dll
Coverage.NET_src.zip
Coverage
Common
Instrument
Properties
Report
Coverage.Counter
Properties
Mono.Cecil
src
AUTHORS
ChangeLog
CodeGen
cecil-gen-attributes.rb
cecil-gen-sources.rb
cecil-gen-tests.rb
cecil-gen-types.rb
cecil-gen.rb
cecil-mig.rb
cecil-update-rev
templates
configure
default.build
Makefile
mono-cecil.pc.in
Mono.Cecil
Mono.Cecil.Binary
Mono.Cecil.Cil
Mono.Cecil.dll.sources
Mono.Cecil.Metadata
Mono.Cecil.Signatures
Mono.Xml
ChangeLog
NEWS
README
standalone.make
TODO
Mono.Tools.Pdb2Mdb
Properties
src
ChangeLog
LICENSE
Makefile
pdb2mdb.exe.sources
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.IO;

namespace Microsoft.Cci.Pdb {
  internal class PdbWriter {
    internal PdbWriter(Stream writer, int pageSize) {
      this.pageSize = pageSize;
      this.usedBytes = pageSize * 3;
      this.writer = writer;

      writer.SetLength(usedBytes);
    }

    internal void WriteMeta(DataStream[] streams, BitAccess bits) {
      PdbFileHeader head = new PdbFileHeader(pageSize);

      WriteDirectory(streams,
                     out head.directoryRoot,
                     out head.directorySize,
                     bits);
      WriteFreeMap();

      head.freePageMap = 2;
      head.pagesUsed = usedBytes / pageSize;

      writer.Seek(0, SeekOrigin.Begin);
      head.Write(writer, bits);
    }

    private void WriteDirectory(DataStream[] streams,
                                out int directoryRoot,
                                out int directorySize,
                                BitAccess bits) {
      DataStream directory = new DataStream();

      int pages = 0;
      for (int s = 0; s < streams.Length; s++) {
        if (streams[s].Length > 0) {
          pages += streams[s].Pages;
        }
      }

      int use = 4 * (1 + streams.Length + pages);
      bits.MinCapacity(use);
      bits.WriteInt32(streams.Length);
      for (int s = 0; s < streams.Length; s++) {
        bits.WriteInt32(streams[s].Length);
      }
      for (int s = 0; s < streams.Length; s++) {
        if (streams[s].Length > 0) {
          bits.WriteInt32(streams[s].pages);
        }
      }
      directory.Write(this, bits.Buffer, use);
      directorySize = directory.Length;

      use = 4 * directory.Pages;
      bits.MinCapacity(use);
      bits.WriteInt32(directory.pages);

      DataStream ddir = new DataStream();
      ddir.Write(this, bits.Buffer, use);

      directoryRoot = ddir.pages[0];
    }

    private void WriteFreeMap() {
      byte[] buffer = new byte[pageSize];

      // We configure the old free map with only the first 3 pages allocated.
      buffer[0] = 0xf8;
      for (int i = 1; i < pageSize; i++) {
        buffer[i] = 0xff;
      }
      Seek(1, 0);
      Write(buffer, 0, pageSize);

      // We configure the new free map with all of the used pages gone.
      int count = usedBytes / pageSize;
      int full = count / 8;
      for (int i = 0; i < full; i++) {
        buffer[i] = 0;
      }
      int rema = count % 8;
      buffer[full] = (byte)(0xff << rema);

      Seek(2, 0);
      Write(buffer, 0, pageSize);
    }

    internal int AllocatePages(int count) {
      int begin = usedBytes;

      usedBytes += count * pageSize;
      writer.SetLength(usedBytes);

      if (usedBytes > pageSize * pageSize * 8) {
        throw new Exception("PdbWriter does not support multiple free maps.");
      }
      return begin / pageSize;
    }

    internal void Seek(int page, int offset) {
      writer.Seek(page * pageSize + offset, SeekOrigin.Begin);
    }

    internal void Write(byte[] bytes, int offset, int count) {
      writer.Write(bytes, offset, count);
    }

    //////////////////////////////////////////////////////////////////////
    //
    internal int PageSize {
      get { return pageSize; }
    }

    //////////////////////////////////////////////////////////////////////
    //
    internal readonly int pageSize;
    private Stream writer;
    private int usedBytes;
  }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Sergiy Sakharov
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 25 Aug 2009
Article Copyright 2009 by Sergiy Sakharov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid