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 "pw_system/crash_snapshot.h"
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker #include <optional>
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker #include "pw_multisink/util.h"
20*61c4878aSAndroid Build Coastguard Worker #include "pw_snapshot/uuid.h"
21*61c4878aSAndroid Build Coastguard Worker #include "pw_status/status.h"
22*61c4878aSAndroid Build Coastguard Worker #include "pw_system/device_handler.h"
23*61c4878aSAndroid Build Coastguard Worker #include "pw_system/log.h"
24*61c4878aSAndroid Build Coastguard Worker
25*61c4878aSAndroid Build Coastguard Worker namespace pw::system {
26*61c4878aSAndroid Build Coastguard Worker
27*61c4878aSAndroid Build Coastguard Worker namespace {
28*61c4878aSAndroid Build Coastguard Worker
29*61c4878aSAndroid Build Coastguard Worker CrashSnapshot crash_snapshot;
30*61c4878aSAndroid Build Coastguard Worker
31*61c4878aSAndroid Build Coastguard Worker PW_PLACE_IN_SECTION(".noinit")
32*61c4878aSAndroid Build Coastguard Worker CrashSnapshotPersistentBuffer persistent_crash_snapshot;
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard Worker std::byte submessage_scratch_buffer
35*61c4878aSAndroid Build Coastguard Worker [snapshot::pwpb::Snapshot::kScratchBufferSizeBytes];
36*61c4878aSAndroid Build Coastguard Worker
37*61c4878aSAndroid Build Coastguard Worker } // namespace
38*61c4878aSAndroid Build Coastguard Worker
GetCrashSnapshotBuffer()39*61c4878aSAndroid Build Coastguard Worker CrashSnapshotPersistentBuffer& GetCrashSnapshotBuffer() {
40*61c4878aSAndroid Build Coastguard Worker return persistent_crash_snapshot;
41*61c4878aSAndroid Build Coastguard Worker }
42*61c4878aSAndroid Build Coastguard Worker
HasCrashSnapshot()43*61c4878aSAndroid Build Coastguard Worker bool HasCrashSnapshot() { return persistent_crash_snapshot.has_value(); }
44*61c4878aSAndroid Build Coastguard Worker
CrashSnapshot()45*61c4878aSAndroid Build Coastguard Worker CrashSnapshot::CrashSnapshot()
46*61c4878aSAndroid Build Coastguard Worker : writer_(persistent_crash_snapshot.GetWriter()) {}
47*61c4878aSAndroid Build Coastguard Worker
Capture(const pw_cpu_exception_State & cpu_state,const std::string_view reason)48*61c4878aSAndroid Build Coastguard Worker void CrashSnapshot::Capture(const pw_cpu_exception_State& cpu_state,
49*61c4878aSAndroid Build Coastguard Worker const std::string_view reason) {
50*61c4878aSAndroid Build Coastguard Worker // clear any old snapshot data prior to populating a new crash snapshot.
51*61c4878aSAndroid Build Coastguard Worker persistent_crash_snapshot.clear();
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard Worker snapshot::pwpb::Snapshot::StreamEncoder snapshot_encoder(
54*61c4878aSAndroid Build Coastguard Worker writer_, submessage_scratch_buffer);
55*61c4878aSAndroid Build Coastguard Worker
56*61c4878aSAndroid Build Coastguard Worker // TODO: b/354772694 - handle encoder problems. Potentially write to memory
57*61c4878aSAndroid Build Coastguard Worker // and log on boot that the snapshot couldn't be written.
58*61c4878aSAndroid Build Coastguard Worker Status status = OkStatus();
59*61c4878aSAndroid Build Coastguard Worker status.Update(CaptureMetadata(reason, snapshot_encoder));
60*61c4878aSAndroid Build Coastguard Worker status.Update(device_handler::CaptureCpuState(cpu_state, snapshot_encoder));
61*61c4878aSAndroid Build Coastguard Worker status.Update(CaptureMainStackThread(cpu_state, snapshot_encoder));
62*61c4878aSAndroid Build Coastguard Worker status.Update(CaptureThreads(cpu_state, snapshot_encoder));
63*61c4878aSAndroid Build Coastguard Worker // Capture logs last as they can be large and fill
64*61c4878aSAndroid Build Coastguard Worker // the crash_snapshot buffer.
65*61c4878aSAndroid Build Coastguard Worker status.Update(CaptureLogs(snapshot_encoder));
66*61c4878aSAndroid Build Coastguard Worker status.Update(snapshot_encoder.status());
67*61c4878aSAndroid Build Coastguard Worker }
68*61c4878aSAndroid Build Coastguard Worker
CaptureMetadata(const std::string_view reason,snapshot::pwpb::Snapshot::StreamEncoder & snapshot_encoder)69*61c4878aSAndroid Build Coastguard Worker Status CrashSnapshot::CaptureMetadata(
70*61c4878aSAndroid Build Coastguard Worker const std::string_view reason,
71*61c4878aSAndroid Build Coastguard Worker snapshot::pwpb::Snapshot::StreamEncoder& snapshot_encoder) {
72*61c4878aSAndroid Build Coastguard Worker snapshot::pwpb::Metadata::StreamEncoder metadata_encoder =
73*61c4878aSAndroid Build Coastguard Worker snapshot_encoder.GetMetadataEncoder();
74*61c4878aSAndroid Build Coastguard Worker
75*61c4878aSAndroid Build Coastguard Worker // TODO: b/354770559 - generate a snapshot UUID.
76*61c4878aSAndroid Build Coastguard Worker std::optional<snapshot::ConstUuidSpan> snapshot_uuid;
77*61c4878aSAndroid Build Coastguard Worker if (snapshot_uuid.has_value()) {
78*61c4878aSAndroid Build Coastguard Worker // TODO: https://pwbug.dev/357138320 - Review IgnoreError calls in this
79*61c4878aSAndroid Build Coastguard Worker // file.
80*61c4878aSAndroid Build Coastguard Worker metadata_encoder.WriteSnapshotUuid(snapshot_uuid.value()).IgnoreError();
81*61c4878aSAndroid Build Coastguard Worker }
82*61c4878aSAndroid Build Coastguard Worker
83*61c4878aSAndroid Build Coastguard Worker if (!reason.empty()) {
84*61c4878aSAndroid Build Coastguard Worker metadata_encoder.WriteReason(as_bytes(span(reason))).IgnoreError();
85*61c4878aSAndroid Build Coastguard Worker }
86*61c4878aSAndroid Build Coastguard Worker
87*61c4878aSAndroid Build Coastguard Worker metadata_encoder.WriteFatal(true).IgnoreError();
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker // TODO: b/354775975 - populate the metadata with version, build uuid
90*61c4878aSAndroid Build Coastguard Worker // and project name.
91*61c4878aSAndroid Build Coastguard Worker
92*61c4878aSAndroid Build Coastguard Worker device_handler::CapturePlatformMetadata(metadata_encoder);
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker return metadata_encoder.status();
95*61c4878aSAndroid Build Coastguard Worker }
96*61c4878aSAndroid Build Coastguard Worker
CaptureMainStackThread(const pw_cpu_exception_State & cpu_state,snapshot::pwpb::Snapshot::StreamEncoder & snapshot_encoder)97*61c4878aSAndroid Build Coastguard Worker Status CrashSnapshot::CaptureMainStackThread(
98*61c4878aSAndroid Build Coastguard Worker const pw_cpu_exception_State& cpu_state,
99*61c4878aSAndroid Build Coastguard Worker snapshot::pwpb::Snapshot::StreamEncoder& snapshot_encoder) {
100*61c4878aSAndroid Build Coastguard Worker thread::proto::pwpb::SnapshotThreadInfo::StreamEncoder* thread_info_encoder =
101*61c4878aSAndroid Build Coastguard Worker static_cast<thread::proto::pwpb::SnapshotThreadInfo::StreamEncoder*>(
102*61c4878aSAndroid Build Coastguard Worker static_cast<protobuf::StreamEncoder*>(&snapshot_encoder));
103*61c4878aSAndroid Build Coastguard Worker return device_handler::CaptureMainStackThread(cpu_state,
104*61c4878aSAndroid Build Coastguard Worker *thread_info_encoder);
105*61c4878aSAndroid Build Coastguard Worker }
106*61c4878aSAndroid Build Coastguard Worker
CaptureThreads(const pw_cpu_exception_State & cpu_state,snapshot::pwpb::Snapshot::StreamEncoder & snapshot_encoder)107*61c4878aSAndroid Build Coastguard Worker Status CrashSnapshot::CaptureThreads(
108*61c4878aSAndroid Build Coastguard Worker const pw_cpu_exception_State& cpu_state,
109*61c4878aSAndroid Build Coastguard Worker snapshot::pwpb::Snapshot::StreamEncoder& snapshot_encoder) {
110*61c4878aSAndroid Build Coastguard Worker thread::proto::pwpb::SnapshotThreadInfo::StreamEncoder* thread_info_encoder =
111*61c4878aSAndroid Build Coastguard Worker static_cast<thread::proto::pwpb::SnapshotThreadInfo::StreamEncoder*>(
112*61c4878aSAndroid Build Coastguard Worker static_cast<protobuf::StreamEncoder*>(&snapshot_encoder));
113*61c4878aSAndroid Build Coastguard Worker return device_handler::CaptureThreads(cpu_state.extended.psp,
114*61c4878aSAndroid Build Coastguard Worker *thread_info_encoder);
115*61c4878aSAndroid Build Coastguard Worker }
116*61c4878aSAndroid Build Coastguard Worker
CaptureLogs(snapshot::pwpb::Snapshot::StreamEncoder & snapshot_encoder)117*61c4878aSAndroid Build Coastguard Worker Status CrashSnapshot::CaptureLogs(
118*61c4878aSAndroid Build Coastguard Worker snapshot::pwpb::Snapshot::StreamEncoder& snapshot_encoder) {
119*61c4878aSAndroid Build Coastguard Worker log::pwpb::LogEntries::StreamEncoder encoder(writer_, ByteSpan());
120*61c4878aSAndroid Build Coastguard Worker // Limit the captured logs to the latest entries that were
121*61c4878aSAndroid Build Coastguard Worker // added to log buffer.
122*61c4878aSAndroid Build Coastguard Worker size_t remaining_bytes = snapshot_encoder.ConservativeWriteLimit();
123*61c4878aSAndroid Build Coastguard Worker multisink::UnsafeDumpMultiSinkLogsFromEnd(
124*61c4878aSAndroid Build Coastguard Worker GetMultiSink(), encoder, remaining_bytes)
125*61c4878aSAndroid Build Coastguard Worker .IgnoreError();
126*61c4878aSAndroid Build Coastguard Worker return snapshot_encoder.status();
127*61c4878aSAndroid Build Coastguard Worker }
128*61c4878aSAndroid Build Coastguard Worker
129*61c4878aSAndroid Build Coastguard Worker } // namespace pw::system
130