forked from mdbell/Noexes
/
Debugger.java
554 lines (497 loc) · 15.7 KB
/
Debugger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package me.mdbell.noexs.core;
import me.mdbell.noexs.misc.BreakpointFlagBuilder;
import me.mdbell.noexs.misc.BreakpointType;
import me.mdbell.noexs.misc.WatchpointFlagBuilder;
import me.mdbell.noexs.ui.NoexsApplication;
import me.mdbell.noexs.ui.models.DataType;
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.concurrent.Semaphore;
public class Debugger implements Commands, Closeable {
private IConnection conn;
private MemoryInfo prev;
private Semaphore semaphore = new Semaphore(1);
private int protocolVersion;
public static final int CURRENT_PROTOCOL_VERSION = (NoexsApplication.VERSION_MAJOR << 16) | (NoexsApplication.VERSION_MINOR) << 8;
public Debugger(IConnection conn) {
this.conn = conn;
}
public IConnection raw() {
return conn;
}
private void acquire() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
throw new ConnectionException(e);
}
}
private void release() {
semaphore.release();
}
public DebuggerStatus getStatus() {
acquire();
try {
conn.writeCommand(COMMAND_STATUS);
conn.flush();
int status = conn.readByte();
int major = conn.readByte();
int minor = conn.readByte();
int patch = conn.readByte();
this.protocolVersion = (major << 16) | (minor << 8);
Result rc = conn.readResult();
if (rc.failed()) {
throw new ConnectionException("This is impossible, so you've done something terribly wrong", rc);
}
if(protocolVersion > CURRENT_PROTOCOL_VERSION) {
throw new ConnectionException(String.format("Unsupported protocol version:%08X", protocolVersion));
}
protocolVersion |= patch; // we don't need to check the patch value, as it should always be backwards compatible.
return DebuggerStatus.forId(status);
} finally {
release();
}
}
public void poke(DataType type, long addr, long value) {
switch (type) {
case BYTE:
poke8(addr, (int) value);
break;
case SHORT:
poke16(addr, (int) value);
break;
case INT:
poke32(addr, (int) value);
break;
case LONG:
poke64(addr, value);
break;
}
}
public void poke8(long addr, int value) {
acquire();
try {
conn.writeCommand(COMMAND_POKE8);
conn.writeLong(addr);
conn.writeByte(value);
conn.flush();
Result rc = conn.readResult();
if (rc.failed()) {
throw new ConnectionException(rc);
}
} finally {
release();
}
}
private byte[] peekBuffer = new byte[8];
public int peek8(long addr) {
ByteBuffer b = readmem(addr, 1, peekBuffer).order(ByteOrder.LITTLE_ENDIAN);
return b.get() & 0xFF;
}
public void poke16(long addr, int value) {
acquire();
try {
conn.writeCommand(COMMAND_POKE16);
conn.writeLong(addr);
conn.writeShort(value);
conn.flush();
Result rc = conn.readResult();
if (rc.failed()) {
throw new ConnectionException(rc);
}
} finally {
release();
}
}
public int peek16(long addr) {
ByteBuffer b = readmem(addr, 2, peekBuffer).order(ByteOrder.LITTLE_ENDIAN);
return b.getShort() & 0xFFFF;
}
public void poke32(long addr, int value) {
acquire();
try {
conn.writeCommand(COMMAND_POKE32);
conn.writeLong(addr);
conn.writeInt(value);
conn.flush();
Result rc = conn.readResult();
if (rc.failed()) {
throw new ConnectionException(rc);
}
} finally {
release();
}
}
public int peek32(long addr) {
ByteBuffer b = readmem(addr, 4, peekBuffer).order(ByteOrder.LITTLE_ENDIAN);
return b.getInt();
}
public void poke64(long addr, long value) {
acquire();
try {
conn.writeCommand(COMMAND_POKE32);
conn.writeLong(addr);
conn.writeLong(value);
conn.flush();
Result rc = conn.readResult();
if (rc.failed()) {
throw new ConnectionException(rc);
}
} finally {
release();
}
}
public long peek64(long addr) {
ByteBuffer b = readmem(addr, 8, peekBuffer).order(ByteOrder.LITTLE_ENDIAN);
return b.getLong();
}
public Result setWatchpoint(boolean read, boolean write, long addr) {
acquire();
try {
Result rc;
WatchpointFlagBuilder.MatchType t;
if (read & write) {
t = WatchpointFlagBuilder.MatchType.ALL;
} else if (read) {
t = WatchpointFlagBuilder.MatchType.LOAD;
} else if (write) {
t = WatchpointFlagBuilder.MatchType.STORE;
} else {
throw new IllegalArgumentException("No flags set for watchpoint");
}
System.out.println(t);
int size = 4;
int id = 0;
int bpId = id + 4;
int offset = (int) (addr - (addr & ~3));
int mask = ((1 << size) - 1) << offset;
BreakpointFlagBuilder bp = new BreakpointFlagBuilder().setEnabled(true)
.setAddressSelect(0xF).setBreakpointType(BreakpointType.LINKED_CONTEXT_IDR_MATCH);
WatchpointFlagBuilder wp = new WatchpointFlagBuilder().setEnabled(true)
.setAccessContol(t).setAddressSelect(mask).setLinkedBreakpointNumber(bpId);
rc = setBreakpoint(bpId, bp.getFlag(), 0);
System.out.println("bp:" + rc);
if (rc.succeeded()) {
rc = setBreakpoint(0x10 + id, wp.getFlag(), addr); //wp
System.out.println("wp:" + rc);
}
return rc;
} finally {
release();
}
}
public Result setBreakpoint(int id, long flags, long addr) {
acquire();
try {
conn.writeCommand(COMMAND_SET_BREAKPOINT);
conn.writeInt(id);
conn.writeLong(addr);
conn.writeLong(flags);
conn.flush();
return conn.readResult();
} finally {
release();
}
}
public Result writemem(byte[] data, long addr) {
return writemem(data, 0, data.length, addr);
}
public Result writemem(byte[] data, int off, int len, long addr) {
acquire();
try {
conn.writeCommand(COMMAND_WRITE);
conn.writeLong(addr);
conn.writeInt(len);
conn.flush();
Result r = conn.readResult();
if (r.succeeded()) {
conn.write(data, off, len);
conn.flush();
} else {
conn.readResult();
return r;
}
return conn.readResult();
} finally {
release();
}
}
public void readmem(MemoryInfo info, OutputStream to) throws IOException {
if (!info.isReadable()) {
return;
}
readmem(info.getAddress(), (int) info.getSize(), to);
}
public void readmem(long start, int size, OutputStream to) throws IOException {
acquire();
try {
conn.writeCommand(COMMAND_READ);
conn.writeLong(start);
conn.writeInt(size);
conn.flush();
Result rc = conn.readResult();
if (rc.succeeded()) {
byte[] buffer = new byte[2048 * 4];
while (size > 0) {
rc = conn.readResult();
if (rc.failed()) {
conn.readResult();
throw new ConnectionException(rc);
}
int len = readCompressed(buffer);
to.write(buffer, 0, len);
size -= len;
}
}
conn.readResult();
} finally {
release();
}
}
public ByteBuffer readmem(long addr, int size, byte[] bytes) {
acquire();
try {
conn.writeCommand(COMMAND_READ);
conn.writeLong(addr);
conn.writeInt(size);
conn.flush();
Result rc = conn.readResult();
if (rc.failed()) {
conn.readResult(); // ignored
throw new ConnectionException(rc);
}
if (bytes == null) {
bytes = new byte[size];
}
int pos = 0;
byte[] buffer = new byte[2048 * 4];
while (pos < size) {
rc = conn.readResult();
if (rc.failed()) {
conn.readResult();
throw new ConnectionException(rc);
}
int len = readCompressed(buffer);
System.arraycopy(buffer, 0, bytes, pos, len);
pos += len;
}
conn.readResult(); // ignored
return ByteBuffer.wrap(bytes);
} finally {
release();
}
}
public Result resume() {
return getResult(COMMAND_CONTINUE);
}
public Result pause() {
return getResult(COMMAND_PAUSE);
}
public Result attach(long pid) {
acquire();
try {
conn.writeCommand(COMMAND_ATTACH);
conn.writeLong(pid);
conn.flush();
return conn.readResult();
} finally {
release();
}
}
public Result detach() {
return getResult(COMMAND_DETATCH);
}
public MemoryInfo query(long address) {
acquire();
try {
if (prev != null && prev.getAddress() != 0 && address >= prev.getAddress() && address < prev.getNextAddress()) {
return prev;
}
conn.writeCommand(COMMAND_QUERY_MEMORY);
conn.writeLong(address);
conn.flush();
return prev = readInfo();
} finally {
release();
}
}
public MemoryInfo[] query(long start, int max) {
acquire();
try {
conn.writeCommand(COMMAND_QUERY_MEMORY_MULTI);
conn.writeLong(start);
conn.writeInt(max);
conn.flush();
MemoryInfo[] res = new MemoryInfo[max];
int count;
for (count = 0; count < max; count++) {
MemoryInfo info = readInfo();
res[count] = info;
if (info.getType() == MemoryType.RESERVED) {
break;
}
}
conn.readResult(); // ignored here, it gets checked in readInfo()
return Arrays.copyOf(res, count);
} finally {
release();
}
}
public long getCurrentPid() {
acquire();
try {
conn.writeCommand(COMMAND_CURRENT_PID);
conn.flush();
long pid = conn.readLong();
Result rc = conn.readResult();
if (rc.failed()) {
pid = 0;
}
return pid;
} finally {
release();
}
}
public long getAttachedPid() {
acquire();
try {
conn.writeCommand(COMMAND_GET_ATTACHED_PID);
conn.flush();
long pid = conn.readLong();
Result rc = conn.readResult();
if (rc.failed()) {
throw new ConnectionException("This is impossible, so you've done something terribly wrong", rc);
}
return pid;
} finally {
release();
}
}
public long[] getPids() {
acquire();
try {
conn.writeCommand(COMMAND_GET_PIDS);
conn.flush();
int count = conn.readInt();
long[] pids = new long[count];
for (int i = 0; i < count; i++) {
pids[i] = conn.readLong();
}
Result rc = conn.readResult();
if (rc.failed()) {
throw new ConnectionException(rc);
}
return pids;
} finally {
release();
}
}
public long getTitleId(long pid) {
acquire();
try {
conn.writeCommand(COMMAND_GET_TITLEID);
conn.writeLong(pid);
conn.flush();
long tid = conn.readLong();
Result rc = conn.readResult();
if (rc.failed()) {
//TODO throw? idk
}
return tid;
} finally {
release();
}
}
private void disconnect() {
acquire();
try {
conn.writeCommand(COMMAND_DISCONNECT);
conn.flush();
Result rc = conn.readResult();
if (rc.failed()) {
throw new ConnectionException("This is impossible, so you've done something terribly wrong", rc);
}
} finally {
release();
}
}
public long getCurrentTitleId() {
long pid = getCurrentPid();
if (pid == 0) {
return 0;
}
return getTitleId(pid);
}
public boolean attached() {
return getAttachedPid() != 0;
}
public boolean connected() {
return conn.connected();
}
@Override
public void close() throws IOException {
if (connected()) {
detach();
disconnect();
}
conn.close();
}
private byte[] compressedBuffer = new byte[2048 * 4 * 6];
private int readCompressed(byte[] buffer) {
int compressedFlag = conn.readByte();
int decompressedLen = conn.readInt();
if (compressedFlag == 0) {
conn.readFully(buffer, 0, decompressedLen);
} else {
int compressedLen = conn.readInt();
conn.readFully(compressedBuffer, 0, compressedLen);
int pos = 0;
for (int i = 0; i < compressedLen; i += 2) {
byte value = compressedBuffer[i];
int count = compressedBuffer[i + 1] & 0xFF;
Arrays.fill(buffer, pos, pos + count, value);
pos += count;
}
}
return decompressedLen;
}
private MemoryInfo readInfo() {
long addr = conn.readLong();
long size = conn.readLong();
int type = conn.readInt();
int perm = conn.readInt();
Result rc = conn.readResult();
if (rc.failed()) {
throw new ConnectionException(rc);
}
return new MemoryInfo(addr, size, type, perm);
}
private Result getResult(int cmd) {
acquire();
try {
conn.writeCommand(cmd);
conn.flush();
return conn.readResult();
} finally {
release();
}
}
public long peek(DataType type, long addr) {
switch (type) {
case BYTE:
return peek8(addr);
case SHORT:
return peek16(addr);
case INT:
return peek32(addr);
case LONG:
return peek64(addr);
}
throw new IllegalArgumentException("Illegal data type:" + type);
}
}