Skip to content

Commit

Permalink
Added support to resolve debug information on Unity Main Thread
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Jan 8, 2019
1 parent 256e7c6 commit 38fe15f
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 17 deletions.
7 changes: 7 additions & 0 deletions ILRuntime/Reflection/ILRuntimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,13 @@ protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindin
if (i.Name == name)
return i;
}
if ((bindingAttr & BindingFlags.DeclaredOnly) != BindingFlags.DeclaredOnly)
{
if (BaseType != null && BaseType is ILRuntimeWrapperType)
{
return BaseType.GetProperty(name, bindingAttr);
}
}
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion ILRuntime/Reflection/ILRuntimeWrapperType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public override EventInfo[] GetEvents(BindingFlags bindingAttr)

protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
{
return et.GetProperty(name, bindingAttr, binder, returnType, types, modifiers);
return et.GetProperty(name, bindingAttr);
}

public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
Expand Down
97 changes: 97 additions & 0 deletions ILRuntime/Runtime/Debugger/DebugService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class DebugService
Runtime.Enviorment.AppDomain domain;
Dictionary<int, LinkedList<BreakpointInfo>> activeBreakpoints = new Dictionary<int, LinkedList<BreakpointInfo>>();
Dictionary<int, BreakpointInfo> breakpointMapping = new Dictionary<int, BreakpointInfo>();
Queue<KeyValuePair<int, VariableReference>> pendingReferences = new Queue<KeyValuePair<int, VariableReference>>();
Queue<KeyValuePair<int, VariableReference>> pendingEnuming = new Queue<KeyValuePair<int, VariableReference>>();
Queue<KeyValuePair<int, KeyValuePair<VariableReference, VariableReference>>> pendingIndexing = new Queue<KeyValuePair<int, KeyValuePair<VariableReference, VariableReference>>>();
AutoResetEvent evt = new AutoResetEvent(false);

public Action<string> OnBreakPoint;
Expand Down Expand Up @@ -472,6 +475,16 @@ internal unsafe VariableInfo[] EnumChildren(int threadHashCode, VariableReferenc
ILIntepreter intepreter;
if (AppDomain.Intepreters.TryGetValue(threadHashCode, out intepreter))
{
#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
if (domain.IsNotUnityMainThread())
{
lock (pendingEnuming)
{
pendingEnuming.Enqueue(new KeyValuePair<int, VariableReference>(threadHashCode, parent));
}
return null;
}
#endif
object obj;
var info = ResolveVariable(threadHashCode, parent, out obj);
if (obj != null)
Expand Down Expand Up @@ -654,6 +667,8 @@ VariableInfo[] EnumObject(object obj, Type t)
{
if (i.GetIndexParameters().Length > 0)
continue;
if (i.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length > 0)
continue;
var val = i.GetValue(obj, null);
VariableInfo info = VariableInfo.FromObject(val);
info.Type = VariableTypes.PropertyReference;
Expand Down Expand Up @@ -682,6 +697,17 @@ internal unsafe VariableInfo ResolveIndexAccess(int threadHashCode, VariableRefe
res = null;
if (AppDomain.Intepreters.TryGetValue(threadHashCode, out intepreter))
{
#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
if (domain.IsNotUnityMainThread())
{
lock (pendingIndexing)
{
pendingIndexing.Enqueue(new KeyValuePair<int, KeyValuePair<VariableReference, VariableReference>>(threadHashCode, new KeyValuePair<VariableReference, VariableReference>(body, idx)));
}
res = null;
return new VariableInfo() { Type = VariableTypes.Pending };
}
#endif
object obj;
var info = ResolveVariable(threadHashCode, body, out obj);
if (obj != null)
Expand Down Expand Up @@ -768,6 +794,63 @@ internal unsafe VariableInfo ResolveIndexAccess(int threadHashCode, VariableRefe
return VariableInfo.NullReferenceExeption;
}

internal void ResolvePendingRequests()
{
lock (pendingReferences)
{
while (pendingReferences.Count > 0)
{
VariableInfo info;
var r = pendingReferences.Dequeue();
try
{
object res;
info = ResolveVariable(r.Key, r.Value, out res);
}
catch (Exception ex)
{
info = VariableInfo.GetException(ex);
}
server.SendSCResolveVariableResult(info);
}
}
lock (pendingEnuming)
{
while (pendingEnuming.Count > 0)
{
VariableInfo[] info;
var r = pendingEnuming.Dequeue();
try
{
info = EnumChildren(r.Key, r.Value);
}
catch (Exception ex)
{
info = new VariableInfo[] { VariableInfo.GetException(ex) };
}
server.SendSCEnumChildrenResult(info);
}
}
lock (pendingIndexing)
{
while (pendingIndexing.Count > 0)
{
VariableInfo info;
var r = pendingIndexing.Dequeue();
try
{
object res;
info = ResolveIndexAccess(r.Key, r.Value.Key, r.Value.Value, out res);
}
catch (Exception ex)
{
info = VariableInfo.GetException(ex);
}
server.SendSCResolveVariableResult(info);
}
}
}

internal unsafe VariableInfo ResolveVariable(int threadHashCode, VariableReference variable, out object res)
{
ILIntepreter intepreter;
Expand All @@ -776,6 +859,17 @@ internal unsafe VariableInfo ResolveVariable(int threadHashCode, VariableReferen
{
if (variable != null)
{
#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
if (domain.IsNotUnityMainThread())
{
lock (pendingReferences)
{
pendingReferences.Enqueue(new KeyValuePair<int, VariableReference>(threadHashCode, variable));
}
res = null;
return new VariableInfo() { Type = VariableTypes.Pending };
}
#endif
switch (variable.Type)
{
case VariableTypes.Normal:
Expand Down Expand Up @@ -999,6 +1093,9 @@ internal void Detach()
{
activeBreakpoints.Clear();
breakpointMapping.Clear();
pendingEnuming.Clear();
pendingReferences.Clear();
pendingIndexing.Clear();
foreach (var j in AppDomain.Intepreters)
{
j.Value.ClearDebugState();
Expand Down
41 changes: 25 additions & 16 deletions ILRuntime/Runtime/Debugger/DebuggerServer/DebuggerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ void OnReceive(DebugMessageType type, byte[] buffer)
{
info = VariableInfo.GetException(ex);
}
SendSCResolveVariableResult(info);
if (info.Type != VariableTypes.Pending)
SendSCResolveVariableResult(info);
}
break;
case DebugMessageType.CSResolveIndexAccess:
Expand All @@ -194,7 +195,8 @@ void OnReceive(DebugMessageType type, byte[] buffer)
{
info = VariableInfo.GetException(ex);
}
SendSCResolveVariableResult(info);
if (info.Type != VariableTypes.Pending)
SendSCResolveVariableResult(info);
}
break;
case DebugMessageType.CSEnumChildren:
Expand All @@ -211,7 +213,8 @@ void OnReceive(DebugMessageType type, byte[] buffer)
{
info = new VariableInfo[] { VariableInfo.GetException(ex) };
}
SendSCEnumChildrenResult(info);
if (info != null)
SendSCEnumChildrenResult(info);
}
break;
}
Expand Down Expand Up @@ -436,27 +439,33 @@ internal void SendSCStepComplete(int intpHash, KeyValuePair<int, StackFrameInfo[
DoSend(DebugMessageType.SCStepComplete);
}

void SendSCResolveVariableResult(VariableInfo info)
internal void SendSCResolveVariableResult(VariableInfo info)
{
sendStream.Position = 0;
WriteVariableInfo(info);
DoSend(DebugMessageType.SCResolveVariableResult);
lock (this)
{
sendStream.Position = 0;
WriteVariableInfo(info);
DoSend(DebugMessageType.SCResolveVariableResult);
}
}

void SendSCEnumChildrenResult(VariableInfo[] info)
internal void SendSCEnumChildrenResult(VariableInfo[] info)
{
sendStream.Position = 0;
if (info != null)
lock (this)
{
bw.Write(info.Length);
for (int i = 0; i < info.Length; i++)
sendStream.Position = 0;
if (info != null)
{
WriteVariableInfo(info[i]);
bw.Write(info.Length);
for (int i = 0; i < info.Length; i++)
{
WriteVariableInfo(info[i]);
}
}
else
bw.Write(0);
DoSend(DebugMessageType.SCEnumChildrenResult);
}
else
bw.Write(0);
DoSend(DebugMessageType.SCEnumChildrenResult);
}

void WriteStackFrames(KeyValuePair<int, StackFrameInfo[]>[] info)
Expand Down
1 change: 1 addition & 0 deletions ILRuntime/Runtime/Debugger/VariableInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum VariableTypes
Error,
NotFound,
Timeout,
Pending,
}

public enum ValueTypes
Expand Down
4 changes: 4 additions & 0 deletions ILRuntime/Runtime/Enviorment/AppDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public class AppDomain

#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
public int UnityMainThreadID { get; set; }
public bool IsNotUnityMainThread()
{
return UnityMainThreadID != 0 && (UnityMainThreadID != System.Threading.Thread.CurrentThread.ManagedThreadId);
}
#endif
public unsafe AppDomain()
{
Expand Down
14 changes: 14 additions & 0 deletions ILRuntime/Runtime/Intepreter/ILIntepreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public unsafe class ILIntepreter
public StackObject* LastStepFrameBase { get; set; }
public int LastStepInstructionIndex { get; set; }
StackObject* ValueTypeBasePointer;
bool mainthreadLock;
public ILIntepreter(Enviorment.AppDomain domain)
{
this.domain = domain;
Expand All @@ -44,6 +45,18 @@ public void Break()
{
//Clear old debug state
ClearDebugState();
#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
if(domain.UnityMainThreadID == Thread.CurrentThread.ManagedThreadId)
{
mainthreadLock = true;
while (mainthreadLock)
{
domain.DebugService.ResolvePendingRequests();
Thread.Sleep(10);
}
return;
}
#endif
lock (_lockObj)
{
Monitor.Wait(_lockObj);
Expand All @@ -52,6 +65,7 @@ public void Break()

public void Resume()
{
mainthreadLock = false;
lock (_lockObj)
Monitor.Pulse(_lockObj);
}
Expand Down

0 comments on commit 38fe15f

Please sign in to comment.