1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "partition_alloc/partition_alloc_base/debug/stack_trace.h" 6 7 #include "build/build_config.h" 8 #include "partition_alloc/partition_alloc_base/debug/debugging_buildflags.h" 9 #include "partition_alloc/partition_alloc_base/numerics/safe_conversions.h" 10 11 // Surprisingly, uClibc defines __GLIBC__ in some build configs, but 12 // execinfo.h and backtrace(3) are really only present in glibc and in macOS 13 // libc. 14 #if BUILDFLAG(IS_APPLE) || \ 15 (defined(__GLIBC__) && !defined(__UCLIBC__) && !defined(__AIX)) 16 #define HAVE_BACKTRACE 17 #include <execinfo.h> 18 #endif 19 20 namespace partition_alloc::internal::base::debug { 21 CollectStackTrace(const void ** trace,size_t count)22size_t CollectStackTrace(const void** trace, size_t count) { 23 // NOTE: This code MUST be async-signal safe (it's used by in-process 24 // stack dumping signal handler). NO malloc or stdio is allowed here. 25 26 #if BUILDFLAG(IS_APPLE) && defined(HAVE_BACKTRACE) 27 // Regarding Apple, no /proc is available. Try backtrace API. 28 // Though the backtrace API man page does not list any possible negative 29 // return values, we take no chance. 30 return base::saturated_cast<size_t>( 31 backtrace(const_cast<void**>(trace), base::saturated_cast<int>(count))); 32 #else 33 // Not able to obtain stack traces. 34 return 0; 35 #endif 36 } 37 38 } // namespace partition_alloc::internal::base::debug 39