├── .gitignore
├── Cargo.lock
├── Cargo.toml
├── README.md
└── src
├── main.rs
├── notes
├── mod.rs
├── nt_file.rs
└── nt_prpsinfo.rs
└── vm.rs
/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 |
--------------------------------------------------------------------------------
/Cargo.lock:
--------------------------------------------------------------------------------
1 | # This file is automatically @generated by Cargo.
2 | # It is not intended for manual editing.
3 | version = 3
4 |
5 | [[package]]
6 | name = "ahash"
7 | version = "0.8.11"
8 | source = "registry+https://github.com/rust-lang/crates.io-index"
9 | checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
10 | dependencies = [
11 | "cfg-if",
12 | "once_cell",
13 | "version_check",
14 | "zerocopy",
15 | ]
16 |
17 | [[package]]
18 | name = "aho-corasick"
19 | version = "1.1.3"
20 | source = "registry+https://github.com/rust-lang/crates.io-index"
21 | checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
22 | dependencies = [
23 | "memchr",
24 | ]
25 |
26 | [[package]]
27 | name = "anstream"
28 | version = "0.6.15"
29 | source = "registry+https://github.com/rust-lang/crates.io-index"
30 | checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526"
31 | dependencies = [
32 | "anstyle",
33 | "anstyle-parse",
34 | "anstyle-query",
35 | "anstyle-wincon",
36 | "colorchoice",
37 | "is_terminal_polyfill",
38 | "utf8parse",
39 | ]
40 |
41 | [[package]]
42 | name = "anstyle"
43 | version = "1.0.8"
44 | source = "registry+https://github.com/rust-lang/crates.io-index"
45 | checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1"
46 |
47 | [[package]]
48 | name = "anstyle-parse"
49 | version = "0.2.5"
50 | source = "registry+https://github.com/rust-lang/crates.io-index"
51 | checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb"
52 | dependencies = [
53 | "utf8parse",
54 | ]
55 |
56 | [[package]]
57 | name = "anstyle-query"
58 | version = "1.1.1"
59 | source = "registry+https://github.com/rust-lang/crates.io-index"
60 | checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a"
61 | dependencies = [
62 | "windows-sys",
63 | ]
64 |
65 | [[package]]
66 | name = "anstyle-wincon"
67 | version = "3.0.4"
68 | source = "registry+https://github.com/rust-lang/crates.io-index"
69 | checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8"
70 | dependencies = [
71 | "anstyle",
72 | "windows-sys",
73 | ]
74 |
75 | [[package]]
76 | name = "anyhow"
77 | version = "1.0.86"
78 | source = "registry+https://github.com/rust-lang/crates.io-index"
79 | checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
80 |
81 | [[package]]
82 | name = "cfg-if"
83 | version = "1.0.0"
84 | source = "registry+https://github.com/rust-lang/crates.io-index"
85 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
86 |
87 | [[package]]
88 | name = "colorchoice"
89 | version = "1.0.2"
90 | source = "registry+https://github.com/rust-lang/crates.io-index"
91 | checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
92 |
93 | [[package]]
94 | name = "coredump-copy"
95 | version = "0.1.6"
96 | dependencies = [
97 | "anyhow",
98 | "elf",
99 | "env_logger",
100 | "log",
101 | "object",
102 | ]
103 |
104 | [[package]]
105 | name = "crc32fast"
106 | version = "1.4.2"
107 | source = "registry+https://github.com/rust-lang/crates.io-index"
108 | checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
109 | dependencies = [
110 | "cfg-if",
111 | ]
112 |
113 | [[package]]
114 | name = "elf"
115 | version = "0.7.4"
116 | source = "registry+https://github.com/rust-lang/crates.io-index"
117 | checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b"
118 |
119 | [[package]]
120 | name = "env_filter"
121 | version = "0.1.2"
122 | source = "registry+https://github.com/rust-lang/crates.io-index"
123 | checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab"
124 | dependencies = [
125 | "log",
126 | "regex",
127 | ]
128 |
129 | [[package]]
130 | name = "env_logger"
131 | version = "0.11.5"
132 | source = "registry+https://github.com/rust-lang/crates.io-index"
133 | checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d"
134 | dependencies = [
135 | "anstream",
136 | "anstyle",
137 | "env_filter",
138 | "humantime",
139 | "log",
140 | ]
141 |
142 | [[package]]
143 | name = "equivalent"
144 | version = "1.0.1"
145 | source = "registry+https://github.com/rust-lang/crates.io-index"
146 | checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
147 |
148 | [[package]]
149 | name = "hashbrown"
150 | version = "0.14.5"
151 | source = "registry+https://github.com/rust-lang/crates.io-index"
152 | checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
153 | dependencies = [
154 | "ahash",
155 | ]
156 |
157 | [[package]]
158 | name = "humantime"
159 | version = "2.1.0"
160 | source = "registry+https://github.com/rust-lang/crates.io-index"
161 | checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
162 |
163 | [[package]]
164 | name = "indexmap"
165 | version = "2.2.6"
166 | source = "registry+https://github.com/rust-lang/crates.io-index"
167 | checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
168 | dependencies = [
169 | "equivalent",
170 | "hashbrown",
171 | ]
172 |
173 | [[package]]
174 | name = "is_terminal_polyfill"
175 | version = "1.70.1"
176 | source = "registry+https://github.com/rust-lang/crates.io-index"
177 | checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
178 |
179 | [[package]]
180 | name = "log"
181 | version = "0.4.22"
182 | source = "registry+https://github.com/rust-lang/crates.io-index"
183 | checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
184 |
185 | [[package]]
186 | name = "memchr"
187 | version = "2.7.4"
188 | source = "registry+https://github.com/rust-lang/crates.io-index"
189 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
190 |
191 | [[package]]
192 | name = "object"
193 | version = "0.36.2"
194 | source = "registry+https://github.com/rust-lang/crates.io-index"
195 | checksum = "3f203fa8daa7bb185f760ae12bd8e097f63d17041dcdcaf675ac54cdf863170e"
196 | dependencies = [
197 | "crc32fast",
198 | "hashbrown",
199 | "indexmap",
200 | "memchr",
201 | ]
202 |
203 | [[package]]
204 | name = "once_cell"
205 | version = "1.19.0"
206 | source = "registry+https://github.com/rust-lang/crates.io-index"
207 | checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
208 |
209 | [[package]]
210 | name = "proc-macro2"
211 | version = "1.0.86"
212 | source = "registry+https://github.com/rust-lang/crates.io-index"
213 | checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
214 | dependencies = [
215 | "unicode-ident",
216 | ]
217 |
218 | [[package]]
219 | name = "quote"
220 | version = "1.0.36"
221 | source = "registry+https://github.com/rust-lang/crates.io-index"
222 | checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
223 | dependencies = [
224 | "proc-macro2",
225 | ]
226 |
227 | [[package]]
228 | name = "regex"
229 | version = "1.10.5"
230 | source = "registry+https://github.com/rust-lang/crates.io-index"
231 | checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
232 | dependencies = [
233 | "aho-corasick",
234 | "memchr",
235 | "regex-automata",
236 | "regex-syntax",
237 | ]
238 |
239 | [[package]]
240 | name = "regex-automata"
241 | version = "0.4.7"
242 | source = "registry+https://github.com/rust-lang/crates.io-index"
243 | checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
244 | dependencies = [
245 | "aho-corasick",
246 | "memchr",
247 | "regex-syntax",
248 | ]
249 |
250 | [[package]]
251 | name = "regex-syntax"
252 | version = "0.8.4"
253 | source = "registry+https://github.com/rust-lang/crates.io-index"
254 | checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
255 |
256 | [[package]]
257 | name = "syn"
258 | version = "2.0.70"
259 | source = "registry+https://github.com/rust-lang/crates.io-index"
260 | checksum = "2f0209b68b3613b093e0ec905354eccaedcfe83b8cb37cbdeae64026c3064c16"
261 | dependencies = [
262 | "proc-macro2",
263 | "quote",
264 | "unicode-ident",
265 | ]
266 |
267 | [[package]]
268 | name = "unicode-ident"
269 | version = "1.0.12"
270 | source = "registry+https://github.com/rust-lang/crates.io-index"
271 | checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
272 |
273 | [[package]]
274 | name = "utf8parse"
275 | version = "0.2.2"
276 | source = "registry+https://github.com/rust-lang/crates.io-index"
277 | checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
278 |
279 | [[package]]
280 | name = "version_check"
281 | version = "0.9.4"
282 | source = "registry+https://github.com/rust-lang/crates.io-index"
283 | checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
284 |
285 | [[package]]
286 | name = "windows-sys"
287 | version = "0.52.0"
288 | source = "registry+https://github.com/rust-lang/crates.io-index"
289 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
290 | dependencies = [
291 | "windows-targets",
292 | ]
293 |
294 | [[package]]
295 | name = "windows-targets"
296 | version = "0.52.6"
297 | source = "registry+https://github.com/rust-lang/crates.io-index"
298 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
299 | dependencies = [
300 | "windows_aarch64_gnullvm",
301 | "windows_aarch64_msvc",
302 | "windows_i686_gnu",
303 | "windows_i686_gnullvm",
304 | "windows_i686_msvc",
305 | "windows_x86_64_gnu",
306 | "windows_x86_64_gnullvm",
307 | "windows_x86_64_msvc",
308 | ]
309 |
310 | [[package]]
311 | name = "windows_aarch64_gnullvm"
312 | version = "0.52.6"
313 | source = "registry+https://github.com/rust-lang/crates.io-index"
314 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
315 |
316 | [[package]]
317 | name = "windows_aarch64_msvc"
318 | version = "0.52.6"
319 | source = "registry+https://github.com/rust-lang/crates.io-index"
320 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
321 |
322 | [[package]]
323 | name = "windows_i686_gnu"
324 | version = "0.52.6"
325 | source = "registry+https://github.com/rust-lang/crates.io-index"
326 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
327 |
328 | [[package]]
329 | name = "windows_i686_gnullvm"
330 | version = "0.52.6"
331 | source = "registry+https://github.com/rust-lang/crates.io-index"
332 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
333 |
334 | [[package]]
335 | name = "windows_i686_msvc"
336 | version = "0.52.6"
337 | source = "registry+https://github.com/rust-lang/crates.io-index"
338 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
339 |
340 | [[package]]
341 | name = "windows_x86_64_gnu"
342 | version = "0.52.6"
343 | source = "registry+https://github.com/rust-lang/crates.io-index"
344 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
345 |
346 | [[package]]
347 | name = "windows_x86_64_gnullvm"
348 | version = "0.52.6"
349 | source = "registry+https://github.com/rust-lang/crates.io-index"
350 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
351 |
352 | [[package]]
353 | name = "windows_x86_64_msvc"
354 | version = "0.52.6"
355 | source = "registry+https://github.com/rust-lang/crates.io-index"
356 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
357 |
358 | [[package]]
359 | name = "zerocopy"
360 | version = "0.7.34"
361 | source = "registry+https://github.com/rust-lang/crates.io-index"
362 | checksum = "ae87e3fcd617500e5d106f0380cf7b77f3c6092aae37191433159dda23cfb087"
363 | dependencies = [
364 | "zerocopy-derive",
365 | ]
366 |
367 | [[package]]
368 | name = "zerocopy-derive"
369 | version = "0.7.34"
370 | source = "registry+https://github.com/rust-lang/crates.io-index"
371 | checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b"
372 | dependencies = [
373 | "proc-macro2",
374 | "quote",
375 | "syn",
376 | ]
377 |
--------------------------------------------------------------------------------
/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "coredump-copy"
3 | version = "0.1.6"
4 | edition = "2021"
5 | description = "Copy coredumps for debugging on a different machine"
6 | license = "MIT"
7 |
8 | [dependencies]
9 | object = { version = "0.36.2", default-features = false, features = ["elf", "read_core", "write_std"] }
10 | anyhow = "1.0.86"
11 | env_logger = "0.11.5"
12 | log = "0.4.22"
13 | elf = "0.7.4"
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | coredump-copy
2 | =============
3 |
4 | # Usage
5 |
6 | ```
7 | coredump-copy
8 | ```
9 |
10 |
11 | - ``: The core dump file to copy. This will copy the core dump file along with all the files it references. The resulting core dump will be modified to reference the new paths. The core dump file will be named `/core`.
12 | - ``: Where files should be copied to.
13 |
14 | # Why
15 |
16 | Sometimes you might want to copy a core dump file to another machine, and debug it there. For example, when your program crashed on a remote machine, or in CI.
17 |
18 | It is not enough to just copy the core dump file and the main binary, you also need to copy all the shared libraries and also maintain the directory structure, and set a prefix in gdb so it can find them. This program does all of that for you, automatically.
19 |
20 | # Caveat
21 |
22 | This program is not fool proof. Since it mangles with the core dump files, it is possible that it can do something wrong and break them. Report a bug if this doesn't work.
23 |
--------------------------------------------------------------------------------
/src/main.rs:
--------------------------------------------------------------------------------
1 | use std::{
2 | collections::HashSet,
3 | ffi::{OsStr, OsString},
4 | mem::MaybeUninit,
5 | os::unix::{ffi::OsStrExt as _, fs::PermissionsExt},
6 | path::{Path, PathBuf},
7 | };
8 |
9 | mod notes;
10 | mod vm;
11 |
12 | use anyhow::Result;
13 | use notes::nt_file::NtFileAny;
14 | use object::{
15 | read::elf::{Dyn as _, ElfFile, FileHeader, ProgramHeader},
16 | Object,
17 | };
18 | use vm::Pod as _;
19 |
20 | trait BitWidth: Copy + Clone {
21 | /// SAFETY: the slice must be at least `Self::BYTES` long.
22 | unsafe fn from_bytes(bytes: &[u8], endian: impl object::Endian) -> Self;
23 | /// SAFETY: the slice must be at least `Self::BYTES` long.
24 | unsafe fn to_bytes(&self, endian: impl object::Endian, bytes: &mut [MaybeUninit]);
25 | fn as_usize(&self) -> usize;
26 | fn from_u64(val: u64) -> Self;
27 | /// Convert the value from `endian` to the native endian.
28 | fn to_native_endian(&self, endian: impl object::Endian) -> Self;
29 | fn from_native_endian(&self, endian: impl object::Endian) -> Self;
30 | }
31 | impl BitWidth for u32 {
32 | unsafe fn from_bytes(bytes: &[u8], endian: impl object::Endian) -> Self {
33 | endian.read_u32_bytes(*(bytes.as_ptr() as *const [u8; 4]))
34 | }
35 | unsafe fn to_bytes(&self, endian: impl object::Endian, bytes: &mut [MaybeUninit]) {
36 | let arr = endian.write_u32_bytes(*self);
37 | bytes[0].as_mut_ptr().copy_from(arr.as_ptr(), 4);
38 | }
39 | fn as_usize(&self) -> usize {
40 | *self as usize
41 | }
42 | fn from_u64(val: u64) -> Self {
43 | Self::try_from(val).unwrap()
44 | }
45 | fn to_native_endian(&self, endian: impl object::Endian) -> Self {
46 | endian.read_u32(*self)
47 | }
48 | fn from_native_endian(&self, endian: impl object::Endian) -> Self {
49 | endian.write_u32(*self)
50 | }
51 | }
52 | impl BitWidth for u64 {
53 | unsafe fn from_bytes(bytes: &[u8], endian: impl object::Endian) -> Self {
54 | endian.read_u64_bytes(*(bytes.as_ptr() as *const [u8; 8]))
55 | }
56 | unsafe fn to_bytes(&self, endian: impl object::Endian, bytes: &mut [MaybeUninit]) {
57 | let arr = endian.write_u64_bytes(*self);
58 | bytes[0].as_mut_ptr().copy_from(arr.as_ptr(), 8);
59 | }
60 | fn as_usize(&self) -> usize {
61 | *self as usize
62 | }
63 | fn from_u64(val: u64) -> Self {
64 | val
65 | }
66 | fn to_native_endian(&self, endian: impl object::Endian) -> Self {
67 | endian.read_u64(*self)
68 | }
69 | fn from_native_endian(&self, endian: impl object::Endian) -> Self {
70 | endian.write_u64(*self)
71 | }
72 | }
73 |
74 | trait EndiannessExt {
75 | /// Split the slice at the given index.
76 | ///
77 | /// Returns a tuple of the slice up to the index as a fixed-size array, if the slice is long
78 | /// enough, and the rest of the slice.
79 | fn read_bits(&self, data: &mut &[u8]) -> Option;
80 | fn write_bits(&self, value: T, data: &mut Vec);
81 | fn to_endianness(&self) -> object::Endianness;
82 | }
83 |
84 | impl EndiannessExt for T {
85 | fn read_bits(&self, data: &mut &[u8]) -> Option