Skip to main content

libdd_crashtracker/crash_info/
ucontext.rs

1// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7/// Structured representation of the CPU register state captured from a
8/// `ucontext_t` at the time of a crash signal.
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
10pub struct Ucontext {
11    /// Target architecture: "x86_64" or "aarch64"
12    pub arch: String,
13    /// Named registers mapped to their hex-formatted values ("rip" -> "0x00007f...")
14    pub registers: HashMap<String, String>,
15    /// Full Debug-formatted ucontext string preserving FPU state, signal mask,
16    /// and alternate-stack info that is not captured in `registers`.
17    #[serde(default, skip_serializing_if = "Option::is_none")]
18    pub raw: Option<String>,
19}
20
21#[cfg(test)]
22impl Ucontext {
23    pub fn test_instance(_seed: u64) -> Self {
24        Self {
25            arch: "x86_64".to_string(),
26            registers: HashMap::from([
27                ("rip".to_string(), "0x00007f7e11d3a2b0".to_string()),
28                ("rsp".to_string(), "0x00007f7e11d3a2b0".to_string()),
29                ("rbp".to_string(), "0x00007f7e11d3a2b0".to_string()),
30                ("rax".to_string(), "0x00007f7e11d3a2b0".to_string()),
31                ("rbx".to_string(), "0x00007f7e11d3a2b0".to_string()),
32                ("rcx".to_string(), "0x00007f7e11d3a2b0".to_string()),
33            ]),
34            raw: Some("ucontext_t { uc_flags: 7, uc_link: 0x0, uc_stack: stack_t { ss_sp: 0x713fa26f1000, ss_flags: 0, ss_size: 65536 } }".to_string()),
35        }
36    }
37}