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