ptools 0.2.23

Utilities for inspecting Linux processes
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//
//   Copyright (c) 2017 Steven Fackler
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//

//! Thread stack traces of remote processes.

use std::borrow::Borrow;
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::fs::File;
use std::fs::{self};
use std::io::Read;
use std::io::{self};
use std::ptr;

use libc::c_void;
use libc::pid_t;
use libc::ptrace;
use libc::waitpid;
use libc::__WALL;
use libc::ESRCH;
use libc::PTRACE_ATTACH;
use libc::PTRACE_CONT;
use libc::PTRACE_DETACH;
use libc::PTRACE_INTERRUPT;
use libc::PTRACE_SEIZE;
use libc::SIGSTOP;
use libc::WIFSTOPPED;
use libc::WSTOPSIG;

/// Information about a remote process.
#[derive(Debug, Clone)]
pub struct Process {
    id: u32,
    threads: Vec<Thread>,
}

impl Process {
    /// Returns the process's ID.
    pub fn id(&self) -> u32 {
        self.id
    }

    /// Returns information about the threads of the process.
    pub fn threads(&self) -> &[Thread] {
        &self.threads
    }
}

/// Information about a thread of a remote process.
#[derive(Debug, Clone)]
pub struct Thread {
    id: u32,
    name: Option<String>,
    frames: Vec<Frame>,
}

impl Thread {
    /// Returns the thread's ID.
    #[inline]
    pub fn id(&self) -> u32 {
        self.id
    }

    /// Returns the thread's name, if known.
    #[inline]
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    /// Returns the frames of the stack trace representing the state of the thread.
    #[inline]
    pub fn frames(&self) -> &[Frame] {
        &self.frames
    }
}

/// Information about a stack frame of a remote process.
#[derive(Debug, Clone)]
pub struct Frame {
    pub(crate) ip: u64,
    pub(crate) is_signal: bool,
    pub(crate) is_inline: bool,
    pub(crate) symbol: Option<Symbol>,
    pub(crate) module: Option<String>,
    pub(crate) source: Option<SourceLocation>,
}

impl Frame {
    /// Returns the instruction pointer of the frame.
    #[inline]
    pub fn ip(&self) -> u64 {
        self.ip
    }

    /// Determines if the frame is from a signal handler.
    #[inline]
    pub fn is_signal(&self) -> bool {
        self.is_signal
    }

    /// Determines if the frame represents an inlined function call.
    #[inline]
    pub fn is_inline(&self) -> bool {
        self.is_inline
    }

    /// Returns information about the symbol corresponding to this frame's instruction pointer, if known.
    #[inline]
    pub fn symbol(&self) -> Option<&Symbol> {
        self.symbol.as_ref()
    }

    /// Returns the module (shared library) containing this frame, if available.
    #[inline]
    pub fn module(&self) -> Option<&str> {
        self.module.as_deref()
    }

    /// Returns the source file and line number for this frame, if available.
    #[inline]
    pub fn source(&self) -> Option<&SourceLocation> {
        self.source.as_ref()
    }
}

/// Source file and line number for a stack frame.
#[derive(Debug, Clone)]
pub struct SourceLocation {
    pub(crate) file: String,
    pub(crate) line: i32,
}

impl SourceLocation {
    /// Returns the source file path.
    #[inline]
    pub fn file(&self) -> &str {
        &self.file
    }

    /// Returns the line number.
    #[inline]
    pub fn line(&self) -> i32 {
        self.line
    }
}

/// Information about the symbol corresponding to a stack frame.
#[derive(Debug, Clone)]
pub struct Symbol {
    pub(crate) name: String,
    pub(crate) offset: u64,
    pub(crate) address: u64,
    pub(crate) size: u64,
}

impl Symbol {
    /// Returns the name of the procedure.
    #[inline]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the offset of the instruction pointer from the symbol's starting address.
    #[inline]
    pub fn offset(&self) -> u64 {
        self.offset
    }

    /// Returns the starting address of the symbol.
    #[inline]
    pub fn address(&self) -> u64 {
        self.address
    }

    /// Returns the size of the symbol.
    #[inline]
    pub fn size(&self) -> u64 {
        self.size
    }
}

/// A convenience wrapper over `TraceOptions` which returns a maximally verbose trace.
pub fn trace(handle: &crate::proc::ProcHandle) -> io::Result<Process> {
    TraceOptions::new()
        .thread_names(true)
        .symbols(true)
        .demangle(true)
        .trace(handle)
}

/// Options controlling the behavior of tracing.
#[derive(Debug, Clone)]
pub struct TraceOptions {
    pub(crate) snapshot: bool,
    pub(crate) thread_names: bool,
    pub(crate) symbols: bool,
    pub(crate) demangle: bool,
    pub(crate) module: bool,
    pub(crate) source: bool,
    pub(crate) inlines: bool,
    pub(crate) ptrace_attach: bool,
    pub(crate) tid: Option<u32>,
    pub(crate) max_frames: usize,
}

impl Default for TraceOptions {
    fn default() -> TraceOptions {
        TraceOptions {
            snapshot: false,
            thread_names: false,
            symbols: false,
            demangle: false,
            module: false,
            source: false,
            inlines: false,
            ptrace_attach: true,
            tid: None,
            max_frames: 0,
        }
    }
}

impl TraceOptions {
    /// Returns a new `TraceOptions` with default settings.
    pub fn new() -> TraceOptions {
        TraceOptions::default()
    }

    /// If set, the threads of the process will be traced in a consistent snapshot.
    ///
    /// A snapshot-mode trace ensures a consistent view of all threads, but requires that all
    /// threads be paused for the entire duration of the trace.
    ///
    /// Defaults to `false`.
    pub fn snapshot(&mut self, snapshot: bool) -> &mut TraceOptions {
        self.snapshot = snapshot;
        self
    }

    /// If set, the names of the process's threads will be recorded.
    ///
    /// Defaults to `false`.
    pub fn thread_names(&mut self, thread_names: bool) -> &mut TraceOptions {
        self.thread_names = thread_names;
        self
    }

    /// If set, information about the symbol at each frame will be recorded.
    ///
    /// Defaults to `false`.
    pub fn symbols(&mut self, symbols: bool) -> &mut TraceOptions {
        self.symbols = symbols;
        self
    }

    /// If set, C++ symbol names will be demangled.
    ///
    /// Only effective when `symbols` is also enabled. Defaults to `false`.
    pub fn demangle(&mut self, demangle: bool) -> &mut TraceOptions {
        self.demangle = demangle;
        self
    }

    /// If set, the module (shared library) file path will be recorded for each frame.
    ///
    /// Defaults to `false`.
    pub fn module(&mut self, module: bool) -> &mut TraceOptions {
        self.module = module;
        self
    }

    /// If set, source file and line number information will be recorded for each frame.
    ///
    /// Requires debug information to be available. Defaults to `false`.
    pub fn source(&mut self, source: bool) -> &mut TraceOptions {
        self.source = source;
        self
    }

    /// If set, inlined function frames will be shown using DWARF debuginfo.
    ///
    /// When a function is inlined at a call site, additional frames are emitted
    /// for each level of inlining. Requires debug information. Defaults to `false`.
    pub fn inlines(&mut self, inlines: bool) -> &mut TraceOptions {
        self.inlines = inlines;
        self
    }

    /// If set, `pstack` will automatically attach to threads via ptrace.
    ///
    /// If disabled, the calling process must already be attached to all traced threads, and the
    /// threads must be in the stopped state.
    ///
    /// Defaults to `true`.
    pub fn ptrace_attach(&mut self, ptrace_attach: bool) -> &mut TraceOptions {
        self.ptrace_attach = ptrace_attach;
        self
    }

    /// If set, only the specified thread will be traced.
    ///
    /// When tracing a live process, this avoids attaching to every thread
    /// just to discard all but one.
    ///
    /// Defaults to `None` (trace all threads).
    pub fn tid(&mut self, tid: u32) -> &mut TraceOptions {
        self.tid = Some(tid);
        self
    }

    /// Sets the maximum number of frames to collect per thread.
    ///
    /// When set to a non-zero value, the backend stops symbolizing frames once
    /// the limit is reached, avoiding expensive DWARF lookups for frames that
    /// would be discarded.
    ///
    /// Defaults to `0` (unlimited).
    pub fn max_frames(&mut self, max_frames: usize) -> &mut TraceOptions {
        self.max_frames = max_frames;
        self
    }

    /// Traces the threads of the specified process.
    pub fn trace(&self, handle: &crate::proc::ProcHandle) -> io::Result<Process> {
        let pid = handle.pid() as u32;
        let mut threads = Vec::new();
        self.trace_each(handle, |thread| threads.push(thread))?;
        Ok(Process { id: pid, threads })
    }

    /// Traces the threads of the specified process, calling `each` per thread.
    pub fn trace_each<F>(&self, handle: &crate::proc::ProcHandle, mut each: F) -> io::Result<()>
    where
        F: FnMut(Thread),
    {
        let pid = handle.pid() as u32;

        if let Some(tid) = self.tid {
            if let Some(thread) = open_thread_or_warn(tid, self.ptrace_attach)? {
                each(thread.info(pid, self, handle));
            }
            return Ok(());
        }

        if self.snapshot {
            self.trace_snapshot_each(handle, pid, &mut each)?;
        } else {
            self.trace_rolling_each(handle, pid, &mut each)?;
        }

        Ok(())
    }

    /// Traces the threads captured in a core dump file.
    ///
    /// The `ProcHandle` provides the core-file ELF data and is used to
    /// resolve the main thread's name via systemd-coredump metadata
    /// (`COREDUMP_COMM`).
    pub fn trace_core(&self, handle: &crate::proc::ProcHandle) -> io::Result<Process> {
        let mut pid = 0;
        let mut threads = Vec::new();
        self.trace_core_each(handle, |p| pid = p, |thread| threads.push(thread))?;
        Ok(Process { id: pid, threads })
    }

    /// Traces the threads captured in a core dump file, calling `each` per thread.
    ///
    /// `header` is called with the core pid before any threads are streamed.
    pub fn trace_core_each<H, F>(
        &self,
        handle: &crate::proc::ProcHandle,
        header: H,
        mut each: F,
    ) -> io::Result<()>
    where
        H: FnOnce(u32),
        F: FnMut(Thread),
    {
        if !handle.is_core() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "ProcHandle has no core file",
            ));
        }
        let pid = handle.pid() as u32;
        header(pid);
        let comm = handle.comm().ok().map(|s| s.to_string_lossy().into_owned());
        let tids = handle.tids()?;
        for tid in tids {
            let tid32 = tid as u32;
            if self.tid.is_some_and(|f| f != tid32) {
                continue;
            }
            let frames = handle.trace_thread(tid32, self);
            let name = if self.thread_names {
                if tid32 == pid {
                    comm.clone()
                } else {
                    None
                }
            } else {
                None
            };
            each(Thread {
                id: tid32,
                name,
                frames,
            });
        }
        Ok(())
    }

    fn trace_snapshot_each<F>(
        &self,
        handle: &crate::proc::ProcHandle,
        pid: u32,
        each: &mut F,
    ) -> io::Result<()>
    where
        F: FnMut(Thread),
    {
        for t in snapshot_threads(pid, self.ptrace_attach)?.iter() {
            each(t.info(pid, self, handle));
        }
        Ok(())
    }

    fn trace_rolling_each<F>(
        &self,
        handle: &crate::proc::ProcHandle,
        pid: u32,
        each: &mut F,
    ) -> io::Result<()>
    where
        F: FnMut(Thread),
    {
        each_thread(pid, |tid| {
            if let Some(thread) = open_thread_or_warn(tid, self.ptrace_attach)? {
                each(thread.info(pid, self, handle));
            }
            Ok(())
        })
    }
}

fn open_thread_or_warn(tid: u32, ptrace_attach: bool) -> io::Result<Option<TracedThread>> {
    let thread = if ptrace_attach {
        TracedThread::attach(tid)
    } else {
        TracedThread::traced(tid)
    };
    match thread {
        Ok(thread) => Ok(Some(thread)),
        Err(ref e) if e.raw_os_error() == Some(ESRCH) => {
            eprintln!("warning: error attaching to thread {tid}: {e}");
            Ok(None)
        }
        Err(e) => Err(e),
    }
}

fn snapshot_threads(pid: u32, ptrace_attach: bool) -> io::Result<BTreeSet<TracedThread>> {
    let mut threads = BTreeSet::new();

    // new threads may be created while we're in the process of stopping them all, so loop a couple
    // of times to hopefully converge
    for _ in 0..5 {
        let prev = threads.len();
        add_threads(&mut threads, pid, ptrace_attach)?;
        if prev == threads.len() {
            break;
        }
    }

    Ok(threads)
}

fn add_threads(
    threads: &mut BTreeSet<TracedThread>,
    pid: u32,
    ptrace_attach: bool,
) -> io::Result<()> {
    each_thread(pid, |tid| {
        if !threads.contains(&tid) {
            if let Some(thread) = open_thread_or_warn(tid, ptrace_attach)? {
                threads.insert(thread);
            }
        }
        Ok(())
    })
}

fn each_thread<F>(pid: u32, mut f: F) -> io::Result<()>
where
    F: FnMut(u32) -> io::Result<()>,
{
    let dir = format!("/proc/{pid}/task");
    for entry in fs::read_dir(dir)? {
        let entry = entry?;

        if let Some(tid) = entry
            .file_name()
            .to_str()
            .and_then(|s| s.parse::<u32>().ok())
        {
            f(tid)?;
        }
    }
    Ok(())
}

struct TracedThread {
    id: u32,
    // True if TraceOptions::ptrace_attach was true (default value)
    // It means that Drop should perform detach
    should_detach: bool,
}

impl Drop for TracedThread {
    fn drop(&mut self) {
        if self.should_detach {
            unsafe {
                ptrace(
                    PTRACE_DETACH,
                    self.id as pid_t,
                    ptr::null_mut::<c_void>(),
                    ptr::null_mut::<c_void>(),
                );
            }
        }
    }
}

// these need to be manually implemented to only work off of id so the borrow impl works
impl PartialOrd for TracedThread {
    fn partial_cmp(&self, other: &TracedThread) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for TracedThread {
    fn cmp(&self, other: &TracedThread) -> Ordering {
        self.id.cmp(&other.id)
    }
}

impl PartialEq for TracedThread {
    fn eq(&self, other: &TracedThread) -> bool {
        self.id == other.id
    }
}

impl Eq for TracedThread {}

impl Borrow<u32> for TracedThread {
    fn borrow(&self) -> &u32 {
        &self.id
    }
}

impl TracedThread {
    fn attach(pid: u32) -> io::Result<TracedThread> {
        unsafe {
            let ret = ptrace(
                PTRACE_SEIZE,
                pid as pid_t,
                ptr::null_mut::<c_void>(),
                ptr::null_mut::<c_void>(),
            );
            if ret != 0 {
                let e = io::Error::last_os_error();
                // ptrace returns ESRCH if PTRACE_SEIZE isn't supported for some reason
                if e.raw_os_error() == Some(ESRCH) {
                    return TracedThread::new_fallback(pid);
                }

                return Err(e);
            }

            let thread = TracedThread {
                id: pid,
                should_detach: true,
            };

            let ret = ptrace(
                PTRACE_INTERRUPT,
                pid as pid_t,
                ptr::null_mut::<c_void>(),
                ptr::null_mut::<c_void>(),
            );
            if ret != 0 {
                return Err(io::Error::last_os_error());
            }

            let mut status = 0;
            while waitpid(pid as pid_t, &mut status, __WALL) < 0 {
                let e = io::Error::last_os_error();
                if e.kind() != io::ErrorKind::Interrupted {
                    return Err(e);
                }
            }

            if !WIFSTOPPED(status) {
                return Err(io::Error::other(format!("unexpected wait status {status}")));
            }

            Ok(thread)
        }
    }

    /// Creates `TracedThread` without attaching to process. Should not be used, if pid is not
    /// traced by current process
    fn traced(pid: u32) -> io::Result<TracedThread> {
        Ok(TracedThread {
            id: pid,
            should_detach: false,
        })
    }

    fn new_fallback(pid: u32) -> io::Result<TracedThread> {
        unsafe {
            let ret = ptrace(
                PTRACE_ATTACH,
                pid as pid_t,
                ptr::null_mut::<c_void>(),
                ptr::null_mut::<c_void>(),
            );
            if ret != 0 {
                return Err(io::Error::last_os_error());
            }

            let thread = TracedThread {
                id: pid,
                should_detach: true,
            };

            let mut status = 0;
            loop {
                let ret = waitpid(pid as pid_t, &mut status, __WALL);
                if ret < 0 {
                    let e = io::Error::last_os_error();
                    if e.kind() != io::ErrorKind::Interrupted {
                        return Err(e);
                    }

                    continue;
                }

                if !WIFSTOPPED(status) {
                    return Err(io::Error::other(format!("unexpected wait status {status}")));
                }

                let sig = WSTOPSIG(status);
                if sig == SIGSTOP {
                    return Ok(thread);
                }

                let ret = ptrace(
                    PTRACE_CONT,
                    pid as pid_t,
                    ptr::null_mut::<c_void>(),
                    sig as *const c_void,
                );
                if ret != 0 {
                    return Err(io::Error::last_os_error());
                }
            }
        }
    }

    fn info(&self, pid: u32, options: &TraceOptions, handle: &crate::proc::ProcHandle) -> Thread {
        let name = if options.thread_names {
            self.name(pid)
        } else {
            None
        };

        let frames = handle.trace_thread(self.id, options);

        Thread {
            id: self.id,
            name,
            frames,
        }
    }

    fn name(&self, pid: u32) -> Option<String> {
        let path = format!("/proc/{}/task/{}/comm", pid, self.id);
        let mut name = vec![];
        match File::open(path).and_then(|mut f| f.read_to_end(&mut name)) {
            Ok(_) => Some(String::from_utf8_lossy(&name).trim().to_string()),
            Err(e) => {
                eprintln!("warning: error getting name for thread {}: {}", self.id, e);
                None
            }
        }
    }
}