private string GetLocalVariableName(ulong instructionPointer, uint localIndex) { ClrMethod method = _context.Runtime.GetMethodByAddress(instructionPointer); ClrModule module = method.Type.Module; string pdbLocation = module.TryDownloadPdb(null); IntPtr iunkMetadataImport = Marshal.GetIUnknownForObject(module.MetadataImport); ISymbolReader reader = null; ISymbolMethod symMethod = null; try { using (var binder = new SymBinder()) { reader = binder.GetReader( iunkMetadataImport, module.FileName, Path.GetDirectoryName(pdbLocation)); symMethod = reader.GetMethod(new SymbolToken((int)method.MetadataToken)); return(GetLocalVariableName(symMethod.RootScope, localIndex)); } } catch (COMException comEx) { // E_FAIL occasionally occurs in ISymbolReader.GetMethod. Nothing we can do about it. if ((uint)comEx.HResult == 0x80004005) { return(""); } // 0x806D0005 occurs when the PDB cannot be found or doesn't contain the necessary // information to create a symbol reader. It's OK to ignore. if ((uint)comEx.HResult == 0x806D0005) { return(""); } throw; } finally { // These interfaces aren't IDisposable, but the underlying objects are. And it is // important to dispose of them properly, because if their finalizer runs on exit, // it crashes with an access violation. if (reader != null) { ((IDisposable)reader).Dispose(); } if (symMethod != null) { ((IDisposable)symMethod).Dispose(); } Marshal.Release(iunkMetadataImport); } }