mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 07:18:30 +00:00
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except it's less noisy since it only logs system calls to stderr. Having this flag is valuable because (1) system call tracing tells us a lot about the behavior of complex programs and (2) it's usually very hard to get system call tracing on various operating systems, e.g. strace, ktrace, dtruss, truss, nttrace, etc. Especially on Apple platforms where even with the special boot trick, debuggers still aren't guaranteed to work. make -j8 o//examples o//examples/hello.com --strace This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg extra information will be printed. make -j8 MODE=dbg o/dbg/examples o/dbg/examples/hello.com --strace |& less This change also changes: - Rename IsText() → _istext() - Rename IsUtf8() → _isutf8() - Fix madvise() on Windows NT - Fix empty string case of inet_ntop() - vfork() wrapper now saves and restores errno - Update xsigaction() to yoink syscall support
This commit is contained in:
parent
c541225af0
commit
14e192e5ba
138 changed files with 1519 additions and 631 deletions
|
@ -21,7 +21,7 @@
|
|||
#include "libc/bits/weaken.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/sysdebug.internal.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
|
@ -40,7 +40,6 @@
|
|||
|
||||
#define IP(X) (intptr_t)(X)
|
||||
#define VIP(X) (void *)IP(X)
|
||||
#define SMALL(n) ((n) <= 0xffffffffffff)
|
||||
#define ALIGNED(p) (!(IP(p) & (FRAMESIZE - 1)))
|
||||
#define ADDR(x) ((int64_t)((uint64_t)(x) << 32) >> 16)
|
||||
#define SHADE(x) (((intptr_t)(x) >> 3) + 0x7fff8000)
|
||||
|
@ -60,7 +59,7 @@ noasan static bool NeedAutomap(char *p, size_t n) {
|
|||
IsMapped(p, n);
|
||||
}
|
||||
|
||||
noasan static bool ChooseInterval(int x, int n, int *res) {
|
||||
noasan static bool ChooseMemoryInterval(int x, int n, int *res) {
|
||||
int i;
|
||||
if (_mmi.i) {
|
||||
i = FindMemoryInterval(&_mmi, x);
|
||||
|
@ -89,18 +88,17 @@ noasan static bool ChooseInterval(int x, int n, int *res) {
|
|||
|
||||
noasan static bool Automap(int n, int *res) {
|
||||
*res = -1;
|
||||
if (ChooseInterval(FRAME(kAutomapStart), n, res)) {
|
||||
if (ChooseMemoryInterval(FRAME(kAutomapStart), n, res)) {
|
||||
assert(*res >= FRAME(kAutomapStart));
|
||||
if (*res + n <= FRAME(kAutomapStart + (kAutomapStart - 1))) {
|
||||
return true;
|
||||
} else {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x) ENOMEM (automap interval exhausted)",
|
||||
ADDR(*res), ADDR(n + 1));
|
||||
STRACE("mmap(%.12p, %p) ENOMEM (automap interval exhausted)", ADDR(*res),
|
||||
ADDR(n + 1));
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x) ENOMEM (automap failed)", ADDR(*res),
|
||||
ADDR(n + 1));
|
||||
STRACE("mmap(%.12p, %p) ENOMEM (automap failed)", ADDR(*res), ADDR(n + 1));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -111,28 +109,28 @@ static noasan void *MapMemory(void *addr, size_t size, int prot, int flags,
|
|||
dm = sys_mmap(addr, size, prot, f, fd, off);
|
||||
if (UNLIKELY(dm.addr == MAP_FAILED)) {
|
||||
if (IsWindows() && (flags & MAP_FIXED)) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x) -> %s (%s)", addr, size, strerror(errno),
|
||||
"can't recover from MAP_FIXED errors on Windows");
|
||||
STRACE("mmap(%.12p, %'ld) → %s (%s)", addr, size, strerror(errno),
|
||||
"can't recover from MAP_FIXED errors on Windows");
|
||||
assert(!"MapMemory() failed");
|
||||
Die();
|
||||
}
|
||||
return MAP_FAILED;
|
||||
}
|
||||
if (UNLIKELY(dm.addr != addr)) {
|
||||
SYSDEBUG("KERNEL DIDN'T RESPECT MAP_FIXED");
|
||||
STRACE("KERNEL DIDN'T RESPECT MAP_FIXED");
|
||||
assert(!"MapMemory() failed");
|
||||
Die();
|
||||
}
|
||||
if (!IsWindows() && (flags & MAP_FIXED)) {
|
||||
if (UntrackMemoryIntervals(addr, size)) {
|
||||
SYSDEBUG("FIXED UNTRACK FAILED %s", strerror(errno));
|
||||
STRACE("FIXED UNTRACK FAILED %s", strerror(errno));
|
||||
assert(!"MapMemory() failed");
|
||||
Die();
|
||||
}
|
||||
}
|
||||
if (TrackMemoryInterval(&_mmi, x, x + (n - 1), dm.maphandle, prot, flags)) {
|
||||
if (sys_munmap(addr, n) == -1) {
|
||||
SYSDEBUG("TRACK MUNMAP FAILED %s", strerror(errno));
|
||||
STRACE("TRACK MUNMAP FAILED %s", strerror(errno));
|
||||
assert(!"MapMemory() failed");
|
||||
Die();
|
||||
}
|
||||
|
@ -151,21 +149,21 @@ static noasan void *MapMemory(void *addr, size_t size, int prot, int flags,
|
|||
* punch holes into existing mappings.
|
||||
*/
|
||||
static textwindows dontinline noasan void *MapMemories(char *addr, size_t size,
|
||||
int prot, int flags,
|
||||
int fd, int64_t off, int f,
|
||||
int x, size_t n) {
|
||||
int prot, int flags,
|
||||
int fd, int64_t off,
|
||||
int f, int x, size_t n) {
|
||||
struct DirectMap dm;
|
||||
size_t i, m = (n - 1) * FRAMESIZE;
|
||||
assert(m < size && m + FRAMESIZE >= size);
|
||||
dm = sys_mmap(addr + m, size - m, prot, f, fd, fd == -1 ? 0 : off + m);
|
||||
if (dm.addr == MAP_FAILED) {
|
||||
SYSDEBUG("MapMemories(%p+%x/%x) %s", addr, m, size, strerror(errno));
|
||||
STRACE("MapMemories(%.12p+%lx/%lx) %m", addr, m, size);
|
||||
return MAP_FAILED;
|
||||
}
|
||||
if (TrackMemoryInterval(&_mmi, x + (n - 1), x + (n - 1), dm.maphandle, prot,
|
||||
flags) == -1) {
|
||||
SYSDEBUG("MapMemories(%p+%x/%x) unrecoverable failure #1 %s", addr, m, size,
|
||||
strerror(errno));
|
||||
STRACE("MapMemories(%.12p+%lx/%lx) unrecoverable failure #1 %m", addr, m,
|
||||
size);
|
||||
assert(!"MapMemories() failed");
|
||||
Die();
|
||||
}
|
||||
|
@ -174,8 +172,8 @@ static textwindows dontinline noasan void *MapMemories(char *addr, size_t size,
|
|||
if (dm.addr == MAP_FAILED ||
|
||||
TrackMemoryInterval(&_mmi, x + i / FRAMESIZE, x + i / FRAMESIZE,
|
||||
dm.maphandle, prot, flags) == -1) {
|
||||
SYSDEBUG("MapMemories(%p+%x/%x) unrecoverable failure #2 %s", addr, i,
|
||||
size, strerror(errno));
|
||||
STRACE("MapMemories(%p+%x/%x) unrecoverable failure #2 %m", addr, i,
|
||||
size);
|
||||
assert(!"MapMemories() failed");
|
||||
Die();
|
||||
}
|
||||
|
@ -221,56 +219,56 @@ noasan void *mmap(void *addr, size_t size, int prot, int flags, int fd,
|
|||
int a, b, i, f, m, n, x;
|
||||
char mode[8], *p = addr;
|
||||
if (UNLIKELY(!size)) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (size=0)", p, size);
|
||||
STRACE("mmap(%.12p, %'zu) EINVAL (size=0)", p, size);
|
||||
return VIP(einval());
|
||||
}
|
||||
if (UNLIKELY(!SMALL(size))) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (size isn't 48-bit)", p, size);
|
||||
if (UNLIKELY(!IsLegalSize(size))) {
|
||||
STRACE("mmap(%.12p, %'zu) EINVAL (size isn't 48-bit)", p, size);
|
||||
return VIP(einval());
|
||||
}
|
||||
if (UNLIKELY(!IsLegalPointer(p))) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (p isn't 48-bit)", p, size);
|
||||
STRACE("mmap(%.12p, %'zu) EINVAL (p isn't 48-bit)", p, size);
|
||||
return VIP(einval());
|
||||
}
|
||||
if (UNLIKELY(!ALIGNED(p))) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (p isn't 64kb aligned)", p, size);
|
||||
STRACE("mmap(%.12p, %'zu) EINVAL (p isn't 64kb aligned)", p, size);
|
||||
return VIP(einval());
|
||||
}
|
||||
if (UNLIKELY(fd < -1)) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x, fd=%d) EBADF", p, size, (long)fd);
|
||||
STRACE("mmap(%.12p, %'zu, fd=%d) EBADF", p, size, fd);
|
||||
return VIP(ebadf());
|
||||
}
|
||||
if (UNLIKELY(!((fd != -1) ^ !!(flags & MAP_ANONYMOUS)))) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x, %s, %d, %d) EINVAL (fd anonymous mismatch)", p,
|
||||
size, DescribeMapping(prot, flags, mode), (long)fd, off);
|
||||
STRACE("mmap(%.12p, %'zu, %s, %d, %'ld) EINVAL (fd anonymous mismatch)", p,
|
||||
size, DescribeMapping(prot, flags, mode), fd, off);
|
||||
return VIP(einval());
|
||||
}
|
||||
if (UNLIKELY(!(!!(flags & MAP_PRIVATE) ^ !!(flags & MAP_SHARED)))) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (MAP_SHARED ^ MAP_PRIVATE)", p, size);
|
||||
STRACE("mmap(%.12p, %'zu) EINVAL (MAP_SHARED ^ MAP_PRIVATE)", p, size);
|
||||
return VIP(einval());
|
||||
}
|
||||
if (UNLIKELY(off < 0)) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x, off=%d) EINVAL (neg off)", p, size, off);
|
||||
STRACE("mmap(%.12p, %'zu, off=%'ld) EINVAL (neg off)", p, size, off);
|
||||
return VIP(einval());
|
||||
}
|
||||
if (UNLIKELY(INT64_MAX - size < off)) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x, off=%d) EINVAL (too large)", p, size, off);
|
||||
STRACE("mmap(%.12p, %'zu, off=%'ld) EINVAL (too large)", p, size, off);
|
||||
return VIP(einval());
|
||||
}
|
||||
if (UNLIKELY(!ALIGNED(off))) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (p isn't 64kb aligned)", p, size);
|
||||
STRACE("mmap(%.12p, %'zu) EINVAL (p isn't 64kb aligned)", p, size);
|
||||
return VIP(einval());
|
||||
}
|
||||
if ((flags & MAP_FIXED_NOREPLACE) && IsMapped(p, size)) {
|
||||
if (OverlapsImageSpace(p, size)) {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x) EFAULT (overlaps image)", p, size);
|
||||
STRACE("mmap(%.12p, %'zu) EFAULT (overlaps image)", p, size);
|
||||
} else {
|
||||
SYSDEBUG("mmap(0x%p, 0x%x) EFAULT (overlaps existing)", p, size);
|
||||
STRACE("mmap(%.12p, %'zu) EFAULT (overlaps existing)", p, size);
|
||||
}
|
||||
return VIP(efault());
|
||||
}
|
||||
SYSDEBUG("mmap(0x%p, 0x%x, %s, %d, %d)", p, size,
|
||||
DescribeMapping(prot, flags, mode), (long)fd, off);
|
||||
STRACE("mmap(%.12p, %'zu, %s, %d, %'ld)% m", p, size,
|
||||
DescribeMapping(prot, flags, mode), fd, off);
|
||||
if (fd == -1) {
|
||||
size = ROUNDUP(size, FRAMESIZE);
|
||||
if (IsWindows()) {
|
||||
|
@ -283,7 +281,7 @@ noasan void *mmap(void *addr, size_t size, int prot, int flags, int fd,
|
|||
x = FRAME(p);
|
||||
if (IsWindows()) {
|
||||
if (UntrackMemoryIntervals(p, size)) {
|
||||
SYSDEBUG("FIXED UNTRACK FAILED %s", strerror(errno));
|
||||
STRACE("FIXED UNTRACK FAILED %m");
|
||||
assert(!"mmap() failed");
|
||||
Die();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue