Skip to content

Commit 14e192e

Browse files
committed
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
1 parent c541225 commit 14e192e

File tree

138 files changed

+1507
-619
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+1507
-619
lines changed

build/config.mk

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#
66
# - `make`
77
# - Backtraces
8+
# - Syscall tracing
89
# - Function tracing
910
# - Reasonably small
1011
# - Reasonably optimized
@@ -13,6 +14,7 @@ ifeq ($(MODE),)
1314
CONFIG_CCFLAGS += \
1415
$(BACKTRACES) \
1516
$(FTRACE) \
17+
-DSYSDEBUG \
1618
-Og
1719
TARGET_ARCH ?= \
1820
-msse3
@@ -23,6 +25,8 @@ endif
2325
# - `make MODE=opt`
2426
# - Backtraces
2527
# - More optimized
28+
# - Syscall tracing
29+
# - Function tracing
2630
# - Reasonably small
2731
# - No memory corruption detection
2832
# - assert() / CHECK_xx() may leak code into binary for debuggability
@@ -35,6 +39,7 @@ CONFIG_CPPFLAGS += \
3539
CONFIG_CCFLAGS += \
3640
$(BACKTRACES) \
3741
$(FTRACE) \
42+
-DSYSDEBUG \
3843
-O3
3944
TARGET_ARCH ?= \
4045
-march=native
@@ -122,6 +127,7 @@ CONFIG_CPPFLAGS += \
122127
CONFIG_CCFLAGS += \
123128
$(BACKTRACES) \
124129
$(FTRACE) \
130+
-DSYSDEBUG \
125131
-O2 \
126132
-fno-inline
127133
CONFIG_COPTS += \
@@ -287,7 +293,7 @@ endif
287293
# LLVM Mode
288294
ifeq ($(MODE), llvm)
289295
TARGET_ARCH ?= -msse3
290-
CONFIG_CCFLAGS += $(BACKTRACES) $(FTRACE) -O2
296+
CONFIG_CCFLAGS += $(BACKTRACES) $(FTRACE) -DSYSDEBUG -O2
291297
AS = clang
292298
CC = clang
293299
CXX = clang++

libc/calls/chdir.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/calls/internal.h"
20+
#include "libc/calls/strace.internal.h"
2021
#include "libc/dce.h"
2122
#include "libc/intrin/asan.internal.h"
2223
#include "libc/sysv/errfuns.h"
@@ -30,10 +31,14 @@
3031
* @see fchdir()
3132
*/
3233
int chdir(const char *path) {
33-
if (IsAsan() && !__asan_is_valid(path, 1)) return efault();
34-
if (!IsWindows()) {
35-
return sys_chdir(path);
34+
int rc;
35+
if (IsAsan() && !__asan_is_valid(path, 1)) {
36+
rc = efault();
37+
} else if (!IsWindows()) {
38+
rc = sys_chdir(path);
3639
} else {
37-
return sys_chdir_nt(path);
40+
rc = sys_chdir_nt(path);
3841
}
42+
STRACE("chdir(%#s) → %d% m", path, rc);
43+
return rc;
3944
}

libc/calls/close.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "libc/bits/weaken.h"
2020
#include "libc/calls/calls.h"
2121
#include "libc/calls/internal.h"
22-
#include "libc/calls/sysdebug.internal.h"
22+
#include "libc/calls/strace.internal.h"
2323
#include "libc/macros.internal.h"
2424
#include "libc/sock/internal.h"
2525
#include "libc/sysv/errfuns.h"
@@ -61,6 +61,6 @@ int close(int fd) {
6161
}
6262
}
6363
__releasefd(fd);
64-
SYSDEBUG("close(%d) -> %d", fd, rc);
64+
STRACE("%s(%d) %d% m", "close", fd, rc);
6565
return rc;
6666
}

libc/calls/commandv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "libc/bits/bits.h"
2020
#include "libc/bits/safemacros.internal.h"
2121
#include "libc/calls/calls.h"
22-
#include "libc/calls/sysdebug.internal.h"
22+
#include "libc/calls/strace.internal.h"
2323
#include "libc/dce.h"
2424
#include "libc/errno.h"
2525
#include "libc/log/libfatal.internal.h"
@@ -159,6 +159,6 @@ noasan char *commandv(const char *name, char pathbuf[hasatleast PATH_MAX]) {
159159
errno = f;
160160
}
161161
}
162-
SYSDEBUG("commandv(%#s, %p) → %#s% m", name, pathbuf, res);
162+
STRACE("commandv(%#s, %p) → %#s% m", name, pathbuf, res);
163163
return res;
164164
}

libc/runtime/describeframe.c renamed to libc/calls/describeframe.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/log/libfatal.internal.h"
19+
#include "libc/intrin/kprintf.h"
2020
#include "libc/macros.internal.h"
2121
#include "libc/runtime/memtrack.internal.h"
2222
#include "libc/runtime/runtime.h"
@@ -28,21 +28,19 @@
2828
noasan const char *DescribeFrame(int x) {
2929
/* asan runtime depends on this function */
3030
char *p;
31-
static char buf[128];
31+
static char buf[32];
3232
if (IsShadowFrame(x)) {
33-
p = buf;
34-
p = __stpcpy(p, " shadow of ");
35-
p = __fixcpy(p, UNSHADOW(ADDR(x)), 48);
33+
ksnprintf(buf, sizeof(buf), " /*shadow:%.12p*/", UNSHADOW(ADDR(x)));
3634
return buf;
37-
return " shadow ";
35+
return " /*shadow*/ ";
3836
} else if (IsAutoFrame(x)) {
39-
return " automap";
37+
return " /*automap*/";
4038
} else if (IsFixedFrame(x)) {
41-
return " fixed ";
39+
return " /*fixed*/ ";
4240
} else if (IsArenaFrame(x)) {
43-
return " arena ";
41+
return " /*arena*/ ";
4442
} else if (IsStaticStackFrame(x)) {
45-
return " stack ";
43+
return " /*stack*/ ";
4644
} else {
4745
return "";
4846
}

libc/runtime/describemapping.c renamed to libc/calls/describemapping.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,26 @@
2020
#include "libc/sysv/consts/map.h"
2121
#include "libc/sysv/consts/prot.h"
2222

23+
static noasan char DescribeMapType(int flags) {
24+
switch (flags & MAP_TYPE) {
25+
case MAP_FILE:
26+
return 'f';
27+
case MAP_PRIVATE:
28+
return 'p';
29+
case MAP_SHARED:
30+
return 's';
31+
default:
32+
return '?';
33+
}
34+
}
35+
2336
noasan char *DescribeMapping(int prot, int flags, char p[hasatleast 8]) {
2437
/* asan runtime depends on this function */
2538
p[0] = (prot & PROT_READ) ? 'r' : '-';
2639
p[1] = (prot & PROT_WRITE) ? 'w' : '-';
2740
p[2] = (prot & PROT_EXEC) ? 'x' : '-';
28-
if (flags & MAP_PRIVATE) {
29-
p[3] = 'p';
30-
} else if (flags & MAP_SHARED) {
31-
p[3] = 's';
32-
} else {
33-
p[3] = '?';
34-
}
35-
p[4] = (flags & MAP_ANONYMOUS) ? 'a' : 'f';
41+
p[3] = DescribeMapType(flags);
42+
p[4] = (flags & MAP_ANONYMOUS) ? 'a' : '-';
3643
p[5] = (flags & MAP_GROWSDOWN) ? 'S' : '-';
3744
p[6] = (flags & MAP_FIXED) ? 'F' : '-';
3845
p[7] = 0;
File renamed without changes.

libc/runtime/directmap-nt.c renamed to libc/calls/directmap-nt.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/calls/calls.h"
2020
#include "libc/calls/internal.h"
21-
#include "libc/calls/sysdebug.internal.h"
21+
#include "libc/calls/strace.internal.h"
2222
#include "libc/macros.internal.h"
2323
#include "libc/nt/enum/filemapflags.h"
2424
#include "libc/nt/enum/pageflags.h"
@@ -46,14 +46,14 @@ textwindows noasan struct DirectMap sys_mmap_nt(void *addr, size_t size,
4646
dm.maphandle = CreateFileMappingNuma(-1, &kNtIsInheritable,
4747
kNtPageExecuteReadwrite, upsize >> 32,
4848
upsize, NULL, kNtNumaNoPreferredNode);
49-
SYSDEBUG("CreateFileMappingNuma(-1, kNtPageExecuteReadwrite, 0x%x/0x%x) -> "
50-
"0x%x",
51-
upsize, size, dm.maphandle);
49+
STRACE(
50+
"CreateFileMappingNuma(-1, kNtPageExecuteReadwrite, %'zu/%'zu) -> %p",
51+
upsize, size, dm.maphandle);
5252
if (dm.maphandle) {
5353
dm.addr =
5454
MapViewOfFileExNuma(dm.maphandle, kNtFileMapWrite | kNtFileMapExecute,
5555
0, 0, upsize, addr, kNtNumaNoPreferredNode);
56-
SYSDEBUG("MapViewOfFileExNuma(WX, 0x%x) -> addr:0x%x", addr, dm.addr);
56+
STRACE("MapViewOfFileExNuma(WX, %p) → addr:%p", addr, dm.addr);
5757
if (dm.addr) {
5858
for (i = 0; i < size; i += got) {
5959
got = 0;
@@ -78,20 +78,16 @@ textwindows noasan struct DirectMap sys_mmap_nt(void *addr, size_t size,
7878
(prot & PROT_WRITE) ? kNtPageExecuteReadwrite : kNtPageExecuteRead,
7979
handle != -1 ? 0 : size >> 32, handle != -1 ? 0 : size, NULL,
8080
kNtNumaNoPreferredNode);
81-
SYSDEBUG("CreateFileMappingNuma(fhand:%d, prot:%s, size:0x%x) -> "
82-
"handle:0x%x",
83-
handle, (prot & PROT_WRITE) ? "XRW" : "XR",
84-
handle != -1 ? 0 : size);
81+
STRACE("CreateFileMappingNuma(fhand:%ld, prot:%s, size:%'zu) → %p", handle,
82+
(prot & PROT_WRITE) ? "XRW" : "XR", handle != -1 ? 0 : size);
8583
if (dm.maphandle) {
8684
dm.addr = MapViewOfFileExNuma(
8785
dm.maphandle,
8886
(prot & PROT_WRITE) ? kNtFileMapWrite | kNtFileMapExecute
8987
: kNtFileMapRead | kNtFileMapExecute,
9088
off >> 32, off, size, addr, kNtNumaNoPreferredNode);
91-
SYSDEBUG(
92-
"MapViewOfFileExNuma(prot:%s, off:0x%x, size:0x%x, addr:0x%x) -> "
93-
"addr:0x%x",
94-
(prot & PROT_WRITE) ? "WX" : "RX", off, size, addr, dm.addr);
89+
STRACE("MapViewOfFileExNuma(prot:%s, off:%'ld, size:%'zu, addr:%p) → %p",
90+
(prot & PROT_WRITE) ? "WX" : "RX", off, size, addr, dm.addr);
9591
if (dm.addr) {
9692
return dm;
9793
} else {

libc/runtime/directmap.c renamed to libc/calls/directmap.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/calls/internal.h"
20-
#include "libc/calls/sysdebug.internal.h"
20+
#include "libc/calls/strace.internal.h"
2121
#include "libc/errno.h"
2222
#include "libc/nt/runtime.h"
2323
#include "libc/runtime/directmap.internal.h"
@@ -36,20 +36,18 @@ noasan struct DirectMap sys_mmap(void *addr, size_t size, int prot, int flags,
3636
int fd, int64_t off) {
3737
/* asan runtime depends on this function */
3838
char mode[8];
39-
struct DirectMap dm;
39+
struct DirectMap d;
4040
if (!IsWindows() && !IsMetal()) {
41-
dm.addr = __sys_mmap(addr, size, prot, flags, fd, off, off);
42-
SYSDEBUG("sys_mmap(0x%p%s, 0x%x, %s, %d, %d) -> 0x%p %s", addr,
43-
DescribeFrame((intptr_t)addr >> 16), size,
44-
DescribeMapping(prot, flags, mode), (long)fd, off, dm.addr,
45-
dm.addr != MAP_FAILED ? "" : strerror(errno));
46-
dm.maphandle = kNtInvalidHandleValue;
47-
return dm;
41+
d.addr = __sys_mmap(addr, size, prot, flags, fd, off, off);
42+
d.maphandle = kNtInvalidHandleValue;
4843
} else if (IsMetal()) {
49-
return sys_mmap_metal(addr, size, prot, flags, fd, off);
44+
d = sys_mmap_metal(addr, size, prot, flags, fd, off);
5045
} else {
51-
return sys_mmap_nt(addr, size, prot, flags,
52-
fd != -1 ? g_fds.p[fd].handle : kNtInvalidHandleValue,
53-
off);
46+
d = sys_mmap_nt(addr, size, prot, flags,
47+
fd != -1 ? g_fds.p[fd].handle : kNtInvalidHandleValue, off);
5448
}
49+
STRACE("sys_mmap(%.12p%s, %'zu, %s, %d, %'ld) → {%.12p, %p}% m", addr,
50+
DescribeFrame((intptr_t)addr >> 16), size,
51+
DescribeMapping(prot, flags, mode), fd, off, d.addr, d.maphandle);
52+
return d;
5553
}

libc/calls/dup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/calls/calls.h"
2020
#include "libc/calls/internal.h"
21-
#include "libc/calls/sysdebug.internal.h"
21+
#include "libc/calls/strace.internal.h"
2222
#include "libc/dce.h"
2323

2424
/**
@@ -36,6 +36,6 @@ int dup(int fd) {
3636
} else {
3737
fd2 = sys_dup_nt(fd, -1, 0);
3838
}
39-
SYSDEBUG("dup(%d) -> %d", fd, fd2);
39+
STRACE("%s(%d) %d% m", "dup", fd, fd2);
4040
return fd2;
4141
}

0 commit comments

Comments
 (0)