Beispiel #1
0
        internal DkmEvaluationResult[] GetChildren(
            DkmEvaluationResult evalResult,
            int initialRequestSize,
            DkmInspectionContext inspectionContext,
            out DkmEvaluationResultEnumContext enumContext
            )
        {
            DkmGetChildrenAsyncResult getChildrenResult = default(DkmGetChildrenAsyncResult);
            var workList = new DkmWorkList();

            evalResult.GetChildren(
                workList,
                initialRequestSize,
                inspectionContext ?? DefaultInspectionContext,
                r =>
            {
                getChildrenResult = r;
            }
                );
            workList.Execute();
            var exception = getChildrenResult.Exception;

            if (exception != null)
            {
                ExceptionDispatchInfo.Capture(exception).Throw();
            }
            enumContext = getChildrenResult.EnumContext;
            return(getChildrenResult.InitialChildren);
        }
Beispiel #2
0
        internal DkmEvaluationResult[] GetItems(
            DkmEvaluationResultEnumContext enumContext,
            int startIndex,
            int count
            )
        {
            DkmEvaluationEnumAsyncResult getItemsResult = default(DkmEvaluationEnumAsyncResult);
            var workList = new DkmWorkList();

            enumContext.GetItems(
                workList,
                startIndex,
                count,
                r =>
            {
                getItemsResult = r;
            }
                );
            workList.Execute();
            var exception = getItemsResult.Exception;

            if (exception != null)
            {
                ExceptionDispatchInfo.Capture(exception).Throw();
            }
            return(getItemsResult.Items);
        }
Beispiel #3
0
        internal DkmEvaluationAsyncResult FormatAsyncResult(
            string name,
            string fullName,
            DkmClrValue value,
            DkmClrType declaredType = null,
            DkmClrCustomTypeInfo declaredTypeInfo  = null,
            DkmInspectionContext inspectionContext = null
            )
        {
            DkmEvaluationAsyncResult asyncResult = default(DkmEvaluationAsyncResult);
            var workList = new DkmWorkList();

            value.GetResult(
                workList,
                DeclaredType: declaredType ?? value.Type,
                CustomTypeInfo: declaredTypeInfo,
                InspectionContext: inspectionContext ?? DefaultInspectionContext,
                FormatSpecifiers: Formatter.NoFormatSpecifiers,
                ResultName: name,
                ResultFullName: fullName,
                CompletionRoutine: r => asyncResult = r
                );
            workList.Execute();
            return(asyncResult);
        }
        private void QueryColumnDetails(int columnCount)
        {
            var exprs = new List <DkmLanguageExpression>();

            try
            {
                DkmProcess  process          = this.inspectionContext.Thread.Process;
                DkmWorkList workList         = DkmWorkList.Create(null);
                SqliteVisualizerException ex = null;
                var columnNamesLocal         = new string[columnCount];
                for (int i = 0; i < columnCount; ++i)
                {
                    var i_local = i;
                    var colName = this.AddFuncEval(
                        workList,
                        $"sqlite3_column_name(*(sqlite3_stmt **){this.procMemForQuery}, {i})",
                        (r) =>
                    {
                        DkmSuccessEvaluationResult suc;
                        if (!this.VerifySuccess(r, out suc))
                        {
                            ex = ex ?? new SqliteVisualizerException(Resources.ErrMsg_FuncEvalFailed, r.FullName);
                            return;
                        }

                        ulong address      = suc.Address.Value;
                        byte[] stringMaybe = process.ReadMemoryString(address, DkmReadMemoryFlags.None, 1, 1024);
                        int len            = stringMaybe.Length;
                        if (len > 0)
                        {
                            // The debugger null terminates all strings, but encoding doesn't strip null when creating a string
                            len--;
                            columnNamesLocal[i_local] = Encoding.UTF8.GetString(stringMaybe, 0, len);
                        }
                    });

                    exprs.Add(colName);
                }

                workList.Execute();

                if (ex != null)
                {
                    throw ex;
                }

                this.ColumnNames = columnNamesLocal;
            }
            finally
            {
                foreach (var e in exprs)
                {
                    e.Close();
                }
            }
        }
Beispiel #5
0
        internal DkmEvaluationResult FormatResult(string name, string fullName, DkmClrValue value, DkmClrType declaredType = null, DkmInspectionContext inspectionContext = null)
        {
            DkmEvaluationResult evaluationResult = null;
            var workList = new DkmWorkList();

            _resultProvider.GetResult(
                value,
                workList,
                declaredType: declaredType ?? value.Type,
                inspectionContext: inspectionContext ?? DefaultInspectionContext,
                formatSpecifiers: Formatter.NoFormatSpecifiers,
                resultName: name,
                resultFullName: null,
                completionRoutine: asyncResult => evaluationResult = asyncResult.Result);
            workList.Execute();
            return(evaluationResult);
        }
        internal DkmEvaluationResult FormatResult(string name, string fullName, DkmClrValue value, DkmClrType declaredType = null, bool[] declaredTypeInfo = null, DkmInspectionContext inspectionContext = null)
        {
            DkmEvaluationResult evaluationResult = null;
            var workList = new DkmWorkList();

            _resultProvider.GetResult(
                value,
                workList,
                declaredType: declaredType ?? value.Type,
                customTypeInfo: new DynamicFlagsCustomTypeInfo(declaredTypeInfo == null ? null : new BitArray(declaredTypeInfo)).GetCustomTypeInfo(),
                inspectionContext: inspectionContext ?? DefaultInspectionContext,
                formatSpecifiers: Formatter.NoFormatSpecifiers,
                resultName: name,
                resultFullName: null,
                completionRoutine: asyncResult => evaluationResult = asyncResult.Result);
            workList.Execute();
            return(evaluationResult);
        }
        private int QueryColumnCount()
        {
            int columnCount            = 0;
            DkmLanguageExpression expr = null;

            try
            {
                DkmWorkList workList         = DkmWorkList.Create(null);
                SqliteVisualizerException ex = null;
                expr = this.AddFuncEval(
                    workList,
                    $"sqlite3_column_count(*(sqlite3_stmt **){this.procMemForQuery})",
                    (r) =>
                {
                    DkmSuccessEvaluationResult suc;
                    if (!this.VerifySuccess(r, out suc))
                    {
                        ex = new SqliteVisualizerException(Resources.ErrMsg_FuncEvalFailed, r.FullName);
                        return;
                    }

                    columnCount = (int)suc.Address.Value;
                });

                workList.Execute();

                if (ex != null)
                {
                    throw ex;
                }
            }
            finally
            {
                if (expr != null)
                {
                    expr.Close();
                }
            }

            return(columnCount);
        }
        private void FinalizeQuery()
        {
            DkmLanguageExpression expr = null;

            try
            {
                SqliteVisualizerException ex = null;
                DkmWorkList workList         = DkmWorkList.Create(null);
                expr = this.AddFuncEval(
                    workList,
                    $"sqlite3_finalize(*(sqlite3_stmt **){this.procMemForQuery})",
                    (r) =>
                {
                    DkmSuccessEvaluationResult suc;
                    if (!this.VerifySuccess(r, out suc))
                    {
                        ex = new SqliteVisualizerException(Resources.ErrMsg_FuncEvalFailed, r.FullName);
                        return;
                    }
                });

                workList.Execute();

                if (ex != null)
                {
                    throw ex;
                }
            }
            finally
            {
                if (expr != null)
                {
                    expr.Close();
                }
            }
        }
        private void PrepareQuery()
        {
            DkmLanguageExpression expr = null;

            try
            {
                SqliteVisualizerException ex = null;
                DkmWorkList workList         = DkmWorkList.Create(null);
                expr = this.AddFuncEval(
                    workList,
                    $"sqlite3_prepare({sqliteInstanceName}, \"{query}\", {query.Length + 1}, (sqlite3_stmt **){this.procMemForQuery}, nullptr)",
                    (r) =>
                {
                    DkmSuccessEvaluationResult suc;
                    if (!this.VerifySuccess(r, out suc))
                    {
                        ex = new SqliteVisualizerException(Resources.ErrMsg_PrepareFailed, r.FullName);
                        return;
                    }
                });

                workList.Execute();

                if (ex != null)
                {
                    throw ex;
                }
            }
            finally
            {
                if (expr != null)
                {
                    expr.Close();
                }
            }
        }
Beispiel #10
0
        public Tuple <ulong, ulong, ulong>[] GetThreadStackTrace(uint threadId, byte[] threadContextBytes)
        {
            return(ExecuteOnDkmInitializedThread(() =>
            {
                DkmThread thread = GetThread(threadId);
                List <DkmStackFrame> frames = new List <DkmStackFrame>();
                DkmProcess process = thread.Process;

                using (DkmInspectionSession dkmInspectionSession = DkmInspectionSession.Create(process, null))
                {
                    using (DkmStackContext dkmStackContext = DkmStackContext.Create(dkmInspectionSession, thread, DkmCallStackFilterOptions.None, new DkmFrameFormatOptions(), new System.Collections.ObjectModel.ReadOnlyCollection <byte>(threadContextBytes), null))
                    {
                        bool done = false;

                        while (!done)
                        {
                            DkmWorkList dkmWorkList = DkmWorkList.Create(null);

                            dkmStackContext.GetNextFrames(dkmWorkList, int.MaxValue, (ar) =>
                            {
                                frames.AddRange(ar.Frames);
                                done = ar.Frames.Length == 0;
                            });

                            dkmWorkList.Execute();
                        }
                    }
                }

                threads[(int)threadId].Frames.Value = frames.ToArray();

                Tuple <ulong, ulong, ulong>[] result = new Tuple <ulong, ulong, ulong> [frames.Count];

                for (int i = 0; i < result.Length; i++)
                {
                    ulong stackOffset, instructionOffset;

                    switch (frames[i].Registers.TagValue)
                    {
                    case DkmFrameRegisters.Tag.X64Registers:
                        {
                            DkmX64FrameRegisters registers = (DkmX64FrameRegisters)frames[i].Registers;

                            instructionOffset = registers.Rip;
                            stackOffset = registers.Rsp;
                        }
                        break;

                    case DkmFrameRegisters.Tag.X86Registers:
                        {
                            DkmX86FrameRegisters registers = (DkmX86FrameRegisters)frames[i].Registers;

                            instructionOffset = registers.Eip;
                            stackOffset = registers.Esp;
                        }
                        break;

                    default:
                        throw new NotImplementedException("Unexpected DkmFrameRegisters.Tag");
                    }

                    bool found = false;
                    ulong frameOffset = 0;

                    for (int j = 0; !found && j < frames[i].Registers.UnwoundRegisters.Count; j++)
                    {
                        switch ((Dia2Lib.CV_HREG_e)frames[i].Registers.UnwoundRegisters[j].Identifier)
                        {
                        case Dia2Lib.CV_HREG_e.CV_AMD64_EBP:
                        case Dia2Lib.CV_HREG_e.CV_AMD64_RBP:
                            {
                                byte[] bytes = frames[i].Registers.UnwoundRegisters[j].Value.ToArray();

                                found = true;
                                frameOffset = bytes.Length == 8 ? BitConverter.ToUInt64(bytes, 0) : BitConverter.ToUInt32(bytes, 0);
                                break;
                            }
                        }
                    }

                    if (instructionOffset != frames[i].InstructionAddress.CPUInstructionPart.InstructionPointer)
                    {
                        throw new Exception("Instruction offset is not the same?");
                    }

                    result[i] = Tuple.Create(instructionOffset, stackOffset, frameOffset);
                }

                return result;
            }));
        }
        private bool TryGetRow(out string[] row)
        {
            // Move to the next row
            bool moreRows = false;
            var  exprs    = new List <DkmLanguageExpression>();

            try
            {
                const int SQLITE_ROW           = 100;
                SqliteVisualizerException ex   = null;
                DkmWorkList           workList = DkmWorkList.Create(null);
                DkmLanguageExpression expr     = this.AddFuncEval(
                    workList,
                    $"sqlite3_step(*(sqlite3_stmt **){this.procMemForQuery})",
                    (r) =>
                {
                    DkmSuccessEvaluationResult suc;
                    if (!this.VerifySuccess(r, out suc))
                    {
                        ex = new SqliteVisualizerException(Resources.ErrMsg_FuncEvalFailed, r.FullName);
                        return;
                    }

                    moreRows = SQLITE_ROW == (int)suc.Address.Value;
                });

                exprs.Add(expr);
                workList.Execute();

                if (ex != null)
                {
                    throw ex;
                }
            }
            finally
            {
                foreach (var e in exprs)
                {
                    e.Close();
                }

                exprs.Clear();
            }

            if (!moreRows)
            {
                row = new string[0];
                return(false);
            }

            // Read each column in the row
            var rowLocal = new string[ColumnNames.Count()];

            try
            {
                SqliteVisualizerException ex = null;
                DkmProcess  process          = this.inspectionContext.Thread.Process;
                DkmWorkList workList         = DkmWorkList.Create(null);
                for (int i = 0; i < rowLocal.Length; i++)
                {
                    var i_local = i;
                    var e       = this.AddFuncEval(
                        workList,
                        $"sqlite3_column_text(*(sqlite3_stmt **){this.procMemForQuery}, {i})",
                        (r) =>
                    {
                        DkmSuccessEvaluationResult suc;
                        if (!this.VerifySuccess(r, out suc))
                        {
                            ex = ex ?? new SqliteVisualizerException(Resources.ErrMsg_FuncEvalFailed, r.FullName);
                            return;
                        }

                        ulong address      = suc.Address.Value;
                        byte[] stringMaybe = process.ReadMemoryString(address, DkmReadMemoryFlags.None, 1, 1024);
                        int len            = stringMaybe.Length;
                        if (len > 0)
                        {
                            // The debugger null terminates all strings, but encoding doesn't strip null when creating a string
                            len--;
                            rowLocal[i_local] = Encoding.UTF8.GetString(stringMaybe, 0, len);
                        }
                    });

                    exprs.Add(e);
                }

                workList.Execute();

                if (ex != null)
                {
                    throw ex;
                }
            }
            finally
            {
                foreach (var e in exprs)
                {
                    e.Close();
                }
            }

            row = rowLocal;
            return(true);
        }