xref: /reactos/drivers/base/kdgdb/utils.c (revision 34593d93)
1 /*
2  * COPYRIGHT:       GPL, see COPYING in the top level directory
3  * PROJECT:         ReactOS kernel
4  * FILE:            drivers/base/kddll/utils.c
5  * PURPOSE:         Misc helper functions.
6  */
7 
8 #include "kdgdb.h"
9 
10 /*
11  * We cannot use PsLookupProcessThreadByCid or alike as we could be running at any IRQL.
12  * So we have to loop over the process list.
13  */
14 
15 PEPROCESS
find_process(_In_ UINT_PTR Pid)16 find_process(
17     _In_ UINT_PTR Pid)
18 {
19     HANDLE ProcessId = gdb_pid_to_handle(Pid);
20     LIST_ENTRY* ProcessEntry;
21     PEPROCESS Process;
22 
23     /* Special case for idle process */
24     if (ProcessId == NULL)
25         return TheIdleProcess;
26 
27     for (ProcessEntry = ProcessListHead->Flink;
28             ProcessEntry != ProcessListHead;
29             ProcessEntry = ProcessEntry->Flink)
30     {
31         Process = CONTAINING_RECORD(ProcessEntry, EPROCESS, ActiveProcessLinks);
32 
33         if (Process->UniqueProcessId == ProcessId)
34             return Process;
35     }
36 
37     return NULL;
38 }
39 
40 PETHREAD
find_thread(_In_ UINT_PTR Pid,_In_ UINT_PTR Tid)41 find_thread(
42     _In_ UINT_PTR Pid,
43     _In_ UINT_PTR Tid)
44 {
45     HANDLE ThreadId = gdb_tid_to_handle(Tid);
46     PETHREAD Thread;
47     PEPROCESS Process;
48     LIST_ENTRY* ThreadEntry;
49 #if MONOPROCESS
50     LIST_ENTRY* ProcessEntry;
51 #endif
52 
53     if (
54 #if !MONOPROCESS
55     (Pid == 0) &&
56 #endif
57     (Tid == 0))
58     {
59         /* Zero means any, so use the current one */
60         return (PETHREAD)(ULONG_PTR)CurrentStateChange.Thread;
61     }
62 
63 #if MONOPROCESS
64 
65     /* Special case for the idle thread */
66     if (Tid == 1)
67         return TheIdleThread;
68 
69     for (ProcessEntry = ProcessListHead->Flink;
70         ProcessEntry != ProcessListHead;
71         ProcessEntry = ProcessEntry->Flink)
72     {
73         Process = CONTAINING_RECORD(ProcessEntry, EPROCESS, ActiveProcessLinks);
74 #else
75 
76     Process = find_process(Pid);
77 
78     /* Special case for the idle thread */
79     if ((Process == TheIdleProcess) && (Tid == 1))
80         return TheIdleThread;
81 
82     if (!Process)
83         return NULL;
84 
85 #endif
86 
87     for (ThreadEntry = Process->ThreadListHead.Flink;
88             ThreadEntry != &Process->ThreadListHead;
89             ThreadEntry = ThreadEntry->Flink)
90     {
91         Thread = CONTAINING_RECORD(ThreadEntry, ETHREAD, ThreadListEntry);
92         /* For GDB, Tid == 0 means any thread */
93         if ((Thread->Cid.UniqueThread == ThreadId) || (Tid == 0))
94         {
95             return Thread;
96         }
97     }
98 
99 #if MONOPROCESS
100     }
101 #endif
102 
103     return NULL;
104 }
105