Example #1
0
        public ColorString ToColorString()
        {
            // return DbgProvider.FormatAddress( BaseAddress,
            //                                   Debugger.TargetIs32Bit,
            //                                   true )
            //             .Append( " " )
            //             .Append( DbgProvider.FormatAddress( BaseAddress + Size,
            //                                                 Debugger.TargetIs32Bit,
            //                                                 true ) )
            //             .Append( " " )
            //             .AppendPushPopFgBg( ConsoleColor.Black,
            //                                 ConsoleColor.DarkYellow,
            //                                 Name )
            //             .Append( Name.Length < 20 ? new String( ' ', 20 - Name.Length ) : String.Empty )
            //             .Append( SymbolStatus );

            return(DbgProvider.ColorizeModuleName(Name)
                   .Append("  ")
                   .Append(DbgProvider.FormatAddress(BaseAddress, Debugger.TargetIs32Bit,
                                                     true))
                   .Append(" ")
                   .Append(DbgProvider.FormatAddress(BaseAddress + Size,
                                                     Debugger.TargetIs32Bit,
                                                     true)));
        } // end ToColorString()
Example #2
0
        } // end _FormatBlocks()

        private ColorString _FormatCharsOnly(uint numColumns, uint bytesPerChar, Func <char, char> toDisplay)
        {
            if (0 == numColumns)
            {
                numColumns = 32; // documentation says it should be 48, but windbg seems to do 32 instead.
            }
            ColorString   cs      = new ColorString();
            StringBuilder sbChars = new StringBuilder((int)numColumns + 8);
            ulong         addr    = StartAddress;

            for (int idx = 0; idx < Count; idx++)
            {
                if (0 == (idx % numColumns))
                {
                    // This is the beginning of a new line.

                    // First finish off the last line if necessary:
                    if (sbChars.Length > 1)
                    {
                        sbChars.Append('"');
                        cs.AppendPushPopFg(ConsoleColor.Cyan, sbChars.ToString());
                    }

                    sbChars.Clear();
                    sbChars.Append('"');

                    if (0 != idx)
                    {
                        cs.AppendLine();
                    }

                    cs.Append(DbgProvider.FormatAddress(addr, m_is32Bit, true, true)).Append("  ");
                    addr += (ulong)numColumns * bytesPerChar;
                } // end start of new line

                // Widen to ulong to accommodate largest possible item.
                ulong val = (ulong)this[idx];

                if (0 == val)
                {
                    break;
                }

                sbChars.Append(toDisplay((char)val));
            } // end for( idx = 0 .. length )

            // Finish off last line.
            if (sbChars.Length > 1)
            {
                sbChars.Append('"');
                cs.AppendPushPopFg(ConsoleColor.Cyan, sbChars.ToString());
            }

            return(cs.MakeReadOnly());
        } // end _FormatCharsOnly()
Example #3
0
        } // end _FormatCharsOnly()

        private ColorString _FormatBits()
        {
            ColorString cs            = new ColorString();
            uint        bytesPerValue = sizeof(uint);
            uint        bitsPerValue  = bytesPerValue * 8;
            ulong       addr          = StartAddress;

            for (int idx = 0; idx < Count; idx++)
            {
                if (0 != idx)
                {
                    cs.AppendLine();
                }

                cs.Append(DbgProvider.FormatAddress(addr, m_is32Bit, true, true)).Append(" ");
                addr += (ulong)bytesPerValue;

                uint val = this[idx];
                for (int i = 0; i < bitsPerValue; i++)
                {
                    if (i % 8 == 0)
                    {
                        cs.Append(" ");
                    }
                    uint mask = 0x80000000 >> i;
                    if ((mask & val) == mask)
                    {
                        cs.AppendPushPopFg(ConsoleColor.Green, "1");
                    }
                    else
                    {
                        cs.AppendPushPopFg(ConsoleColor.DarkGreen, "0");
                    }
                }

                cs.Append("  " + val.ToString("x").PadLeft(8, '0'));
            } // end for( idx = 0 .. length )

            return(cs.MakeReadOnly());
        } // end _FormatBits()
Example #4
0
        } // end _ZeroPad()

        private ColorString _FormatBlocks(int elemSize,
                                          int charsPerBlock,
                                          uint numColumns,
                                          AddtlInfo addtlInfo)
        {
            if (0 == numColumns)
            {
                numColumns = (uint)(16 / elemSize);
            }

            if (addtlInfo.HasFlag(AddtlInfo.Symbols))
            {
                numColumns = 1;
            }

            int           desiredLen = elemSize * 2;
            ColorString   cs         = new ColorString();
            StringBuilder sbChars    = new StringBuilder(20);
            ulong         addr       = StartAddress;

            cs.AppendPushFg(ConsoleColor.DarkGreen);

            for (int idx = 0; idx < Count; idx++)
            {
                if (0 == (idx % numColumns))
                {
                    // This is the beginning of a new line.

                    // First finish off the last line if necessary:
                    if (addtlInfo.HasFlag(AddtlInfo.Ascii))
                    {
                        if (sbChars.Length > 2)
                        {
                            cs.AppendPushPopFg(ConsoleColor.Cyan, sbChars.ToString());
                        }

                        sbChars.Clear();
                        sbChars.Append("  ");
                    }

                    if (0 != idx)
                    {
                        cs.AppendLine();
                    }

                    cs.Append(DbgProvider.FormatAddress(addr, m_is32Bit, true, true)).Append("  ");
                    addr += (ulong)(numColumns * elemSize);
                } // end start of new line
                else
                {
                    cs.Append(" ");
                }

                // Widen to ulong to accommodate largest possible item.
                ulong val = (ulong)this[idx];

                // This highlights the non-zero portion in [bright] green.
                cs.Append(_ZeroPad(val, desiredLen));

                if (addtlInfo.HasFlag(AddtlInfo.Symbols))
                {
                    ColorString csSym = ColorString.Empty;
                    if (val > 4096)  // don't even bother trying if it's too low.
                    {
                        csSym = m_lookupSymbol(val);
                        if (csSym.Length > 0)
                        {
                            cs.Append(" ").Append(csSym);
                        }
                    }
                    if (addtlInfo.HasFlag(AddtlInfo.Ascii))
                    {
                        if (0 == csSym.Length)
                        {
                            cs.Append("                                          ");
                            _AppendChars(sbChars, idx * elemSize, elemSize);
                        }
                    }
                } // end if( symbols )
                else if (addtlInfo.HasFlag(AddtlInfo.Ascii))
                {
                    _AppendChars(sbChars, idx * elemSize, elemSize);
                } // end else if( Ascii )
            }     // end for( idx = 0 .. length )

            // Finish off last line.
            if (sbChars.Length > 2)
            {
                // It could be a partial line, so we may need to adjust for that.
                int numMissing = (int)numColumns - ((sbChars.Length - 2) / elemSize);
                Util.Assert(numMissing >= 0);
                if (numMissing > 0)
                {
                    cs.Append(new String(' ', numMissing * charsPerBlock));
                }
                cs.AppendPushPopFg(ConsoleColor.Cyan, sbChars.ToString());
            }

            cs.AppendPop(); // pop DarkGreen

            return(cs.MakeReadOnly());
        } // end _FormatBlocks()
Example #5
0
 public ColorString ToColorString(ConsoleColor color) => DbgProvider.FormatAddress(Value, Is32Bit, true, true, color);
Example #6
0
 public ColorString ToColorString() => DbgProvider.FormatAddress(Value, Is32Bit, true, true);
Example #7
0
        public ColorString ToColorString()
        {
            if (DEBUG_EVENT.NONE == EventType)
            {
                return(ColorString.Empty);
            }

            ColorString csThreadId;

            if (0xffffffff == ThreadId)
            {
                csThreadId = new ColorString(ConsoleColor.Red, "ffffffff");
            }
            else
            {
                csThreadId = new ColorString(m_debugger.GetUModeThreadByDebuggerId(ThreadId).Tid.ToString("x"));
            }

            ColorString cs = new ColorString(ConsoleColor.Black, ConsoleColor.White, "     Last event:")
                             .Append(" ")
                             .Append(m_debugger.GetProcessSystemId(ProcessId).ToString("x"))
                             .Append(".")
                             .Append(csThreadId)
                             .Append(": ");

            DbgModuleInfo mod = null;
            string        tmp = null;

            string[] lines = null;

            switch (EventType)
            {
            case DEBUG_EVENT.NONE:
                Util.Fail("can't get here");       // we returned ColorString.Empty above
                break;

            case DEBUG_EVENT.BREAKPOINT:
                cs.Append(Description);

                break;

            case DEBUG_EVENT.EXCEPTION:
                tmp = ExceptionEventArgs._CreateMessage(m_debugger,
                                                        ExtraInformation.Exception.ExceptionRecord,
                                                        ExtraInformation.Exception.FirstChance != 0,
                                                        0,
                                                        null,
                                                        false).ToString(DbgProvider.HostSupportsColor);

                lines = tmp.Split(sm_newline, StringSplitOptions.None);

                cs.AppendLine(lines[0]);
                for (int i = 1; i < lines.Length; i++)
                {
                    cs.AppendPushPopFgBg(ConsoleColor.Black, ConsoleColor.White, "                ")
                    .Append(" ")
                    .Append(lines[i]);

                    if (i != lines.Length - 1)
                    {
                        cs.AppendLine();
                    }
                }

                break;

            case DEBUG_EVENT.CREATE_THREAD:
                cs.Append(Description);

                break;

            case DEBUG_EVENT.EXIT_THREAD:
                cs.Append(Description);

                break;

            case DEBUG_EVENT.CREATE_PROCESS:
                cs.Append(Description);

                break;

            case DEBUG_EVENT.EXIT_PROCESS:
                cs.Append(Description);

                break;

            case DEBUG_EVENT.LOAD_MODULE:
                //
                //   Last event: 9c84.3984: Load module C:\WINDOWS\system32\ADVAPI32.dll at 00007ffb`b0cd0000
                //     debugger time: Mon Oct 20 19:30:56.164 2014 (UTC - 7:00)

                mod = m_debugger.GetModuleByAddress(ExtraInformation.LoadModule.Base);
                cs.Append("Loaded module ")
                .AppendPushPopFg(ConsoleColor.White, mod.ImageName)
                .Append(" at ")
                .Append(DbgProvider.FormatAddress(ExtraInformation.LoadModule.Base,
                                                  m_debugger.TargetIs32Bit,
                                                  true));
                break;

            case DEBUG_EVENT.UNLOAD_MODULE:
                // TODO: test this!
                mod = m_debugger.GetModuleByAddress(ExtraInformation.UnloadModule.Base);
                cs.AppendPushPopFg(ConsoleColor.DarkRed, "Unloaded module ")
                .AppendPushPopFg(ConsoleColor.White, mod.ImageName)
                .Append(" at ")
                .Append(DbgProvider.FormatAddress(ExtraInformation.UnloadModule.Base,
                                                  m_debugger.TargetIs32Bit,
                                                  true));
                break;

            case DEBUG_EVENT.SYSTEM_ERROR:
                // TODO: test this!
                cs.AppendPushPopFgBg(ConsoleColor.White, ConsoleColor.Red, "System error:")
                .Append(" ")
                .AppendPushPopFg(ConsoleColor.Red, Util.FormatErrorCode(ExtraInformation.SystemError.Error))
                .Append(".")
                .AppendPushPopFg(ConsoleColor.Red, Util.FormatErrorCode(ExtraInformation.SystemError.Level));

                // TODO: This is a hack.
                var w32e = new System.ComponentModel.Win32Exception((int)ExtraInformation.SystemError.Error);
                cs.AppendLine();
                cs.AppendPushPopFg(ConsoleColor.Red, w32e.Message).Append(".");

                break;

            case DEBUG_EVENT.SESSION_STATUS:
                // DbgEng doesn't save these as "last events".
                Util.Fail(Util.Sprintf("Unexpected last event type: {0}", EventType));

                break;

            case DEBUG_EVENT.CHANGE_DEBUGGEE_STATE:
                // DbgEng doesn't save these as "last events".
                Util.Fail(Util.Sprintf("Unexpected last event type: {0}", EventType));

                break;

            case DEBUG_EVENT.CHANGE_ENGINE_STATE:
                // DbgEng doesn't save these as "last events".
                Util.Fail(Util.Sprintf("Unexpected last event type: {0}", EventType));

                break;

            case DEBUG_EVENT.CHANGE_SYMBOL_STATE:
                // DbgEng doesn't save these as "last events".
                Util.Fail(Util.Sprintf("Unexpected last event type: {0}", EventType));

                break;

            default:
                Util.Fail(Util.Sprintf("Unexpected event type: {0}", EventType));
                throw new Exception(Util.Sprintf("Unexpected event type: {0}", EventType));
            } // end switch( event type )


            cs.AppendLine()
            .AppendPushPopFgBg(ConsoleColor.Black, ConsoleColor.White, "  Debugger time:")
            .Append(" ")
            .Append(DbgProvider.FormatTimestamp(_GetDbgEngLastEventTimestamp(), true));

            return(cs);
        } // end ToColorString()
Example #8
0
        } // end property Displacement


        public override string ToString()
        {
            if (null == Function)
            {
                // Let's see if dbgeng can do any better.
                try
                {
                    Util.Fail("We don't have a Function.");
                    ulong  displacement;
                    string fullname = GetNameByInlineContext(InstructionPointer,
                                                             NativeFrameEx.InlineFrameContext,
                                                             out displacement);
                    string modName, funcName;
                    ulong  dontCare;
                    DbgProvider.ParseSymbolName(fullname,
                                                out modName,
                                                out funcName,
                                                out dontCare);

                    if (displacement == 0)
                    {
                        return(fullname);
                    }
                    else
                    {
                        return(Util.Sprintf("{0}+0x{1:x}", fullname, displacement));
                    }
                }
                catch (DbgProviderException dpe)
                {
                    LogManager.Trace("Still couldn't get symbolic name: {0}", Util.GetExceptionMessages(dpe));
                }
            } // end if( !Function )

            try
            {
                string disp = String.Empty;

                if (Displacement != 0)
                {
                    disp = "+0x" + Displacement.ToString("x");
                }

                if (null != ManagedFrame)
                {
                    // TODO: Ideally we wouldn't deal with this at this layer; we would
                    // synthesize a DbgModuleInfo instead. But for now this is a handy
                    // workaround.
                    // https://github.com/Microsoft/DbgShell/issues/35

                    // It seems like dbgeng likes to replace dots in module names with
                    // underbars... why? I guess I should do the same?
                    return(ManagedFrame.ModuleName.Replace('.', '_') + "!" + ManagedFrame.Method.GetFullSignature() + disp);
                }

                return(SymbolName + disp);
            }
            catch (DbgProviderException dpe)
            {
                LogManager.Trace("Could not get symbol name: {0}", Util.GetExceptionMessages(dpe));
            }

            return(DbgProvider.FormatAddress(InstructionPointer,
                                             Debugger.TargetIs32Bit,
                                             useTick: true));
        } // end ToString()
Example #9
0
 public DbgMemoryAccessException(ulong address, bool is32bit)
     : this(address,
            Util.Sprintf("Could not access memory: {0}",
                         DbgProvider.FormatAddress(address, is32bit, true)))
 {
 }