[sdb] Add an API to make the contents of the 'State Machine Hoisted Local Scopes...
authorZoltan Varga <vargaz@gmail.com>
Fri, 2 Jun 2017 17:43:56 +0000 (13:43 -0400)
committerGitHub <noreply@github.com>
Fri, 2 Jun 2017 17:43:56 +0000 (13:43 -0400)
mcs/class/Mono.Debugger.Soft/Mono.Debugger.Soft/Connection.cs
mcs/class/Mono.Debugger.Soft/Mono.Debugger.Soft/MethodMirror.cs
mcs/class/Mono.Debugger.Soft/Test/dtest.cs
mono/metadata/debug-internals.h
mono/metadata/debug-mono-ppdb.c
mono/metadata/metadata-internals.h
mono/metadata/metadata.c
mono/metadata/row-indexes.h
mono/mini/debugger-agent.c

index af3e1716ca42f92e05e6976a0d5dffb9ea8dc5fa..6172b440f4944d6fcf4d0c6b55ee3c1221bb89aa 100644 (file)
@@ -119,6 +119,9 @@ namespace Mono.Debugger.Soft
                public int[] live_range_end;
                public int[] scopes_start;
                public int[] scopes_end;
+               public int nhoisted;
+               public int[] hoisted_scopes_start;
+               public int[] hoisted_scopes_end;
        }
 
        struct PropInfo {
@@ -420,7 +423,7 @@ namespace Mono.Debugger.Soft
                 * with newer runtimes, and vice versa.
                 */
                internal const int MAJOR_VERSION = 2;
-               internal const int MINOR_VERSION = 45;
+               internal const int MINOR_VERSION = 46;
 
                enum WPSuspendPolicy {
                        NONE = 0,
@@ -1927,6 +1930,15 @@ namespace Mono.Debugger.Soft
                                        info.scopes_end [i] = info.scopes_start [i] + res.ReadInt ();
                                        last_start = info.scopes_start [i];
                                }
+                               if (Version.AtLeast (2, 46)) {
+                                       info.nhoisted = res.ReadInt ();
+                                       info.hoisted_scopes_start = new int [info.nhoisted];
+                                       info.hoisted_scopes_end = new int [info.nhoisted];
+                                       for (int i = 0; i < info.nhoisted; ++i) {
+                                               info.hoisted_scopes_start [i] = res.ReadInt ();
+                                               info.hoisted_scopes_end [i] = res.ReadInt ();
+                                       }
+                               }
                        }
 
                        int nlocals = res.ReadInt ();
index fb10f7117c30258350bad2e93053dac7cbb14cbe..65e3abc54dc5f1d614d4d3d157af55b11fb3cc51 100644 (file)
@@ -24,6 +24,7 @@ namespace Mono.Debugger.Soft
                MethodBodyMirror body;
                MethodMirror gmd;
                TypeMirror[] type_args;
+               LocalScope[] hoisted_scopes;
 
                internal MethodMirror (VirtualMachine vm, long id) : base (vm, id) {
                }
@@ -246,6 +247,16 @@ namespace Mono.Debugger.Soft
                        return scopes;
                }
 
+               //
+               // Return the contents of the State Machine Hoisted Locals Scope record:
+               // https://github.com/dotnet/corefx/blob/master/src/System.Reflection.Metadata/specs/PortablePdb-Metadata.md#state-machine-hoisted-local-scopes-c--vb-compilers
+               //
+               public LocalScope [] GetHoistedScopes () {
+                       vm.CheckProtocolVersion (2, 46);
+                       GetLocals ();
+                       return hoisted_scopes;
+               }
+
                public LocalVariable[] GetLocals () {
                        if (locals == null) {
                                LocalsInfo li = new LocalsInfo ();
@@ -271,6 +282,10 @@ namespace Mono.Debugger.Soft
                                        for (int i = 0; i < scopes.Length; ++i)
                                                scopes [i] = new LocalScope (vm, this, li.scopes_start [i], li.scopes_end [i]);
                                }
+
+                               hoisted_scopes = new LocalScope [li.nhoisted];
+                               for (int i = 0; i < li.nhoisted; ++i)
+                                       hoisted_scopes [i] = new LocalScope (vm, this, li.hoisted_scopes_start [i], li.hoisted_scopes_end [i]);
                        }
                        return locals;
                }
index 631b5985db1ff7129d11b1443984be1b7c2fabcb..2586a315eb4561e0147fb5b6739e4563f670ad07 100644 (file)
@@ -1050,6 +1050,11 @@ public class DebuggerTests
 
                e = step_in_await ("ss_await", e);//ss_await_1_exc (true, false).Wait ();//out
                e = step_in_await ("MoveNext", e);//{
+
+               // Check hoisted scope information
+               var hoisted = (e as StepEvent).Method.GetHoistedScopes ();
+               Assert.AreEqual (2, hoisted.Length);
+
                e = step_out_await ("ss_await", e);//ss_await_1_exc (true, true).Wait ();//out
        }
 
index 5ac4f103adf48a874e10f09a3377cc0fbe05511e..98174b4b438617c071b70804b310f29b0b2220eb 100644 (file)
@@ -28,6 +28,10 @@ typedef struct {
        MonoDebugCodeBlock *block;
 } MonoDebugLocalVar;
 
+typedef struct {
+       int start_offset, end_offset;
+} MonoHoistedLocalScope;
+
 /*
  * Information about local variables retrieved from a symbol file.
  */
@@ -36,6 +40,8 @@ struct _MonoDebugLocalsInfo {
        MonoDebugLocalVar *locals;
        int num_blocks;
        MonoDebugCodeBlock *code_blocks;
+       int num_hoisted;
+       MonoHoistedLocalScope *hoisted;
 };
 
 /*
index 3ad5235610a05a02fff9f0ff3282548d6fe13715..9a5b01a6b2b21c54d87a83d2b11f63e0adae2a6b 100644 (file)
@@ -61,6 +61,9 @@ typedef struct {
        guint64 referenced_tables;
 } PdbStreamHeader;
 
+static const char*
+lookup_custom_debug_information (MonoImage* image, guint32 token, uint8_t parent_type, guint8* guid);
+
 static gboolean
 get_pe_debug_guid (MonoImage *image, guint8 *out_guid, gint32 *out_age, gint32 *out_timestamp)
 {
@@ -606,6 +609,27 @@ mono_ppdb_lookup_locals (MonoDebugMethodInfo *minfo)
                }
        }
 
+       /*
+        * Read scope information for hoisted variables, if it exists.
+        * Its stored in the CustomDebugInformation table.
+        */
+       guint8 hoisted_scopes_guid [16] = { 0x1E, 0xA6, 0xA9, 0x6D,  0xC7, 0xF8, 0x74, 0x48,   0xBE, 0x62, 0x68, 0xBC, 0x56, 0x30, 0xDF, 0x71 };
+       char const *blob = lookup_custom_debug_information (image, method->token, MONO_HASCUSTOMDEBUGINFO_METHODDEF, hoisted_scopes_guid);
+       if (blob) {
+               const char *ptr = blob;
+               int size = mono_metadata_decode_blob_size (ptr, &ptr);
+               g_assert (size % 4 == 0);
+               size /= 4;
+               res->num_hoisted = size;
+               res->hoisted = g_new0 (MonoHoistedLocalScope, res->num_hoisted);
+               for (int hindex = 0; hindex < size; ++hindex) {
+                       res->hoisted [hindex].start_offset = read16 (ptr);
+                       ptr += 4;
+                       res->hoisted [hindex].end_offset = res->hoisted [hindex].start_offset + read16 (ptr);
+                       ptr += 4;
+               }
+       }
+
        return res;
 }
 
index a6a936758e66e1794a69b848902a47c722075e89..2afcd3d17b45c18459be84ee17a9ec21ca55b3c8 100644 (file)
@@ -950,5 +950,8 @@ mono_loader_set_strict_strong_names (gboolean enabled);
 gboolean
 mono_loader_get_strict_strong_names (void);
 
+guint32
+mono_metadata_customdebuginfo_from_index (MonoImage *meta, guint32 index);
+
 #endif /* __MONO_METADATA_INTERNALS_H__ */
 
index dd1ea04a33edf386bbbe223a2b2c5ded843b5ce5..22e6a66592b85d5d0e0ce3c49496fa5a4a6dd1d2 100644 (file)
@@ -4743,6 +4743,38 @@ mono_metadata_localscope_from_methoddef (MonoImage *meta, guint32 index)
        return loc.result + 1;
 }
 
+/*
+ * mono_metadata_customdebuginfo_from_index:
+ * @meta: metadata context
+ * @index: hascustomdebuginfo coded index
+ * 
+ * Returns: the 1-based index into the CustomDebugInformation table of the first
+ * scope which belongs to the metadata object described by @index.
+ * Returns 0 if no such row is found.
+ */
+guint32
+mono_metadata_customdebuginfo_from_index (MonoImage *meta, guint32 index)
+{
+       MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMDEBUGINFORMATION];
+       locator_t loc;
+
+       if (!tdef->base)
+               return 0;
+
+       loc.idx = index;
+       loc.col_idx = MONO_CUSTOMDEBUGINFORMATION_PARENT;
+       loc.t = tdef;
+
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+               return 0;
+
+       /* Find the first entry by searching backwards */
+       while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOMDEBUGINFORMATION_PARENT) == index))
+               loc.result --;
+
+       return loc.result + 1;
+}
+
 #ifdef DEBUG
 static void
 mono_backtrace (int limit)
index 992dbdeea99f65b44a9b43ae68ba43284b63b244..f5c71abd201667602fca8b46a85853869df32c82 100644 (file)
@@ -486,6 +486,12 @@ enum {
        MONO_TYPEORMETHOD_MASK = 1
 };
 
+enum {
+       MONO_HASCUSTOMDEBUGINFO_METHODDEF = 0,
+       MONO_HASCUSTOMDEBUGINFO_BITS = 5,
+       MONO_HASCUSTOMDEBUGINFO_MASK = 0x1f
+};
+
 #endif /* __MONO_METADATA_ROW_INDEXES_H__ */
 
 
index bfdddb37547e5dac5d9f4f094b1084d87b359826..1a4940a82cc1fabd67952e792b27ff8bbf1f7ee8 100644 (file)
@@ -275,7 +275,7 @@ typedef struct {
 #define HEADER_LENGTH 11
 
 #define MAJOR_VERSION 2
-#define MINOR_VERSION 45
+#define MINOR_VERSION 46
 
 typedef enum {
        CMD_SET_VM = 1,
@@ -9057,6 +9057,14 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                                        buffer_add_int (buf, locals->code_blocks [i].end_offset - locals->code_blocks [i].start_offset);
                                        last_start = locals->code_blocks [i].start_offset;
                                }
+                               if (CHECK_PROTOCOL_VERSION (2, 46)) {
+                                       /* Scopes for hoisted locals */
+                                       buffer_add_int (buf, locals->num_hoisted);
+                                       for (i = 0; i < locals->num_hoisted; ++i) {
+                                               buffer_add_int (buf, locals->code_blocks [i].start_offset);
+                                               buffer_add_int (buf, locals->code_blocks [i].end_offset);
+                                       }
+                               }
                        }
 
                        num_locals = locals->num_locals;