private void AddLocalVariablesToList(CorFrame frame, int ip, ArrayList listToAdd, ISymbolScope scope) { Debug.Assert(frame.FunctionToken == m_function.Token); foreach (ISymbolVariable isv in scope.GetLocals()) { Debug.Assert(isv.AddressKind == SymAddressKind.ILOffset); CorValue v = null; try { v = frame.GetLocalVariable(isv.AddressField1); } catch (System.Runtime.InteropServices.COMException e) { if (e.ErrorCode != (int)Microsoft.Samples.Debugging.CorDebug.HResult.CORDBG_E_IL_VAR_NOT_AVAILABLE) { throw; } } listToAdd.Add(new MDbgValue(m_module.Process, isv.Name, v)); } foreach (ISymbolScope s in scope.GetChildren()) { if (s.StartOffset <= ip && s.EndOffset >= ip) { AddLocalVariablesToList(frame, ip, listToAdd, s); } } }
public Scope(ISymbolScope scope, VariableDefinition[] locals, Dictionary <int, Instruction> insns) { m_start = insns[scope.StartOffset]; m_end = insns[scope.EndOffset]; foreach (var v in scope.GetLocals()) { if (v.AddressKind == SymAddressKind.ILOffset) { var local = locals[v.AddressField1]; if (m_variables == null) { m_variables = new List <VariableDebugInformation> (); } m_variables.Add(new VariableDebugInformation(local, v.Name)); } } foreach (var s in scope.GetChildren()) { if (m_scopes == null) { m_scopes = new List <Scope> (); } m_scopes.Add(new Scope(s, locals, insns)); } }
static void ReadScopeAndLocals(ISymbolScope scope, Cil.Scope parent, Cil.MethodBody body, IDictionary instructions) { Cil.Scope s = new Cil.Scope(); s.Start = GetInstruction(body, instructions, scope.StartOffset); s.End = GetInstruction(body, instructions, scope.EndOffset); if (parent != null) { parent.Scopes.Add(s); } else { body.Scopes.Add(s); } foreach (ISymbolVariable local in scope.GetLocals()) { Cil.VariableDefinition variable = body.Variables [local.AddressField1]; variable.Name = local.Name; s.Variables.Add(variable); } foreach (ISymbolScope child in scope.GetChildren()) { ReadScopeAndLocals(child, s, body, instructions); } }
private Scope ReadScope(ISymbolScope scope) { Scope scopeData = new Scope(); // If this is the root scope, then it was created implicitly and should not be explicitly // opened by a writer if (scope.Parent == null) { scopeData.isImplicit = true; } scopeData.startOffset = scope.StartOffset; scopeData.endOffset = scope.EndOffset; // Read the locals, constants and namespaces in this scope (may be empty) scopeData.locals = ReadLocals(scope); scopeData.constants = ReadConstants(scope); scopeData.usedNamespaces = ReadUsedNamespaces(scope); // Read the child scopes recursively scopeData.scopes = new List <Scope>(); foreach (ISymbolScope child in scope.GetChildren()) { Scope childData = ReadScope(child); scopeData.scopes.Add(childData); } return(scopeData); }
// Helper method to write the local variables in the given scope. // Scopes match an IL range, and also have child scopes. void WriteLocalsHelper(ISymbolScope scope) { foreach (ISymbolVariable l in scope.GetLocals()) { m_writer.WriteStartElement("local"); { m_writer.WriteAttributeString("name", l.Name); // Each local maps to a unique "IL Index" or "slot" number. // This index is what you pass to ICorDebugILFrame::GetLocalVariable() to get // a specific local variable. Debug.Assert(l.AddressKind == SymAddressKind.ILOffset); int slot = l.AddressField1; m_writer.WriteAttributeString("il_index", slot.ToString()); // Provide scope range m_writer.WriteAttributeString("il_start", Util.AsIlOffset(scope.StartOffset)); m_writer.WriteAttributeString("il_end", Util.AsIlOffset(scope.EndOffset)); } m_writer.WriteEndElement(); // local } foreach (ISymbolScope childScope in scope.GetChildren()) { WriteLocalsHelper(childScope); } }
IEnumerable <ValueReference> GetLocals(CorEvaluationContext ctx, ISymbolScope scope, int offset, bool showHidden) { if (ctx.Frame.FrameType != CorFrameType.ILFrame) { yield break; } if (scope == null) { ISymbolMethod met = ctx.Frame.Function.GetSymbolMethod(ctx.Session); if (met != null) { scope = met.RootScope; } else { int count = ctx.Frame.GetLocalVariablesCount(); for (int n = 0; n < count; n++) { int locn = n; CorValRef vref = new CorValRef(delegate { return(ctx.Frame.GetLocalVariable(locn)); }); yield return(new VariableReference(ctx, vref, "local_" + (n + 1), ObjectValueFlags.Variable)); } yield break; } } foreach (ISymbolVariable var in scope.GetLocals()) { if (var.Name == "$site") { continue; } if (var.Name.IndexOfAny(new char[] { '$', '<', '>' }) == -1 || showHidden) { int addr = var.AddressField1; CorValRef vref = new CorValRef(delegate { return(ctx.Frame.GetLocalVariable(addr)); }); yield return(new VariableReference(ctx, vref, var.Name, ObjectValueFlags.Variable)); } } foreach (ISymbolScope cs in scope.GetChildren()) { if (cs.StartOffset <= offset && cs.EndOffset >= offset) { foreach (VariableReference var in GetLocals(ctx, cs, offset, showHidden)) { yield return(var); } } } }
public static IEnumerable <ISymbolScope> GetInnerScopesRecursive(this ISymbolScope scope) { yield return(scope); foreach (var innerScope in scope.GetChildren() .SelectMany(innerScope => innerScope.GetInnerScopesRecursive())) { yield return(innerScope); } }
void VisitLocals(ISymbolScope iSymbolScope) { foreach (var s in iSymbolScope.GetLocals()) { _names [s.AddressField1] = s.Name; } foreach (var c in iSymbolScope.GetChildren()) { VisitLocals(c); } }
void VisitLocals(ISymbolScope iSymbolScope) { foreach (var s in iSymbolScope.GetLocals()) { Console.WriteLine(" Found Local Variable: " + s.Name); _names[s.AddressField1] = s.Name; } foreach (var c in iSymbolScope.GetChildren()) { VisitLocals(c); } }
private Variable[] enumerateLocals(ISymbolScope scope) { var variables = new List<Variable>(); foreach (var local in scope.GetLocals()) { if (isHidden(local)) continue; var value = _thread.ActiveFrame.GetLocalVariable(local.AddressField1); var something = getValue(value); variables.Add(new Variable(local.Name, something)); } foreach (var child in scope.GetChildren()) variables.AddRange(enumerateLocals(child)); return variables.ToArray(); }
static void ReadScopeAndLocals (ISymbolScope scope, Cil.Scope parent, Cil.MethodBody body, IDictionary instructions) { Cil.Scope s = new Cil.Scope (); s.Start = GetInstruction (body, instructions, scope.StartOffset); s.End = GetInstruction (body, instructions, scope.EndOffset); if (parent != null) parent.Scopes.Add (s); else body.Scopes.Add (s); foreach (ISymbolVariable local in scope.GetLocals ()) { Cil.VariableDefinition variable = body.Variables [local.AddressField1]; variable.Name = local.Name; s.Variables.Add (variable); } foreach (ISymbolScope child in scope.GetChildren ()) ReadScopeAndLocals (child, s, body, instructions); }
private string GetLocalVariableName(ISymbolScope scope, uint localIndex) { ISymbolVariable[] localVariables = null; try { localVariables = scope.GetLocals(); foreach (var localVar in localVariables) { if (localVar.AddressKind == SymAddressKind.ILOffset && localVar.AddressField1 == localIndex) { return(localVar.Name); } } foreach (var childScope in scope.GetChildren()) { string result = GetLocalVariableName(childScope, localIndex); if (result != null) { return(result); } } return(null); } finally { if (localVariables != null) { foreach (var localVar in localVariables) { ((IDisposable)localVar).Dispose(); } } ((IDisposable)scope).Dispose(); } }
private string GetLocalVariableName(ISymbolScope scope, uint localIndex) { ISymbolVariable[] localVariables = null; try { localVariables = scope.GetLocals(); foreach (var localVar in localVariables) { if (localVar.AddressKind == SymAddressKind.ILOffset && localVar.AddressField1 == localIndex) return localVar.Name; } foreach (var childScope in scope.GetChildren()) { string result = GetLocalVariableName(childScope, localIndex); if (result != null) return result; } return null; } finally { if (localVariables != null) { foreach (var localVar in localVariables) ((IDisposable)localVar).Dispose(); } ((IDisposable)scope).Dispose(); } }
private void AddLocalVariablesToList(CorFrame frame, int ip, ArrayList listToAdd, ISymbolScope scope) { Debug.Assert(frame.FunctionToken == m_function.Token); foreach (ISymbolVariable isv in scope.GetLocals()) { Debug.Assert(isv.AddressKind == SymAddressKind.ILOffset); CorValue v = null; try { v = frame.GetLocalVariable(isv.AddressField1); } catch (System.Runtime.InteropServices.COMException e) { if (e.ErrorCode != (int)Microsoft.Samples.Debugging.CorDebug.HResult.CORDBG_E_IL_VAR_NOT_AVAILABLE) throw; } listToAdd.Add(new MDbgValue(m_module.Process, isv.Name, v)); } foreach (ISymbolScope s in scope.GetChildren()) { if (s.StartOffset <= ip && s.EndOffset >= ip) AddLocalVariablesToList(frame, ip, listToAdd, s); } }
private Scope ReadScope(ISymbolScope scope) { Scope scopeData = new Scope(); // If this is the root scope, then it was created implicitly and should not be explicitly // opened by a writer if (scope.Parent == null) { scopeData.isImplicit = true; } scopeData.startOffset = scope.StartOffset; scopeData.endOffset = scope.EndOffset; // Read the locals, constants and namespaces in this scope (may be empty) scopeData.locals = ReadLocals(scope); scopeData.constants = ReadConstants(scope); scopeData.usedNamespaces = ReadUsedNamespaces(scope); // Read the child scopes recursively scopeData.scopes = new List<Scope>(); foreach (ISymbolScope child in scope.GetChildren()) { Scope childData = ReadScope(child); scopeData.scopes.Add(childData); } return scopeData; }
private void WriteLocalsHelper(ISymbolScope scope, Dictionary<int, ImmutableArray<string>> slotNames, bool includeChildScopes) { foreach (ISymbolVariable l in scope.GetLocals()) { writer.WriteStartElement("local"); { writer.WriteAttributeString("name", l.Name); // Each local maps to a "IL Index" or "slot" number. // The index is not necessarily unique. Several locals may refer to the same slot. // It just means that the same local is known under different names inside the same or different scopes. // This index is what you pass to ICorDebugILFrame::GetLocalVariable() to get // a specific local variable. // NOTE: VB emits "fake" locals for resumable locals which are actually backed by fields. // These locals always map to the slot #0 which is just a valid number that is // not used. Only scoping information is used by EE in this case. Debug.Assert(l.AddressKind == SymAddressKind.ILOffset); int slot = l.AddressField1; writer.WriteAttributeString("il_index", CultureInvariantToString(slot)); bool reusingSlot = false; // collect slot names so that we can verify ISymUnmanagedReader APIs if (slotNames != null) { ImmutableArray<string> existingNames; if (slotNames.TryGetValue(slot, out existingNames)) { slotNames[slot] = existingNames.Add(l.Name); reusingSlot = true; } else { slotNames.Add(slot, ImmutableArray.Create(l.Name)); } } // Provide scope range writer.WriteAttributeString("il_start", AsILOffset(scope.StartOffset)); writer.WriteAttributeString("il_end", AsILOffset(scope.EndOffset)); writer.WriteAttributeString("attributes", l.Attributes.ToString()); if (reusingSlot) { writer.WriteAttributeString("reusingslot", reusingSlot.ToString(CultureInfo.InvariantCulture)); } } writer.WriteEndElement(); // </local> } foreach (ISymbolConstant c in ((ISymbolScope2)scope).GetConstants()) { // Note: We can retrieve constant tokens by saving it into signature blob // in our implementation of IMetadataImport.GetSigFromToken. writer.WriteStartElement("constant"); { writer.WriteAttributeString("name", c.GetName()); object value = c.GetValue(); string typeName = value.GetType().Name; // certain Unicode characters will give Xml writers fits...in order to avoid this, we'll replace // problematic characters/sequences with their hexadecimal equivalents, like U+0000, etc... var chars = value as string; if (chars != null) { PooledStringBuilder pooled = PooledStringBuilder.GetInstance(); var valueWithPlaceholders = pooled.Builder; foreach (var ch in chars) { // if we end up with more, we can add them here if (0 == (int)ch) { valueWithPlaceholders.AppendFormat("U+{0:X4}", (int)ch); } else { valueWithPlaceholders.Append(ch); } } if (valueWithPlaceholders.Length > chars.Length) { value = valueWithPlaceholders.ToString(); } pooled.Free(); } writer.WriteAttributeString("value", value.ToString()); writer.WriteAttributeString("type", typeName); } writer.WriteEndElement(); // </constant> } if (includeChildScopes) { foreach (ISymbolScope childScope in scope.GetChildren()) { WriteLocalsHelper(childScope, slotNames, includeChildScopes); } } }