Skip to content

Commit 8513a26

Browse files
committed
A lot of calls in CorObjectAdaptor were made more safe - wrapped with try-catch and logging.
(cherry picked from commit 80432e5)
1 parent 17500a5 commit 8513a26

File tree

3 files changed

+75
-63
lines changed

3 files changed

+75
-63
lines changed

Mono.Debugging.Win32/CorDebugUtil.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Mono.Debugging.Client;
4+
5+
namespace Mono.Debugging.Win32
6+
{
7+
public static class CorDebugUtil
8+
{
9+
public static T CallHandlingComExceptions<T> (Func<T> factory, string callName, T defaultValue = default(T))
10+
{
11+
try {
12+
return factory ();
13+
} catch (COMException e) {
14+
DebuggerLoggingService.LogMessage ("Exception in {0}: {1}", callName, e.Message);
15+
return defaultValue;
16+
}
17+
}
18+
}
19+
}

Mono.Debugging.Win32/CorObjectAdaptor.cs

Lines changed: 55 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,16 +1364,13 @@ internal static bool IsGeneratedType (Type tm)
13641364

13651365
ValueReference GetHoistedThisReference (CorEvaluationContext cx)
13661366
{
1367-
try {
1367+
return CorDebugUtil.CallHandlingComExceptions (() => {
13681368
CorValRef vref = new CorValRef (delegate {
13691369
return cx.Frame.GetArgument (0);
13701370
});
13711371
var type = (CorType) GetValueType (cx, vref);
13721372
return GetHoistedThisReference (cx, type, vref);
1373-
} catch (COMException e) {
1374-
DebuggerLoggingService.LogMessage ("Exception in GetHoistedThisReference(): {0}", e.Message);
1375-
}
1376-
return null;
1373+
}, "GetHoistedThisReference()");
13771374
}
13781375

13791376
ValueReference GetHoistedThisReference (CorEvaluationContext cx, CorType type, object val)
@@ -1451,7 +1448,7 @@ ValueReference GetThisReference (CorEvaluationContext ctx)
14511448
if (mi == null || mi.IsStatic)
14521449
return null;
14531450

1454-
try {
1451+
return CorDebugUtil.CallHandlingComExceptions (() => {
14551452
CorValRef vref = new CorValRef (delegate {
14561453
var result = ctx.Frame.GetArgument (0);
14571454
if (result.Type == CorElementType.ELEMENT_TYPE_BYREF)
@@ -1460,12 +1457,18 @@ ValueReference GetThisReference (CorEvaluationContext ctx)
14601457
});
14611458

14621459
return new VariableReference (ctx, vref, "this", ObjectValueFlags.Variable | ObjectValueFlags.ReadOnly);
1463-
} catch (COMException e) {
1464-
DebuggerLoggingService.LogMessage("Exception in GetThisReference(): {0}", e.Message);
1465-
return null;
1466-
}
1460+
}, "GetThisReference()");
1461+
}
1462+
1463+
static VariableReference CreateParameterReference (CorEvaluationContext ctx, int paramIndex, string paramName, ObjectValueFlags flags = ObjectValueFlags.Parameter)
1464+
{
1465+
CorValRef vref = new CorValRef (delegate {
1466+
return ctx.Frame.GetArgument (paramIndex);
1467+
});
1468+
return new VariableReference (ctx, vref, paramName, flags);
14671469
}
14681470

1471+
14691472
protected override IEnumerable<ValueReference> OnGetParameters (EvaluationContext gctx)
14701473
{
14711474
CorEvaluationContext ctx = (CorEvaluationContext) gctx;
@@ -1476,28 +1479,19 @@ protected override IEnumerable<ValueReference> OnGetParameters (EvaluationContex
14761479
int pos = pi.Position;
14771480
if (met.IsStatic)
14781481
pos--;
1479-
CorValRef vref = null;
1480-
try {
1481-
vref = new CorValRef (delegate {
1482-
return ctx.Frame.GetArgument (pos);
1483-
});
1484-
}
1485-
catch (Exception /*ex*/) {
1486-
}
1487-
if (vref != null)
1488-
yield return new VariableReference (ctx, vref, pi.Name, ObjectValueFlags.Parameter);
1482+
1483+
yield return CorDebugUtil.CallHandlingComExceptions (() => CreateParameterReference (ctx, pos, pi.Name),
1484+
string.Format ("Get parameter {0} of {1}", pi.Name, met.Name));
14891485
}
14901486
yield break;
14911487
}
14921488
}
14931489

1494-
int count = ctx.Frame.GetArgumentCount ();
1490+
int count = CorDebugUtil.CallHandlingComExceptions (() => ctx.Frame.GetArgumentCount (), "GetArgumentCount()", 0);
14951491
for (int n = 0; n < count; n++) {
14961492
int locn = n;
1497-
var vref = new CorValRef (delegate {
1498-
return ctx.Frame.GetArgument (locn);
1499-
});
1500-
yield return new VariableReference (ctx, vref, "arg_" + (n + 1), ObjectValueFlags.Parameter);
1493+
yield return CorDebugUtil.CallHandlingComExceptions (() => CreateParameterReference (ctx, locn, "arg_" + (locn + 1)),
1494+
string.Format ("Get parameter {0}", n));
15011495
}
15021496
}
15031497

@@ -1568,42 +1562,39 @@ IEnumerable<ValueReference> GetLocalVariables (CorEvaluationContext cx)
15681562
{
15691563
uint offset;
15701564
CorDebugMappingResult mr;
1571-
try {
1565+
return CorDebugUtil.CallHandlingComExceptions (() => {
15721566
cx.Frame.GetIP (out offset, out mr);
15731567
return GetLocals (cx, null, (int) offset, false);
1574-
} catch (Exception e) {
1575-
cx.WriteDebuggerError (e);
1576-
return null;
1577-
}
1568+
}, "GetLocalVariables()", new ValueReference[0]);
15781569
}
15791570

15801571
public override ValueReference GetCurrentException (EvaluationContext ctx)
15811572
{
15821573
CorEvaluationContext wctx = (CorEvaluationContext) ctx;
15831574
CorValue exception = wctx.Thread.CurrentException;
15841575

1585-
try {
1586-
if (exception != null)
1587-
{
1588-
CorHandleValue exceptionHandle = wctx.Session.GetHandle (exception);
1589-
1590-
CorValRef vref = new CorValRef (delegate {
1591-
return exceptionHandle;
1592-
});
1593-
1594-
return new VariableReference (ctx, vref, ctx.Options.CurrentExceptionTag, ObjectValueFlags.Variable);
1595-
}
1596-
return base.GetCurrentException(ctx);
1597-
} catch (Exception e) {
1598-
ctx.WriteDebuggerError (e);
1576+
if (exception == null)
15991577
return null;
1600-
}
1578+
return CorDebugUtil.CallHandlingComExceptions (() => {
1579+
CorValRef vref = new CorValRef (delegate {
1580+
return wctx.Session.GetHandle (exception);
1581+
});
1582+
return new VariableReference (ctx, vref, ctx.Options.CurrentExceptionTag, ObjectValueFlags.Variable);
1583+
}, "Get current exception");
1584+
}
1585+
1586+
static VariableReference CreateLocalVariableReference (CorEvaluationContext ctx, int varIndex, string varName, ObjectValueFlags flags = ObjectValueFlags.Variable)
1587+
{
1588+
CorValRef vref = new CorValRef (delegate {
1589+
return ctx.Frame.GetLocalVariable (varIndex);
1590+
});
1591+
return new VariableReference (ctx, vref, varName, flags);
16011592
}
16021593

16031594
IEnumerable<ValueReference> GetLocals (CorEvaluationContext ctx, ISymbolScope scope, int offset, bool showHidden)
16041595
{
1605-
if (ctx.Frame.FrameType != CorFrameType.ILFrame)
1606-
yield break;
1596+
if (ctx.Frame.FrameType != CorFrameType.ILFrame)
1597+
yield break;
16071598

16081599
if (scope == null) {
16091600
ISymbolMethod met = ctx.Frame.Function.GetSymbolMethod (ctx.Session);
@@ -1613,10 +1604,8 @@ IEnumerable<ValueReference> GetLocals (CorEvaluationContext ctx, ISymbolScope sc
16131604
int count = ctx.Frame.GetLocalVariablesCount ();
16141605
for (int n = 0; n < count; n++) {
16151606
int locn = n;
1616-
CorValRef vref = new CorValRef (delegate {
1617-
return ctx.Frame.GetLocalVariable (locn);
1618-
});
1619-
yield return new VariableReference (ctx, vref, "local_" + (n + 1), ObjectValueFlags.Variable);
1607+
yield return CorDebugUtil.CallHandlingComExceptions (() => CreateLocalVariableReference (ctx, locn, "local_" + (locn + 1)),
1608+
string.Format ("Get local variable {0}", locn));
16201609
}
16211610
yield break;
16221611
}
@@ -1626,20 +1615,23 @@ IEnumerable<ValueReference> GetLocals (CorEvaluationContext ctx, ISymbolScope sc
16261615
if (var.Name == "$site")
16271616
continue;
16281617
if (IsClosureReferenceLocal (var)) {
1629-
int addr = var.AddressField1;
1630-
var vref = new CorValRef (delegate {
1631-
return ctx.Frame.GetLocalVariable (addr);
1632-
});
1633-
1634-
foreach (var gv in GetHoistedLocalVariables (ctx, new VariableReference (ctx, vref, var.Name, ObjectValueFlags.Variable))) {
1635-
yield return gv;
1618+
var variableReference = CorDebugUtil.CallHandlingComExceptions (() => {
1619+
int addr = var.AddressField1;
1620+
return CreateLocalVariableReference (ctx, addr, var.Name);
1621+
}, string.Format ("Get local variable {0}", var.Name));
1622+
1623+
if (variableReference != null) {
1624+
foreach (var gv in GetHoistedLocalVariables (ctx, variableReference)) {
1625+
yield return gv;
1626+
}
16361627
}
16371628
} else if (!IsGeneratedTemporaryLocal (var) || showHidden) {
1638-
int addr = var.AddressField1;
1639-
var vref = new CorValRef (delegate {
1640-
return ctx.Frame.GetLocalVariable (addr);
1641-
});
1642-
yield return new VariableReference (ctx, vref, var.Name, ObjectValueFlags.Variable);
1629+
var variableReference = CorDebugUtil.CallHandlingComExceptions (() => {
1630+
int addr = var.AddressField1;
1631+
return CreateLocalVariableReference (ctx, addr, var.Name);
1632+
}, string.Format ("Get local variable {0}", var.Name));
1633+
if (variableReference != null)
1634+
yield return variableReference;
16431635
}
16441636
}
16451637

Mono.Debugging.Win32/Mono.Debugging.Win32.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<Compile Include="Properties\AssemblyInfo.cs" />
7070
<Compile Include="PropertyReference.cs" />
7171
<Compile Include="StringAdaptor.cs" />
72+
<Compile Include="CorDebugUtil.cs" />
7273
<Compile Include="VariableReference.cs" />
7374
<Compile Include="Win32.cs" />
7475
</ItemGroup>

0 commit comments

Comments
 (0)