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 #include "pw_cpu_exception_cortex_m/crash.h"
15
16 #include <cstdarg>
17 #include <mutex>
18
19 #include "pw_cpu_exception_cortex_m/cpu_state.h"
20 #include "pw_cpu_exception_cortex_m_private/cortex_m_constants.h"
21 #include "pw_status/status.h"
22 #include "pw_string/string_builder.h"
23 #include "pw_sync/lock_annotations.h"
24 #include "pw_sync/mutex.h"
25 #include "pw_unit_test/framework.h"
26
27 namespace pw::cpu_exception::cortex_m {
28 namespace {
29 const char* kSampleThreadName = "BadThread";
30
31 class FakeCrashHandler : public testing::Test {
32 public:
CaptureCrashAnalysis(const char * fmt,va_list args)33 void CaptureCrashAnalysis(const char* fmt, va_list args) {
34 StringBuilder message_builder(buffer_);
35 ASSERT_EQ(message_builder.FormatVaList(fmt, args).status(), OkStatus());
36 }
37
38 protected:
39 std::array<char, 124> buffer_;
40 };
41
42 pw::sync::Mutex crash_handler_lock;
43 FakeCrashHandler* crash_handler PW_GUARDED_BY(crash_handler_lock) = nullptr;
44
TEST_F(FakeCrashHandler,CaptureCrashInfoDivideByZero)45 TEST_F(FakeCrashHandler, CaptureCrashInfoDivideByZero) {
46 std::lock_guard lock(crash_handler_lock);
47 crash_handler = this;
48 pw_cpu_exception_State cpu_state = {};
49 cpu_state.extended.cfsr = kCfsrDivbyzeroMask;
50 AnalyzeCpuStateAndCrash(cpu_state, kSampleThreadName);
51 std::string_view analysis{buffer_.data(), sizeof(buffer_)};
52 EXPECT_NE(analysis.find(kSampleThreadName), static_cast<size_t>(0));
53 EXPECT_NE(analysis.find("DIVBYZERO"), static_cast<size_t>(0));
54 crash_handler = nullptr;
55 }
56
TEST_F(FakeCrashHandler,CaptureCrashInfoNoThreadName)57 TEST_F(FakeCrashHandler, CaptureCrashInfoNoThreadName) {
58 std::lock_guard lock(crash_handler_lock);
59 crash_handler = this;
60 pw_cpu_exception_State cpu_state = {};
61 cpu_state.extended.cfsr = kCfsrDivbyzeroMask;
62 AnalyzeCpuStateAndCrash(cpu_state, nullptr);
63 std::string_view analysis{buffer_.data(), sizeof(buffer_)};
64 EXPECT_NE(analysis.find(kSampleThreadName), static_cast<size_t>(0));
65 EXPECT_NE(analysis.find("Thread=?"), static_cast<size_t>(0));
66 crash_handler = nullptr;
67 }
68
69 } // namespace
70
CaptureCrashAnalysisForTest(const pw_cpu_exception_State &,const char * fmt,...)71 void CaptureCrashAnalysisForTest(const pw_cpu_exception_State&,
72 const char* fmt,
73 ...) {
74 ASSERT_NE(crash_handler, nullptr);
75 va_list args;
76 va_start(args, fmt);
77 crash_handler->CaptureCrashAnalysis(fmt, args);
78 va_end(args);
79 }
80
81 } // namespace pw::cpu_exception::cortex_m
82