xref: /aosp_15_r20/external/pigweed/pw_assert_trap/trap_handler.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include <cstring>
16 
17 #include "pw_assert_trap/config.h"
18 #include "pw_assert_trap/handler.h"
19 #include "pw_assert_trap/message.h"
20 #include "pw_string/string_builder.h"
21 #include "pw_sync/interrupt_spin_lock.h"
22 
23 static pw::sync::InterruptSpinLock interrupt_spin_lock;
24 PW_CONSTINIT static pw::InlineString<PW_ASSERT_TRAP_BUFFER_SIZE> message_buffer;
25 
pw_assert_trap_get_message()26 const std::string_view pw_assert_trap_get_message() { return message_buffer; }
27 
pw_assert_trap_clear_message()28 void pw_assert_trap_clear_message() { message_buffer.clear(); }
29 
pw_assert_trap_interrupt_lock(void)30 void pw_assert_trap_interrupt_lock(void) { interrupt_spin_lock.lock(); }
31 
pw_assert_trap_interrupt_unlock(void)32 void pw_assert_trap_interrupt_unlock(void) { interrupt_spin_lock.unlock(); }
33 
pw_assert_trap_HandleAssertFailure(const char * file_name,int line_number,const char * function_name)34 void pw_assert_trap_HandleAssertFailure(
35     [[maybe_unused]] const char* file_name,
36     [[maybe_unused]] int line_number,
37     [[maybe_unused]] const char* function_name) {
38   pw::StringBuilder builder(message_buffer);
39 
40   builder.append("PW_ASSERT() or PW_DASSERT() failure");
41 
42 #if !PW_ASSERT_TRAP_DISABLE_LOCATION_CAPTURE
43   builder.append(" at ");
44   if (file_name != nullptr && line_number != -1) {
45     builder.Format("%s:%d", file_name, line_number);
46   }
47   if (function_name != nullptr) {
48     builder.Format(" %s: ", function_name);
49   }
50 #endif  // !PW_ASSERT_TRAP_DISABLE_LOCATION_CAPTURE
51 }
52 
pw_assert_trap_HandleCheckFailure(const char * file_name,int line_number,const char * function_name,const char * format,...)53 void pw_assert_trap_HandleCheckFailure(
54     [[maybe_unused]] const char* file_name,
55     [[maybe_unused]] int line_number,
56     [[maybe_unused]] const char* function_name,
57     const char* format,
58     ...) {
59   pw::StringBuilder builder(message_buffer);
60 
61 #if !PW_ASSERT_TRAP_DISABLE_LOCATION_CAPTURE
62   if (file_name != nullptr && line_number != -1) {
63     builder.Format("%s:%d", file_name, line_number);
64   }
65   if (function_name != nullptr) {
66     builder.Format(" %s: ", function_name);
67   }
68 #endif  // !PW_ASSERT_TRAP_DISABLE_LOCATION_CAPTURE
69 
70   va_list args;
71   va_start(args, format);
72   builder.FormatVaList(format, args);
73   va_end(args);
74 }
75