Skip to content

Commit

Permalink
[crashtracker] Send pid to the receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsn committed Jun 11, 2024
1 parent 718e036 commit 3be83ab
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
2 changes: 2 additions & 0 deletions crashtracker/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ pub const DD_CRASHTRACK_BEGIN_CONFIG: &str = "DD_CRASHTRACK_BEGIN_CONFIG";
pub const DD_CRASHTRACK_BEGIN_COUNTERS: &str = "DD_CRASHTRACK_BEGIN_COUNTERS";
pub const DD_CRASHTRACK_BEGIN_FILE: &str = "DD_CRASHTRACK_BEGIN_FILE";
pub const DD_CRASHTRACK_BEGIN_METADATA: &str = "DD_CRASHTRACK_BEGIN_METADATA";
pub const DD_CRASHTRACK_BEGIN_PROCINFO: &str = "DD_CRASHTRACK_BEGIN_PROCESSINFO";
pub const DD_CRASHTRACK_BEGIN_SIGINFO: &str = "DD_CRASHTRACK_BEGIN_SIGINFO";
pub const DD_CRASHTRACK_BEGIN_STACKTRACE: &str = "DD_CRASHTRACK_BEGIN_STACKTRACE";
pub const DD_CRASHTRACK_DONE: &str = "DD_CRASHTRACK_DONE";
pub const DD_CRASHTRACK_END_CONFIG: &str = "DD_CRASHTRACK_END_CONFIG";
pub const DD_CRASHTRACK_END_COUNTERS: &str = "DD_CRASHTRACK_END_COUNTERS";
pub const DD_CRASHTRACK_END_FILE: &str = "DD_CRASHTRACK_END_FILE";
pub const DD_CRASHTRACK_END_METADATA: &str = "DD_CRASHTRACK_END_METADATA";
pub const DD_CRASHTRACK_END_PROCINFO: &str = "DD_CRASHTRACK_END_PROCESSINFO";
pub const DD_CRASHTRACK_END_SIGINFO: &str = "DD_CRASHTRACK_END_SIGINFO";
pub const DD_CRASHTRACK_END_STACKTRACE: &str = "DD_CRASHTRACK_END_STACKTRACE";
9 changes: 9 additions & 0 deletions crashtracker/src/crash_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ fn emit_metadata(w: &mut impl Write, metadata_str: &str) -> anyhow::Result<()> {
Ok(())
}

fn emit_procinfo(w: &mut impl Write) -> anyhow::Result<()> {
writeln!(w, "{DD_CRASHTRACK_BEGIN_PROCINFO}")?;
let pid = nix::unistd::getpid();
writeln!(w, "{{\"pid\": {pid} }}")?;
writeln!(w, "{DD_CRASHTRACK_END_PROCINFO}")?;
Ok(())
}

fn emit_siginfo(w: &mut impl Write, signum: i32) -> anyhow::Result<()> {
let signame = if signum == libc::SIGSEGV {
"SIGSEGV"
Expand All @@ -348,6 +356,7 @@ fn emit_crashreport(
emit_metadata(pipe, metadata_string)?;
emit_config(pipe, config_str)?;
emit_siginfo(pipe, signum)?;
emit_procinfo(pipe)?;
pipe.flush()?;
emit_counters(pipe)?;
pipe.flush()?;
Expand Down
15 changes: 15 additions & 0 deletions crashtracker/src/crash_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub struct SigInfo {
pub signame: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProcessInfo {
pub pid: u32,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CrashInfo {
#[serde(skip_serializing_if = "HashMap::is_empty")]
Expand All @@ -64,6 +69,9 @@ pub struct CrashInfo {
pub os_info: os_info::Info,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub proc_info: Option<ProcessInfo>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub siginfo: Option<SigInfo>,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
Expand Down Expand Up @@ -126,6 +134,7 @@ impl CrashInfo {
incomplete: false,
metadata: None,
os_info,
proc_info: None,
siginfo: None,
stacktrace: vec![],
tags: HashMap::new(),
Expand Down Expand Up @@ -180,6 +189,12 @@ impl CrashInfo {
Ok(())
}

pub fn set_procinfo(&mut self, proc_info: ProcessInfo) -> anyhow::Result<()> {
anyhow::ensure!(self.proc_info.is_none());
self.proc_info = Some(proc_info);
Ok(())
}

pub fn set_siginfo(&mut self, siginfo: SigInfo) -> anyhow::Result<()> {
anyhow::ensure!(self.siginfo.is_none());
self.siginfo = Some(siginfo);
Expand Down
22 changes: 16 additions & 6 deletions crashtracker/src/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@

use super::*;
use anyhow::Context;
use nix::unistd::getppid;
use std::{io::BufReader, os::unix::net::UnixListener};

pub fn resolve_frames(
config: &CrashtrackerConfiguration,
crash_info: &mut CrashInfo,
) -> anyhow::Result<()> {
if config.resolve_frames == StacktraceCollection::EnabledWithSymbolsInReceiver {
// The receiver is the direct child of the crashing process
// TODO: This pid should be sent over the wire, so that
// it can be used in a sidecar.
let ppid: u32 = getppid().as_raw().try_into()?;
crash_info.resolve_names_from_process(ppid)?
let proc_info = crash_info
.proc_info
.as_ref()
.context("Unable to resolve frames: No PID specified")?;
crash_info.resolve_names_from_process(proc_info.pid)?
}
Ok(())
}
Expand Down Expand Up @@ -84,6 +83,7 @@ enum StdinState {
File(String, Vec<String>),
InternalError(String),
Metadata,
ProcInfo,
SigInfo,
StackTrace(Vec<StackFrame>),
Waiting,
Expand Down Expand Up @@ -147,6 +147,13 @@ fn process_line(
StdinState::Metadata
}

StdinState::ProcInfo if line.starts_with(DD_CRASHTRACK_END_PROCINFO) => StdinState::Waiting,
StdinState::ProcInfo => {
let proc_info = serde_json::from_str(&line)?;
crashinfo.set_procinfo(proc_info)?;
StdinState::ProcInfo
}

StdinState::SigInfo if line.starts_with(DD_CRASHTRACK_END_SIGINFO) => StdinState::Waiting,
StdinState::SigInfo => {
let siginfo = serde_json::from_str(&line)?;
Expand Down Expand Up @@ -176,6 +183,9 @@ fn process_line(
StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_METADATA) => {
StdinState::Metadata
}
StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_PROCINFO) => {
StdinState::ProcInfo
}
StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_SIGINFO) => StdinState::SigInfo,
StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_STACKTRACE) => {
StdinState::StackTrace(vec![])
Expand Down
1 change: 1 addition & 0 deletions crashtracker/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ mod tests {
signum: 11,
signame: Some("SIGSEGV".to_owned()),
}),
proc_info: None,
stacktrace: vec![],
additional_stacktraces: HashMap::new(),
tags: HashMap::new(),
Expand Down

0 comments on commit 3be83ab

Please sign in to comment.