Add some temporary logging statements

This commit is contained in:
Justine Tunney 2025-01-04 23:37:32 -08:00
parent 53c6edfd18
commit f71f61cd40
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
3 changed files with 33 additions and 7 deletions

View file

@ -54,6 +54,7 @@
#include "libc/thread/thread.h"
#ifdef __x86_64__
__msabi extern typeof(CloseHandle) *const __imp_CloseHandle;
__msabi extern typeof(TerminateProcess) *const __imp_TerminateProcess;
extern pthread_mutex_t __sig_worker_lock;
@ -69,8 +70,11 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
// execve() needs to be @asyncsignalsafe
sigset_t sigmask = __sig_block();
STRACE("execve step #1");
_pthread_mutex_lock(&__sig_worker_lock); // order matters
_pthread_lock(); // order matters
STRACE("execve step #2");
_pthread_lock(); // order matters
STRACE("execve step #3");
// new process should be a child of our parent
int64_t hParentProcess =
@ -79,6 +83,8 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
sys_getppid_nt_win32)
: 0;
STRACE("execve step #4");
// inherit pid
char pidvar[11 + 21];
FormatUint64(stpcpy(pidvar, "_COSMO_PID="), __pid);
@ -97,6 +103,8 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
setenv("_COSMO_PPID", ppidvar, true);
}
STRACE("execve step #5");
// define stdio handles for the spawned subprocess
struct NtStartupInfo si = {
.cb = sizeof(struct NtStartupInfo),
@ -110,6 +118,8 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
}
}
STRACE("execve step #6");
// which process is responsible for spawning the child?
int64_t hCreatorProcess;
if (hParentProcess) {
@ -125,28 +135,34 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
if (!(fdspec = __describe_fds(g_fds.p, g_fds.n, &si, hCreatorProcess,
&lpExplicitHandles, &dwExplicitHandleCount))) {
if (hParentProcess)
CloseHandle(hParentProcess);
__imp_CloseHandle(hParentProcess);
sys_execve_nt_abort(sigmask);
return -1;
}
STRACE("execve step #7");
// inherit pending signals
atomic_fetch_or_explicit(
__sig.process,
atomic_load_explicit(&__get_tls()->tib_sigpending, memory_order_acquire),
memory_order_release);
STRACE("execve step #8");
// launch the process
struct NtProcessInformation pi;
int rc = ntspawn(&(struct NtSpawnArgs){
AT_FDCWD, program, argv, envp,
(char *[]){fdspec, maskvar, pidvar, ppidvar, 0}, 0, 0, hCreatorProcess,
lpExplicitHandles, dwExplicitHandleCount, &si, &pi});
STRACE("execve step #9");
__undescribe_fds(hCreatorProcess, lpExplicitHandles, dwExplicitHandleCount);
STRACE("execve step #10");
if (rc == -1) {
free(fdspec);
if (hParentProcess)
CloseHandle(hParentProcess);
__imp_CloseHandle(hParentProcess);
sys_execve_nt_abort(sigmask);
if (GetLastError() == kNtErrorSharingViolation) {
return etxtbsy();
@ -161,9 +177,11 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
int64_t handle;
if (DuplicateHandle(GetCurrentProcess(), pi.hProcess, hParentProcess,
&handle, 0, false, kNtDuplicateSameAccess)) {
STRACE("execve step #11");
unassert(!(handle & 0xFFFFFFFFFF000000));
__imp_TerminateProcess(-1, 0x23000000u | handle);
} else {
STRACE("execve step #12");
// TODO(jart): Why does `make loc` print this?
// kprintf("DuplicateHandle failed w/ %d\n", GetLastError());
__imp_TerminateProcess(-1, ECHILD);
@ -194,7 +212,7 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
if (status != kNtStillActive) {
if ((status & 0xFF000000u) == 0x23000000u) {
// handle child execve()
CloseHandle(pi.hProcess);
__imp_CloseHandle(pi.hProcess);
pi.hProcess = status & 0x00FFFFFF;
} else {
// handle child _Exit()