1 // Copyright 2021 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 <optional> 17 #include <string_view> 18 19 #include "pw_bytes/span.h" 20 #include "pw_function/function.h" 21 #include "pw_protobuf/encoder.h" 22 #include "pw_status/status.h" 23 #include "pw_thread_protos/thread.pwpb.h" 24 25 namespace pw::thread { 26 27 // Stack dump callback functions populate a thread's raw_backtrace or raw_stack 28 // field. This should encode either raw_backtrace or raw_stack to the provided 29 // Thread stream encoder. 30 using ProcessThreadStackCallback = 31 Function<Status(proto::pwpb::Thread::StreamEncoder&, ConstByteSpan)>; 32 33 struct StackContext { 34 std::string_view thread_name; 35 uintptr_t stack_low_addr; 36 uintptr_t stack_high_addr; 37 uintptr_t stack_pointer; 38 std::optional<uintptr_t> stack_pointer_est_peak; 39 }; 40 41 // Takes the provided StackContext, and writes stack context to the provided 42 // Thread encoder. After stack context is captured, the thread_stack_callback is 43 // invoked to capture either the raw_stack or raw_backtrace to the same encoder. 44 // 45 // Captures the following proto fields: 46 // pw.thread.Thread: 47 // stack_start_pointer 48 // stack_end_pointer 49 // stack_pointer 50 Status SnapshotStack(const StackContext& stack, 51 proto::pwpb::Thread::StreamEncoder& encoder, 52 const ProcessThreadStackCallback& thread_stack_callback); 53 54 } // namespace pw::thread 55