33import com .beaudoin .jmm .misc .Cacheable ;
44import com .beaudoin .jmm .misc .MemoryBuffer ;
55import com .beaudoin .jmm .misc .Strings ;
6+ import com .beaudoin .jmm .misc .Utils ;
7+ import com .beaudoin .jmm .natives .mac .mac ;
68import com .beaudoin .jmm .natives .win32 .Kernel32 ;
9+ import com .beaudoin .jmm .process .impl .mac .MacProcess ;
710import com .beaudoin .jmm .process .impl .unix .UnixProcess ;
8- import com .beaudoin .jmm .process .impl .win32 .Wind32Process ;
11+ import com .beaudoin .jmm .process .impl .win32 .Win32Process ;
912import com .sun .jna .Native ;
1013import com .sun .jna .Platform ;
1114import com .sun .jna .Pointer ;
1215import com .sun .jna .platform .win32 .Tlhelp32 ;
13-
14- import java .io .IOException ;
15- import java .util .Scanner ;
16+ import com .sun .jna .ptr .IntByReference ;
1617
1718import static com .beaudoin .jmm .misc .Cacheable .buffer ;
1819
@@ -35,17 +36,8 @@ static NativeProcess byName(String name) {
3536 } finally {
3637 Kernel32 .CloseHandle (snapshot );
3738 }
38- } else if (Platform .isMac ()) {
39- throw new UnsupportedOperationException ("Unknown mac system! (" + System .getProperty ("os.name" ) + ")" );
40- //MAC
41- } else if (Platform .isLinux ()) {
42- try {
43- int processid = Integer .parseInt (new Scanner (Runtime .getRuntime ().exec ("ps -C " +name +" -o pid" ).getInputStream ()).useDelimiter ("\\ A" ).next ().replaceAll ("[^0-9]" ,"" ));
44- return byId (processid );
45- } catch (Exception e ) {
46- e .printStackTrace ();
47- //throw new IllegalStateException("Process " + name + " was not found. Are you sure its running?");
48- }
39+ } else if (Platform .isMac () || Platform .isLinux ()) {
40+ return byId (Utils .exec ("bash" , "-c" , "ps -A | grep -m1 \" clion\" | awk '{print $1}'" ));
4941 } else {
5042 throw new UnsupportedOperationException ("Unknown operating system! (" + System .getProperty ("os.name" ) + ")" );
5143 }
@@ -54,21 +46,22 @@ static NativeProcess byName(String name) {
5446
5547 static NativeProcess byId (int id ) {
5648 if (Platform .isWindows ()) {
57- return new Wind32Process (id , Kernel32 .OpenProcess (0x438 , true , id ));
49+ return new Win32Process (id , Kernel32 .OpenProcess (0x438 , true , id ));
5850 } else if (Platform .isMac ()) {
59- throw new UnsupportedOperationException ("Unknown mac system! (" + System .getProperty ("os.name" ) + ")" );
60- //MAC
51+ IntByReference out = new IntByReference ();
52+ if (mac .task_for_pid (mac .mach_task_self (), id , out ) != 0 ) {
53+ throw new IllegalStateException ("Failed to find mach task port for process, ensure you are running as sudo." );
54+ }
55+ return new MacProcess (id , out .getValue ());
6156 } else if (Platform .isLinux ()) {
62- return new UnixProcess (id , null );
57+ return new UnixProcess (id );
6358 } else {
6459 throw new IllegalStateException ("Process " + id + " was not found. Are you sure its running?" );
6560 }
6661 }
6762
6863 int id ();
6964
70- Pointer pointer ();
71-
7265 void initModules ();
7366
7467 Module findModule (String moduleName );
@@ -116,27 +109,27 @@ default MemoryBuffer read(long address, int size) {
116109 default NativeProcess writeBoolean (long address , boolean value ) {
117110 return write (Cacheable .pointer (address ), buffer (1 ).putBoolean (value ));
118111 }
119-
112+
120113 default NativeProcess writeByte (long address , int value ) {
121114 return write (Cacheable .pointer (address ), buffer (1 ).putByte (value ));
122115 }
123-
116+
124117 default NativeProcess writeShort (long address , int value ) {
125118 return write (Cacheable .pointer (address ), buffer (2 ).putShort (value ));
126119 }
127-
120+
128121 default NativeProcess writeInt (long address , int value ) {
129122 return write (Cacheable .pointer (address ), buffer (4 ).putInt (value ));
130123 }
131-
124+
132125 default NativeProcess writeLong (long address , long value ) {
133126 return write (Cacheable .pointer (address ), buffer (8 ).putLong (value ));
134127 }
135-
128+
136129 default NativeProcess writeFloat (long address , float value ) {
137130 return write (Cacheable .pointer (address ), buffer (4 ).putFloat (value ));
138131 }
139-
132+
140133 default NativeProcess writeDouble (long address , double value ) {
141134 return write (Cacheable .pointer (address ), buffer (8 ).putDouble (value ));
142135 }
0 commit comments