WriteErrorLine() public method

public WriteErrorLine ( string format ) : void
format string
return void
示例#1
0
        public void Execute(CommandExecutionContext context)
        {
            if (!InMemoryOnly)
            {
                if (context.TargetType == TargetType.LiveProcess)
                {
                    context.WriteErrorLine(
                        "Generating a persistent heap index for a live process is not recommended. " +
                        "Use the --nofile switch to generate an in-memory index instead.");
                    return;
                }
                if (String.IsNullOrEmpty(HeapIndexFileName))
                {
                    HeapIndexFileName = Path.ChangeExtension(context.DumpFile, ".heapindex");
                    context.WriteLine("Using an automatically generated heap index filename: '{0}'", HeapIndexFileName);
                }
            }

            if (InMemoryOnly && !String.IsNullOrEmpty(HeapIndexFileName))
            {
                context.WriteErrorLine("The --nofile and -f options are incompatible.");
                return;
            }

            // If the index fails to build, clear it so that other commands know we don't have an index.
            context.HeapIndex = new HeapIndex(context);
            if (!context.HeapIndex.Build(ChunkSize, InMemoryOnly ? null : HeapIndexFileName, !EnumerateRootsFast))
                context.HeapIndex = null;
        }
示例#2
0
        public void Execute(CommandExecutionContext context)
        {
            if (!InMemoryOnly)
            {
                if (context.TargetType == TargetType.LiveProcess)
                {
                    context.WriteErrorLine(
                        "Generating a persistent heap index for a live process is not recommended. " +
                        "Use the --nofile switch to generate an in-memory index instead.");
                    return;
                }
                if (String.IsNullOrEmpty(HeapIndexFileName))
                {
                    HeapIndexFileName = Path.ChangeExtension(context.DumpFile, ".heapindex");
                    context.WriteLine("Using an automatically generated heap index filename: '{0}'", HeapIndexFileName);
                }
            }

            if (InMemoryOnly && !String.IsNullOrEmpty(HeapIndexFileName))
            {
                context.WriteErrorLine("The --nofile and -f options are incompatible.");
                return;
            }

            // If the index fails to build, clear it so that other commands know we don't have an index.
            context.HeapIndex = new HeapIndex(context);
            if (!context.HeapIndex.Build(ChunkSize, InMemoryOnly ? null : HeapIndexFileName, !EnumerateRootsFast))
            {
                context.HeapIndex = null;
            }
        }
示例#3
0
        public void Execute(CommandExecutionContext context)
        {
            if (ExceptionAddress != 0)
            {
                var heap = context.Runtime.GetHeap();
                var type = heap.GetObjectType(ExceptionAddress);
                if (type == null || !type.IsException)
                {
                    context.WriteErrorLine("The specified address is not the address of an Exception-derived object.");
                    return;
                }

                DisplayException(heap.GetExceptionObject(ExceptionAddress), context);
            }
            else
            {
                var thread = context.CurrentThread;
                if (thread == null)
                {
                    context.WriteErrorLine("There is no current managed thread");
                    return;
                }
                if (thread.CurrentException == null)
                {
                    context.WriteLine("There is no current managed exception on this thread");
                    return;
                }
                DisplayException(thread.CurrentException, context);
            }
        }
示例#4
0
        public void Execute(CommandExecutionContext context)
        {
            if (ExceptionAddress != 0)
            {
                var heap = context.Runtime.GetHeap();
                var type = heap.GetObjectType(ExceptionAddress);
                if (type == null || !type.IsException)
                {
                    context.WriteErrorLine("The specified address is not the address of an Exception-derived object.");
                    return;
                }

                DisplayException(heap.GetExceptionObject(ExceptionAddress), context);
            }
            else
            {
                var thread = context.CurrentThread;
                if (thread == null)
                {
                    context.WriteErrorLine("There is no current managed thread");
                    return;
                }
                if (thread.CurrentException == null)
                {
                    context.WriteLine("There is no current managed exception on this thread");
                    return;
                }
                DisplayException(thread.CurrentException, context);
            }
        }
示例#5
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;
            _output  = new PlainTextOutput();

            if (!String.IsNullOrEmpty(AssemblyName))
            {
                ClrModule module = context.Runtime.Modules.SingleOrDefault(
                    m => Path.GetFileNameWithoutExtension(m.FileName).Equals(
                        Path.GetFileNameWithoutExtension(AssemblyName),
                        StringComparison.InvariantCultureIgnoreCase
                        )
                    );
                if (module == null)
                {
                    context.WriteErrorLine("Could not find the assembly '{0}'.", AssemblyName);
                    return;
                }
                if (!String.IsNullOrEmpty(TypeName))
                {
                    DecompileTypeFromModule(TypeName, module.FileName);
                }
                else
                {
                    DecompileModule(module);
                }
            }
            else if (!String.IsNullOrEmpty(TypeName))
            {
                ClrType type = context.Heap.GetTypeByName(TypeName);
                if (type == null)
                {
                    context.WriteErrorLine(
                        "Could not find the type '{0}' on the heap. Try specifying the assembly name.",
                        TypeName);
                    return;
                }
                if (!String.IsNullOrEmpty(MethodName))
                {
                    var methods = type.Methods.Where(m => m.Name == MethodName).ToArray();
                    if (methods.Length == 0)
                    {
                        context.WriteErrorLine("Could not find the method '{0}'.", MethodName);
                        return;
                    }
                    DecompileMethods(methods);
                }
                else
                {
                    DecompileType(type);
                }
            }
            else
            {
                context.WriteErrorLine("At least one of --assembly or --type must be specified.");
            }
        }
示例#6
0
        public void Execute(CommandExecutionContext context)
        {
            if (!CommandHelpers.VerifyValidObjectAddress(context, ObjectAddress))
                return;

            var type = context.Heap.GetObjectType(ObjectAddress);
            if (!type.IsArray)
            {
                context.WriteErrorLine("The object at the specified address is not an array.");
                return;
            }

            var size = type.GetSize(ObjectAddress);
            var length = type.GetArrayLength(ObjectAddress);
            context.WriteLine("Name:  {0}", type.Name);
            context.WriteLine("Size:  {0}(0x{1:x}) bytes", size, size);
            context.WriteLine("Array: Number of elements {0}, Type {1} {2}",
                length, type.ComponentType.Name,
                type.ComponentType.IsValueClass ? "(value type)" : "(reference type)");

            for (int i = 0; i < length; ++i)
            {
                context.Write("[{0}] ", i);

                object value;
                if (type.ComponentType.IsValueClass)
                {
                    value = type.GetArrayElementAddress(ObjectAddress, i);
                    if (value != null)
                    {
                        context.WriteLink(
                            String.Format("{0:x16}", value),
                            String.Format("!do {0:x16} --type {1}", value, type.ComponentType.Name)
                            );
                    }
                    else
                    {
                        context.Write("<null>");
                    }
                }
                else
                {
                    value = type.GetArrayElementValue(ObjectAddress, i);
                    ulong elementAddr = type.GetArrayElementAddress(ObjectAddress, i);
                    ulong elementRef;
                    if (context.Runtime.ReadPointer(elementAddr, out elementRef))
                    {
                        context.WriteLink(
                            String.Format("{0:x16}", value ?? "<null>"),
                            String.Format("!do {0:x16}", elementRef)
                            );
                    }
                    else
                    {
                        context.Write("{0:x16}", value ?? "<null>");
                    }
                }
                context.WriteLine();
            }
        }
示例#7
0
        public void Execute(CommandExecutionContext context)
        {
            if (!CommandHelpers.VerifyValidObjectAddress(context, ObjectAddress))
                return;

            var type = context.Heap.GetObjectType(ObjectAddress);
            var dynamicObj = context.Heap.GetDynamicObject(ObjectAddress);
            context.WriteLine("Type:   {0}", type.Name);
            if (type.IsArray || dynamicObj.IsList())
            {
                int length = dynamicObj.GetLength();
                context.WriteLine("Length: {0}", length);
                for (int i = 0; i < length; ++i)
                {
                    context.WriteLine("[{0}] {1}", i, dynamicObj[i]);
                }
            }
            else if (dynamicObj.IsDictionary())
            {
                context.WriteLine("Size:   {0}", dynamicObj.GetLength());
                context.WriteLine("{0,-40} {1,-40}", "Key", "Value");
                foreach (var kvp in dynamicObj.GetDictionaryItems())
                {
                    context.WriteLine("{0,-40} {1,-40}", kvp.Item1, kvp.Item2);
                }
            }
            else
            {
                context.WriteErrorLine("The specified object is not an array, list, or dictionary. " +
                    "These are the only collection types that are currently supported. For other " +
                    "collections, try using the !hq command to inspect the object's contents.");
                return;
            }
        }
示例#8
0
文件: GCRoot.cs 项目: goldshtn/msos
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            if (!CommandHelpers.VerifyValidObjectAddress(context, ObjectAddress))
                return;

            _heap = context.Heap;
            if (!_heap.CanWalkHeap)
            {
                context.WriteErrorLine("The heap is not in a walkable state.");
                return;
            }

            var type = _heap.GetObjectType(ObjectAddress);

            _visitedObjects = new ObjectSet(_heap);
            _targets[ObjectAddress] = new Node(ObjectAddress, type);
            FillRootDictionary();

            foreach (var root in _roots)
            {
                if (_visitedRoots.Contains(root) || _visitedObjects.Contains(root.Object))
                    continue;

                Node path = TryFindPathToTarget(root);
                if (path != null)
                    PrintOnePath(path);
            }
        }
示例#9
0
        private void Bail(string format, params object[] args)
        {
            string errorMessage = String.Format(format, args);

            _context.WriteErrorLine(errorMessage);
            throw new AnalysisFailedException(errorMessage);
        }
示例#10
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            try
            {
                SetStrategy();

                if (SpecificOSThreadId != 0)
                {
                    UnifiedThread thread = SpecificThread;

                    if (SpecificThread == null)
                    {
                        _context.WriteErrorLine("There is no thread with the id '{0}'.", SpecificOSThreadId);
                        return;
                    }

                    DisplayChainForThread(thread, 0, new HashSet <uint>());
                }
                else
                {
                    _threads.ForEach(thread => DisplayChainForThread(thread, 0, new HashSet <uint>()));
                }
            }
            finally
            {
                _temporaryDbgEngTarget?.Dispose();
            }
        }
示例#11
0
 public static void ExecuteDbgEngCommand(this DataTarget target, string command, CommandExecutionContext context)
 {
     IDebugControl6 control = (IDebugControl6)target.DebuggerInterface;
     int hr = control.ExecuteWide(
         DEBUG_OUTCTL.THIS_CLIENT, command, DEBUG_EXECUTE.DEFAULT);
     if (HR.Failed(hr))
         context.WriteErrorLine("Command execution failed with hr = {0:x8}", hr);
 }
示例#12
0
        private void AttachToProcessByName()
        {
            string processName = _options.ProcessName;

            Process[] processes = Process.GetProcessesByName(processName);
            if (processes.Length == 0)
            {
                Bail("There are no processes matching the name '{0}'.", processName);
            }
            if (processes.Length > 1)
            {
                _context.WriteErrorLine("There is more than one process matching the name '{0}', use --pid to disambiguate.", processName);
                _context.WriteInfoLine("Matching process ids: {0}", String.Join(", ", processes.Select(p => p.Id).ToArray()));
                Bail();
            }
            _target = new AnalysisTarget(processes[0].Id, _context, _options.ClrVersion);
        }
示例#13
0
文件: Alias.cs 项目: tiandian/msos
 public void Execute(CommandExecutionContext context)
 {
     if (!context.Aliases.Remove(AliasName))
     {
         context.WriteErrorLine("Unknown alias '{0}'", AliasName);
         return;
     }
 }
示例#14
0
 public static bool VerifyHasHeapIndex(CommandExecutionContext context)
 {
     if (context.HeapIndex == null)
     {
         context.WriteErrorLine("This command requires a heap index. Build one with !bhi or load one with !lhi.");
         return(false);
     }
     return(true);
 }
示例#15
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;
            _output = new PlainTextOutput();

            if (!String.IsNullOrEmpty(AssemblyName))
            {
                ClrModule module = context.Runtime.Modules.SingleOrDefault(
                    m => Path.GetFileNameWithoutExtension(m.FileName).Equals(
                        Path.GetFileNameWithoutExtension(AssemblyName),
                        StringComparison.InvariantCultureIgnoreCase
                        )
                    );
                if (module == null)
                {
                    context.WriteErrorLine("Could not find the assembly '{0}'.", AssemblyName);
                    return;
                }
                if (!String.IsNullOrEmpty(TypeName))
                {
                    DecompileTypeFromModule(TypeName, module.FileName);
                }
                else
                {
                    DecompileModule(module);
                }
            }
            else if (!String.IsNullOrEmpty(TypeName))
            {
                ClrType type = context.Heap.GetTypeByName(TypeName);
                if (type == null)
                {
                    context.WriteErrorLine(
                        "Could not find the type '{0}' on the heap. Try specifying the assembly name.",
                        TypeName);
                    return;
                }
                if (!String.IsNullOrEmpty(MethodName))
                {
                    var methods = type.Methods.Where(m => m.Name == MethodName).ToArray();
                    if (methods.Length == 0)
                    {
                        context.WriteErrorLine("Could not find the method '{0}'.", MethodName);
                        return;
                    }
                    DecompileMethods(methods);
                }
                else
                {
                    DecompileType(type);
                }
            }
            else
            {
                context.WriteErrorLine("At least one of --assembly or --type must be specified.");
            }
        }
示例#16
0
文件: Alias.cs 项目: tiandian/msos
 public void Execute(CommandExecutionContext context)
 {
     if (context.Aliases.ContainsKey(AliasName))
     {
         context.WriteErrorLine("The specified alias already exists. Clear it first with .rmalias.");
         return;
     }
     context.Aliases.Add(AliasName, AliasCommand);
 }
示例#17
0
文件: Alias.cs 项目: goldshtn/msos
 public void Execute(CommandExecutionContext context)
 {
     if (context.Aliases.ContainsKey(AliasName))
     {
         context.WriteErrorLine("The specified alias already exists. Clear it first with .rmalias.");
         return;
     }
     context.Aliases.Add(AliasName, AliasCommand);
 }
示例#18
0
 public static bool VerifyHasHeapIndex(CommandExecutionContext context)
 {
     if (context.HeapIndex == null)
     {
         context.WriteErrorLine("This command requires a heap index. Build one with !bhi or load one with !lhi.");
         return false;
     }
     return true;
 }
示例#19
0
文件: Define.cs 项目: goldshtn/msos
 public void Execute(CommandExecutionContext context)
 {
     if (HelperIndex < 0 || HelperIndex >= context.Defines.Count)
     {
         context.WriteErrorLine(
             "There is no helper method at index {0} defined. Use .listdefines to get the indexes.",
             HelperIndex);
         return;
     }
     context.Defines.RemoveAt(HelperIndex);
 }
示例#20
0
 public void Execute(CommandExecutionContext context)
 {
     if (context.Runtime.Threads.Any(t => t.ManagedThreadId == ManagedThreadId))
     {
         context.CurrentManagedThreadId = ManagedThreadId;
     }
     else
     {
         context.WriteErrorLine("No thread has the managed thread id {0}", ManagedThreadId);
     }
 }
示例#21
0
 public void Execute(CommandExecutionContext context)
 {
     if (HelperIndex < 0 || HelperIndex >= context.Defines.Count)
     {
         context.WriteErrorLine(
             "There is no helper method at index {0} defined. Use .listdefines to get the indexes.",
             HelperIndex);
         return;
     }
     context.Defines.RemoveAt(HelperIndex);
 }
示例#22
0
        public static bool VerifyValidObjectAddress(
            CommandExecutionContext context, ulong objectAddress)
        {
            var heap = context.Runtime.GetHeap();
            var type = heap.GetObjectType(objectAddress);

            if (type == null || String.IsNullOrEmpty(type.Name))
            {
                context.WriteErrorLine("The specified address does not point to a valid object.");
                return false;
            }

            if (type.IsFree)
            {
                context.WriteErrorLine("The specified address points to a free object.");
                return false;
            }

            return true;
        }
示例#23
0
 public void Execute(CommandExecutionContext context)
 {
     if (context.Runtime.Threads.Any(t => t.ManagedThreadId == ManagedThreadId))
     {
         context.CurrentManagedThreadId = ManagedThreadId;
     }
     else
     {
         context.WriteErrorLine("No thread has the managed thread id {0}", ManagedThreadId);
     }
 }
示例#24
0
        public static void ExecuteDbgEngCommand(this DataTarget target, string command, CommandExecutionContext context)
        {
            IDebugControl6 control = (IDebugControl6)target.DebuggerInterface;
            int            hr      = control.ExecuteWide(
                DEBUG_OUTCTL.THIS_CLIENT, command, DEBUG_EXECUTE.DEFAULT);

            if (HR.Failed(hr))
            {
                context.WriteErrorLine("Command execution failed with hr = {0:x8}", hr);
            }
        }
示例#25
0
        public static bool VerifyValidObjectAddress(
            CommandExecutionContext context, ulong objectAddress)
        {
            var heap = context.Runtime.GetHeap();
            var type = heap.GetObjectType(objectAddress);

            if (type == null || String.IsNullOrEmpty(type.Name))
            {
                context.WriteErrorLine("The specified address does not point to a valid object.");
                return(false);
            }

            if (type.IsFree)
            {
                context.WriteErrorLine("The specified address points to a free object.");
                return(false);
            }

            return(true);
        }
示例#26
0
文件: CLRStack.cs 项目: goldshtn/msos
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            var thread = context.CurrentThread;
            if (thread == null)
            {
                context.WriteErrorLine("There is no current managed thread");
                return;
            }

            thread.WriteCurrentStackTraceToContext(context, DisplayArgumentsAndLocals);
        }
示例#27
0
文件: CLRStack.cs 项目: tiandian/msos
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            var thread = context.CurrentThread;

            if (thread == null)
            {
                context.WriteErrorLine("There is no current managed thread");
                return;
            }

            thread.WriteCurrentStackTraceToContext(context, DisplayArgumentsAndLocals);
        }
示例#28
0
        public void Execute(CommandExecutionContext context)
        {
            AppDomainSetup setupInfo = new AppDomainSetup();

            setupInfo.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
            setupInfo.PrivateBinPath  = "bin";
            AppDomain appDomain = AppDomain.CreateDomain("RunQueryAppDomain", null, setupInfo);

            string compilationOutputDirectory = null;

            object[] arguments;
            if (context.ProcessId != 0)
            {
                arguments = new object[] { context.ProcessId, context.ClrVersionIndex, context.DacLocation, context.Printer };
            }
            else
            {
                arguments = new object[] { context.DumpFile, context.ClrVersionIndex, context.DacLocation, context.Printer };
            }
            using (RunInSeparateAppDomain runner = (RunInSeparateAppDomain)appDomain.CreateInstanceAndUnwrap(
                       typeof(RunInSeparateAppDomain).Assembly.FullName,
                       typeof(RunInSeparateAppDomain).FullName,
                       false, System.Reflection.BindingFlags.CreateInstance, null,
                       arguments, null, null
                       )
                   )
            {
                try
                {
                    compilationOutputDirectory = runner.RunQuery(
                        OutputFormat.ToString(),
                        Query,
                        context.Defines);
                }
                catch (Exception ex)
                {
                    // Catching everything here because the input is user-controlled, so we can have
                    // compilation errors, dynamic binder errors, and a variety of other things I haven't
                    // even thought of yet.
                    context.WriteErrorLine(ex.Message);
                }
            }

            AppDomain.Unload(appDomain);
            if (compilationOutputDirectory != null)
            {
                Directory.Delete(compilationOutputDirectory, recursive: true);
            }
        }
示例#29
0
        public void Execute(CommandExecutionContext context)
        {
            if (String.IsNullOrEmpty(HeapIndexFileName))
            {
                if (context.TargetType != TargetType.DumpFile)
                {
                    context.WriteErrorLine("You must specify the heap index's location.");
                    return;
                }
                HeapIndexFileName = Path.ChangeExtension(context.DumpFile, ".heapindex");
                context.WriteLine("Using automatically generated heap index filename: '{0}'", HeapIndexFileName);
            }

            if (!File.Exists(HeapIndexFileName))
            {
                context.WriteErrorLine("The heap index file '{0}' does not exist.", HeapIndexFileName);
                return;
            }

            context.HeapIndex = new HeapIndex(context);
            context.HeapIndex.Load(HeapIndexFileName);

            context.WriteLine("Loaded heap index from file '{0}'", HeapIndexFileName);
        }
示例#30
0
文件: Alias.cs 项目: goldshtn/msos
 public void Execute(CommandExecutionContext context)
 {
     string aliasCommand;
     if (!context.Aliases.TryGetValue(AliasName, out aliasCommand))
     {
         context.WriteErrorLine("Unknown alias '{0}'", AliasName);
         return;
     }
     int index = 1;
     foreach (var paramValue in AliasParameters.Split(' '))
     {
         aliasCommand = aliasCommand.Replace("$" + index, paramValue);
     }
     context.WriteInfoLine("Alias '{0}' expanded to '{1}'", AliasName, aliasCommand);
     context.ExecuteCommand(aliasCommand);
 }
示例#31
0
        public void Execute(CommandExecutionContext context)
        {
            if (Disable)
            {
                context.Printer.RowsPerPage = 0;
                return;
            }

            if (RowsPerPage == 0)
            {
                context.WriteErrorLine("Can't have 0 rows per page. Use --disable to disable paging altogether.");
                return;
            }

            context.Printer.RowsPerPage = RowsPerPage;
        }
示例#32
0
        public void Execute(CommandExecutionContext context)
        {
            if (Disable)
            {
                context.Printer.RowsPerPage = 0;
                return;
            }

            if (RowsPerPage == 0)
            {
                context.WriteErrorLine("Can't have 0 rows per page. Use --disable to disable paging altogether.");
                return;
            }

            context.Printer.RowsPerPage = RowsPerPage;
        }
示例#33
0
        public void Execute(CommandExecutionContext context)
        {
            AppDomainSetup setupInfo = new AppDomainSetup();
            setupInfo.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
            setupInfo.PrivateBinPath = "bin";
            AppDomain appDomain = AppDomain.CreateDomain("RunQueryAppDomain", null, setupInfo);

            string compilationOutputDirectory = null;
            object[] arguments;
            if (context.ProcessId != 0)
            {
                arguments = new object[] { context.ProcessId, context.ClrVersionIndex, context.DacLocation, context.Printer };
            }
            else
            {
                arguments = new object[] { context.DumpFile, context.ClrVersionIndex, context.DacLocation, context.Printer };
            }
            using (RunInSeparateAppDomain runner = (RunInSeparateAppDomain)appDomain.CreateInstanceAndUnwrap(
                typeof(RunInSeparateAppDomain).Assembly.FullName,
                typeof(RunInSeparateAppDomain).FullName,
                false, System.Reflection.BindingFlags.CreateInstance, null,
                arguments, null, null
                )
            )
            {
                try
                {
                    compilationOutputDirectory = runner.RunQuery(
                        OutputFormat.ToString(),
                        Query,
                        context.Defines);
                }
                catch (Exception ex)
                {
                    // Catching everything here because the input is user-controlled, so we can have
                    // compilation errors, dynamic binder errors, and a variety of other things I haven't
                    // even thought of yet.
                    context.WriteErrorLine(ex.Message);
                }
            }

            AppDomain.Unload(appDomain);
            if (compilationOutputDirectory != null)
            {
                Directory.Delete(compilationOutputDirectory, recursive: true);
            }
        }
示例#34
0
        public bool Build(int chunkSize, string indexFileName, bool enumerateAllRoots)
        {
            if (chunkSize < 256 || chunkSize > 1048576 || chunkSize % 16 != 0)
            {
                _context.WriteErrorLine("Chunk size must be between 256 bytes and 1MB, and must be a multiple of 16.");
                return(false);
            }

            _chunkSize             = chunkSize;
            _staticRootsEnumerated = enumerateAllRoots;
            Measure(() =>
            {
                _allRoots = (from root in _heap.EnumerateRoots(enumerateStatics: enumerateAllRoots)
                             select new SimplifiedRoot(root)).ToList();
            }, "Enumerating roots");

            // Build an index of N-byte chunks in all heap segments. The index is from chunk-id (int?) to
            // the first non-free object in the chunk (not to start of the chunk, which could be in the middle
            // of an object). If a chunk is completely empty or doesn't contain the start of any object, it
            // doesn't have an id.
            Measure(BuildChunks, "Building chunks");

            // Traverse all object relationships on the heap from roots. For each chunk, specify which other
            // chunks contain references to objects in that chunk. When traversing this information, we need to
            // keep in mind that roots can also contain references to objects in the chunk -- we don't store this
            // information again because it's very easy to obtain by enumerating the roots. This decision can be
            // changed if root enumeration turns out to be slow when there are many roots.
            // Note that only live objects are being enumerated. For dead objects, it's not interesting to ask who
            // has a reference to the object -- because the referencing object is also dead.
            Measure(BuildChunkIndex, "Building chunk index");

            DisplayStatistics();

            if (!String.IsNullOrEmpty(indexFileName))
            {
                Measure(() => Save(indexFileName), "Saving index to disk");
            }
            else
            {
                _context.WriteWarningLine("You did not specify a file name, so the index will be stored only in memory. " +
                                          "If you plan to perform further analysis in another session, it is recommended that you store " +
                                          "the index to disk and later load it using the !lhi command.");
            }

            return(true);
        }
示例#35
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            ClrMethod method = context.Runtime.GetMethodByAddress(InstructionPointer);
            if (method == null)
            {
                context.WriteErrorLine("There is no managed method at the address {0:x16}.", InstructionPointer);
                return;
            }

            _sourceFileCache = new Dictionary<string, string[]>();
            using (var target = context.CreateTemporaryDbgEngTarget())
            {
                _control = (IDebugControl)target.DebuggerInterface;
                DisassembleMethod(method);
            }
        }
示例#36
0
文件: Alias.cs 项目: tiandian/msos
        public void Execute(CommandExecutionContext context)
        {
            string aliasCommand;

            if (!context.Aliases.TryGetValue(AliasName, out aliasCommand))
            {
                context.WriteErrorLine("Unknown alias '{0}'", AliasName);
                return;
            }
            int index = 1;

            foreach (var paramValue in AliasParameters.Split(' '))
            {
                aliasCommand = aliasCommand.Replace("$" + index, paramValue);
            }
            context.WriteInfoLine("Alias '{0}' expanded to '{1}'", AliasName, aliasCommand);
            context.ExecuteCommand(aliasCommand);
        }
示例#37
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            ClrMethod method = context.Runtime.GetMethodByAddress(InstructionPointer);

            if (method == null)
            {
                context.WriteErrorLine("There is no managed method at the address {0:x16}.", InstructionPointer);
                return;
            }

            _sourceFileCache = new Dictionary <string, string[]>();
            using (var target = context.CreateTemporaryDbgEngTarget())
            {
                _control = (IDebugControl)target.DebuggerInterface;
                DisassembleMethod(method);
            }
        }
示例#38
0
 public void Execute(CommandExecutionContext context)
 {
     byte[] buffer = new byte[Columns];
     for (int remaining = Length; remaining > 0; remaining -= Columns, Address += (uint)Columns)
     {
         int read = 0;
         if (!context.Runtime.ReadMemory(Address, buffer, Math.Min(remaining, Columns), out read))
         {
             context.WriteErrorLine("Error reading memory at {0:x16}, could only read {1} bytes while {2} requested",
                                    Address, read, Columns);
             return;
         }
         string bytes = "";
         string chars = "";
         for (int col = 0; col < read; ++col)
         {
             bytes += String.Format("{0:x2} ", buffer[col]);
             chars += (buffer[col] >= 32 && buffer[col] <= 126) ? (char)buffer[col] : '.';
         }
         context.WriteLine("{0:x16}  {1}  {2}", Address, bytes, chars);
     }
 }
示例#39
0
文件: DB.cs 项目: goldshtn/msos
 public void Execute(CommandExecutionContext context)
 {
     byte[] buffer = new byte[Columns];
     for (int remaining = Length; remaining > 0; remaining -= Columns, Address += (uint)Columns)
     {
         int read = 0;
         if (!context.Runtime.ReadMemory(Address, buffer, Math.Min(remaining, Columns), out read))
         {
             context.WriteErrorLine("Error reading memory at {0:x16}, could only read {1} bytes while {2} requested",
                 Address, read, Columns);
             return;
         }
         string bytes = "";
         string chars = "";
         for (int col = 0; col < read; ++col)
         {
             bytes += String.Format("{0:x2} ", buffer[col]);
             chars += (buffer[col] >= 32 && buffer[col] <= 126) ? (char)buffer[col] : '.';
         }
         context.WriteLine("{0:x16}  {1}  {2}", Address, bytes, chars);
     }
 }
示例#40
0
        public void Execute(CommandExecutionContext context)
        {
            if (!CommandHelpers.VerifyValidObjectAddress(context, ObjectAddress))
            {
                return;
            }

            var type       = context.Heap.GetObjectType(ObjectAddress);
            var dynamicObj = context.Heap.GetDynamicObject(ObjectAddress);

            context.WriteLine("Type:   {0}", type.Name);
            if (type.IsArray || dynamicObj.IsList())
            {
                int length = dynamicObj.GetLength();
                context.WriteLine("Length: {0}", length);
                for (int i = 0; i < length; ++i)
                {
                    context.WriteLine("[{0}] {1}", i, dynamicObj[i]);
                }
            }
            else if (dynamicObj.IsDictionary())
            {
                context.WriteLine("Size:   {0}", dynamicObj.GetLength());
                context.WriteLine("{0,-40} {1,-40}", "Key", "Value");
                foreach (var kvp in dynamicObj.GetDictionaryItems())
                {
                    context.WriteLine("{0,-40} {1,-40}", kvp.Item1, kvp.Item2);
                }
            }
            else
            {
                context.WriteErrorLine("The specified object is not an array, list, or dictionary. " +
                                       "These are the only collection types that are currently supported. For other " +
                                       "collections, try using the !hq command to inspect the object's contents.");
                return;
            }
        }
示例#41
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            if (!CommandHelpers.VerifyValidObjectAddress(context, ObjectAddress))
            {
                return;
            }

            _heap = context.Heap;
            if (!_heap.CanWalkHeap)
            {
                context.WriteErrorLine("The heap is not in a walkable state.");
                return;
            }

            var type = _heap.GetObjectType(ObjectAddress);

            _visitedObjects         = new ObjectSet(_heap);
            _targets[ObjectAddress] = new Node(ObjectAddress, type);
            FillRootDictionary();

            foreach (var root in _roots)
            {
                if (_visitedRoots.Contains(root) || _visitedObjects.Contains(root.Object))
                {
                    continue;
                }

                Node path = TryFindPathToTarget(root);
                if (path != null)
                {
                    PrintOnePath(path);
                }
            }
        }
示例#42
0
        public void Execute(CommandExecutionContext context)
        {
            if (String.IsNullOrEmpty(HeapIndexFileName))
            {
                if (context.TargetType != TargetType.DumpFile)
                {
                    context.WriteErrorLine("You must specify the heap index's location.");
                    return;
                }
                HeapIndexFileName = Path.ChangeExtension(context.DumpFile, ".heapindex");
                context.WriteLine("Using automatically generated heap index filename: '{0}'", HeapIndexFileName);
            }

            if (!File.Exists(HeapIndexFileName))
            {
                context.WriteErrorLine("The heap index file '{0}' does not exist.", HeapIndexFileName);
                return;
            }

            context.HeapIndex = new HeapIndex(context);
            context.HeapIndex.Load(HeapIndexFileName);

            context.WriteLine("Loaded heap index from file '{0}'", HeapIndexFileName);
        }
示例#43
0
文件: Alias.cs 项目: goldshtn/msos
 public void Execute(CommandExecutionContext context)
 {
     if (!context.Aliases.Remove(AliasName))
     {
         context.WriteErrorLine("Unknown alias '{0}'", AliasName);
         return;
     }
 }
示例#44
0
        public void Execute(CommandExecutionContext context)
        {
            if (!CommandHelpers.VerifyValidObjectAddress(context, ObjectAddress))
            {
                return;
            }

            var type = context.Heap.GetObjectType(ObjectAddress);

            if (!type.IsArray)
            {
                context.WriteErrorLine("The object at the specified address is not an array.");
                return;
            }

            var size   = type.GetSize(ObjectAddress);
            var length = type.GetArrayLength(ObjectAddress);

            context.WriteLine("Name:  {0}", type.Name);
            context.WriteLine("Size:  {0}(0x{1:x}) bytes", size, size);
            context.WriteLine("Array: Number of elements {0}, Type {1} {2}",
                              length, type.ComponentType.Name,
                              type.ComponentType.IsValueClass ? "(value type)" : "(reference type)");

            for (int i = 0; i < length; ++i)
            {
                context.Write("[{0}] ", i);

                object value;
                if (type.ComponentType.IsValueClass)
                {
                    value = type.GetArrayElementAddress(ObjectAddress, i);
                    if (value != null)
                    {
                        context.WriteLink(
                            String.Format("{0:x16}", value),
                            String.Format("!do {0:x16} --type {1}", value, type.ComponentType.Name)
                            );
                    }
                    else
                    {
                        context.Write("<null>");
                    }
                }
                else
                {
                    value = type.GetArrayElementValue(ObjectAddress, i);
                    ulong elementAddr = type.GetArrayElementAddress(ObjectAddress, i);
                    ulong elementRef;
                    if (context.Runtime.ReadPointer(elementAddr, out elementRef))
                    {
                        context.WriteLink(
                            String.Format("{0:x16}", value ?? "<null>"),
                            String.Format("!do {0:x16}", elementRef)
                            );
                    }
                    else
                    {
                        context.Write("{0:x16}", value ?? "<null>");
                    }
                }
                context.WriteLine();
            }
        }
示例#45
0
        public void Execute(CommandExecutionContext context)
        {
            if (!String.IsNullOrEmpty(TypeRegex))
            {
                try
                {
                    new Regex(TypeRegex);
                }
                catch (ArgumentException)
                {
                    context.WriteErrorLine("The regular expression specified for --type is not valid; did you forget to escape regex characters?");
                    return;
                }
            }

            _heap = context.Runtime.GetHeap();
            if (!_heap.CanWalkHeap)
            {
                context.WriteErrorLine("The heap is not in a walkable state.");
                return;
            }

            var  typeInfos        = new Dictionary <ulong, TypeInfo>(); // MT to TypeInfo
            long totalObjectCount = 0;

            if (!StatisticsOnly)
            {
                context.WriteLine("{0,-20} {1,-20} {2}", "MT", "Address", "Size");
            }
            foreach (var obj in _heap.EnumerateObjectAddresses())
            {
                ulong mt = 0;
                context.Runtime.ReadPointer(obj, out mt);

                if (!FilterObject(obj, mt))
                {
                    continue;
                }

                var type = _heap.GetObjectType(obj);
                if (type == null || String.IsNullOrEmpty(type.Name))
                {
                    continue;
                }

                var size = type.GetSize(obj);

                if (!StatisticsOnly)
                {
                    context.WriteLine("{0,-20:x16} {1,-20:x16} {2,-10}", mt, obj, size);
                }
                if (typeInfos.ContainsKey(mt))
                {
                    var current = typeInfos[mt];
                    current.Count += 1;
                    current.Size  += size;
                    typeInfos[mt]  = current;
                }
                else
                {
                    var objType     = _heap.GetObjectType(obj);
                    var objTypeName = objType != null ? objType.Name : "<no name>";
                    typeInfos.Add(mt, new TypeInfo {
                        Size = size, Count = 1, TypeName = objTypeName
                    });
                }
                ++totalObjectCount;
            }

            context.WriteLine("Statistics:");
            context.WriteLine("{0,-20} {1,-10} {2,-10} {3}", "MT", "Count", "TotalSize", "Class Name");
            foreach (var kvp in (from e in typeInfos orderby e.Value.Size ascending select e))
            {
                context.WriteLine("{0,-20:x16} {1,-10} {2,-10} {3}",
                                  kvp.Key, kvp.Value.Count, kvp.Value.Size, kvp.Value.TypeName);
            }
            context.WriteLine("Total {0} objects", totalObjectCount);
        }
示例#46
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            ClrType type;

            if (!String.IsNullOrEmpty(TypeName))
            {
                type = context.Heap.GetTypeByName(TypeName);
                if (type == null)
                {
                    context.WriteErrorLine("There is no type named '{0}'.", TypeName);
                    return;
                }
            }
            else
            {
                type = context.Heap.GetObjectType(ObjectAddress);
                if (type == null || String.IsNullOrEmpty(type.Name))
                {
                    context.WriteErrorLine("The specified address is not an object.");
                    return;
                }
            }

            ulong mt = 0;

            if (type.IsObjectReference && !context.Runtime.ReadPointer(ObjectAddress, out mt))
            {
                context.WriteWarningLine("Unable to retrieve MT for object.");
            }

            var size = type.GetSize(ObjectAddress);

            context.Write("Name:     {0}", type.Name);
            var dynamicObj = context.Heap.GetDynamicObject(ObjectAddress);

            if (type.IsArray || dynamicObj.IsList() || dynamicObj.IsDictionary())
            {
                context.WriteLink("   <display elements>", String.Format("!dumpcollection {0:x16}", ObjectAddress));
            }
            context.WriteLine();
            if (mt != 0)
            {
                context.WriteLine("MT:       {0:x16}", mt);
            }
            context.WriteLine("Size:     {0}(0x{1:x}) bytes", size, size);
            if (type.IsArray)
            {
                context.WriteLine("Array:    size {0}, element type {1}",
                                  type.GetArrayLength(ObjectAddress),
                                  type.ComponentType != null ? type.ComponentType.Name : "<unknown>");
            }
            context.WriteLine("Assembly: {0}", type.Module.FileName);
            if (type.HasSimpleValue)
            {
                context.WriteLine("Value:    {0}", type.GetValue(ObjectAddress));
            }

            if (!NoFields && type.Fields.Count > 0)
            {
                context.WriteLine("Fields:");
                context.WriteLine("{0,-8} {1,-20} {2,-3} {3,-10} {4,-20} {5}",
                                  "Offset", "Type", "VT", "Attr", "Value", "Name");
                foreach (var field in type.Fields)
                {
                    DisplayFieldRecursively(field, new InstanceFieldValueForDisplayRetriever(ObjectAddress), field.Offset, depth: type.IsValueClass ? 1 : 0);
                }
                foreach (var field in type.ThreadStaticFields)
                {
                    context.WriteLine("{0,-8:x} {1,-20} {2,-3} {3,-10} {4,-20:x16} {5}",
                                      field.Offset, field.GetFieldTypeNameTrimmed(),
                                      (field.IsOfPrimitiveType() || field.IsOfValueClass()) ? 1 : 0,
                                      "shared", "thrstatic", field.Name);
                    foreach (var appDomain in context.Runtime.AppDomains)
                    {
                        foreach (var thread in context.Runtime.Threads)
                        {
                            DisplayFieldRecursively(field, new ThreadStaticFieldValueForDisplayRetriever(appDomain, thread), field.Offset);
                        }
                    }
                }
                foreach (var field in type.StaticFields)
                {
                    context.WriteLine("{0,-8:x} {1,-20} {2,-3} {3,-10} {4,-20:x16} {5}",
                                      field.Offset, field.GetFieldTypeNameTrimmed(),
                                      (field.IsOfPrimitiveType() || field.IsOfValueClass()) ? 1 : 0,
                                      "shared", "static", field.Name);
                    foreach (var appDomain in context.Runtime.AppDomains)
                    {
                        DisplayFieldRecursively(field, new StaticFieldValueForDisplayRetriever(appDomain), field.Offset);
                    }
                }
            }
        }
示例#47
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            try
            {
                SetStrategy();

                if (SpecificOSThreadId != 0)
                {
                    UnifiedThread thread = SpecificThread;

                    if (SpecificThread == null)
                    {
                        _context.WriteErrorLine("There is no thread with the id '{0}'.", SpecificOSThreadId);
                        return;
                    }

                    DisplayChainForThread(thread, 0, new HashSet<uint>());
                }
                else
                {
                    _threads.ForEach(thread => DisplayChainForThread(thread, 0, new HashSet<uint>()));
                }
            }
            finally
            {
                _temporaryDbgEngTarget?.Dispose();
            }
        }
示例#48
0
文件: DumpHeap.cs 项目: goldshtn/msos
        public void Execute(CommandExecutionContext context)
        {
            if (!String.IsNullOrEmpty(TypeRegex))
            {
                try
                {
                    new Regex(TypeRegex);
                }
                catch (ArgumentException)
                {
                    context.WriteErrorLine("The regular expression specified for --type is not valid; did you forget to escape regex characters?");
                    return;
                }
            }

            _heap = context.Runtime.GetHeap();
            if (!_heap.CanWalkHeap)
            {
                context.WriteErrorLine("The heap is not in a walkable state.");
                return;
            }

            var typeInfos = new Dictionary<ulong, TypeInfo>(); // MT to TypeInfo
            long totalObjectCount = 0;
            if (!StatisticsOnly)
            {
                context.WriteLine("{0,-20} {1,-20} {2}", "MT", "Address", "Size");
            }
            foreach (var obj in _heap.EnumerateObjectAddresses())
            {
                ulong mt = 0;
                context.Runtime.ReadPointer(obj, out mt);

                if (!FilterObject(obj, mt))
                    continue;

                var type = _heap.GetObjectType(obj);
                if (type == null || String.IsNullOrEmpty(type.Name))
                    continue;

                var size = type.GetSize(obj);

                if (!StatisticsOnly)
                {
                    context.WriteLine("{0,-20:x16} {1,-20:x16} {2,-10}", mt, obj, size);
                }
                if (typeInfos.ContainsKey(mt))
                {
                    var current = typeInfos[mt];
                    current.Count += 1;
                    current.Size += size;
                    typeInfos[mt] = current;
                }
                else
                {
                    var objType = _heap.GetObjectType(obj);
                    var objTypeName = objType != null ? objType.Name : "<no name>";
                    typeInfos.Add(mt, new TypeInfo { Size = size, Count = 1, TypeName = objTypeName });
                }
                ++totalObjectCount;
            }

            context.WriteLine("Statistics:");
            context.WriteLine("{0,-20} {1,-10} {2,-10} {3}", "MT", "Count", "TotalSize", "Class Name");
            foreach (var kvp in (from e in typeInfos orderby e.Value.Size ascending select e))
            {
                context.WriteLine("{0,-20:x16} {1,-10} {2,-10} {3}",
                    kvp.Key, kvp.Value.Count, kvp.Value.Size, kvp.Value.TypeName);
            }
            context.WriteLine("Total {0} objects", totalObjectCount);
        }