Skip to content

Commit eb624ae

Browse files
author
Gustavo Brown Rodriguez
committed
Various fixes
- add a specific Id for .NET code coverage sessions (Id=3) - Add a public static OnExit entry to allow for third party code which instantiates objects that end up being marked as Main in the same process to do a proper cleanup before exiting the last object. - Update OnExit/1 to be more resilient by ensuring every DbgItem with ticks not set to have a valid tick count - Add (conditionally compiled) code to generate a verbose log file of the trace being written. To enable this trace you have to build GxClasses.dll with _LOG_WRITER symbol defined Issue: 202777
1 parent 7609191 commit eb624ae

File tree

1 file changed

+130
-6
lines changed

1 file changed

+130
-6
lines changed

dotnet/src/dotnetframework/GxClasses/Diagnostics/GXDebugManager.cs

Lines changed: 130 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1+
//#define _DEBUG_DEBUGGER
2+
//#define _LOG_WRITER
13
using System;
24
using System.Collections.Concurrent;
35
using System.Collections.Generic;
46
using System.Diagnostics;
57
using System.IO;
8+
using System.Text;
69
using System.Threading;
710
using GeneXus.Application;
11+
using static GeneXus.Diagnostics.GXDebugStream;
812

913
namespace GeneXus.Diagnostics
1014
{
11-
1215
public class GXDebugManager
1316
{
17+
internal const string _LOG_WRITER = "_LOG_WRITER";
1418
public const short GXDEBUG_VERSION = 2;
15-
internal const GXDebugGenId GENERATOR_ID = GXDebugGenId.CSHARP;
19+
#if NETCORE
20+
internal const GXDebugGenId GENERATOR_ID = GXDebugGenId.NET;
21+
#else
22+
internal const GXDebugGenId GENERATOR_ID = GXDebugGenId.NETFRAMEWORK;
23+
#endif
1624

1725
internal const int PGM_INFO_NO_PARENT = 0;
1826
internal static double MICRO_FREQ = Stopwatch.Frequency / 10e6D;
@@ -220,9 +228,25 @@ private void Save(GXDebugItem[] ToSave, int saveTop = -1, bool saveInThread = tr
220228
else mSave(new object[] { ToSave, saveTop, saveCount });
221229
}
222230

231+
public static void OnExit()
232+
{
233+
if(initialized)
234+
{
235+
Instance.OnExit(null);
236+
}
237+
}
238+
223239
internal void OnExit(GXDebugInfo dbgInfo)
224240
{
225241
PushSystem((int)GXDebugMsgCode.EXIT);
242+
lock (saveLock)
243+
{
244+
for (int i = 0; i < dbgIndex; i++)
245+
{
246+
if (Current[i].Ticks == TICKS_NOT_SET)
247+
Current[i].Ticks = TICKS_NOT_NEEDED;
248+
}
249+
}
226250
Save();
227251
}
228252

@@ -292,12 +316,14 @@ private void mSave(object state1)
292316
#if _DEBUG_DEBUGGER
293317
Console.WriteLine("mSave-" + saveTop);
294318
#endif
319+
LogWriter.Append($"\n SaveTop = {saveTop}\n");
295320
for (; idx < saveTop; idx++)
296321
{
297322
GXDebugItem dbgItem = Data[idx];
298323
#if _DEBUG_DEBUGGER
299324
Console.WriteLine($"item({idx}): { dbgItem }");
300325
#endif
326+
LogWriter.Log($"<{idx}:{dbgItem}>");
301327
switch (dbgItem.MsgType)
302328
{
303329
case GXDebugMsgType.SYSTEM:
@@ -357,8 +383,11 @@ private void mSave(object state1)
357383
continue;
358384
}
359385
ClearDebugItem(dbgItem);
386+
LogWriter.LogPosition(stream);
360387
}
388+
LogWriter.Append($"\n ENDSAVE ");
361389
}
390+
LogWriter.Flush();
362391
}
363392
catch (Exception ex)
364393
{
@@ -421,8 +450,9 @@ internal enum GXDebugMsgCode : byte
421450

422451
internal enum GXDebugGenId : byte
423452
{
424-
CSHARP = 1,
453+
NETFRAMEWORK = 1,
425454
JAVA = 2,
455+
NET = 3,
426456

427457
INVALID = 0xF
428458
}
@@ -538,6 +568,7 @@ public enum ESCAPE : byte
538568

539569
public GXDebugStream(string FileName, FileMode fileMode) : base(FileName, fileMode, fileMode == FileMode.Open ? FileAccess.Read : (fileMode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.None)
540570
{
571+
LogWriter.SetLogName($"{FileName}.log");
541572
Last = 0;
542573
LastLast = 0;
543574
InitializeNewBlock();
@@ -567,8 +598,16 @@ private void WriteEpilog()
567598
}
568599

569600
public void Write(byte[] data) => Write(data, 0, data.Length);
570-
public void WriteRaw(byte[] data, int from, int length) => base.Write(data, from, length);
571-
public void WriteRaw(byte value) => base.WriteByte(value);
601+
public void WriteRaw(byte[] data, int from, int length)
602+
{
603+
LogWriter.LogWriteRaw(data, from, length);
604+
base.Write(data, from, length);
605+
}
606+
public void WriteRaw(byte value)
607+
{
608+
LogWriter.LogWriteByte(value);
609+
base.WriteByte(value);
610+
}
572611

573612
public override void Write(byte[] data, int offset, int count)
574613
{
@@ -582,11 +621,13 @@ public override void Write(byte[] data, int offset, int count)
582621
private byte Last, LastLast;
583622
public override void WriteByte(byte value)
584623
{
624+
LogWriter.LogWriteByte(value);
585625
base.WriteByte(value);
586626
if (value == 0xFF &&
587627
value == Last &&
588628
value == LastLast)
589629
{
630+
LogWriter.Append("<3xFF> ");
590631
WriteRaw(ESCAPE.TRIPLE_FF.ToByte());
591632
Last = LastLast = 0;
592633
}
@@ -602,6 +643,7 @@ public void WriteVLUInt(int value)
602643
if (value < 0) throw new ArgumentException("Cannot handle negative values");
603644
else if (value > 0x1FFFFFFFL)
604645
throw new ArgumentException("Cannot handle > 29bit values");
646+
LogWriter.LogWrite("VLUI", value);
605647
if (value < 0x80)
606648
WriteByte((byte)value);
607649
else if (value < 0x4000)
@@ -616,6 +658,7 @@ public void WriteVLUInt(int value)
616658
public void WriteVLUShort(short value)
617659
{
618660
if (value < 0) throw new ArgumentException("Cannot handle negative values");
661+
LogWriter.LogWrite("VLUS", value);
619662
if (value < 0x80)
620663
WriteByte((byte)value);
621664
else
@@ -627,6 +670,9 @@ public void WriteVLUShort(short value)
627670

628671
public void WriteHeader(Guid sessionGuid, short version, int saveCount)
629672
{
673+
LogWriter.Log("Header", ( "Guid", sessionGuid), ( "Version", version), ("SaveCount", saveCount));
674+
LogWriter.LogPosition(this);
675+
630676
WriteProlog(version);
631677
WriteVLUInt(saveCount);
632678
Write(sessionGuid.ToByteArray());
@@ -689,6 +735,8 @@ internal void WritePgmTrace(int SId, int line1, int col, long ticks)
689735
public void WriteScaledLong(long N)
690736
{
691737
if (N < 0) throw new ArgumentException("Cannot handle negative values");
738+
LogWriter.LogWrite("SCAL", N);
739+
692740
int m = 0;
693741
while (N > 31)
694742
{
@@ -874,6 +922,82 @@ public void InitializeNewBlock()
874922
LastSId = 0;
875923
LastLine1 = 0;
876924
}
925+
926+
internal class LogWriter
927+
{
928+
private static string LogWriterName = "GXDebugCoverage.log";
929+
public static object LogWriterLock = new object();
930+
private static StringBuilder m_Buffer = new StringBuilder();
931+
932+
[Conditional(GXDebugManager._LOG_WRITER)]
933+
public static void SetLogName(string name)
934+
{
935+
LogWriterName = name;
936+
}
937+
938+
[Conditional(GXDebugManager._LOG_WRITER)]
939+
public static void LogPosition(GXDebugStream stream)
940+
{
941+
Append($"\nPos: {stream.Position}\n");
942+
}
943+
944+
[Conditional(GXDebugManager._LOG_WRITER)]
945+
public static void Append(string msg)
946+
{
947+
lock (LogWriterLock)
948+
{
949+
m_Buffer.Append(msg);
950+
}
951+
}
952+
953+
[Conditional(GXDebugManager._LOG_WRITER)]
954+
public static void Flush()
955+
{
956+
try
957+
{
958+
lock (LogWriterLock)
959+
{
960+
File.AppendAllText(LogWriterName, m_Buffer.ToString());
961+
}
962+
}
963+
catch (Exception e)
964+
{
965+
Console.WriteLine(e.StackTrace);
966+
}
967+
m_Buffer.Clear();
968+
}
969+
970+
[Conditional(GXDebugManager._LOG_WRITER)]
971+
internal static void Log(string title, params (string key, object value)[] values)
972+
{
973+
StringBuilder sb = new StringBuilder();
974+
Append($"=========================================\n{title}\n");
975+
if (values != null)
976+
{
977+
foreach ((string key, object value) in values)
978+
{
979+
sb.Append($" {key} = {value?.ToString()}\n");
980+
}
981+
}
982+
Append(sb.ToString());
983+
}
984+
985+
[Conditional(GXDebugManager._LOG_WRITER)]
986+
internal static void LogWrite(string type, long value) => Append($"{type}:{value} ");
987+
988+
[Conditional(GXDebugManager._LOG_WRITER)]
989+
internal static void LogWriteByte(byte value) => Append($"({value.ToString("X2")}) ");
990+
991+
[Conditional(GXDebugManager._LOG_WRITER)]
992+
internal static void LogWriteRaw(byte[] data, int from, int length)
993+
{
994+
lock (LogWriterLock)
995+
{
996+
for (int i = 0; i < length; i++)
997+
m_Buffer.Append($"({data[from+i].ToString("X2")}) ");
998+
}
999+
}
1000+
}
8771001
}
8781002

8791003
#if _DEBUG_DEBUGGER
@@ -897,7 +1021,7 @@ public GXDebugReader(string FileName)
8971021
this.FileName = FileName;
8981022
}
8991023

900-
public static int Main(String[] args)
1024+
public static int Main(string[] args)
9011025
{
9021026
try
9031027
{

0 commit comments

Comments
 (0)