memprof_linux.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "sanitizer_common/sanitizer_platform.h"
  14. #if !SANITIZER_LINUX
  15. #error Unsupported OS
  16. #endif
  17. #include "memprof_interceptors.h"
  18. #include "memprof_internal.h"
  19. #include "memprof_thread.h"
  20. #include "sanitizer_common/sanitizer_flags.h"
  21. #include "sanitizer_common/sanitizer_freebsd.h"
  22. #include "sanitizer_common/sanitizer_libc.h"
  23. #include "sanitizer_common/sanitizer_procmaps.h"
  24. #include <dlfcn.h>
  25. #include <fcntl.h>
  26. #include <limits.h>
  27. #include <link.h>
  28. #include <pthread.h>
  29. #include <stdio.h>
  30. #include <sys/mman.h>
  31. #include <sys/resource.h>
  32. #include <sys/syscall.h>
  33. #include <sys/time.h>
  34. #include <sys/types.h>
  35. #include <sys/ucontext.h>
  36. #include <unistd.h>
  37. #include <unwind.h>
  38. extern ElfW(Dyn) _DYNAMIC[];
  39. typedef enum {
  40. MEMPROF_RT_VERSION_UNDEFINED = 0,
  41. MEMPROF_RT_VERSION_DYNAMIC,
  42. MEMPROF_RT_VERSION_STATIC,
  43. } memprof_rt_version_t;
  44. // FIXME: perhaps also store abi version here?
  45. extern "C" {
  46. SANITIZER_INTERFACE_ATTRIBUTE
  47. memprof_rt_version_t __memprof_rt_version;
  48. }
  49. namespace __memprof {
  50. void InitializePlatformInterceptors() {}
  51. void InitializePlatformExceptionHandlers() {}
  52. void *MemprofDoesNotSupportStaticLinkage() {
  53. // This will fail to link with -static.
  54. return &_DYNAMIC;
  55. }
  56. uptr FindDynamicShadowStart() {
  57. uptr shadow_size_bytes = MemToShadowSize(kHighMemEnd);
  58. return MapDynamicShadow(shadow_size_bytes, SHADOW_SCALE,
  59. /*min_shadow_base_alignment*/ 0, kHighMemEnd);
  60. }
  61. void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
  62. ucontext_t *ucp = (ucontext_t *)context;
  63. *stack = (uptr)ucp->uc_stack.ss_sp;
  64. *ssize = ucp->uc_stack.ss_size;
  65. }
  66. void *MemprofDlSymNext(const char *sym) { return dlsym(RTLD_NEXT, sym); }
  67. } // namespace __memprof