Skip to content

Commit 8cfd5d6

Browse files
author
Jonathan Beaudoin
committed
Removed the requirement for a handle in all processes (only needed in windows)
1 parent 8b63e77 commit 8cfd5d6

3 files changed

Lines changed: 27 additions & 43 deletions

File tree

src/main/java/com/beaudoin/jmm/process/NativeProcess.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
import com.beaudoin.jmm.misc.Cacheable;
44
import com.beaudoin.jmm.misc.MemoryBuffer;
55
import com.beaudoin.jmm.misc.Strings;
6+
import com.beaudoin.jmm.misc.Utils;
7+
import com.beaudoin.jmm.natives.mac.mac;
68
import com.beaudoin.jmm.natives.win32.Kernel32;
9+
import com.beaudoin.jmm.process.impl.mac.MacProcess;
710
import 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;
912
import com.sun.jna.Native;
1013
import com.sun.jna.Platform;
1114
import com.sun.jna.Pointer;
1215
import 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

1718
import 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
}

src/main/java/com/beaudoin/jmm/process/impl/unix/UnixProcess.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ public final class UnixProcess implements NativeProcess {
2222
private uio.iovec remote = new uio.iovec();
2323

2424
private final int id;
25-
private final Pointer handle;
2625
private Map<String, Module> modules = new HashMap<>();
2726

28-
public UnixProcess(int id, Pointer handle) {
27+
public UnixProcess(int id) {
2928
this.id = id;
30-
this.handle = handle;
3129
initModules();
3230
}
3331

@@ -36,11 +34,6 @@ public int id() {
3634
return id;
3735
}
3836

39-
@Override
40-
public Pointer pointer() {
41-
return handle;
42-
}
43-
4437
@Override
4538
public void initModules() {
4639
try {

src/main/java/com/beaudoin/jmm/process/impl/win32/Wind32Process.java renamed to src/main/java/com/beaudoin/jmm/process/impl/win32/Win32Process.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,30 @@
1010
import com.sun.jna.Pointer;
1111
import com.sun.jna.platform.win32.Win32Exception;
1212

13-
import java.util.HashMap;
1413
import java.util.Map;
1514

1615
/**
1716
* Created by Jonathan on 11/13/2015.
1817
*/
19-
public final class Wind32Process implements NativeProcess {
18+
public final class Win32Process implements NativeProcess {
2019

2120
private final int id;
2221
private final Pointer handle;
2322
private Map<String, Module> modules;
2423

25-
public Wind32Process(int id, Pointer handle) {
24+
public Win32Process(int id, Pointer handle) {
2625
this.id = id;
2726
this.handle = handle;
2827
initModules();
2928
}
3029

31-
@Override
32-
public int id() {
33-
return id;
30+
public Pointer pointer() {
31+
return handle;
3432
}
3533

3634
@Override
37-
public Pointer pointer() {
38-
return handle;
35+
public int id() {
36+
return id;
3937
}
4038

4139
@Override

0 commit comments

Comments
 (0)