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
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/dce.h"
|
2022-08-14 00:20:50 +00:00
|
|
|
#include "libc/errno.h"
|
2022-07-23 13:47:01 +00:00
|
|
|
#include "libc/intrin/promises.internal.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/log/libfatal.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/log/log.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/nexgen32e/vendor.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/struct/teb.h"
|
2022-05-28 12:50:01 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
|
|
|
|
2021-10-14 00:27:13 +00:00
|
|
|
#define kBufSize 1024
|
|
|
|
#define kPid "TracerPid:\t"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-05-28 12:50:01 +00:00
|
|
|
static textwindows noasan bool IsBeingDebugged(void) {
|
|
|
|
return !!NtGetPeb()->BeingDebugged;
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* Determines if gdb, strace, windbg, etc. is controlling process.
|
|
|
|
* @return non-zero if attached, otherwise 0
|
|
|
|
*/
|
2022-05-28 12:50:01 +00:00
|
|
|
int IsDebuggerPresent(bool force) {
|
2021-10-14 00:27:13 +00:00
|
|
|
/* asan runtime depends on this function */
|
2020-06-15 14:18:57 +00:00
|
|
|
ssize_t got;
|
2022-08-06 10:51:50 +00:00
|
|
|
int e, fd, res;
|
2021-10-14 00:27:13 +00:00
|
|
|
char *p, buf[1024];
|
2022-04-18 07:01:26 +00:00
|
|
|
if (!force && IsGenuineCosmo()) return 0;
|
2022-05-28 12:50:01 +00:00
|
|
|
if (!force && __getenv(environ, "HEISENDEBUG")) return 0;
|
|
|
|
if (IsWindows()) return IsBeingDebugged();
|
2022-04-18 15:54:42 +00:00
|
|
|
if (__isworker) return false;
|
2022-07-23 13:47:01 +00:00
|
|
|
if (!PLEDGED(RPATH)) return false;
|
2022-04-18 07:01:26 +00:00
|
|
|
res = 0;
|
2022-08-06 10:51:50 +00:00
|
|
|
e = errno;
|
2022-04-18 07:01:26 +00:00
|
|
|
if ((fd = __sysv_open("/proc/self/status", O_RDONLY, 0)) >= 0) {
|
|
|
|
if ((got = __sysv_read(fd, buf, sizeof(buf) - 1)) > 0) {
|
|
|
|
buf[got] = '\0';
|
|
|
|
if ((p = __strstr(buf, kPid))) {
|
|
|
|
p += sizeof(kPid) - 1;
|
|
|
|
res = __atoul(p);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-18 07:01:26 +00:00
|
|
|
__sysv_close(fd);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-08-06 10:51:50 +00:00
|
|
|
errno = e;
|
2022-04-18 07:01:26 +00:00
|
|
|
return res;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|