2021-07-12 06:17:47 +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 │
|
|
|
|
│ │
|
|
|
|
│ 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. │
|
|
|
|
│ │
|
|
|
|
│ 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. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-05-19 23:57:49 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2022-09-09 11:07:08 +00:00
|
|
|
#include "libc/errno.h"
|
2022-05-18 23:41:29 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
2022-05-20 11:46:42 +00:00
|
|
|
#include "libc/intrin/cmpxchg.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2022-09-10 18:55:39 +00:00
|
|
|
#include "libc/intrin/nopl.internal.h"
|
2021-07-12 06:17:47 +00:00
|
|
|
#include "libc/macros.internal.h"
|
|
|
|
#include "libc/nexgen32e/stackframe.h"
|
2022-07-11 15:04:58 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2023-05-09 08:56:56 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2022-05-18 23:41:29 +00:00
|
|
|
#include "libc/runtime/stack.h"
|
2021-07-12 06:17:47 +00:00
|
|
|
#include "libc/runtime/symbols.internal.h"
|
2022-09-10 09:56:25 +00:00
|
|
|
#include "libc/thread/tls.h"
|
|
|
|
#include "libc/thread/tls2.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
|
2021-07-12 06:17:47 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Plain-text function call logging.
|
|
|
|
*
|
|
|
|
* Able to log ~2 million function calls per second, which is mostly
|
|
|
|
* bottlenecked by system call overhead. Log size is reasonable if piped
|
|
|
|
* into gzip.
|
|
|
|
*/
|
|
|
|
|
2023-06-06 06:35:31 +00:00
|
|
|
#define MAX_NESTING 512
|
|
|
|
|
|
|
|
#ifdef __x86_64__
|
|
|
|
#define DETOUR_SKEW 2
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
#define DETOUR_SKEW 8
|
|
|
|
#endif
|
|
|
|
|
2021-07-12 06:17:47 +00:00
|
|
|
void ftrace_hook(void);
|
|
|
|
|
2022-07-11 15:04:58 +00:00
|
|
|
static int g_stackdigs;
|
2022-09-10 09:56:25 +00:00
|
|
|
static struct CosmoFtrace g_ftrace;
|
2022-07-11 12:55:17 +00:00
|
|
|
|
2022-05-21 14:52:58 +00:00
|
|
|
static privileged inline int GetNestingLevelImpl(struct StackFrame *frame) {
|
2021-07-12 06:17:47 +00:00
|
|
|
int nesting = -2;
|
|
|
|
while (frame) {
|
|
|
|
++nesting;
|
|
|
|
frame = frame->next;
|
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
return MAX(0, nesting);
|
|
|
|
}
|
|
|
|
|
2022-09-10 09:56:25 +00:00
|
|
|
static privileged inline int GetNestingLevel(struct CosmoFtrace *ft,
|
2022-07-11 15:04:58 +00:00
|
|
|
struct StackFrame *sf) {
|
2021-09-28 05:58:51 +00:00
|
|
|
int nesting;
|
2022-07-11 15:04:58 +00:00
|
|
|
nesting = GetNestingLevelImpl(sf);
|
2022-09-10 09:56:25 +00:00
|
|
|
if (nesting < ft->ft_skew) ft->ft_skew = nesting;
|
|
|
|
nesting -= ft->ft_skew;
|
2021-09-28 05:58:51 +00:00
|
|
|
return MIN(MAX_NESTING, nesting);
|
2021-07-12 06:17:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints name of function being called.
|
|
|
|
*
|
|
|
|
* We insert CALL instructions that point to this function, in the
|
|
|
|
* prologues of other functions. We assume those functions behave
|
|
|
|
* according to the System Five NexGen32e ABI.
|
|
|
|
*/
|
2022-05-18 23:41:29 +00:00
|
|
|
privileged void ftracer(void) {
|
2023-06-06 06:35:31 +00:00
|
|
|
uintptr_t fn;
|
2022-05-19 23:57:49 +00:00
|
|
|
long stackuse;
|
2022-11-02 05:36:03 +00:00
|
|
|
struct CosmoTib *tib;
|
2022-07-11 15:04:58 +00:00
|
|
|
struct StackFrame *sf;
|
2022-11-02 05:36:03 +00:00
|
|
|
struct CosmoFtrace *ft;
|
2023-05-09 08:56:56 +00:00
|
|
|
if (__ftrace <= 0) return;
|
2022-09-09 11:07:08 +00:00
|
|
|
if (__tls_enabled) {
|
2022-11-02 05:36:03 +00:00
|
|
|
tib = __get_tls_privileged();
|
|
|
|
if (tib->tib_ftrace <= 0) return;
|
|
|
|
ft = &tib->tib_ftracer;
|
2022-09-09 11:07:08 +00:00
|
|
|
} else {
|
|
|
|
ft = &g_ftrace;
|
|
|
|
}
|
2022-09-10 09:56:25 +00:00
|
|
|
if (_cmpxchg(&ft->ft_once, false, true)) {
|
|
|
|
ft->ft_lastaddr = -1;
|
|
|
|
ft->ft_skew = GetNestingLevelImpl(__builtin_frame_address(0));
|
2022-07-11 15:04:58 +00:00
|
|
|
}
|
2022-09-10 09:56:25 +00:00
|
|
|
if (_cmpxchg(&ft->ft_noreentry, false, true)) {
|
2022-07-11 15:04:58 +00:00
|
|
|
sf = __builtin_frame_address(0);
|
|
|
|
sf = sf->next;
|
2023-06-06 06:35:31 +00:00
|
|
|
fn = sf->addr + DETOUR_SKEW;
|
|
|
|
if (fn != ft->ft_lastaddr) {
|
2022-07-11 15:04:58 +00:00
|
|
|
stackuse = GetStackAddr() + GetStackSize() - (intptr_t)sf;
|
|
|
|
kprintf("%rFUN %6P %'13T %'*ld %*s%t\n", g_stackdigs, stackuse,
|
2023-06-06 06:35:31 +00:00
|
|
|
GetNestingLevel(ft, sf) * 2, "", fn);
|
|
|
|
ft->ft_lastaddr = fn;
|
2022-05-19 23:57:49 +00:00
|
|
|
}
|
2022-09-10 09:56:25 +00:00
|
|
|
ft->ft_noreentry = false;
|
2021-07-12 06:17:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 01:55:28 +00:00
|
|
|
textstartup int ftrace_install(void) {
|
2022-04-16 17:40:23 +00:00
|
|
|
if (GetSymbolTable()) {
|
2022-07-11 15:04:58 +00:00
|
|
|
g_stackdigs = LengthInt64Thousands(GetStackSize());
|
2022-04-23 01:55:28 +00:00
|
|
|
return __hook(ftrace_hook, GetSymbolTable());
|
2021-07-12 06:17:47 +00:00
|
|
|
} else {
|
2022-05-20 00:46:14 +00:00
|
|
|
kprintf("error: --ftrace failed to open symbol table\n");
|
2022-04-23 01:55:28 +00:00
|
|
|
return -1;
|
2021-07-12 06:17:47 +00:00
|
|
|
}
|
|
|
|
}
|