• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- sanitizer_symbolizer_linux.cc -------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 // Linux-specific implementation of symbolizer parts.
13 //===----------------------------------------------------------------------===//
14 
15 #include "sanitizer_platform.h"
16 #if SANITIZER_LINUX
17 #include "sanitizer_common.h"
18 #include "sanitizer_linux.h"
19 
20 namespace __sanitizer {
21 
22 #if SANITIZER_ANDROID
SymbolizerPrepareForSandboxing()23 void SymbolizerPrepareForSandboxing() {
24   // Do nothing on Android.
25 }
26 #else
27 static char proc_self_exe_cache_str[kMaxPathLength];
28 static uptr proc_self_exe_cache_len = 0;
29 
30 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
31   uptr module_name_len = internal_readlink(
32       "/proc/self/exe", buf, buf_len);
33   int readlink_error;
34   if (internal_iserror(buf_len, &readlink_error)) {
35     if (proc_self_exe_cache_len) {
36       // If available, use the cached module name.
37       CHECK_LE(proc_self_exe_cache_len, buf_len);
38       internal_strncpy(buf, proc_self_exe_cache_str, buf_len);
39       module_name_len = internal_strlen(proc_self_exe_cache_str);
40     } else {
41       // We can't read /proc/self/exe for some reason, assume the name of the
42       // binary is unknown.
43       Report("WARNING: readlink(\"/proc/self/exe\") failed with errno %d, "
44              "some stack frames may not be symbolized\n", readlink_error);
45       module_name_len = internal_snprintf(buf, buf_len, "/proc/self/exe");
46     }
47     CHECK_LT(module_name_len, buf_len);
48     buf[module_name_len] = '\0';
49   }
50   return module_name_len;
51 }
52 
53 void SymbolizerPrepareForSandboxing() {
54   if (!proc_self_exe_cache_len) {
55     proc_self_exe_cache_len =
56         ReadBinaryName(proc_self_exe_cache_str, kMaxPathLength);
57   }
58 }
59 #endif  // SANITIZER_ANDROID
60 
61 }  // namespace __sanitizer
62 
63 #endif  // SANITIZER_LINUX
64