• 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 
15 #define PW_LOG_LEVEL PW_THREAD_CONFIG_LOG_LEVEL
16 
17 #include "pw_thread/snapshot.h"
18 
19 #include <string_view>
20 
21 #include "pw_bytes/span.h"
22 #include "pw_function/function.h"
23 #include "pw_log/log.h"
24 #include "pw_protobuf/encoder.h"
25 #include "pw_status/status.h"
26 #include "pw_thread/config.h"
27 #include "pw_thread_protos/thread.pwpb.h"
28 
29 namespace pw::thread {
30 
SnapshotStack(const StackContext & stack,Thread::StreamEncoder & encoder,const ProcessThreadStackCallback & thread_stack_callback)31 Status SnapshotStack(const StackContext& stack,
32                      Thread::StreamEncoder& encoder,
33                      const ProcessThreadStackCallback& thread_stack_callback) {
34   // TODO(pwbug/422): Add support for ascending stacks.
35   encoder.WriteStackStartPointer(stack.stack_high_addr);
36   encoder.WriteStackEndPointer(stack.stack_low_addr);
37   encoder.WriteStackPointer(stack.stack_pointer);
38   PW_LOG_DEBUG("Active stack: 0x%08x-0x%08x (%ld bytes)",
39                stack.stack_high_addr,
40                stack.stack_pointer,
41                static_cast<long>(stack.stack_high_addr) -
42                    static_cast<long>(stack.stack_pointer));
43   if (stack.stack_pointer_est_peak.has_value()) {
44     const uintptr_t stack_pointer_est_peak =
45         stack.stack_pointer_est_peak.value();
46     encoder.WriteStackPointerEstPeak(stack_pointer_est_peak);
47     PW_LOG_DEBUG("Est peak stack: 0x%08x-0x%08x (%ld bytes)",
48                  stack.stack_high_addr,
49                  stack_pointer_est_peak,
50                  static_cast<long>(stack.stack_high_addr) -
51                      static_cast<long>(stack_pointer_est_peak));
52   }
53   PW_LOG_DEBUG("Stack Limits: 0x%08x-0x%08x (%ld bytes)",
54                stack.stack_low_addr,
55                stack.stack_high_addr,
56                static_cast<long>(stack.stack_high_addr) -
57                    static_cast<long>(stack.stack_low_addr));
58 
59   if (stack.stack_pointer > stack.stack_high_addr) {
60     PW_LOG_ERROR("%s's stack underflowed by %lu bytes",
61                  stack.thread_name.data(),
62                  static_cast<long unsigned>(stack.stack_pointer -
63                                             stack.stack_high_addr));
64     return Status::OutOfRange();
65   }
66 
67   // Log an error, but don't prevent the capture.
68   if (stack.stack_pointer < stack.stack_low_addr) {
69     PW_LOG_ERROR(
70         "%s's stack overflowed by %lu bytes",
71         stack.thread_name.data(),
72         static_cast<long unsigned>(stack.stack_low_addr - stack.stack_pointer));
73   }
74 
75   return thread_stack_callback(
76       encoder,
77       ConstByteSpan(reinterpret_cast<const std::byte*>(stack.stack_pointer),
78                     stack.stack_high_addr - stack.stack_pointer));
79 }
80 
81 }  // namespace pw::thread
82