File indexing completed on 2024-04-14 15:32:43

0001 /******************************************************************
0002  *
0003  * kdbgwin - Helper application for DrKonqi
0004  *
0005  * This file is part of the KDE project
0006  *
0007  * SPDX-FileCopyrightText: 2010 Ilie Halip <lupuroshu@gmail.com>
0008  *
0009  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0010  *****************************************************************/
0011 
0012 #pragma once
0013 
0014 #include "common.h"
0015 
0016 #include <QMap>
0017 #include <QString>
0018 
0019 typedef QMap<DWORD, HANDLE> TThreadsMap;
0020 typedef QMap<QString, HMODULE> TModulesMap;
0021 
0022 /**
0023  * \brief Describes a process.
0024  *
0025  * This is a helper class when dealing with another process. When kdbgwin starts,
0026  * it attaches to the crashing process and tries to retrieve useful information:
0027  * pid, threads, modules, image path. These will be used later.
0028  */
0029 class Process
0030 {
0031 private:
0032     /// Flag to check if the information about this process is valid and can be used
0033     BOOL m_bValid;
0034 
0035     /// Process ID
0036     DWORD m_dwPid;
0037 
0038     /// Failing thread ID - I need this because for the crashing thread, I need to get
0039     /// the CONTEXT from a piece of shared memory in KCrash
0040     DWORD m_dwThread;
0041 
0042     /// A handle to the process
0043     HANDLE m_hProcess;
0044 
0045     /// A QMap<DWORD, HANDLE> which associates thread IDs with opened handles for each
0046     /// of them
0047     TThreadsMap m_threads;
0048 
0049     /// The full path to the executable file which started this process
0050     QString m_path;
0051 
0052     /// A QMap<QString, HMODULE> which contains the paths to the loaded modules and
0053     /// handles to each of them
0054     TModulesMap m_modules;
0055 
0056 public:
0057     Process();
0058 
0059 public:
0060     /// kdbgwin needs to enable the debug privilege in order to read from
0061     /// another process's memory.
0062     static BOOL EnableDebugPrivilege();
0063 
0064 public:
0065     /// Attaches to the process and gets all required information
0066     /// @return TRUE if operation succeeds
0067     BOOL GetInfo(const char *pid, const char *threadId);
0068 
0069     /// Checks if the information is valid
0070     BOOL IsValid() const
0071     {
0072         assert(m_bValid);
0073         return m_bValid;
0074     }
0075 
0076     /// Get the process ID
0077     DWORD GetId() const
0078     {
0079         assert(m_dwPid);
0080         return m_dwPid;
0081     }
0082 
0083     /// Returns an open handle to the process (opened with PROCESS_ALL_ACCESS)
0084     HANDLE GetHandle() const
0085     {
0086         assert(m_hProcess);
0087         return m_hProcess;
0088     }
0089 
0090     /// Returns the thread ID of the thread that caused the exception
0091     DWORD GetThreadId() const
0092     {
0093         assert(m_dwThread);
0094         return m_dwThread;
0095     }
0096 
0097     /// Returns the threads map
0098     const TThreadsMap &GetThreads() const
0099     {
0100         return m_threads;
0101     }
0102 
0103     /// Returns the full path to the executable on the disk
0104     const QString &GetPath() const
0105     {
0106         return m_path;
0107     }
0108 
0109     /// Returns a map of all the loaded modules
0110     const TModulesMap &GetModules() const
0111     {
0112         return m_modules;
0113     }
0114 };