summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/rust_minidump_writer_linux/src/lib.rs
blob: 02b71c972bbfb915deb21421ca2721f41df7de46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
extern crate minidump_writer;

use anyhow;
use libc::pid_t;
use minidump_writer::crash_context::CrashContext;
use minidump_writer::minidump_writer::MinidumpWriter;
use nsstring::nsCString;
use std::ffi::CStr;
use std::mem;
use std::os::raw::c_char;

// This structure will be exposed to C++
#[repr(C)]
#[derive(Clone)]
pub struct InternalCrashContext {
    pub context: crash_context::ucontext_t,
    #[cfg(not(target_arch = "arm"))]
    pub float_state: crash_context::fpregset_t,
    pub siginfo: libc::signalfd_siginfo,
    pub pid: libc::pid_t,
    pub tid: libc::pid_t,
}

// This function will be exposed to C++
#[no_mangle]
pub unsafe extern "C" fn write_minidump_linux(
    dump_path: *const c_char,
    child: pid_t,
    child_blamed_thread: pid_t,
    error_msg: &mut nsCString,
) -> bool {
    assert!(!dump_path.is_null());
    let c_path = CStr::from_ptr(dump_path);
    let path = match c_path.to_str() {
        Ok(s) => s,
        Err(x) => {
            error_msg.assign(&format!(
                "Wrapper error. Path not convertable: {:#}",
                anyhow::Error::new(x)
            ));
            return false;
        }
    };

    let mut dump_file = match std::fs::OpenOptions::new()
        .create(true) // Create file if it doesn't exist
        .write(true) // Truncate file
        .open(path)
    {
        Ok(f) => f,
        Err(x) => {
            error_msg.assign(&format!(
                "Wrapper error when opening minidump destination at {:?}: {:#}",
                path,
                anyhow::Error::new(x)
            ));
            return false;
        }
    };

    match MinidumpWriter::new(child, child_blamed_thread).dump(&mut dump_file) {
        Ok(_) => {
            return true;
        }
        Err(x) => {
            error_msg.assign(&format!("{:#}", anyhow::Error::new(x)));
            return false;
        }
    }
}

// This function will be exposed to C++
#[no_mangle]
pub unsafe extern "C" fn write_minidump_linux_with_context(
    dump_path: *const c_char,
    child: pid_t,
    context: *const InternalCrashContext,
    error_msg: &mut nsCString,
) -> bool {
    assert!(!dump_path.is_null());
    let c_path = CStr::from_ptr(dump_path);

    assert!(!context.is_null());
    let cc: CrashContext = mem::transmute_copy(&(*(context as *const CrashContext)));
    let path = match c_path.to_str() {
        Ok(s) => s,
        Err(x) => {
            error_msg.assign(&format!(
                "Wrapper error. Path not convertable: {:#}",
                anyhow::Error::new(x)
            ));
            return false;
        }
    };

    let mut dump_file = match std::fs::OpenOptions::new()
        .create(true) // Create file if it doesn't exist
        .write(true) // Truncate file
        .open(path)
    {
        Ok(f) => f,
        Err(x) => {
            error_msg.assign(&format!(
                "Wrapper error when opening minidump destination at {:?}: {:#}",
                path,
                anyhow::Error::new(x)
            ));
            return false;
        }
    };

    match MinidumpWriter::new(child, cc.inner.tid)
        .set_crash_context(cc)
        .dump(&mut dump_file)
    {
        Ok(_) => {
            return true;
        }
        Err(x) => {
            error_msg.assign(&format!("{:#}", anyhow::Error::new(x)));
            return false;
        }
    }
}