blob: 4f603f0d58d91f25c1a523761a3f7ff25de66626 [file] [log] [blame]
// Copyright 2021 The Pigweed Authors
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#include "pw_thread/snapshot.h"
#include <string_view>
#include "pw_bytes/span.h"
#include "pw_function/function.h"
#include "pw_log/log.h"
#include "pw_protobuf/encoder.h"
#include "pw_status/status.h"
#include "pw_thread_protos/thread.pwpb.h"
namespace pw::thread {
Status SnapshotStack(const StackContext& stack,
Thread::StreamEncoder& encoder,
ProcessThreadStackCallback& thread_stack_callback) {
// TODO(pwbug/422): Add support for ascending stacks.
if (stack.stack_pointer > stack.stack_high_addr) {
PW_LOG_ERROR("%s's stack underflowed by %lu bytes",,
static_cast<long unsigned>(stack.stack_pointer -
return Status::OutOfRange();
// Log an error, but don't prevent the capture.
if (stack.stack_pointer < stack.stack_low_addr) {
"%s's stack overflowed by %lu bytes",,
static_cast<long unsigned>(stack.stack_low_addr - stack.stack_pointer));
return thread_stack_callback(
ConstByteSpan(reinterpret_cast<const std::byte*>(stack.stack_pointer),
stack.stack_high_addr - stack.stack_pointer));
} // namespace pw::thread