2020-06-15 14:18:57 +00:00
|
|
|
/*-*- 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 │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-08-26 04:35:58 +00:00
|
|
|
#include "libc/assert.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/internal.h"
|
2022-04-12 12:20:17 +00:00
|
|
|
#include "libc/calls/sig.internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/struct/rusage.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2020-12-09 23:04:54 +00:00
|
|
|
#include "libc/fmt/conv.h"
|
2022-10-03 05:14:33 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/accounting.h"
|
2022-03-23 02:51:27 +00:00
|
|
|
#include "libc/nt/enum/accessmask.h"
|
|
|
|
#include "libc/nt/enum/processaccess.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/enum/status.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/nt/enum/wait.h"
|
2022-03-23 02:51:27 +00:00
|
|
|
#include "libc/nt/process.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/runtime.h"
|
|
|
|
#include "libc/nt/struct/filetime.h"
|
2022-03-23 02:51:27 +00:00
|
|
|
#include "libc/nt/struct/processmemorycounters.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/synchronization.h"
|
2022-03-25 14:11:44 +00:00
|
|
|
#include "libc/runtime/ezmap.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/stdio/lcg.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
2022-03-23 02:51:27 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2022-04-12 12:20:17 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/w.h"
|
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2022-04-12 12:20:17 +00:00
|
|
|
static textwindows int sys_wait4_nt_impl(int pid, int *opt_out_wstatus,
|
|
|
|
int options,
|
|
|
|
struct rusage *opt_out_rusage) {
|
2020-11-13 09:27:49 +00:00
|
|
|
int pids[64];
|
2022-03-23 02:51:27 +00:00
|
|
|
int64_t handle;
|
2020-11-13 09:27:49 +00:00
|
|
|
int64_t handles[64];
|
2020-06-15 14:18:57 +00:00
|
|
|
uint32_t dwExitCode;
|
2022-03-25 14:11:44 +00:00
|
|
|
bool shouldinterrupt;
|
2022-03-23 04:04:08 +00:00
|
|
|
uint32_t i, j, base, count, timeout;
|
2022-03-23 02:51:27 +00:00
|
|
|
struct NtProcessMemoryCountersEx memcount;
|
2020-06-15 14:18:57 +00:00
|
|
|
struct NtFileTime createfiletime, exitfiletime, kernelfiletime, userfiletime;
|
2022-03-25 14:11:44 +00:00
|
|
|
if (_check_interrupts(true, g_fds.p)) return eintr();
|
2022-06-09 03:01:28 +00:00
|
|
|
__fds_lock();
|
2022-03-23 04:04:08 +00:00
|
|
|
if (pid != -1 && pid != 0) {
|
|
|
|
if (pid < 0) {
|
|
|
|
/* XXX: this is sloppy */
|
|
|
|
pid = -pid;
|
|
|
|
}
|
2020-11-28 20:01:51 +00:00
|
|
|
if (!__isfdkind(pid, kFdProcess)) {
|
2022-03-23 02:51:27 +00:00
|
|
|
/* XXX: this is sloppy (see fork-nt.c) */
|
|
|
|
if (!__isfdopen(pid) &&
|
|
|
|
(handle = OpenProcess(kNtSynchronize | kNtProcessQueryInformation,
|
|
|
|
true, pid))) {
|
2022-05-17 11:14:28 +00:00
|
|
|
if ((pid = __reservefd_unlocked(-1)) != -1) {
|
2022-03-23 02:51:27 +00:00
|
|
|
g_fds.p[pid].kind = kFdProcess;
|
|
|
|
g_fds.p[pid].handle = handle;
|
|
|
|
g_fds.p[pid].flags = O_CLOEXEC;
|
|
|
|
} else {
|
2022-06-09 03:01:28 +00:00
|
|
|
__fds_unlock();
|
2022-03-23 02:51:27 +00:00
|
|
|
CloseHandle(handle);
|
|
|
|
return echild();
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-09 03:01:28 +00:00
|
|
|
__fds_unlock();
|
2022-03-23 02:51:27 +00:00
|
|
|
return echild();
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
handles[0] = g_fds.p[pid].handle;
|
|
|
|
pids[0] = pid;
|
|
|
|
count = 1;
|
|
|
|
} else {
|
2022-03-25 14:11:44 +00:00
|
|
|
count = __sample_pids(pids, handles, false);
|
2022-05-17 11:14:28 +00:00
|
|
|
if (!count) {
|
2022-06-09 03:01:28 +00:00
|
|
|
__fds_unlock();
|
2022-05-17 11:14:28 +00:00
|
|
|
return echild();
|
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
2022-06-09 03:01:28 +00:00
|
|
|
__fds_unlock();
|
2020-11-13 09:27:49 +00:00
|
|
|
for (;;) {
|
2022-03-25 14:11:44 +00:00
|
|
|
if (_check_interrupts(true, 0)) return eintr();
|
2020-11-13 09:27:49 +00:00
|
|
|
dwExitCode = kNtStillActive;
|
|
|
|
if (options & WNOHANG) {
|
|
|
|
i = WaitForMultipleObjects(count, handles, false, 0);
|
2021-08-26 04:35:58 +00:00
|
|
|
if (i == kNtWaitTimeout) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2022-04-24 16:59:22 +00:00
|
|
|
i = WaitForMultipleObjects(count, handles, false,
|
|
|
|
__SIG_POLLING_INTERVAL_MS);
|
|
|
|
if (i == kNtWaitTimeout) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
2021-08-26 04:35:58 +00:00
|
|
|
if (i == kNtWaitFailed) {
|
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
2022-03-19 01:07:28 +00:00
|
|
|
STRACE("%s failed %u", "WaitForMultipleObjects", GetLastError());
|
2021-08-26 04:35:58 +00:00
|
|
|
return __winerr();
|
|
|
|
}
|
|
|
|
if (!GetExitCodeProcess(handles[i], &dwExitCode)) {
|
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
2022-03-19 01:07:28 +00:00
|
|
|
STRACE("%s failed %u", "GetExitCodeProcess", GetLastError());
|
2021-08-26 04:35:58 +00:00
|
|
|
return __winerr();
|
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
if (dwExitCode == kNtStillActive) continue;
|
|
|
|
if (opt_out_wstatus) { /* @see WEXITSTATUS() */
|
|
|
|
*opt_out_wstatus = (dwExitCode & 0xff) << 8;
|
|
|
|
}
|
|
|
|
if (opt_out_rusage) {
|
2021-09-28 05:58:51 +00:00
|
|
|
bzero(opt_out_rusage, sizeof(*opt_out_rusage));
|
2022-04-15 06:39:48 +00:00
|
|
|
bzero(&memcount, sizeof(memcount));
|
|
|
|
memcount.cb = sizeof(struct NtProcessMemoryCountersEx);
|
2022-03-23 02:51:27 +00:00
|
|
|
if (GetProcessMemoryInfo(handles[i], &memcount, sizeof(memcount))) {
|
2022-04-01 00:07:22 +00:00
|
|
|
opt_out_rusage->ru_maxrss = memcount.PeakWorkingSetSize / 1024;
|
2022-03-23 02:51:27 +00:00
|
|
|
opt_out_rusage->ru_majflt = memcount.PageFaultCount;
|
|
|
|
} else {
|
|
|
|
STRACE("%s failed %u", "GetProcessMemoryInfo", GetLastError());
|
|
|
|
}
|
|
|
|
if (GetProcessTimes(handles[i], &createfiletime, &exitfiletime,
|
|
|
|
&kernelfiletime, &userfiletime)) {
|
2021-08-26 04:35:58 +00:00
|
|
|
opt_out_rusage->ru_utime =
|
|
|
|
WindowsDurationToTimeVal(ReadFileTime(userfiletime));
|
|
|
|
opt_out_rusage->ru_stime =
|
|
|
|
WindowsDurationToTimeVal(ReadFileTime(kernelfiletime));
|
|
|
|
} else {
|
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
2022-03-19 01:07:28 +00:00
|
|
|
STRACE("%s failed %u", "GetProcessTimes", GetLastError());
|
2021-08-16 22:26:31 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-03-23 02:51:27 +00:00
|
|
|
CloseHandle(handles[i]);
|
|
|
|
__releasefd(pids[i]);
|
2020-11-13 09:27:49 +00:00
|
|
|
return pids[i];
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-12 12:20:17 +00:00
|
|
|
|
|
|
|
textwindows int sys_wait4_nt(int pid, int *opt_out_wstatus, int options,
|
|
|
|
struct rusage *opt_out_rusage) {
|
|
|
|
int rc;
|
2022-10-03 05:14:33 +00:00
|
|
|
sigset_t oldmask, mask = {0};
|
2022-04-12 12:20:17 +00:00
|
|
|
sigaddset(&mask, SIGCHLD);
|
|
|
|
__sig_mask(SIG_BLOCK, &mask, &oldmask);
|
|
|
|
rc = sys_wait4_nt_impl(pid, opt_out_wstatus, options, opt_out_rusage);
|
|
|
|
__sig_mask(SIG_SETMASK, &oldmask, 0);
|
|
|
|
return rc;
|
|
|
|
}
|