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│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2021-10-14 00:27:13 +00:00
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
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/calls/calls.h"
|
|
|
|
#include "libc/dce.h"
|
2022-10-10 05:38:28 +00:00
|
|
|
#include "libc/errno.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
2022-08-20 19:32:51 +00:00
|
|
|
#include "libc/intrin/atomic.h"
|
2022-07-16 01:07:34 +00:00
|
|
|
#include "libc/intrin/promises.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/runtime/stack.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/o.h"
|
|
|
|
|
2021-10-14 00:27:13 +00:00
|
|
|
static char stacklog[1024];
|
2021-02-03 14:22:51 +00:00
|
|
|
|
2023-06-09 08:23:18 +00:00
|
|
|
noasan size_t GetStackUsage(char *s, size_t n) {
|
2022-05-18 23:41:29 +00:00
|
|
|
// RHEL5 MAP_GROWSDOWN seems to only grow to 68kb :'(
|
|
|
|
// So we count non-zero bytes down from the top
|
|
|
|
// First clear 64 bytes is considered the end
|
|
|
|
long *p;
|
|
|
|
size_t got;
|
|
|
|
p = (long *)(s + n);
|
|
|
|
got = 0;
|
|
|
|
for (;;) {
|
|
|
|
p -= 8;
|
|
|
|
if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5] | p[6] | p[7]) {
|
|
|
|
++got;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return got * 8 * sizeof(long);
|
2021-02-03 14:22:51 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-10-14 00:27:13 +00:00
|
|
|
static textexit void LogStackUse(void) {
|
|
|
|
bool quote;
|
|
|
|
char *p, *q;
|
2022-10-10 05:38:28 +00:00
|
|
|
int i, e, fd;
|
2021-10-14 00:27:13 +00:00
|
|
|
size_t n, usage;
|
2022-07-23 13:47:01 +00:00
|
|
|
if (!PLEDGED(STDIO) || !PLEDGED(WPATH) || !PLEDGED(CPATH)) return;
|
2022-07-11 12:55:17 +00:00
|
|
|
usage = GetStackUsage((char *)GetStackAddr(), GetStackSize());
|
2022-10-10 05:38:28 +00:00
|
|
|
e = errno;
|
|
|
|
if ((fd = open(stacklog, O_APPEND | O_CREAT | O_WRONLY, 0644)) != -1) {
|
|
|
|
p = FormatUint64(stacklog, usage);
|
|
|
|
for (i = 0; i < __argc; ++i) {
|
|
|
|
n = strlen(__argv[i]);
|
|
|
|
if ((q = memchr(__argv[i], '\n', n))) n = q - __argv[i];
|
|
|
|
if (p - stacklog + 1 + 1 + n + 1 + 1 < sizeof(stacklog)) {
|
|
|
|
quote = !!memchr(__argv[i], ' ', n);
|
|
|
|
*p++ = ' ';
|
|
|
|
if (quote) *p++ = '\'';
|
|
|
|
p = mempcpy(p, __argv[i], n);
|
|
|
|
if (quote) *p++ = '\'';
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-10-10 05:38:28 +00:00
|
|
|
*p++ = '\n';
|
|
|
|
write(fd, stacklog, p - stacklog);
|
|
|
|
close(fd);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-10-10 05:38:28 +00:00
|
|
|
errno = e;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
|
|
|
|
static textstartup void LogStackUseInit(void) {
|
2022-07-16 01:07:34 +00:00
|
|
|
if (!PLEDGED(WPATH)) return;
|
2021-10-14 00:27:13 +00:00
|
|
|
if (isdirectory("o/" MODE) &&
|
|
|
|
getcwd(stacklog, sizeof(stacklog) - strlen("/o/" MODE "/stack.log"))) {
|
|
|
|
strcat(stacklog, "/o/" MODE "/stack.log");
|
|
|
|
atexit(LogStackUse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *const stack_usage_logging[] initarray = {
|
|
|
|
LogStackUseInit,
|
|
|
|
};
|