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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::dir_section::FileWriterError;
use crate::maps_reader::MappingInfo;
use crate::mem_writer::MemoryWriterError;
use crate::thread_info::Pid;
use goblin;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum InitError {
    #[error("IO error for file {0}")]
    IOError(String, #[source] std::io::Error),
    #[error("No auxv entry found for PID {0}")]
    NoAuxvEntryFound(Pid),
    #[error("crash thread does not reference principal mapping")]
    PrincipalMappingNotReferenced,
    #[error("Failed Android specific late init")]
    AndroidLateInitError(#[from] AndroidError),
}

#[derive(Error, Debug)]
pub enum MapsReaderError {
    // parse_from_line()
    #[error("Map entry malformed: No {0} found")]
    MapEntryMalformed(&'static str),
    #[error("Couldn't parse address")]
    UnparsableInteger(#[from] std::num::ParseIntError),
    #[error("Linux gate location doesn't fit in the required integer type")]
    LinuxGateNotConvertable(#[from] std::num::TryFromIntError),

    // get_mmap()
    #[error("Not safe to open mapping {0}")]
    NotSafeToOpenMapping(String),
    #[error("IO Error")]
    FileError(#[from] std::io::Error),
    #[error("Mmapped file empty or not an ELF file")]
    MmapSanityCheckFailed,
    #[error("Symlink does not match ({0} vs. {1}")]
    SymlinkError(std::path::PathBuf, std::path::PathBuf),

    // handle_deleted_file_in_mapping()
    #[error("Couldn't parse as ELF file")]
    ELFParsingFailed(#[from] goblin::error::Error),
    #[error("No soname found (filename: {0}")]
    NoSoName(String),
}

#[derive(Debug, Error)]
pub enum AuxvReaderError {
    #[error("Invalid auxv format (should not hit EOF before AT_NULL)")]
    InvalidFormat,
    #[error("IO Error")]
    IOError(#[from] std::io::Error),
}

#[derive(Debug, Error)]
pub enum CpuInfoError {
    #[error("IO error for file /proc/cpuinfo")]
    IOError(#[from] std::io::Error),
    #[error("Not all entries of /proc/cpuinfo found!")]
    NotAllProcEntriesFound,
    #[error("Couldn't parse core from file")]
    UnparsableInteger(#[from] std::num::ParseIntError),
    #[error("Couldn't parse cores: {0}")]
    UnparsableCores(String),
}

#[derive(Error, Debug)]
pub enum ThreadInfoError {
    #[error("Index out of bounds: Got {0}, only have {1}")]
    IndexOutOfBounds(usize, usize),
    #[error("Either ppid ({1}) or tgid ({2}) not found in {0}")]
    InvalidPid(String, Pid, Pid),
    #[error("IO error")]
    IOError(#[from] std::io::Error),
    #[error("Couldn't parse address")]
    UnparsableInteger(#[from] std::num::ParseIntError),
    #[error("nix::ptrace() error")]
    PtraceError(#[from] nix::Error),
    #[error("Invalid line in /proc/{0}/status: {1}")]
    InvalidProcStatusFile(Pid, String),
}

#[derive(Debug, Error)]
pub enum AndroidError {
    #[error("Failed to copy memory from process")]
    CopyFromProcessError(#[from] DumperError),
    #[error("Failed slice conversion")]
    TryFromSliceError(#[from] std::array::TryFromSliceError),
    #[error("No Android rel found")]
    NoRelFound,
}

#[derive(Debug, Error)]
pub enum DumperError {
    #[error("Failed to get PAGE_SIZE from system")]
    SysConfError(#[from] nix::Error),
    #[error("wait::waitpid(Pid={0}) failed")]
    WaitPidError(Pid, #[source] nix::Error),
    #[error("nix::ptrace::attach(Pid={0}) failed")]
    PtraceAttachError(Pid, #[source] nix::Error),
    #[error("nix::ptrace::detach(Pid={0}) failed")]
    PtraceDetachError(Pid, #[source] nix::Error),
    #[error("Copy from process {0} failed (source {1}, offset: {2}, length: {3})")]
    CopyFromProcessError(Pid, usize, usize, usize, #[source] nix::Error),
    #[error("Skipped thread {0} due to it being part of the seccomp sandbox's trusted code")]
    DetachSkippedThread(Pid),
    #[error("No threads left to suspend out of {0}")]
    SuspendNoThreadsLeft(usize),
    #[error("No mapping for stack pointer found")]
    NoStackPointerMapping,
    #[error("Failed slice conversion")]
    TryFromSliceError(#[from] std::array::TryFromSliceError),
    #[error("Couldn't parse as ELF file")]
    ELFParsingFailed(#[from] goblin::error::Error),
    #[error("No build-id found")]
    NoBuildIDFound,
    #[error("Not safe to open mapping: {0}")]
    NotSafeToOpenMapping(String),
    #[error("Failed integer conversion")]
    TryFromIntError(#[from] std::num::TryFromIntError),
    #[error("Maps reader error")]
    MapsReaderError(#[from] MapsReaderError),
}

#[derive(Debug, Error)]
pub enum SectionAppMemoryError {
    #[error("Failed to copy memory from process")]
    CopyFromProcessError(#[from] DumperError),
    #[error("Failed to write to memory")]
    MemoryWriterError(#[from] MemoryWriterError),
}

#[derive(Debug, Error)]
pub enum SectionExceptionStreamError {
    #[error("Failed to write to memory")]
    MemoryWriterError(#[from] MemoryWriterError),
}

#[derive(Debug, Error)]
pub enum SectionMappingsError {
    #[error("Failed to write to memory")]
    MemoryWriterError(#[from] MemoryWriterError),
    #[error("Failed to get effective path of mapping ({0:?})")]
    GetEffectivePathError(MappingInfo, #[source] MapsReaderError),
}

#[derive(Debug, Error)]
pub enum SectionMemListError {
    #[error("Failed to write to memory")]
    MemoryWriterError(#[from] MemoryWriterError),
}

#[derive(Debug, Error)]
pub enum SectionSystemInfoError {
    #[error("Failed to write to memory")]
    MemoryWriterError(#[from] MemoryWriterError),
    #[error("Failed to get CPU Info")]
    CpuInfoError(#[from] CpuInfoError),
}

#[derive(Debug, Error)]
pub enum SectionThreadListError {
    #[error("Failed to write to memory")]
    MemoryWriterError(#[from] MemoryWriterError),
    #[error("Failed integer conversion")]
    TryFromIntError(#[from] std::num::TryFromIntError),
    #[error("Failed to copy memory from process")]
    CopyFromProcessError(#[from] DumperError),
    #[error("Failed to get thread info")]
    ThreadInfoError(#[from] ThreadInfoError),
    #[error("Failed to write to memory buffer")]
    IOError(#[from] std::io::Error),
}

#[derive(Debug, Error)]
pub enum SectionThreadNamesError {
    #[error("Failed integer conversion")]
    TryFromIntError(#[from] std::num::TryFromIntError),
    #[error("Failed to write to memory")]
    MemoryWriterError(#[from] MemoryWriterError),
    #[error("Failed to write to memory buffer")]
    IOError(#[from] std::io::Error),
}

#[derive(Debug, Error)]
pub enum SectionDsoDebugError {
    #[error("Failed to write to memory")]
    MemoryWriterError(#[from] MemoryWriterError),
    #[error("Could not find: {0}")]
    CouldNotFind(&'static str),
    #[error("Failed to copy memory from process")]
    CopyFromProcessError(#[from] DumperError),
    #[error("Failed to copy memory from process")]
    FromUTF8Error(#[from] std::string::FromUtf8Error),
}

#[derive(Debug, Error)]
pub enum WriterError {
    #[error("Error during init phase")]
    InitError(#[from] InitError),
    #[error(transparent)]
    DumperError(#[from] DumperError),
    #[error("Failed when writing section AppMemory")]
    SectionAppMemoryError(#[from] SectionAppMemoryError),
    #[error("Failed when writing section ExceptionStream")]
    SectionExceptionStreamError(#[from] SectionExceptionStreamError),
    #[error("Failed when writing section MappingsError")]
    SectionMappingsError(#[from] SectionMappingsError),
    #[error("Failed when writing section MemList")]
    SectionMemListError(#[from] SectionMemListError),
    #[error("Failed when writing section SystemInfo")]
    SectionSystemInfoError(#[from] SectionSystemInfoError),
    #[error("Failed when writing section ThreadList")]
    SectionThreadListError(#[from] SectionThreadListError),
    #[error("Failed when writing section ThreadNameList")]
    SectionThreadNamesError(#[from] SectionThreadNamesError),
    #[error("Failed when writing section DsoDebug")]
    SectionDsoDebugError(#[from] SectionDsoDebugError),
    #[error("Failed to write to memory")]
    MemoryWriterError(#[from] MemoryWriterError),
    #[error("Failed to write to file")]
    FileWriterError(#[from] FileWriterError),
    #[error("Failed to get current timestamp when writing header of minidump")]
    SystemTimeError(#[from] std::time::SystemTimeError),
}