xref: /aosp_15_r20/external/pigweed/pw_system/public/pw_system/crash_snapshot.h (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 #pragma once
15 
16 #include "pw_cpu_exception/state.h"
17 #include "pw_persistent_ram/persistent_buffer.h"
18 #include "pw_snapshot_protos/snapshot.pwpb.h"
19 #include "pw_system/transfer_handlers.h"
20 
21 namespace pw::system {
22 
23 CrashSnapshotPersistentBuffer& GetCrashSnapshotBuffer();
24 bool HasCrashSnapshot();
25 
26 // CrashSnapshot is the main entry point for populating a crash snapshot.
27 // Information common to pw_system such as logs will be captured directly by
28 // this class, and any device specific information such as cpu_state and
29 // back traces will be delegated to the device backend handler.
30 class CrashSnapshot {
31  public:
32   CrashSnapshot();
33 
34   void Capture(const pw_cpu_exception_State& cpu_state,
35                const std::string_view reason);
36 
37  private:
38   Status CaptureMetadata(
39       const std::string_view reason,
40       snapshot::pwpb::Snapshot::StreamEncoder& snapshot_encoder);
41 
42   Status CaptureMainStackThread(
43       const pw_cpu_exception_State& cpu_state,
44       snapshot::pwpb::Snapshot::StreamEncoder& snapshot_encoder);
45 
46   Status CaptureThreads(
47       const pw_cpu_exception_State& cpu_state,
48       snapshot::pwpb::Snapshot::StreamEncoder& snapshot_encoder);
49 
50   Status CaptureLogs(snapshot::pwpb::Snapshot::StreamEncoder& snapshot_encoder);
51 
52  private:
53   persistent_ram::PersistentBufferWriter writer_;
54 };
55 
56 }  // namespace pw::system
57