1*4bdff4beSrobert //===----------------------------------------------------------------------===//
2*4bdff4beSrobert //
3*4bdff4beSrobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*4bdff4beSrobert // See https://llvm.org/LICENSE.txt for license information.
5*4bdff4beSrobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*4bdff4beSrobert //
7*4bdff4beSrobert //===----------------------------------------------------------------------===//
8*4bdff4beSrobert 
9*4bdff4beSrobert #include <__config>
10*4bdff4beSrobert #include <cstdio>
11*4bdff4beSrobert #include <cstdlib>
12*4bdff4beSrobert #include <string>
13*4bdff4beSrobert 
14*4bdff4beSrobert // This file defines the legacy default debug handler and related mechanisms
15*4bdff4beSrobert // to set it. This is for backwards ABI compatibility with code that has been
16*4bdff4beSrobert // using this debug handler previously.
17*4bdff4beSrobert 
18*4bdff4beSrobert _LIBCPP_BEGIN_NAMESPACE_STD
19*4bdff4beSrobert 
20*4bdff4beSrobert struct _LIBCPP_TEMPLATE_VIS __libcpp_debug_info {
21*4bdff4beSrobert   _LIBCPP_EXPORTED_FROM_ABI string what() const;
22*4bdff4beSrobert 
23*4bdff4beSrobert   const char* __file_;
24*4bdff4beSrobert   int __line_;
25*4bdff4beSrobert   const char* __pred_;
26*4bdff4beSrobert   const char* __msg_;
27*4bdff4beSrobert };
28*4bdff4beSrobert 
what() const29*4bdff4beSrobert std::string __libcpp_debug_info::what() const {
30*4bdff4beSrobert   string msg = __file_;
31*4bdff4beSrobert   msg += ":" + std::to_string(__line_) + ": _LIBCPP_ASSERT '";
32*4bdff4beSrobert   msg += __pred_;
33*4bdff4beSrobert   msg += "' failed. ";
34*4bdff4beSrobert   msg += __msg_;
35*4bdff4beSrobert   return msg;
36*4bdff4beSrobert }
37*4bdff4beSrobert 
__libcpp_abort_debug_function(__libcpp_debug_info const & info)38*4bdff4beSrobert _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __libcpp_abort_debug_function(__libcpp_debug_info const& info) {
39*4bdff4beSrobert   std::fprintf(stderr, "%s\n", info.what().c_str());
40*4bdff4beSrobert   std::abort();
41*4bdff4beSrobert }
42*4bdff4beSrobert 
43*4bdff4beSrobert typedef void (*__libcpp_debug_function_type)(__libcpp_debug_info const&);
44*4bdff4beSrobert 
45*4bdff4beSrobert _LIBCPP_EXPORTED_FROM_ABI
46*4bdff4beSrobert constinit __libcpp_debug_function_type __libcpp_debug_function = __libcpp_abort_debug_function;
47*4bdff4beSrobert 
48*4bdff4beSrobert _LIBCPP_EXPORTED_FROM_ABI
__libcpp_set_debug_function(__libcpp_debug_function_type __func)49*4bdff4beSrobert bool __libcpp_set_debug_function(__libcpp_debug_function_type __func) {
50*4bdff4beSrobert   __libcpp_debug_function = __func;
51*4bdff4beSrobert   return true;
52*4bdff4beSrobert }
53*4bdff4beSrobert 
54*4bdff4beSrobert _LIBCPP_END_NAMESPACE_STD
55