Skip to content

Commit 0a436ba

Browse files
committed
[swift-inspect] minor cleanup
1 parent ac3fec1 commit 0a436ba

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

tools/swift-inspect/Sources/AndroidCLib/heap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,4 @@ bool heap_iterate_metadata_process(
111111
}
112112

113113
return true;
114-
}
114+
}

tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/ptrace.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <linux/ptrace.h>
1919

2020
static inline
21-
int ptrace_wrapper(int op, pid_t pid, void *addr, void *data) {
21+
int ptrace_retry(int op, pid_t pid, void *addr, void *data) {
2222
int retries = 3;
2323
int result;
2424
do {
@@ -29,35 +29,35 @@ int ptrace_wrapper(int op, pid_t pid, void *addr, void *data) {
2929

3030
static inline
3131
int ptrace_attach(pid_t pid) {
32-
return ptrace_wrapper(PTRACE_ATTACH, pid, 0, 0);
32+
return ptrace_retry(PTRACE_ATTACH, pid, 0, 0);
3333
}
3434

3535
static inline
3636
int ptrace_detach(pid_t pid) {
37-
return ptrace_wrapper(PTRACE_DETACH, pid, 0, 0);
37+
return ptrace_retry(PTRACE_DETACH, pid, 0, 0);
3838
}
3939

4040
static inline
4141
int ptrace_continue(pid_t pid) {
42-
return ptrace_wrapper(PTRACE_CONT, pid, 0, 0);
42+
return ptrace_retry(PTRACE_CONT, pid, 0, 0);
4343
}
4444

4545
static inline
4646
int ptrace_getsiginfo(pid_t pid, siginfo_t *siginfo) {
47-
return ptrace_wrapper(PTRACE_GETSIGINFO, pid, 0, siginfo);
47+
return ptrace_retry(PTRACE_GETSIGINFO, pid, 0, siginfo);
4848
}
4949

5050
static inline
5151
int ptrace_pokedata(pid_t pid, unsigned long addr, unsigned long value) {
52-
return ptrace_wrapper(PTRACE_POKEDATA, pid, (void*)(uintptr_t)addr, (void*)(uintptr_t)value);
52+
return ptrace_retry(PTRACE_POKEDATA, pid, (void*)(uintptr_t)addr, (void*)(uintptr_t)value);
5353
}
5454

5555
static inline
5656
int ptrace_getregset(pid_t pid, int type, struct iovec *regs) {
57-
return ptrace_wrapper(PTRACE_GETREGSET, pid, (void*)(uintptr_t)type, regs);
57+
return ptrace_retry(PTRACE_GETREGSET, pid, (void*)(uintptr_t)type, regs);
5858
}
5959

6060
static inline
6161
int ptrace_setregset(pid_t pid, int type, struct iovec *regs) {
62-
return ptrace_wrapper(PTRACE_SETREGSET, pid, (void*)(uintptr_t)type, regs);
62+
return ptrace_retry(PTRACE_SETREGSET, pid, (void*)(uintptr_t)type, regs);
6363
}

tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/wait.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@
1313
#include <stdbool.h>
1414
#include <sys/wait.h>
1515

16-
static inline bool wIfStopped(int status) {
16+
static inline
17+
bool wIfStopped(int status) {
1718
return WIFSTOPPED(status) != 0;
1819
}
1920

20-
static inline bool wIfExited(int status) {
21+
static inline
22+
bool wIfExited(int status) {
2123
return WIFEXITED(status) != 0;
2224
}
2325

24-
static inline bool wIfSignaled(int status) {
26+
static inline
27+
bool wIfSignaled(int status) {
2528
return WIFSIGNALED(status) != 0;
2629
}
2730

28-
static inline int wStopSig(int status) {
31+
static inline
32+
int wStopSig(int status) {
2933
return WSTOPSIG(status);
3034
}

tools/swift-inspect/Sources/swift-inspect/AndroidRemoteProcess.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ internal final class AndroidRemoteProcess: LinuxRemoteProcess {
5050
}
5151

5252
override internal func iterateHeap(_ body: (swift_addr_t, UInt64) -> Void) {
53+
var regionCount = 0
54+
var allocCount = 0
5355
for entry in self.memoryMap.entries {
5456
// Limiting malloc_iterate calls to only memory regions that are known
5557
// to contain heap allocations is not strictly necessary but it does
@@ -61,14 +63,27 @@ internal final class AndroidRemoteProcess: LinuxRemoteProcess {
6163
do {
6264
allocations = try self.iterateHeapRegion(
6365
startAddr: entry.startAddr, endAddr: entry.endAddr)
66+
regionCount += 1
6467
} catch {
6568
print("failed iterating remote heap: \(error)")
6669
return
6770
}
6871

72+
allocCount += allocations.count
73+
6974
// process all of the collected allocations
7075
for alloc in allocations { body(alloc.base, alloc.len) }
7176
}
77+
78+
if allocCount == 0 {
79+
// This condition most likely indicates the MemoryMap.Entry.isHeapRegion
80+
// filtering is needs to be modified to support a new heap region naming
81+
// convention in a newer Android version.
82+
print("WARNING: no heap regions found")
83+
print("swift-inspect may need to be updated for a newer Android version")
84+
} else if allocCount == 0 {
85+
print("WARNING: no heap items enumerated")
86+
}
7287
}
7388

7489
// Linux and Android have no supported method to enumerate allocations in the
@@ -103,7 +118,6 @@ internal final class AndroidRemoteProcess: LinuxRemoteProcess {
103118

104119
// Allocate a page-sized buffer in the remote process that malloc_iterate
105120
// will populaate with metadata describing each heap entry it enumerates.
106-
//let dataLen = 32
107121
let dataLen = sysconf(Int32(_SC_PAGESIZE))
108122
var mmapArgs = [0, UInt64(dataLen), UInt64(PROT_READ | PROT_WRITE), UInt64(MAP_ANON | MAP_PRIVATE)]
109123
let remoteDataAddr: UInt64 = try self.ptrace.callRemoteFunction(at: mmapAddr, with: mmapArgs)

0 commit comments

Comments
 (0)