Index: lib/sanitizer_common/sanitizer_common.h =================================================================== --- lib/sanitizer_common/sanitizer_common.h +++ lib/sanitizer_common/sanitizer_common.h @@ -206,6 +206,9 @@ const char *StripModuleName(const char *module); // OS +uptr ReadBinaryName(/*out*/char *buf, uptr buf_len); +const char *GetBinaryName(); +void CacheBinaryName(); void DisableCoreDumperIfNecessary(); void DumpProcessMap(); bool FileExists(const char *filename); Index: lib/sanitizer_common/sanitizer_common.cc =================================================================== --- lib/sanitizer_common/sanitizer_common.cc +++ lib/sanitizer_common/sanitizer_common.cc @@ -55,7 +55,9 @@ internal_close(fd); } - internal_snprintf(full_path, kMaxPathLength, "%s.%zu", path_prefix, pid); + const char *pname = StripModuleName(GetBinaryName()); + internal_snprintf(full_path, kMaxPathLength, "%s.%s.%zu", path_prefix, pname, + pid); uptr openrv = OpenFile(full_path, true); if (internal_iserror(openrv)) { const char *ErrorMsgPrefix = "ERROR: Can't open file: "; @@ -219,6 +221,8 @@ return 0; if (const char *slash_pos = internal_strrchr(module, '/')) return slash_pos + 1; + else if (const char *backslash_pos = internal_strrchr(module, '\\')) + return backslash_pos + 1; return module; } @@ -288,6 +292,24 @@ atomic_fetch_sub(&g_total_mmaped, size, memory_order_relaxed); } +static BlockingMutex binary_name_cache_lock(LINKER_INITIALIZED); +static char binary_name_cache_str[kMaxPathLength]; +static bool binary_name_cache_initialized = false; + +const char *GetBinaryName() { + BlockingMutexLock l(&binary_name_cache_lock); + if (!binary_name_cache_initialized) { + ReadBinaryName(binary_name_cache_str, sizeof(binary_name_cache_str)); + binary_name_cache_initialized = true; + } + return binary_name_cache_str; +} + +void CacheBinaryName() { + // Call once to make sure that binary_name_cache_str is initialized + GetBinaryName(); +} + } // namespace __sanitizer using namespace __sanitizer; // NOLINT Index: lib/sanitizer_common/sanitizer_flags.inc =================================================================== --- lib/sanitizer_common/sanitizer_flags.inc +++ lib/sanitizer_common/sanitizer_flags.inc @@ -48,8 +48,8 @@ "Max number of stack frames kept for each allocation/deallocation.") COMMON_FLAG( const char *, log_path, "stderr", - "Write logs to \"log_path.pid\". The special values are \"stdout\" and " - "\"stderr\". The default is \"stderr\".") + "Write logs to \"log_path.pname.pid\". The special values are \"stdout\" " + "and \"stderr\". The default is \"stderr\".") COMMON_FLAG( int, verbosity, 0, "Verbosity level (0 - silent, 1 - a bit of output, 2+ - more output).") Index: lib/sanitizer_common/sanitizer_linux.h =================================================================== --- lib/sanitizer_common/sanitizer_linux.h +++ lib/sanitizer_common/sanitizer_linux.h @@ -80,11 +80,6 @@ // information). bool LibraryNameIs(const char *full_name, const char *base_name); -// Read the name of the current binary from /proc/self/exe. -uptr ReadBinaryName(/*out*/char *buf, uptr buf_len); -// Cache the value of /proc/self/exe. -void CacheBinaryName(); - // Call cb for each region mapped by map. void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)); } // namespace __sanitizer Index: lib/sanitizer_common/sanitizer_linux.cc =================================================================== --- lib/sanitizer_common/sanitizer_linux.cc +++ lib/sanitizer_common/sanitizer_linux.cc @@ -704,47 +704,31 @@ #endif } -static char proc_self_exe_cache_str[kMaxPathLength]; -static uptr proc_self_exe_cache_len = 0; - uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) { - if (proc_self_exe_cache_len > 0) { - // If available, use the cached module name. - uptr module_name_len = - internal_snprintf(buf, buf_len, "%s", proc_self_exe_cache_str); - CHECK_LT(module_name_len, buf_len); - return module_name_len; - } #if SANITIZER_FREEBSD - const int Mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + const int Mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + const char *default_module_name = "kern.proc.pathname"; size_t Size = buf_len; - bool IsErr = (sysctl(Mib, 4, buf, &Size, NULL, 0) != 0); + bool IsErr = (sysctl(Mib, ARRAY_SIZE(Mib), buf, &Size, NULL, 0) != 0); int readlink_error = IsErr ? errno : 0; uptr module_name_len = Size; #else + const char *default_module_name = "/proc/self/exe"; uptr module_name_len = internal_readlink( - "/proc/self/exe", buf, buf_len); + default_module_name, buf, buf_len); int readlink_error; bool IsErr = internal_iserror(module_name_len, &readlink_error); #endif if (IsErr) { - // We can't read /proc/self/exe for some reason, assume the name of the - // binary is unknown. - Report("WARNING: readlink(\"/proc/self/exe\") failed with errno %d, " + // We can't read binary name for some reason, assume it's unknown. + Report("WARNING: reading executable name failed with errno %d, " "some stack frames may not be symbolized\n", readlink_error); - module_name_len = internal_snprintf(buf, buf_len, "/proc/self/exe"); + module_name_len = internal_snprintf(buf, buf_len, default_module_name); CHECK_LT(module_name_len, buf_len); } return module_name_len; } -void CacheBinaryName() { - if (!proc_self_exe_cache_len) { - proc_self_exe_cache_len = - ReadBinaryName(proc_self_exe_cache_str, kMaxPathLength); - } -} - // Match full names of the form /path/to/base_name{-,.}* bool LibraryNameIs(const char *full_name, const char *base_name) { const char *name = full_name; Index: lib/sanitizer_common/sanitizer_mac.cc =================================================================== --- lib/sanitizer_common/sanitizer_mac.cc +++ lib/sanitizer_common/sanitizer_mac.cc @@ -328,6 +328,13 @@ void *internal_start_thread(void (*func)(void *arg), void *arg) { return 0; } void internal_join_thread(void *th) { } +uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) { + // FIXME: Actually implement this function. + CHECK_GT(buf_len, 0); + buf[0] = 0; + return 0; +} + } // namespace __sanitizer #endif // SANITIZER_MAC Index: lib/sanitizer_common/sanitizer_printf.cc =================================================================== --- lib/sanitizer_common/sanitizer_printf.cc +++ lib/sanitizer_common/sanitizer_printf.cc @@ -253,9 +253,11 @@ needed_length = 0; if (append_pid) { int pid = internal_getpid(); - needed_length += internal_snprintf(buffer, buffer_size, "==%d==", pid); + const char *pname = StripModuleName(GetBinaryName()); + needed_length += internal_snprintf(buffer, buffer_size, + "==%s %d==", pname, pid); if (needed_length >= buffer_size) { - // The pid doesn't fit into the current buffer. + // Process name + pid do not fit into the current buffer. if (!use_mmap) continue; RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n"); Index: lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc =================================================================== --- lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc +++ lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc @@ -644,11 +644,8 @@ } void PrepareForSandboxing() override { -#if SANITIZER_LINUX && !SANITIZER_ANDROID BlockingMutexLock l(&mu_); - // Cache /proc/self/exe on Linux. CacheBinaryName(); -#endif } private: Index: lib/sanitizer_common/sanitizer_win.cc =================================================================== --- lib/sanitizer_common/sanitizer_win.cc +++ lib/sanitizer_common/sanitizer_win.cc @@ -606,6 +606,13 @@ return true; } +uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) { + // FIXME: Actually implement this function. + CHECK_GT(buf_len, 0); + buf[0] = 0; + return 0; +} + } // namespace __sanitizer #endif // _WIN32 Index: test/asan/TestCases/verbose-log-path_test.cc =================================================================== --- /dev/null +++ test/asan/TestCases/verbose-log-path_test.cc @@ -0,0 +1,27 @@ +// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=316 +// XFAIL: android +// +// RUN: %clangxx_asan %s -o %t + +// Good log_path. +// RUN: rm -f %t.log.* +// RUN: env ASAN_OPTIONS=log_path=%t.log not %run %t 2> %t.out +// RUN: FileCheck %s --check-prefix=CHECK-ERROR < %t.log.verbose-log-path_test.cc* + +// FIXME: log_path is not supported on Windows yet. +// XFAIL: win32 +// +// FIXME: only FreeBSD and Linux have verbose log paths. +// REQUIRES: verbose-log-path + +#include +#include +int main(int argc, char **argv) { + if (argc > 2) return 0; + char *x = (char*)malloc(10); + memset(x, 0, 10); + int res = x[argc * 10]; // BOOOM + free(x); + return res; +} +// CHECK-ERROR: ERROR: AddressSanitizer Index: test/asan/lit.cfg =================================================================== --- test/asan/lit.cfg +++ test/asan/lit.cfg @@ -132,6 +132,10 @@ if config.target_arch != 'arm': config.available_features.add('stable-runtime') +# Allow tests to use REQUIRES=verbose-log-path. +if config.host_os in ['FreeBSD', 'Linux']: + config.available_features.add('verbose-log-path') + # Turn on leak detection on 64-bit Linux. if config.host_os == 'Linux' and config.target_arch == 'x86_64': config.available_features.add('leak-detection')