1 //===----------------------------------------------------------------------===//
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 #include <__config>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <string>
13 
14 // This file defines the legacy default debug handler and related mechanisms
15 // to set it. This is for backwards ABI compatibility with code that has been
16 // using this debug handler previously.
17 
18 _LIBCPP_BEGIN_NAMESPACE_STD
19 
20 struct _LIBCPP_TEMPLATE_VIS __libcpp_debug_info {
21   _LIBCPP_EXPORTED_FROM_ABI string what() const;
22 
23   const char* __file_;
24   int __line_;
25   const char* __pred_;
26   const char* __msg_;
27 };
28 
what() const29 std::string __libcpp_debug_info::what() const {
30   string msg = __file_;
31   msg += ":" + std::to_string(__line_) + ": _LIBCPP_ASSERT '";
32   msg += __pred_;
33   msg += "' failed. ";
34   msg += __msg_;
35   return msg;
36 }
37 
__libcpp_abort_debug_function(__libcpp_debug_info const & info)38 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __libcpp_abort_debug_function(__libcpp_debug_info const& info) {
39   std::fprintf(stderr, "%s\n", info.what().c_str());
40   std::abort();
41 }
42 
43 typedef void (*__libcpp_debug_function_type)(__libcpp_debug_info const&);
44 
45 _LIBCPP_EXPORTED_FROM_ABI
46 constinit __libcpp_debug_function_type __libcpp_debug_function = __libcpp_abort_debug_function;
47 
48 _LIBCPP_EXPORTED_FROM_ABI
__libcpp_set_debug_function(__libcpp_debug_function_type __func)49 bool __libcpp_set_debug_function(__libcpp_debug_function_type __func) {
50   __libcpp_debug_function = __func;
51   return true;
52 }
53 
54 _LIBCPP_END_NAMESPACE_STD
55