Fix bugs with recent change

This change makes further effort towards improving our poll()
implementation on the New Technology. The stdin worker didn't work out
so well for Python so it's not being used for now. System call tracing
with the --strace flag should now be less noisy now on Windows unless
you modify the strace.internal.h defines to turn on some optional ones
that are most useful for debugging the system call wrappers.
This commit is contained in:
Justine Tunney 2022-04-16 10:40:23 -07:00
parent 933411ba99
commit dc0ea6640e
127 changed files with 1354 additions and 866 deletions

View file

@ -36,6 +36,6 @@ textwindows bool32 CloseHandle(int64_t hObject) {
__winerr();
if (weaken(__die)) weaken(__die)();
}
STRACE("CloseHandle(%ld) → %hhhd% m", hObject, ok);
NTTRACE("CloseHandle(%ld) → %hhhd% m", hObject, ok);
return ok;
}

View file

@ -36,7 +36,7 @@ CreateDirectory(const char16_t *lpPathName,
bool32 ok;
ok = __imp_CreateDirectoryW(lpPathName, lpSecurityAttributes);
if (!ok) __winerr();
STRACE("CreateDirectory(%#hs, %s) → %hhhd% m", lpPathName,
DescribeNtSecurityAttributes(lpSecurityAttributes), ok);
NTTRACE("CreateDirectory(%#hs, %s) → %hhhd% m", lpPathName,
DescribeNtSecurityAttributes(lpSecurityAttributes), ok);
return ok;
}

View file

@ -40,12 +40,12 @@ textwindows int64_t CreateFile(
opt_lpSecurityAttributes, dwCreationDisposition,
dwFlagsAndAttributes, opt_hTemplateFile);
if (hHandle == -1) __winerr();
STRACE("CreateFile(%#hs, %s, %s, %s, %s, %s, %ld) → %ld% m", lpFileName,
DescribeNtFileAccessFlags(dwDesiredAccess),
DescribeNtFileShareFlags(dwShareMode),
DescribeNtSecurityAttributes(opt_lpSecurityAttributes),
DescribeNtCreationDisposition(dwCreationDisposition),
DescribeNtFileFlagsAndAttributes(dwFlagsAndAttributes),
opt_hTemplateFile, hHandle);
NTTRACE("CreateFile(%#hs, %s, %s, %s, %s, %s, %ld) → %ld% m", lpFileName,
DescribeNtFileAccessFlags(dwDesiredAccess),
DescribeNtFileShareFlags(dwShareMode),
DescribeNtSecurityAttributes(opt_lpSecurityAttributes),
DescribeNtCreationDisposition(dwCreationDisposition),
DescribeNtFileFlagsAndAttributes(dwFlagsAndAttributes),
opt_hTemplateFile, hHandle);
return hHandle;
}

View file

@ -43,10 +43,10 @@ textwindows int64_t CreateFileMapping(
flProtect, dwMaximumSizeHigh,
dwMaximumSizeLow, opt_lpName);
if (!hHandle) __winerr();
STRACE("CreateFileMapping(%ld, %s, %s, %'zu, %#hs) → %ld% m", opt_hFile,
DescribeNtSecurityAttributes(opt_lpFileMappingAttributes),
DescribeNtPageFlags(flProtect),
(uint64_t)dwMaximumSizeHigh << 32 | dwMaximumSizeLow, opt_lpName,
hHandle);
NTTRACE("CreateFileMapping(%ld, %s, %s, %'zu, %#hs) → %ld% m", opt_hFile,
DescribeNtSecurityAttributes(opt_lpFileMappingAttributes),
DescribeNtPageFlags(flProtect),
(uint64_t)dwMaximumSizeHigh << 32 | dwMaximumSizeLow, opt_lpName,
hHandle);
return hHandle;
}

View file

@ -44,10 +44,10 @@ textwindows int64_t CreateFileMappingNuma(
opt_hFile, opt_lpFileMappingAttributes, flProtect, dwMaximumSizeHigh,
dwMaximumSizeLow, opt_lpName, nndDesiredNumaNode);
if (!hHandle) __winerr();
STRACE("CreateFileMappingNuma(%ld, %s, %s, %'zu, %#hs) → %ld% m", opt_hFile,
DescribeNtSecurityAttributes(opt_lpFileMappingAttributes),
DescribeNtPageFlags(flProtect),
(uint64_t)dwMaximumSizeHigh << 32 | dwMaximumSizeLow, opt_lpName,
hHandle);
NTTRACE("CreateFileMappingNuma(%ld, %s, %s, %'zu, %#hs) → %ld% m", opt_hFile,
DescribeNtSecurityAttributes(opt_lpFileMappingAttributes),
DescribeNtPageFlags(flProtect),
(uint64_t)dwMaximumSizeHigh << 32 | dwMaximumSizeLow, opt_lpName,
hHandle);
return hHandle;
}

View file

@ -41,10 +41,10 @@ textwindows int64_t CreateNamedPipe(
nMaxInstances, nOutBufferSize, nInBufferSize,
nDefaultTimeOutMs, opt_lpSecurityAttributes);
if (hServer == -1) __winerr();
STRACE("CreateNamedPipe(%#hs, %s, %s, %u, %'u, %'u, %'u, %s) → %ld% m",
lpName, DescribeNtPipeOpenFlags(dwOpenMode),
DescribeNtPipeModeFlags(dwPipeMode), nMaxInstances, nOutBufferSize,
nInBufferSize, nDefaultTimeOutMs,
DescribeNtSecurityAttributes(opt_lpSecurityAttributes), hServer);
NTTRACE("CreateNamedPipe(%#hs, %s, %s, %u, %'u, %'u, %'u, %s) → %ld% m",
lpName, DescribeNtPipeOpenFlags(dwOpenMode),
DescribeNtPipeModeFlags(dwPipeMode), nMaxInstances, nOutBufferSize,
nInBufferSize, nDefaultTimeOutMs,
DescribeNtSecurityAttributes(opt_lpSecurityAttributes), hServer);
return hServer;
}

View file

@ -36,8 +36,8 @@ textwindows bool32 CreatePipe(
ok = __imp_CreatePipe(out_hReadPipe, out_hWritePipe, opt_lpPipeAttributes,
nSize);
if (!ok) __winerr();
STRACE("CreatePipe([%ld], [%ld], %s, %'zu) → %hhhd% m", *out_hReadPipe,
*out_hWritePipe, DescribeNtSecurityAttributes(opt_lpPipeAttributes),
nSize, ok);
NTTRACE("CreatePipe([%ld], [%ld], %s, %'zu) → %hhhd% m", *out_hReadPipe,
*out_hWritePipe, DescribeNtSecurityAttributes(opt_lpPipeAttributes),
nSize, ok);
return ok;
}

View file

@ -44,7 +44,7 @@ CreateProcess(const char16_t *opt_lpApplicationName, char16_t *lpCommandLine,
opt_lpCurrentDirectory, lpStartupInfo,
opt_out_lpProcessInformation);
if (!ok) __winerr();
STRACE(
NTTRACE(
"CreateFile(%#hs, %#hs, %s, %s, %hhhd, %u, %p, %#hs, %p, %p) → %hhhd% m",
opt_lpApplicationName, lpCommandLine,
DescribeNtSecurityAttributes(opt_lpProcessAttributes),

View file

@ -39,8 +39,9 @@ textwindows int64_t CreateThread(
hHandle = __imp_CreateThread(lpThreadAttributes, dwStackSize, lpStartAddress,
lpParameter, dwCreationFlags, opt_lpThreadId);
if (hHandle == -1) __winerr();
STRACE("CreateThread(%s, %'zu, %p, %p, %s, %p) → %ld% m",
DescribeNtSecurityAttributes(lpThreadAttributes), dwStackSize,
lpStartAddress, lpParameter, dwCreationFlags, opt_lpThreadId, hHandle);
NTTRACE("CreateThread(%s, %'zu, %p, %p, %s, %p) → %ld% m",
DescribeNtSecurityAttributes(lpThreadAttributes), dwStackSize,
lpStartAddress, lpParameter, dwCreationFlags, opt_lpThreadId,
hHandle);
return hHandle;
}

View file

@ -36,7 +36,6 @@
void __cxa_finalize(void *pred) {
unsigned i, mask;
struct CxaAtexitBlock *b, *b2;
STRACE("__cxa_finalize()");
StartOver:
if ((b = __cxa_blocks.p)) {
for (;;) {
@ -47,6 +46,7 @@ StartOver:
if (!pred || pred == b->p[i].pred) {
b->mask &= ~(1u << i);
if (b->p[i].fp) {
STRACE("__cxa_finalize(%t, %p)", b->p[i].fp, b->p[i].arg);
((void (*)(void *))b->p[i].fp)(b->p[i].arg);
goto StartOver;
}

View file

@ -32,6 +32,6 @@ textwindows bool32 DeleteFile(const char16_t *lpPathName) {
bool32 ok;
ok = __imp_DeleteFileW(lpPathName);
if (!ok) __winerr();
STRACE("DeleteFile(%#hs) → %hhhd% m", lpPathName, ok);
NTTRACE("DeleteFile(%#hs) → %hhhd% m", lpPathName, ok);
return ok;
}

View file

@ -40,8 +40,8 @@ textwindows bool32 DeviceIoControl(int64_t hDevice, uint32_t dwIoControlCode,
nInBufferSize, lpOutBuffer, nOutBufferSize,
lpBytesReturned, lpOverlapped);
if (!ok) __winerr();
STRACE("DeviceIoControl(%ld, %#x, %p, %'zu, %p, %'zu, %p, %p) → %hhhd% m",
hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer,
nOutBufferSize, lpBytesReturned, lpOverlapped, ok);
NTTRACE("DeviceIoControl(%ld, %#x, %p, %'zu, %p, %'zu, %p, %p) → %hhhd% m",
hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer,
nOutBufferSize, lpBytesReturned, lpOverlapped, ok);
return ok;
}

View file

@ -31,6 +31,6 @@ textwindows bool32 FindClose(int64_t hFindFile) {
bool32 ok;
ok = __imp_FindClose(hFindFile);
if (!ok) __winerr();
STRACE("FindClose(%ld) → %hhhd% m", hFindFile, ok);
NTTRACE("FindClose(%ld) → %hhhd% m", hFindFile, ok);
return ok;
}

View file

@ -35,7 +35,7 @@ textwindows int64_t FindFirstFile(const char16_t *lpFileName,
int64_t hFindFile;
hFindFile = __imp_FindFirstFileW(lpFileName, out_lpFindFileData);
if (hFindFile != -1) {
STRACE(
NTTRACE(
"FindFirstFile(%#hs, [{"
".cFileName=%#hs, "
".dwFileAttributes=%s, "
@ -46,7 +46,7 @@ textwindows int64_t FindFirstFile(const char16_t *lpFileName,
DescribeNtFiletypeFlags(out_lpFindFileData->dwFileType), hFindFile);
} else {
__winerr();
STRACE("FindFirstFile(%#hs, [n/a]) → %ld% m", lpFileName, hFindFile);
NTTRACE("FindFirstFile(%#hs, [n/a]) → %ld% m", lpFileName, hFindFile);
}
return hFindFile;
}

View file

@ -37,7 +37,7 @@ textwindows bool32 FindNextFile(int64_t hFindFile,
bool32 ok;
ok = __imp_FindNextFileW(hFindFile, out_lpFindFileData);
if (ok) {
STRACE(
NTTRACE(
"FindNextFile(%ld, [{"
".cFileName=%#hs, "
".dwFileAttributes=%s, "
@ -48,7 +48,7 @@ textwindows bool32 FindNextFile(int64_t hFindFile,
DescribeNtFiletypeFlags(out_lpFindFileData->dwFileType), ok);
} else {
if (GetLastError() != kNtErrorNoMoreFiles) __winerr();
STRACE("FindNextFile(%ld) → %hhhd% m", hFindFile, ok);
NTTRACE("FindNextFile(%ld) → %hhhd% m", hFindFile, ok);
}
return ok;
}

View file

@ -38,6 +38,6 @@ textwindows bool32 FlushFileBuffers(int64_t hFile) {
bool32 ok;
ok = __imp_FlushFileBuffers(hFile);
if (!ok) __winerr();
STRACE("FlushFileBuffers(%ld) → %hhhd% m", hFile, ok);
NTTRACE("FlushFileBuffers(%ld) → %hhhd% m", hFile, ok);
return ok;
}

View file

@ -37,7 +37,7 @@ textwindows bool32 FlushViewOfFile(const void *lpBaseAddress,
bool32 ok;
ok = __imp_FlushViewOfFile(lpBaseAddress, dwNumberOfBytesToFlush);
if (!ok) __winerr();
STRACE("FlushViewOfFile(%p, %'zu) → %hhhd% m", lpBaseAddress,
dwNumberOfBytesToFlush, ok);
NTTRACE("FlushViewOfFile(%p, %'zu) → %hhhd% m", lpBaseAddress,
dwNumberOfBytesToFlush, ok);
return ok;
}

View file

@ -35,7 +35,7 @@ textwindows bool32 GenerateConsoleCtrlEvent(uint32_t dwCtrlEvent,
bool32 ok;
ok = __imp_GenerateConsoleCtrlEvent(dwCtrlEvent, dwProcessGroupId);
if (!ok) __winerr();
STRACE("GenerateConsoleCtrlEvent(%x, %d) → %hhhd% m", dwCtrlEvent,
dwProcessGroupId, ok);
NTTRACE("GenerateConsoleCtrlEvent(%x, %d) → %hhhd% m", dwCtrlEvent,
dwProcessGroupId, ok);
return ok;
}

View file

@ -21,6 +21,7 @@
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/libfatal.internal.h"
#include "libc/runtime/runtime.h"
forceinline int Identity(int c) {
@ -66,6 +67,11 @@ char *getenv(const char *s) {
} else {
r = GetEnv(s, ToUpper);
}
STRACE("getenv(%#s) → %#s", s, r);
#if SYSDEBUG
if (!(s[0] == 'T' && s[1] == 'Z' && !s[2])) {
// TODO(jart): memoize TZ or something
STRACE("getenv(%#s) → %#s", s, r);
}
#endif
return r;
}

View file

@ -35,7 +35,7 @@ textwindows uint32_t GetFileAttributes(const char16_t *lpPathName) {
uint32_t flags;
flags = __imp_GetFileAttributesW(lpPathName);
if (flags == -1u) __winerr();
STRACE("GetFileAttributes(%#hs) → %s% m", lpPathName,
DescribeNtFileFlagsAndAttributes(flags));
NTTRACE("GetFileAttributes(%#hs) → %s% m", lpPathName,
DescribeNtFileFlagsAndAttributes(flags));
return flags;
}

View file

@ -40,6 +40,7 @@
#include "libc/nt/winsock.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/str/str.h"
#include "libc/str/tpenc.h"
#include "libc/str/utf16.h"
@ -515,19 +516,27 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
case 'G':
x = va_arg(va, int);
if (weaken(strsignal)) {
s = weaken(strsignal)(x);
if (weaken(strsignal) && (s = weaken(strsignal)(x))) {
goto FormatString;
} else {
if (p + 3 <= e) {
p[0] = 'S';
p[1] = 'I';
p[2] = 'G';
}
p += 3;
goto FormatDecimal;
}
case 't': {
int idx;
x = va_arg(va, intptr_t);
if (weaken(__get_symbol) &&
(idx = weaken(__get_symbol)(0, x)) != -1) {
if (p + 1 <= e) *p++ = '&';
s = weaken(GetSymbolTable)()->name_base +
weaken(GetSymbolTable)()->names[idx];
goto FormatString;
}
base = 4;
hash = '&';
goto FormatNumber;
}
case 'n':
// nonstandard %n specifier
// used to print newlines that work in raw terminal modes
@ -558,8 +567,8 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
if (!(s = va_arg(va, const void *))) {
s = sign != ' ' ? "NULL" : "";
FormatString:
type = 0;
hash = 0;
type = 0;
} else if (!dang && (kisdangerous(s) || kischarmisaligned(s, type))) {
if (sign == ' ') {
if (p < e) *p = ' ';
@ -847,6 +856,7 @@ privileged void kvprintf(const char *fmt, va_list v) {
* - `o` octal
* - `b` binary
* - `s` string
* - `t` symbol
* - `p` pointer
* - `d` decimal
* - `n` newline
@ -885,6 +895,10 @@ privileged void kvprintf(const char *fmt, va_list v) {
* - `% m` formats error with leading space if errno isn't zero
* - `%lm` means favor WSAGetLastError() over GetLastError() if linked
*
* You need to link and load the symbol table before `%t` will work. You
* can do that by calling `GetSymbolTable()`. If that hasn't happened it
* will print `&hexnumber` instead.
*
* @asyncsignalsafe
* @vforksafe
*/

View file

@ -45,9 +45,9 @@ textwindows void *MapViewOfFileEx(int64_t hFileMappingObject,
hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh, dwFileOffsetLow,
dwNumberOfBytesToMap, opt_lpDesiredBaseAddress);
if (!pStartingAddress) __winerr();
STRACE("MapViewOfFileEx(%ld, %s, %'ld, %'zu, %p) → %p% m", hFileMappingObject,
DescribeNtFileMapFlags(dwDesiredAccess),
(uint64_t)dwFileOffsetHigh << 32 | dwFileOffsetLow,
dwNumberOfBytesToMap, opt_lpDesiredBaseAddress, pStartingAddress);
NTTRACE("MapViewOfFileEx(%ld, %s, %'ld, %'zu, %p) → %p% m",
hFileMappingObject, DescribeNtFileMapFlags(dwDesiredAccess),
(uint64_t)dwFileOffsetHigh << 32 | dwFileOffsetLow,
dwNumberOfBytesToMap, opt_lpDesiredBaseAddress, pStartingAddress);
return pStartingAddress;
}

View file

@ -47,9 +47,9 @@ textwindows void *MapViewOfFileExNuma(int64_t hFileMappingObject,
hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh, dwFileOffsetLow,
dwNumberOfBytesToMap, opt_lpDesiredBaseAddress, nndDesiredNumaNode);
if (!pStartingAddress) __winerr();
STRACE("MapViewOfFileExNuma(%ld, %s, %'ld, %'zu, %p) → %p% m",
hFileMappingObject, DescribeNtFileMapFlags(dwDesiredAccess),
(uint64_t)dwFileOffsetHigh << 32 | dwFileOffsetLow,
dwNumberOfBytesToMap, opt_lpDesiredBaseAddress, pStartingAddress);
NTTRACE("MapViewOfFileExNuma(%ld, %s, %'ld, %'zu, %p) → %p% m",
hFileMappingObject, DescribeNtFileMapFlags(dwDesiredAccess),
(uint64_t)dwFileOffsetHigh << 32 | dwFileOffsetLow,
dwNumberOfBytesToMap, opt_lpDesiredBaseAddress, pStartingAddress);
return pStartingAddress;
}

View file

@ -39,8 +39,8 @@ textwindows int64_t OpenProcess(uint32_t dwDesiredAccess, bool32 bInheritHandle,
int64_t hHandle;
hHandle = __imp_OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
if (!hHandle) __winerr();
STRACE("OpenProcess(%s, %hhhd, %u) → %ld% m",
DescribeNtProcessAccessFlags(dwDesiredAccess), bInheritHandle,
dwProcessId, hHandle);
NTTRACE("OpenProcess(%s, %hhhd, %u) → %ld% m",
DescribeNtProcessAccessFlags(dwDesiredAccess), bInheritHandle,
dwProcessId, hHandle);
return hHandle;
}

View file

@ -32,6 +32,6 @@ textwindows bool32 RemoveDirectory(const char16_t *lpPathName) {
bool32 ok;
ok = __imp_RemoveDirectoryW(lpPathName);
if (!ok) __winerr();
STRACE("RemoveDirectory(%#hs) → %hhhd% m", lpPathName, ok);
NTTRACE("RemoveDirectory(%#hs) → %hhhd% m", lpPathName, ok);
return ok;
}

View file

@ -36,9 +36,9 @@ int64_t ReOpenFile(int64_t hOriginalFile, uint32_t dwDesiredAccess,
hHandle = __imp_ReOpenFile(hOriginalFile, dwDesiredAccess, dwShareMode,
dwFlagsAndAttributes);
if (hHandle == -1) __winerr();
STRACE("ReOpenFile(%ld, %s, %s, %s) → %ld% m", hOriginalFile,
DescribeNtFileAccessFlags(dwDesiredAccess),
DescribeNtFileShareFlags(dwShareMode),
DescribeNtFileFlagsAndAttributes(dwFlagsAndAttributes), hHandle);
NTTRACE("ReOpenFile(%ld, %s, %s, %s) → %ld% m", hOriginalFile,
DescribeNtFileAccessFlags(dwDesiredAccess),
DescribeNtFileShareFlags(dwShareMode),
DescribeNtFileFlagsAndAttributes(dwFlagsAndAttributes), hHandle);
return hHandle;
}

View file

@ -37,7 +37,7 @@ const char kConsoleHandles[3] = {
noasan void __restorewintty(void) {
int i;
if (!IsWindows()) return;
STRACE("__restorewintty()");
NTTRACE("__restorewintty()");
if (GetCurrentProcessId() == __winmainpid) {
for (i = 0; i < 3; ++i) {
SetConsoleMode(GetStdHandle(kConsoleHandles[i]), __ntconsolemode[i]);

View file

@ -32,6 +32,6 @@ textwindows bool32 SetCurrentDirectory(const char16_t *lpPathName) {
bool32 ok;
ok = __imp_SetCurrentDirectoryW(lpPathName);
if (!ok) __winerr();
STRACE("SetCurrentDirectory(%#hs) → %hhhd% m", lpPathName, ok);
NTTRACE("SetCurrentDirectory(%#hs) → %hhhd% m", lpPathName, ok);
return ok;
}

View file

@ -32,6 +32,6 @@ textwindows bool32 TerminateProcess(int64_t hProcess, uint32_t uExitCode) {
bool32 ok;
ok = __imp_TerminateProcess(hProcess, uExitCode);
if (!ok) __winerr();
STRACE("TerminateProcess(%ld, %u) → %hhhd% m", hProcess, uExitCode, ok);
NTTRACE("TerminateProcess(%ld, %u) → %hhhd% m", hProcess, uExitCode, ok);
return ok;
}

View file

@ -30,6 +30,6 @@ textwindows bool32 UnmapViewOfFile(const void *lpBaseAddress) {
bool32 ok;
ok = __imp_UnmapViewOfFile(lpBaseAddress);
if (!ok) __winerr();
STRACE("UnmapViewOfFile(%p) → %hhhd% m", lpBaseAddress, ok);
NTTRACE("UnmapViewOfFile(%p) → %hhhd% m", lpBaseAddress, ok);
return ok;
}

View file

@ -40,7 +40,7 @@ textwindows bool32 VirtualProtect(void *lpAddress, uint64_t dwSize,
__winerr();
__stpcpy(oldbuf, "n/a");
}
STRACE("VirtualProtect(%p, %'zu, %s, [%s]) → %hhhd% m", lpAddress, dwSize,
DescribeNtPageFlags(flNewProtect), oldbuf, bOk);
NTTRACE("VirtualProtect(%p, %'zu, %s, [%s]) → %hhhd% m", lpAddress, dwSize,
DescribeNtPageFlags(flNewProtect), oldbuf, bOk);
return bOk;
}