2011-07-04 19:52:53 +00:00
|
|
|
unit ProcessHandlerUnit;
|
|
|
|
|
|
|
|
|
|
{$MODE Delphi}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Will handle all process specific stuff like openening and closing a process
|
|
|
|
|
The ProcessHandler variable will be in cefuncproc, but a tabswitch to another
|
|
|
|
|
process will set it to the different tab's process
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
2020-03-24 11:32:08 +01:00
|
|
|
uses
|
|
|
|
|
{$ifdef darwin}
|
|
|
|
|
macport,
|
|
|
|
|
{$endif}
|
2020-06-04 23:11:49 +02:00
|
|
|
{$ifdef windows}
|
|
|
|
|
windows,
|
|
|
|
|
{$endif}
|
2020-03-24 11:32:08 +01:00
|
|
|
{$ifndef jni}LCLIntf, {$endif}
|
|
|
|
|
newkernelhandler, classes, sysutils;
|
2011-07-04 19:52:53 +00:00
|
|
|
|
2013-07-23 17:35:12 +00:00
|
|
|
type
|
|
|
|
|
TSystemArchitecture=(archX86=0, archArm=1);
|
2021-11-27 12:27:21 +01:00
|
|
|
TOperatingsystemABI=(abiWindows=0, abiSystemV=1);
|
2013-07-23 17:35:12 +00:00
|
|
|
|
2011-07-04 19:52:53 +00:00
|
|
|
type TProcessHandler=class
|
|
|
|
|
private
|
|
|
|
|
fis64bit: boolean;
|
2023-08-30 16:15:12 +02:00
|
|
|
fIsAndroid: boolean;
|
2011-07-04 19:52:53 +00:00
|
|
|
fprocesshandle: THandle;
|
|
|
|
|
fpointersize: integer;
|
2013-07-23 17:35:12 +00:00
|
|
|
fSystemArchitecture: TSystemArchitecture;
|
2021-11-27 12:27:21 +01:00
|
|
|
fOSABI: TOperatingsystemABI; //for c-code
|
2021-06-28 20:06:24 +02:00
|
|
|
fHexDigitPreference: integer;
|
2011-07-04 19:52:53 +00:00
|
|
|
procedure setIs64bit(state: boolean);
|
|
|
|
|
procedure setProcessHandle(processhandle: THandle);
|
|
|
|
|
public
|
|
|
|
|
processid: dword;
|
|
|
|
|
|
2013-08-28 22:33:40 +00:00
|
|
|
|
2011-07-04 19:52:53 +00:00
|
|
|
procedure Open;
|
2013-08-28 22:33:40 +00:00
|
|
|
function isNetwork: boolean; //perhaps name it isLinux ?
|
2015-10-28 18:17:52 +01:00
|
|
|
procedure overridePointerSize(newsize: integer);
|
2022-04-19 00:08:51 +02:00
|
|
|
|
2017-01-13 23:48:40 +01:00
|
|
|
property is64Bit: boolean read fIs64Bit write setIs64bit;
|
2023-08-30 16:15:12 +02:00
|
|
|
property isAndroid: boolean read fIsAndroid;
|
2011-07-04 19:52:53 +00:00
|
|
|
property pointersize: integer read fPointersize;
|
|
|
|
|
property processhandle: THandle read fProcessHandle write setProcessHandle;
|
2022-04-19 00:08:51 +02:00
|
|
|
property SystemArchitecture: TSystemArchitecture read fSystemArchitecture write fSystemArchitecture;
|
2021-11-27 12:27:21 +01:00
|
|
|
property OSABI: TOperatingsystemABI read fOSABI;
|
2021-06-28 20:06:24 +02:00
|
|
|
property hexdigitpreference: integer read fHexDigitPreference;
|
2011-07-04 19:52:53 +00:00
|
|
|
end;
|
|
|
|
|
|
2014-09-08 12:00:14 +00:00
|
|
|
|
|
|
|
|
var processhandler: TProcessHandler;
|
|
|
|
|
|
|
|
|
|
function processhandle: THandle; inline;
|
|
|
|
|
function processid: dword; inline;
|
|
|
|
|
|
|
|
|
|
|
2011-07-04 19:52:53 +00:00
|
|
|
implementation
|
|
|
|
|
|
2014-09-08 12:00:14 +00:00
|
|
|
{$ifdef jni}
|
|
|
|
|
uses networkinterface, networkInterfaceApi;
|
|
|
|
|
{$else}
|
2023-11-13 01:47:23 +01:00
|
|
|
uses LuaHandler, mainunit, networkInterface, networkInterfaceApi, ProcessList, lua,
|
|
|
|
|
FileUtil, globals;
|
2014-09-08 12:00:14 +00:00
|
|
|
{$endif}
|
2011-07-04 19:52:53 +00:00
|
|
|
|
2015-10-28 18:17:52 +01:00
|
|
|
procedure TProcessHandler.overridePointerSize(newsize: integer);
|
|
|
|
|
begin
|
|
|
|
|
fpointersize:=newsize;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-08-28 22:33:40 +00:00
|
|
|
function TProcessHandler.isNetwork: boolean;
|
|
|
|
|
begin
|
|
|
|
|
result:=(((processhandle shr 24) and $ff)=$ce) and (getConnection<>nil);
|
|
|
|
|
end;
|
|
|
|
|
|
2011-07-04 19:52:53 +00:00
|
|
|
procedure TProcessHandler.setIs64bit(state: boolean);
|
|
|
|
|
begin
|
|
|
|
|
fis64bit:=state;
|
|
|
|
|
if state then
|
2021-06-28 20:06:24 +02:00
|
|
|
fpointersize:=8
|
2011-07-04 19:52:53 +00:00
|
|
|
else
|
|
|
|
|
fpointersize:=4;
|
2021-06-28 20:06:24 +02:00
|
|
|
|
|
|
|
|
fhexdigitpreference:=fpointersize*2;
|
2011-07-04 19:52:53 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TProcessHandler.setProcessHandle(processhandle: THandle);
|
2019-12-18 12:07:17 +01:00
|
|
|
var
|
|
|
|
|
c: TCEConnection;
|
2013-07-23 17:35:12 +00:00
|
|
|
arch: integer;
|
2021-11-27 12:27:21 +01:00
|
|
|
abi: integer;
|
2011-07-04 19:52:53 +00:00
|
|
|
begin
|
2022-07-03 13:31:15 +02:00
|
|
|
//outputdebugstring('TProcessHandler.setProcessHandle');
|
2020-06-04 11:16:13 +02:00
|
|
|
if (fprocesshandle<>0) and (fprocesshandle<>getcurrentprocess) and (processhandle<>getcurrentprocess) then
|
2020-02-24 15:20:33 +01:00
|
|
|
begin
|
|
|
|
|
try
|
|
|
|
|
closehandle(fprocesshandle);
|
|
|
|
|
except //debugger issue
|
|
|
|
|
end;
|
|
|
|
|
fprocesshandle:=0;
|
|
|
|
|
end;
|
|
|
|
|
|
2011-07-04 19:52:53 +00:00
|
|
|
fprocesshandle:=processhandle;
|
2022-09-27 16:31:24 +02:00
|
|
|
|
2013-07-23 17:35:12 +00:00
|
|
|
c:=getConnection;
|
|
|
|
|
if c<>nil then
|
|
|
|
|
begin
|
2023-08-30 16:15:12 +02:00
|
|
|
fIsAndroid:=c.isAndroid;
|
2022-04-19 00:08:51 +02:00
|
|
|
arch:=c.getArchitecture(fprocesshandle);
|
2013-07-23 17:35:12 +00:00
|
|
|
case arch of
|
|
|
|
|
0: //i386
|
|
|
|
|
begin
|
|
|
|
|
fSystemArchitecture:=archX86;
|
2013-09-07 11:54:15 +00:00
|
|
|
setIs64Bit(false);
|
2013-07-23 17:35:12 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
1: //x86_64
|
|
|
|
|
begin
|
|
|
|
|
fSystemArchitecture:=archX86;
|
2013-09-07 11:54:15 +00:00
|
|
|
setIs64Bit(true);
|
2013-07-23 17:35:12 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
2: //arm
|
|
|
|
|
begin
|
|
|
|
|
fSystemArchitecture:=archArm;
|
2013-09-07 11:54:15 +00:00
|
|
|
setIs64Bit(false);
|
2013-07-23 17:35:12 +00:00
|
|
|
end;
|
|
|
|
|
|
2022-04-19 00:08:51 +02:00
|
|
|
3: //arm64
|
2013-07-23 17:35:12 +00:00
|
|
|
begin
|
|
|
|
|
fSystemArchitecture:=archArm;
|
2013-09-07 11:54:15 +00:00
|
|
|
setIs64Bit(true);
|
2013-07-23 17:35:12 +00:00
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2021-11-27 12:27:21 +01:00
|
|
|
abi:=c.GetABI;
|
|
|
|
|
case abi of
|
|
|
|
|
0: fOSABI:=abiWindows;
|
|
|
|
|
1: fOSABI:=abiSystemV;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-07-23 17:35:12 +00:00
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
2023-08-30 16:15:12 +02:00
|
|
|
fIsAndroid:=false;
|
2022-07-03 13:31:15 +02:00
|
|
|
//outputdebugstring('setProcessHandle not windows');
|
2022-06-01 13:10:34 +02:00
|
|
|
|
|
|
|
|
{$ifdef darwin}
|
2023-11-22 16:59:38 +01:00
|
|
|
outputdebugstring('setProcessHandle');
|
2022-06-01 13:10:34 +02:00
|
|
|
if MacIsArm64 then //rosetta2 or I finally ported it to full armv8
|
2023-10-22 22:32:34 +02:00
|
|
|
begin
|
2023-10-26 16:09:29 +02:00
|
|
|
if isProcessTranslated(processid) then
|
2023-11-13 01:47:23 +01:00
|
|
|
begin
|
2023-11-22 16:59:38 +01:00
|
|
|
outputdebugstring('rosetta. So x86');
|
2023-11-13 01:47:23 +01:00
|
|
|
fSystemArchitecture:=archX86;
|
|
|
|
|
globals.SystemSupportsWritableExecutableMemory:=true;
|
|
|
|
|
end
|
2023-10-22 22:32:34 +02:00
|
|
|
else
|
2023-11-22 16:59:38 +01:00
|
|
|
begin
|
|
|
|
|
outputdebugstring('not rosetta. So pure ARM');
|
2023-10-22 22:32:34 +02:00
|
|
|
fSystemArchitecture:=archArm;
|
2023-11-22 16:59:38 +01:00
|
|
|
globals.SystemSupportsWritableExecutableMemory:=false;
|
|
|
|
|
end;
|
2023-10-22 22:32:34 +02:00
|
|
|
end;
|
2022-06-01 13:10:34 +02:00
|
|
|
{$else}
|
2013-07-23 17:35:12 +00:00
|
|
|
fSystemArchitecture:=archX86;
|
2022-06-01 13:10:34 +02:00
|
|
|
{$endif}
|
2021-11-27 12:27:21 +01:00
|
|
|
{$ifdef windows}
|
|
|
|
|
fOSABI:=abiWindows;
|
|
|
|
|
{$else}
|
|
|
|
|
fOSABI:=abiSystemV;
|
|
|
|
|
{$endif}
|
2017-01-13 01:31:36 +01:00
|
|
|
|
2019-12-18 12:07:17 +01:00
|
|
|
setIs64Bit(newkernelhandler.Is64BitProcess(fProcessHandle));
|
2013-07-23 17:35:12 +00:00
|
|
|
end;
|
2011-07-04 19:52:53 +00:00
|
|
|
|
2013-09-07 11:54:15 +00:00
|
|
|
{$ifdef ARMTEST}
|
|
|
|
|
fSystemArchitecture:=archArm;
|
2021-11-27 12:27:21 +01:00
|
|
|
fOSABI:=abiSystemV;
|
2013-09-07 11:54:15 +00:00
|
|
|
setIs64Bit(false);
|
|
|
|
|
{$endif}
|
|
|
|
|
|
2014-06-07 01:37:50 +00:00
|
|
|
if processhandle<>0 then
|
2015-07-13 00:35:42 +02:00
|
|
|
begin
|
2022-06-01 13:10:34 +02:00
|
|
|
outputdebugstring('calling open');
|
2014-06-07 01:37:50 +00:00
|
|
|
open;
|
2015-07-13 00:35:42 +02:00
|
|
|
end;
|
|
|
|
|
|
2011-07-04 19:52:53 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TProcessHandler.Open;
|
2015-12-20 22:11:08 +01:00
|
|
|
var mn: string;
|
2011-07-04 19:52:53 +00:00
|
|
|
begin
|
2022-10-23 10:39:22 +02:00
|
|
|
// outputdebugstring('TProcessHandler.Open');
|
2015-12-20 22:11:08 +01:00
|
|
|
//GetFirstModuleNa
|
2014-09-08 12:00:14 +00:00
|
|
|
{$ifndef jni}
|
2020-03-28 15:09:12 +01:00
|
|
|
|
2015-12-20 22:11:08 +01:00
|
|
|
if processid<>0 then
|
|
|
|
|
begin
|
|
|
|
|
mn:=GetFirstModuleName(processid);
|
2020-03-28 15:09:12 +01:00
|
|
|
|
2015-12-20 22:11:08 +01:00
|
|
|
lua_pushstring(luavm, pchar(extractfilename(mn)));
|
|
|
|
|
lua_setglobal(luavm, 'process');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
2014-01-02 13:20:59 +00:00
|
|
|
LUA_functioncall('onOpenProcess', [ptruint(processid)]); //todo: Change to a callback array/list
|
2014-09-08 12:00:14 +00:00
|
|
|
{$endif}
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function processhandle: THandle; inline;
|
|
|
|
|
begin
|
|
|
|
|
result:=processhandler.processhandle;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function processid: dword; inline;
|
|
|
|
|
begin
|
|
|
|
|
result:=processhandler.processid;
|
2011-07-04 19:52:53 +00:00
|
|
|
end;
|
|
|
|
|
|
2014-09-08 12:00:14 +00:00
|
|
|
initialization
|
|
|
|
|
processhandler:=TProcessHandler.create;
|
|
|
|
|
|
2011-07-04 19:52:53 +00:00
|
|
|
end.
|
|
|
|
|
|
2020-03-24 11:32:08 +01:00
|
|
|
|