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