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:
Justine Tunney 2022-03-18 18:07:28 -07:00
parent c541225af0
commit 14e192e5ba
138 changed files with 1519 additions and 631 deletions

View file

@ -0,0 +1,37 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
const char *__describe_os(void) {
if (IsLinux()) {
return "gnu/systemd";
} else if (IsXnu()) {
return "xnu's not unix!";
} else if (IsFreebsd()) {
return "free besiyata dishmaya";
} else if (IsNetbsd()) {
return "net besiyata dishmaya";
} else if (IsOpenbsd()) {
return "open besiyata dishmaya";
} else if (IsWindows()) {
return "the new technology";
} else {
return "wut";
}
}

21
libc/sysv/strace.greg.c Normal file
View file

@ -0,0 +1,21 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/strace.internal.h"
int __strace;

View file

@ -16,13 +16,14 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/strace.internal.h"
#include "libc/dce.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/nr.h"
#include "libc/sysv/consts/map.h"
#include "libc/macros.internal.h"
#include "libc/sysv/consts/prot.h"
#include "libc/nexgen32e/macros.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/nr.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/prot.h"
/*
@ -306,8 +307,29 @@ _init_systemfive_magnums:
jnz 3b
xchg %rbx,%rax
stosq
#ifdef SYSDEBUG
inc %r8d
#endif
jmp 2b
5: pop %rdi
5: nop
#ifdef SYSDEBUG
push %rdi
push %rsi
push %r8
call __describe_os
mov %rax,%rdx
pop %r8
.weak __stracef
ezlea __stracef,ax
test %rax,%rax
jz 5f
ezlea .Llog,di
mov %r8d,%esi
call *%rax
5: pop %rsi
pop %rdi
#endif /* DEBUGSYS */
pop %rdi
pop %rsi
pop %rbx
// 𝑠𝑙𝑖𝑑𝑒
@ -325,8 +347,12 @@ _init_systemfive_pid:
.endfn _init_systemfive_pid
#endif
#if SupportsSystemv() && !defined(TINY)
_init_systemfive_stack: # determinism ftw!
#if SupportsWindows() || SupportsMetal()
// Create a stack with deterministic readable addresses.
// If ape_execve() already created us a stack that meets
// the requirements of STATIC_STACK_SIZE() then we skip.
_init_systemfive_stack:
#if SupportsWindows() || SupportsMetal() || SupportsOpenbsd()
testb $WINDOWS|METAL,__hostos(%rip)
jnz _init_systemfive_done
#endif
@ -512,6 +538,14 @@ syscon_windows:/*
.globl syscon_windows
#endif
#ifdef SYSDEBUG
.rodata.str1.1
.Llog: .ascii STRACE_PROLOGUE
.ascii "bell system five system call support"
.asciz " %'u magnums loaded on %s%n"
.previous
#endif /* DEBUGSYS */
.weak ape_stack_vaddr
.weak ape_stack_memsz
.weak ape_stack_align

View file

@ -22,6 +22,7 @@ LIBC_SYSV_ARTIFACTS += LIBC_SYSV_A
LIBC_SYSV_A = o/$(MODE)/libc/sysv/sysv.a
LIBC_SYSV_A_HDRS = $(filter %.h,$(LIBC_SYSV_A_FILES))
LIBC_SYSV_A_INCS = $(filter %.inc,$(LIBC_SYSV_A_FILES))
LIBC_SYSV_A_SRCS_C = $(filter %.c,$(LIBC_SYSV_A_FILES))
LIBC_SYSV_A_SRCS_A = $(filter %.s,$(LIBC_SYSV_A_FILES))
LIBC_SYSV_A_SRCS_S = $(filter %.S,$(LIBC_SYSV_A_FILES))
LIBC_SYSV_A_CHECKS = $(LIBC_SYSV_A).pkg
@ -36,15 +37,19 @@ LIBC_SYSV_A_FILES := \
libc/sysv/restorert.S \
libc/sysv/syscall.S \
libc/sysv/systemfive.S \
libc/sysv/strace.greg.c \
libc/sysv/describeos.greg.c \
$(wildcard libc/sysv/consts/*) \
$(wildcard libc/sysv/errfuns/*)
LIBC_SYSV_A_SRCS = \
$(LIBC_SYSV_A_SRCS_A) \
$(LIBC_SYSV_A_SRCS_C) \
$(LIBC_SYSV_A_SRCS_S)
LIBC_SYSV_A_OBJS = \
$(LIBC_SYSV_A_SRCS_A:%.s=o/$(MODE)/%.o) \
$(LIBC_SYSV_A_SRCS_C:%.c=o/$(MODE)/%.o) \
$(LIBC_SYSV_A_SRCS_S:%.S=o/$(MODE)/%.o)
LIBC_SYSV_A_DEPS := \