Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
[Debugger] Implemented pointer support.
Browse files Browse the repository at this point in the history
Fixes bug #4697.
  • Loading branch information
jstedfast committed Apr 28, 2012
1 parent c47c74e commit 8b6890d
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public override bool IsPrimitive (EvaluationContext ctx, object val)
return (v is CorGenericValue) || (v is CorStringValue);
}

public override bool IsPointer (EvaluationContext ctx, object val)
{
// FIXME: implement this correctly.
return false;
}

public override bool IsEnum (EvaluationContext ctx, object val)
{
CorType type = (CorType) GetValueType (ctx, val);
Expand Down
1 change: 1 addition & 0 deletions main/contrib/Mono.Debugger.Soft/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ FILES = \
Mono.Debugger.Soft/ObjectCollectedException.cs \
Mono.Debugger.Soft/ObjectMirror.cs \
Mono.Debugger.Soft/ParameterInfoMirror.cs \
Mono.Debugger.Soft/PointerValue.cs \
Mono.Debugger.Soft/PrimitiveValue.cs \
Mono.Debugger.Soft/PropertyInfoMirror.cs \
Mono.Debugger.Soft/StackFrame.cs \
Expand Down
5 changes: 3 additions & 2 deletions main/contrib/Mono.Debugger.Soft/Mono.Debugger.Soft.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -106,6 +106,7 @@
<Compile Include="Mono.Debugger.Soft\VMStartEvent.cs" />
<Compile Include="Mono.Debugger.Soft\AssemblyLoadEventRequest.cs" />
<Compile Include="Locale.cs" />
<Compile Include="Mono.Debugger.Soft\PointerValue.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Makefile.am" />
Expand Down Expand Up @@ -136,4 +137,4 @@
<Name>Mono.Cecil</Name>
</ProjectReference>
</ItemGroup>
</Project>
</Project>
63 changes: 63 additions & 0 deletions main/contrib/Mono.Debugger.Soft/Mono.Debugger.Soft/PointerValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// PointerValue.cs
//
// Author: Jeffrey Stedfast <jeff@xamarin.com>
//
// Copyright (c) 2012 Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;

namespace Mono.Debugger.Soft
{
/*
* Represents a value of a pointer type in the debuggee
*/
public class PointerValue : Value {
long addr;

public PointerValue (VirtualMachine vm, long addr) : base (vm, 0) {
this.addr = addr;
}

public long Address {
get { return addr; }
}

public object Value {
get { return addr; }
}

public override bool Equals (object obj) {
if (obj != null && obj is PointerValue)
return addr == (obj as PointerValue).addr;
return base.Equals (obj);
}

public override int GetHashCode () {
return base.GetHashCode ();
}

public override string ToString () {
return string.Format ("PointerValue<0x{0:x}>", addr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public override string CallToString (EvaluationContext ctx, object obj)
}
else if (obj is PrimitiveValue)
return ((PrimitiveValue)obj).Value.ToString ();
else if (obj is PointerValue)
return string.Format ("0x{0:x}", ((PointerValue)obj).Address);
else if ((obj is StructMirror) && ((StructMirror)obj).Type.IsPrimitive) {
// Boxed primitive
StructMirror sm = (StructMirror) obj;
Expand Down Expand Up @@ -841,12 +843,17 @@ public override bool IsClass (object type)

public override bool IsNull (EvaluationContext ctx, object val)
{
return val == null || ((val is PrimitiveValue) && ((PrimitiveValue)val).Value == null);
return val == null || ((val is PrimitiveValue) && ((PrimitiveValue)val).Value == null) || ((val is PointerValue) && ((PointerValue)val).Address == 0);
}

public override bool IsPrimitive (EvaluationContext ctx, object val)
{
return val is PrimitiveValue || val is StringMirror || ((val is StructMirror) && ((StructMirror)val).Type.IsPrimitive);
return val is PrimitiveValue || val is StringMirror || ((val is StructMirror) && ((StructMirror)val).Type.IsPrimitive) || val is PointerValue;
}

public override bool IsPointer (EvaluationContext ctx, object val)
{
return val is PointerValue;
}

public override bool IsEnum (EvaluationContext ctx, object val)
Expand Down Expand Up @@ -1184,9 +1191,11 @@ public override object TargetObjectToObject (EvaluationContext gctx, object obj)
}

return str;
} else if (obj is PrimitiveValue)
} else if (obj is PrimitiveValue) {
return ((PrimitiveValue)obj).Value;
else if ((obj is StructMirror) && ((StructMirror)obj).Type.IsPrimitive) {
} else if (obj is PointerValue) {
return new IntPtr (((PointerValue)obj).Address);
} else if ((obj is StructMirror) && ((StructMirror)obj).Type.IsPrimitive) {
// Boxed primitive
StructMirror sm = (StructMirror) obj;
if (sm.Type.FullName == "System.IntPtr")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ public override object Value {
get {
SoftEvaluationContext ctx = (SoftEvaluationContext) Context;
try {
return ctx.Frame.GetValue (variable);
var value = ctx.Frame.GetValue (variable);

if (variable.Type.IsPointer) {
long addr = (long) ((PrimitiveValue) value).Value;
value = new PointerValue (value.VirtualMachine, addr);
}

return value;
} catch (AbsentInformationException) {
throw new EvaluatorException ("Value not available");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public ObjectValue[] GetChildren (ObjectPath path, int firstItemIndex, int count
else {
curIndex [curIndex.Length - 1] = index;
object elem = array.GetElement (curIndex);
val = cctx.Adapter.CreateObjectValue (cctx, this, newPath.Append (sidx), elem, ObjectValueFlags.ArrayElement);
val = cctx.Adapter.CreateObjectValue (cctx, this, newPath.Append (sidx), null, elem, ObjectValueFlags.ArrayElement);
if (elem != null && !cctx.Adapter.IsNull (cctx, elem)) {
TypeDisplayData tdata = cctx.Adapter.GetTypeDisplayData (cctx, cctx.Adapter.GetValueType (cctx, elem));
if (!string.IsNullOrEmpty (tdata.NameDisplayString))
Expand Down Expand Up @@ -296,7 +296,7 @@ public ObjectValue GetValue (ObjectPath path, EvaluationOptions options)
int[] idx = StringToIndices (path [1]);
object elem = array.GetElement (idx);
EvaluationContext cctx = ctx.WithOptions (options);
ObjectValue val = cctx.Adapter.CreateObjectValue (cctx, this, path, elem, ObjectValueFlags.ArrayElement);
ObjectValue val = cctx.Adapter.CreateObjectValue (cctx, this, path, null, elem, ObjectValueFlags.ArrayElement);
if (elem != null && !cctx.Adapter.IsNull (cctx, elem)) {
TypeDisplayData tdata = cctx.Adapter.GetTypeDisplayData (cctx, cctx.Adapter.GetValueType (cctx, elem));
if (!string.IsNullOrEmpty (tdata.NameDisplayString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public void Dispose ()
asyncOperationManager.Dispose ();
}

public ObjectValue CreateObjectValue (EvaluationContext ctx, IObjectValueSource source, ObjectPath path, object obj, ObjectValueFlags flags)
public ObjectValue CreateObjectValue (EvaluationContext ctx, IObjectValueSource source, ObjectPath path, object type, object obj, ObjectValueFlags flags)
{
try {
return CreateObjectValueImpl (ctx, source, path, obj, flags);
return CreateObjectValueImpl (ctx, source, path, type, obj, flags);
} catch (Exception ex) {
ctx.WriteDebuggerError (ex);
return ObjectValue.CreateFatalError (path.LastName, ex.Message, flags);
Expand Down Expand Up @@ -210,13 +210,23 @@ int FindTypeEnd (string s, int i, int endIndex)
return i;
}

public virtual string GetShortTypeName (string tname)
public virtual string GetShortTypeName (string typeName)
{
string res;
if (netToCSharpTypes.TryGetValue (tname, out res))
return res;
else
return tname;
int star = typeName.IndexOf ('*');

This comment has been minimized.

Copy link
@slluis

slluis Apr 30, 2012

Member

This is not right. The pointer case has to be checked in GetDisplayTypeName.

string name, ptr, csharp;

if (star != -1) {
name = typeName.Substring (0, star);
ptr = typeName.Substring (star);
} else {
ptr = string.Empty;
name = typeName;
}

if (netToCSharpTypes.TryGetValue (name, out csharp))
return csharp + ptr;

return typeName;
}

public virtual void OnBusyStateChanged (BusyStateEventArgs e)
Expand All @@ -231,6 +241,7 @@ public virtual void OnBusyStateChanged (BusyStateEventArgs e)

public abstract bool IsNull (EvaluationContext ctx, object val);
public abstract bool IsPrimitive (EvaluationContext ctx, object val);
public abstract bool IsPointer (EvaluationContext ctx, object val);
public abstract bool IsString (EvaluationContext ctx, object val);
public abstract bool IsArray (EvaluationContext ctx, object val);
public abstract bool IsEnum (EvaluationContext ctx, object val);
Expand Down Expand Up @@ -324,10 +335,20 @@ public virtual void GetNamespaceContents (EvaluationContext ctx, string namspace
childTypes = childNamespaces = new string[0];
}

protected virtual ObjectValue CreateObjectValueImpl (EvaluationContext ctx, Mono.Debugging.Backend.IObjectValueSource source, ObjectPath path, object obj, ObjectValueFlags flags)
protected virtual ObjectValue CreateObjectValueImpl (EvaluationContext ctx, Mono.Debugging.Backend.IObjectValueSource source, ObjectPath path, object type, object obj, ObjectValueFlags flags)
{
string typeName = obj != null ? GetValueTypeName (ctx, obj) : "";

string typeName;

if (obj != null && type != null) {
typeName = IsPointer (ctx, obj) ? GetTypeName (ctx, type) : GetValueTypeName (ctx, obj);
} else if (obj != null) {
typeName = GetValueTypeName (ctx, obj);
} else if (type != null) {
typeName = GetTypeName (ctx, type);
} else {
typeName = "";
}

if (obj == null || IsNull (ctx, obj)) {
return ObjectValue.CreateNullObject (source, path, GetDisplayTypeName (typeName), flags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,23 @@ protected virtual ObjectValue OnCreateObjectValue (EvaluationOptions options)

EvaluationContext newCtx = GetContext (options);
EvaluationContext oldCtx = Context;
object type = null;
object val = null;

try {
// Note: The Value property implementation may make use of the EvaluationOptions,
// so we need to override our context temporarily to do the evaluation.
ctx = newCtx;
type = Type;
val = Value;
} finally {
ctx = oldCtx;
}

if (val != null)
return newCtx.Adapter.CreateObjectValue (newCtx, this, new ObjectPath (name), val, Flags);
return newCtx.Adapter.CreateObjectValue (newCtx, this, new ObjectPath (name), type, val, Flags);
else
return Mono.Debugging.Client.ObjectValue.CreateNullObject (this, name, newCtx.Adapter.GetTypeName (newCtx, Type), Flags);
return Mono.Debugging.Client.ObjectValue.CreateNullObject (this, name, newCtx.Adapter.GetTypeName (newCtx, type), Flags);
}

ObjectValue IObjectValueSource.GetValue (ObjectPath path, EvaluationOptions options)
Expand Down

3 comments on commit 8b6890d

@mhutch
Copy link
Member

@mhutch mhutch commented on 8b6890d Apr 28, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to add the type parameter to CreateObjectValueImpl now that you have PointerValue? Couldn't the PointerValue include its type, and GetValueType could access it?

@slluis
Copy link
Member

@slluis slluis commented on 8b6890d Apr 30, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Michael

@jstedfast
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented.

Please sign in to comment.