Improve system call support

This commit is contained in:
Justine Tunney 2021-08-25 21:35:58 -07:00
parent 63b867bd2f
commit 3085ac7837
65 changed files with 900 additions and 544 deletions

View file

@ -19,7 +19,9 @@
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/log/color.internal.h"
#include "libc/log/gdb.h"
#include "libc/log/internal.h"
#include "libc/log/log.h"
#include "libc/nexgen32e/stackframe.h"
#include "libc/nexgen32e/vendor.internal.h"
@ -32,8 +34,6 @@
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/w.h"
#define RESTORE_TTY "\e[?1000;1002;1015;1006l\e[?25h"
/**
* Launches GDB debugger GUI for current process.
*
@ -59,7 +59,7 @@ relegated int(attachdebugger)(intptr_t continuetoaddr) {
(ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) == -1) {
return -1;
}
write(ttyfd, RESTORE_TTY, strlen(RESTORE_TTY));
__restore_tty(ttyfd);
snprintf(pidstr, sizeof(pidstr), "%u", getpid());
layout = "layout asm";
if ((elf = FindDebugBinary())) {

View file

@ -17,9 +17,11 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/log/color.internal.h"
#include "libc/log/log.h"
#include "libc/nt/enum/version.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
@ -47,7 +49,9 @@ bool cancolor(void) {
static bool once;
static bool result;
if (!once) {
result = !!strcmp(nulltoempty(getenv("DONTANSIMEBRO")), "1") &&
result = (!IsWindows() || NtGetVersion() >= kNtVersionWindows10 ||
!ischardev(1)) &&
!!strcmp(nulltoempty(getenv("DONTANSIMEBRO")), "1") &&
!!strcmp(nulltoempty(getenv("TERM")), "dumb");
once = true;
}

View file

@ -30,7 +30,7 @@
relegated wontreturn void __die(void) {
static bool once;
if (cmpxchg(&once, false, true)) {
__restore_tty();
__restore_tty(1);
if (IsDebuggerPresent(false)) DebugBreak();
ShowBacktrace(2, NULL);
}

View file

@ -14,7 +14,7 @@ extern struct sigaction g_oldcrashacts[8] hidden;
void __start_fatal(const char *, int) hidden;
void __start_fatal_ndebug(void) hidden;
void __oncrash(int, struct siginfo *, struct ucontext *) relegated;
void __restore_tty(void);
void __restore_tty(int);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -265,7 +265,7 @@ relegated void __oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
: 0);
}
if (gdbpid > 0 && (sig == SIGTRAP || sig == SIGQUIT)) return;
__restore_tty();
__restore_tty(1);
ShowCrashReport(err, STDERR_FILENO, sig, si, ctx);
exit(128 + sig);
unreachable;

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/calls.h"
#include "libc/calls/termios.h"
#include "libc/log/color.internal.h"
#include "libc/log/internal.h"
#include "libc/str/str.h"
@ -36,9 +37,9 @@ const void *const g_oldtermios_ctor[] initarray = {
g_oldtermios_init,
};
void __restore_tty(void) {
if (g_oldtermios.c_lflag && isatty(1)) {
write(1, ANSI_RESTORE, strlen(ANSI_RESTORE));
tcsetattr(1, TCSAFLUSH, &g_oldtermios);
void __restore_tty(int fd) {
if (g_oldtermios.c_lflag && isatty(fd) && cancolor()) {
write(fd, ANSI_RESTORE, strlen(ANSI_RESTORE));
tcsetattr(fd, TCSAFLUSH, &g_oldtermios);
}
}

View file

@ -32,7 +32,7 @@
relegated void __start_fatal(const char *file, int line) {
bool colorful;
char s[16 + 16 + 16 + 16 + PATH_MAX + 16 + NAME_MAX + 16], *p = s;
__restore_tty();
__restore_tty(1);
colorful = cancolor();
*p++ = '\r';
if (colorful) p = stpcpy(p, "\e[J\e[30;101m");

View file

@ -29,7 +29,7 @@
*/
relegated void __start_fatal_ndebug(void) {
char s[16 + 16 + 16 + 16 + PATH_MAX + 16], *p = s;
__restore_tty();
__restore_tty(1);
*p++ = '\r';
if (cancolor()) p = stpcpy(p, "\e[J");
p = stpcpy(p, "error:");