xref: /freebsd/contrib/llvm-project/compiler-rt/lib/memprof/memprof_linux.cpp (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 //===-- memprof_linux.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of MemProfiler, a memory profiler.
10 //
11 // Linux-specific details.
12 //===----------------------------------------------------------------------===//
13 
14 #include "sanitizer_common/sanitizer_platform.h"
15 #if !SANITIZER_LINUX
16 #error Unsupported OS
17 #endif
18 
19 #include "memprof_interceptors.h"
20 #include "memprof_internal.h"
21 #include "memprof_thread.h"
22 #include "sanitizer_common/sanitizer_flags.h"
23 #include "sanitizer_common/sanitizer_freebsd.h"
24 #include "sanitizer_common/sanitizer_libc.h"
25 #include "sanitizer_common/sanitizer_procmaps.h"
26 
27 #include <dlfcn.h>
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <link.h>
31 #include <pthread.h>
32 #include <stdio.h>
33 #include <sys/mman.h>
34 #include <sys/resource.h>
35 #include <sys/syscall.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <sys/ucontext.h>
39 #include <unistd.h>
40 #include <unwind.h>
41 
42 extern ElfW(Dyn) _DYNAMIC[];
43 
44 typedef enum {
45   MEMPROF_RT_VERSION_UNDEFINED = 0,
46   MEMPROF_RT_VERSION_DYNAMIC,
47   MEMPROF_RT_VERSION_STATIC,
48 } memprof_rt_version_t;
49 
50 // FIXME: perhaps also store abi version here?
51 extern "C" {
52 SANITIZER_INTERFACE_ATTRIBUTE
53 memprof_rt_version_t __memprof_rt_version;
54 }
55 
56 namespace __memprof {
57 
58 void InitializePlatformInterceptors() {}
59 void InitializePlatformExceptionHandlers() {}
60 
61 void *MemprofDoesNotSupportStaticLinkage() {
62   // This will fail to link with -static.
63   return &_DYNAMIC;
64 }
65 
66 uptr FindDynamicShadowStart() {
67   uptr shadow_size_bytes = MemToShadowSize(kHighMemEnd);
68   return MapDynamicShadow(shadow_size_bytes, SHADOW_SCALE,
69                           /*min_shadow_base_alignment*/ 0, kHighMemEnd);
70 }
71 
72 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
73   ucontext_t *ucp = (ucontext_t *)context;
74   *stack = (uptr)ucp->uc_stack.ss_sp;
75   *ssize = ucp->uc_stack.ss_size;
76 }
77 
78 void *MemprofDlSymNext(const char *sym) { return dlsym(RTLD_NEXT, sym); }
79 
80 } // namespace __memprof
81