-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e92fba
commit ac7d013
Showing
10 changed files
with
392 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
unit TdSnapshot; | ||
|
||
interface | ||
|
||
uses | ||
Winapi.WinNt; | ||
|
||
type | ||
TThreadFlag = (tfGUI); | ||
TThreadFlags = set of TThreadFlag; | ||
|
||
TThreadData = record | ||
TID: TThreadID; | ||
Flags: TThreadFlags; | ||
end; | ||
|
||
// Find all threads that belong to this process | ||
function SnapshotThreads(PID: TProcessId): TArray<TThreadData>; | ||
|
||
implementation | ||
|
||
uses | ||
Ntapi.ntdef, Ntapi.ntpsapi, Ntapi.ntobapi, Ntapi.ntstatus, | ||
NtUtils.Objects, NtUtils.WinUser; | ||
|
||
function SnapshotThreads(PID: TProcessId): TArray<TThreadData>; | ||
var | ||
ProcessInfo, ThreadInfo: TObjectTypeInfo; | ||
i: Integer; | ||
CID: TClientId; | ||
hThread: THandle; | ||
ObjAttr: TObjectAttributes; | ||
Status: NTSTATUS; | ||
begin | ||
Result := nil; | ||
|
||
// Determine the total amount of processes and threads | ||
if not NtxQueryTypeObject(NtCurrentProcess, ProcessInfo).IsSuccess then | ||
ProcessInfo.Other.HighWaterNumberOfObjects := 300; | ||
|
||
// Fallback to reasonable limits on failure | ||
if not NtxQueryTypeObject(NtCurrentThread, ThreadInfo).IsSuccess then | ||
ThreadInfo.Other.HighWaterNumberOfObjects := 2000; | ||
|
||
InitializeObjectAttributes(ObjAttr); | ||
CID.UniqueProcess := PID; | ||
|
||
for i := 3 to ProcessInfo.Other.HighWaterNumberOfObjects + | ||
ThreadInfo.Other.HighWaterNumberOfObjects do | ||
begin | ||
// Thread IDs are always divisable by 4 | ||
CID.UniqueThread := i shl 2; | ||
|
||
// Thy this pair of PID + TID | ||
Status := NtOpenThread(hThread, 0, ObjAttr, CID); | ||
|
||
// NtOpenThread performs a lookup first, and since we supplied both | ||
// process and thread IDs, the status indicates whether the thread | ||
// belongs to the process or not. | ||
if Status <> STATUS_INVALID_CID then | ||
begin | ||
// Save it | ||
SetLength(Result, Succ(Length(Result))); | ||
Result[High(Result)].TID := CID.UniqueThread; | ||
end; | ||
|
||
if NT_SUCCESS(Status) then | ||
NtClose(hThread); | ||
end; | ||
|
||
// Mark all GUI threads | ||
for i := 0 to High(Result) do | ||
if UsrxIsGuiThread(Result[i].TID) then | ||
Include(Result[i].Flags, tfGUI); | ||
end; | ||
|
||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
object FormProcessInfo: TFormProcessInfo | ||
Left = 0 | ||
Top = 0 | ||
BorderIcons = [biSystemMenu, biMaximize] | ||
Caption = 'Process Information' | ||
ClientHeight = 305 | ||
ClientWidth = 288 | ||
Color = clBtnFace | ||
Font.Charset = DEFAULT_CHARSET | ||
Font.Color = clWindowText | ||
Font.Height = -11 | ||
Font.Name = 'Tahoma' | ||
Font.Style = [] | ||
KeyPreview = True | ||
OldCreateOrder = False | ||
OnClose = FormClose | ||
OnCreate = FormCreate | ||
OnKeyPress = FormKeyPress | ||
PixelsPerInch = 96 | ||
TextHeight = 13 | ||
object PageControl: TPageControl | ||
AlignWithMargins = True | ||
Left = 3 | ||
Top = 3 | ||
Width = 282 | ||
Height = 299 | ||
ActivePage = ThreadsTab | ||
Align = alClient | ||
TabOrder = 0 | ||
object ThreadsTab: TTabSheet | ||
Caption = 'Threads' | ||
object lvThreads: TListViewEx | ||
AlignWithMargins = True | ||
Left = 3 | ||
Top = 3 | ||
Width = 268 | ||
Height = 265 | ||
Align = alClient | ||
Columns = < | ||
item | ||
Caption = 'Thread ID' | ||
Width = 120 | ||
end | ||
item | ||
Caption = 'Flags' | ||
Width = 100 | ||
end> | ||
DoubleBuffered = True | ||
GridLines = True | ||
MultiSelect = True | ||
ReadOnly = True | ||
RowSelect = True | ||
ParentDoubleBuffered = False | ||
TabOrder = 0 | ||
ViewStyle = vsReport | ||
ColoringItems = True | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.