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

@ -16,11 +16,12 @@
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/sock/internal.h"
#include "libc/sock/sock.h"
#include "libc/sock/sockdebug.h"
#include "libc/sysv/consts/af.h"
#include "libc/sysv/errfuns.h"
/**
* Creates new system resource for network communication, e.g.
@ -38,17 +39,15 @@
* @asyncsignalsafe
*/
int socket(int family, int type, int protocol) {
if (family == AF_UNSPEC) {
family = AF_INET;
} else if (family == AF_INET6) {
/* Recommend IPv6 on frontend serving infrastructure only. That's
what Google Cloud does. It's more secure. It also means poll()
will work on Windows, which doesn't allow mixing third layers. */
return epfnosupport();
}
int rc;
if (family == AF_UNSPEC) family = AF_INET;
if (!IsWindows()) {
return sys_socket(family, type, protocol);
rc = sys_socket(family, type, protocol);
} else {
return sys_socket_nt(family, type, protocol);
rc = sys_socket_nt(family, type, protocol);
}
STRACE("socket(%s, %s, %s) -> %d% m", __describe_socket_family(family),
__describe_socket_type(type), __describe_socket_protocol(protocol),
rc);
return rc;
}