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

@ -17,14 +17,10 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/calls/strace.internal.h"
#include "libc/macros.internal.h"
.privileged
#ifdef __FSANITIZE_ADDRESS__
vfork: jmp fork # TODO: asan and vfork don't mix?
.endfn vfork,globl
#else
// Forks process without copying page tables.
//
// This is the same as fork() except it's optimized for the case
@ -40,11 +36,24 @@ vfork: jmp fork # TODO: asan and vfork don't mix?
// @returnstwice
// @vforksafe
vfork:
#ifdef __FSANITIZE_ADDRESS__
jmp fork # TODO: asan and vfork don't mix?
.endfn vfork,globl
#else
#if SupportsWindows()
testb IsWindows()
jnz sys_fork_nt
jnz sys_fork_nt # and we're lucky to have that
#endif
#if SupportsOpenbsd()
testb IsOpenbsd()
jnz fork # fake vfork plus msyscall issues
#endif
#ifdef SYSDEBUG
ezlea .Llog,di
call __stracef
#endif /* SYSDEBUG */
mov __NR_vfork(%rip),%eax
mov errno(%rip),%r8d # avoid question of @vforksafe errno
pop %rsi # saves return address in a register
#if SupportsBsd()
testb IsBsd()
@ -56,7 +65,8 @@ vfork:
cmp $-4095,%eax
jae systemfive_error
#endif
0: ezlea __vforked,di
0: mov %r8d,errno(%rip)
ezlea __vforked,di
test %eax,%eax
jz 1f
decl (%rdi)
@ -81,4 +91,11 @@ vfork.bsd:
.endfn vfork.bsd
#endif /* BSD */
#ifdef SYSDEBUG
.rodata.str1.1
.Llog: .ascii STRACE_PROLOGUE
.asciz "vfork()%n"
.previous
#endif /* DEBUGSYS */
#endif /* __FSANITIZE_ADDRESS__ */