blob: 16764ae9d0be3c2e7a3de89fe57467040d600345 [file] [edit]
// Copyright 2026 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SRC_DEVELOPER_DEBUG_DEBUG_AGENT_ZXDUMP_COLLECTOR_H_
#define SRC_DEVELOPER_DEBUG_DEBUG_AGENT_ZXDUMP_COLLECTOR_H_
#include <lib/zx/handle.h>
#include <lib/zx/result.h>
#include <lib/zx/socket.h>
#include <zircon/syscalls/object.h>
#include <zircon/types.h>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <span>
namespace debug_agent {
// How a process takes part in a shared address space, derived from the
// ZX_INFO_MAPS_TYPE_ASPACE entries of its ZX_INFO_PROCESS_MAPS.
enum class AspaceKind : uint8_t {
// One address space at the normal user base. This process shares nothing.
kNormal,
// One address space above the normal user base. This is the prototype process created with
// ZX_PROCESS_SHARED (starnix_kernel) and it owns the shared address space.
kSharedOwner,
// Two address spaces. This process was created with zx_process_create_shared() (a Starnix
// Linux process). The first is private and the second is the shared address space.
kSharedChild,
};
struct AspaceLayout {
AspaceKind kind = AspaceKind::kNormal;
// The [shared_base, shared_base + shared_size) range of the shared address space. Zero for
// kNormal.
uint64_t shared_base = 0;
uint64_t shared_size = 0;
bool operator==(const AspaceLayout&) const = default;
};
// Classifies a process from its ZX_INFO_PROCESS_MAPS entries.
AspaceLayout ClassifyAspaces(std::span<const zx_info_maps_t> maps);
// Returns whether |mapping| falls inside the shared address space of a kSharedChild process.
// A kSharedChild leaves out its shared address space so only the kSharedOwner holds a copy in a
// job archive.
bool IsSharedChildMapping(const zx_info_maps_t& mapping, const AspaceLayout& layout);
// Returns whether a core holds the bytes of a writable mapping.
// - A kSharedChild leaves out its shared address space (via IsSharedChildMapping).
// - A mapping without populated pages has nothing to dump and avoids faulting in pages from a
// suspended userpager.
// - Physical VMOs and non-cached VMOs are left out to avoid reading device memory.
// - A mapping of a pager-backed VMO itself (not a copy-on-write clone of it) holds file contents
// and is left out to avoid faulting in from a suspended userpager.
bool ShouldDumpWritableMapping(const zx_info_maps_t& mapping, const zx_info_vmo_t& vmo,
const AspaceLayout& layout);
// Finds the process that owns the shared address space among the processes of one job.
// Returns std::nullopt when no process in the job takes part in a shared address space.
// Returns ZX_ERR_NOT_FOUND when a kSharedChild is present without a kSharedOwner, because the
// job archive would then have no copy of the shared address space.
zx::result<std::optional<size_t>> FindSharedOwner(std::span<const AspaceLayout> layouts);
// Collects a zstd-compressed Zircon core dump (for a process handle) or job archive (for a
// job handle) in a single pass and streams it over |destination|.
//
// For a job handle, every direct process of the target job is suspended before any memory is
// read (child jobs are not recursed into). A process that exits before it is suspended is left
// out. After that, a process that cannot be collected fails the whole dump, because a partial
// archive of a shared address space is not consistent.
//
// A job archive stores a shared address space once, in the core of its owner, and fails if a
// kSharedChild has no owner. Every kSharedChild core leaves out the writable memory of the shared
// address space. For a standalone kSharedChild process handle, shared address space writable
// memory is likewise omitted (capturing only private restricted-mode memory), because
// single-process dumps do not suspend the shared owner or sibling processes.
zx_status_t StreamZxdump(zx::handle task, zx::socket destination);
} // namespace debug_agent
#endif // SRC_DEVELOPER_DEBUG_DEBUG_AGENT_ZXDUMP_COLLECTOR_H_