Bug 32315

Summary: Inspecting a variable gives information from the incorrect symbol
Product: [Desktop] Xamarin Studio Reporter: John Miller [MSFT] <john.miller>
Component: DebuggerAssignee: David Karlaš <david.karlas>
Status: RESOLVED FIXED    
Severity: normal CC: marius.ungureanu, mono-bugs+monodevelop, mono-bugs+mono, yshteinman
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Description John Miller [MSFT] 2015-07-22 12:24:58 UTC
**Overview:**

   While inspecting a variable with the debugger when stepping through code, a variable shows as `null` when it is not. 

**Steps to Reproduce:**

   1. Run the attached sample on iOS simulator. 
   2. Press the Continue button (no credentials needed)
   3. Press the Go Back button (breakpoint is hit)
   4. Inspect the "page" variable near the // FIXME: comment

**Actual Results:**

   "page" will show as null when it is not. 

**Expected Results:**

   "page" should have a value while being inspected.

**Build Date & Platform:**

   === Xamarin Studio ===

Version 5.9.4 (build 5)
Installation UUID: e01c3049-a2d2-4e0a-aad8-afe6fb627c4d
Runtime:
	Mono 4.0.2 ((detached/c99aa0c)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400020005

=== Apple Developer Tools ===

Xcode 6.3 (7569)
Build 6D570

=== Xamarin.iOS ===

Version: 8.10.3.2 (Business Edition)
Hash: 8b265d6
Branch: master
Build date: 2015-06-30 15:19:13-0400

=== Xamarin.Android ===

Version: 5.1.4.16 (Business Edition)
Android SDK: /Users/johnmiller/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.4   (API level 19)
		5.0   (API level 21)
Java SDK: /usr
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.0.2.35 (Business Edition)

=== Build Information ===

Release ID: 509040005
Git revision: 8010a90f6e246b32364e3fb46ef2c9d1be9c9a2b
Build date: 2015-06-08 16:52:06-04
Xamarin addins: 7e93e9c3503f28770f23ce1b7eafd829919f18e8

=== Operating System ===

Mac OS X 10.10.2

**Additional Information:**

   Marius investigated this and mentioned the following: "One of the elements in play here (either the runtime or the XS debugger) is giving information on member 'page' from the wrong symbol point, since multiple ones are defined in the same function, so data for which one comes in is undefined (or just the first one always)."

Workaround: Rename the "page" variable to something unique that does not exist somewhere in this method e.g. "otherPage".
Comment 3 David Karlaš 2015-07-22 12:29:38 UTC
From my investigation into this it appears that problem is that objects that are lifting variables are not present in .mdb so scopes of variables can't be determined, hence picking wrong "page" variable.

Moving to mcs bugs.
Comment 4 Marius Ungureanu 2015-07-22 14:01:22 UTC
Related: https://github.com/mono/mono/pull/1581
Comment 5 Marek Safar 2016-01-29 19:26:04 UTC
Implemented compiler part of the fix in master.

mcs now similarly to csc and roslyn emits lifted local variables for state machine (async + iterators) using naming convention which includes scope index.

Consider

using System;
using System.Threading.Tasks;

namespace Console65
{
  class MainClass
  {
    public static void Main()
    {
      new MainClass().Test(0).Wait();
    }

    async Task Test (int arg)
    {
      if (arg > 0)
      {
        var x = 1;
        await Task.Yield();

      } else
      {
        var x = 2;
        await Task.Yield();

      }
    }
  }
}


This emits fields as

    .field assembly int32 '<x>__1'
    .field assembly int32 '<x>__2'

where format is <original-name>__scope-index, scope-index is 1 based to match scope index logic for local variables.

.mdb file then contains section like

      <scopes>
        <entry index="0" start="0x44" end="0x98" />
        <entry index="1" start="0x9e" end="0xf2" />
        <entry index="2" start="0x32" end="0x37" />
        <entry index="3" start="0x33" end="0x36" />
        <entry index="4" start="0x34" end="0x35" />
      </scopes>

which can be used to match scope-index to actual IL range.

Portable pdb does the same thing so I tried to implement it in similar way.
Comment 6 xamarin-release-manager 2016-09-12 08:58:02 UTC
Fixed in version 6.2.0.482 (master)

Author: David Karlas??
Commit: f6732039675317f6b3629f2a778b300620753594 (mono/debugger-libs)
Included in Commit: bf92ff6e50a1c1aed237ac5651b44887abd22fbc (mono/monodevelop)
Comment 7 David Karlaš 2016-09-12 09:00:09 UTC
Notice that for this to work, also runtime side needs to be new...
This means, fix will be available as part of Cycle 9 builds.(Mono needs to be 4.7.0.579 or newer)