Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --syscalls option for tracing syscalls (linux only) #421

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ license = "MIT"
build="build.rs"
edition="2021"

[features]
default = ["trace_syscalls"]
trace_syscalls = ["syscalls"]

[target.'cfg(all(target_os="linux", target_arch="x86_64"))'.dependencies]
syscalls = {version="0.3.3", optional=true}

[dependencies]
clap = {version="3.1", features=["wrap_help", "cargo", "derive"]}
clap_complete="3.1"
Expand Down
34 changes: 33 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub struct Config {
pub full_filenames: bool,
#[doc(hidden)]
pub lineno: LineNo,
#[doc(hidden)]
pub trace_syscalls: bool,
}

#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -116,7 +118,7 @@ impl Default for Config {
duration: RecordDuration::Unlimited, native: false,
gil_only: false, include_idle: false, include_thread_ids: false,
hide_progress: false, capture_output: true, dump_json: false, dump_locals: 0, subprocesses: false,
full_filenames: false, lineno: LineNo::LastInstruction }
full_filenames: false, lineno: LineNo::LastInstruction, trace_syscalls: false}
}
}

Expand All @@ -142,6 +144,11 @@ impl Config {
.short('n')
.long("native")
.help("Collect stack traces from native extensions written in Cython, C or C++");
#[cfg(all(target_os="linux", target_arch="x86_64", feature="trace_syscalls"))]
let trace_syscalls = Arg::with_name("trace_syscalls")
.short("S")
.long("syscalls")
.help("Collect syscall traces (Linux only)");

#[cfg(not(target_os="freebsd"))]
let nonblocking = Arg::new("nonblocking")
Expand Down Expand Up @@ -270,6 +277,14 @@ impl Config {
#[cfg(unwind)]
let dump = dump.arg(native.clone());

// add syscall traces if appropriate
#[cfg(all(target_os="linux", target_arch="x86_64", feature="trace_syscalls"))]
let record = record.arg(trace_syscalls.clone());
#[cfg(all(target_os="linux", target_arch="x86_64", feature="trace_syscalls"))]
let top = top.arg(trace_syscalls.clone());
#[cfg(all(target_os="linux", target_arch="x86_64", feature="trace_syscalls"))]
let dump = dump.arg(trace_syscalls.clone());

// Nonblocking isn't an option for freebsd, remove
#[cfg(not(target_os="freebsd"))]
let record = record.arg(nonblocking.clone());
Expand Down Expand Up @@ -346,6 +361,23 @@ impl Config {

// options that can be shared between subcommands
config.pid = matches.value_of("pid").map(|p| p.parse().expect("invalid pid"));
config.python_program = matches.values_of("python_program").map(|vals| {
vals.map(|v| v.to_owned()).collect()
});
config.show_line_numbers = matches.occurrences_of("nolineno") == 0;
config.include_idle = matches.occurrences_of("idle") > 0;
config.gil_only = matches.occurrences_of("gil") > 0;
config.include_thread_ids = matches.occurrences_of("threads") > 0;
#[cfg(all(target_os="linux", target_arch="x86_64", feature="trace_syscalls"))]
{
config.trace_syscalls = matches.occurrences_of("trace_syscalls") > 0;
}

config.native = matches.occurrences_of("native") > 0;
config.hide_progress = matches.occurrences_of("hideprogress") > 0;
config.dump_json = matches.occurrences_of("json") > 0;
config.dump_locals = matches.occurrences_of("locals");
config.subprocesses = matches.occurrences_of("subprocesses") > 0;
config.full_filenames = matches.occurrences_of("full_filenames") > 0;
if cfg!(unwind) {
config.native = matches.occurrences_of("native") > 0;
Expand Down
32 changes: 32 additions & 0 deletions src/python_spy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use lazy_static::lazy_static;
use remoteprocess::{Process, ProcessMemory, Pid, Tid};
use proc_maps::{get_process_maps, MapRange};

#[cfg(all(target_os="linux", target_arch="x86_64", feature="trace_syscalls"))]
use syscalls::SyscallNo;

use crate::binary_parser::{parse_binary, BinaryInfo};
use crate::config::{Config, LockingStrategy, LineNo};
Expand Down Expand Up @@ -263,6 +265,36 @@ impl PythonSpy {
trace.active = !self._heuristic_is_thread_idle(&trace);
}

// Read current syscall (if any)
#[cfg(all(target_os="linux", target_arch="x86_64", feature="trace_syscalls"))]
{
if self.config.trace_syscalls {
if let Some(tid) = os_thread_id {
let path = format!("/proc/{}/task/{}/syscall", self.pid, tid);
if let Ok(contents) = std::fs::read_to_string(&path) {
let mut it = contents.splitn(2, " ");
if let Some(syscall) = it.next() {
if let Ok(syscall_no) = &syscall[..].parse::<u32>() {
let syscall = SyscallNo::new(*syscall_no as usize);
let name = syscall.map(|s| format!("{} syscall", s.name()));
let name = name.unwrap_or_else(|| format!("syscall #{}", syscall_no));

let frame = crate::stack_trace::Frame {
name,
filename: "<kernel>".to_string(),
line: 0,
module: None,
short_filename: None,
locals: None,
};
trace.frames.insert(0, frame);
}
}
}
}
}
}

// Merge in the native stack frames if necessary
#[cfg(unwind)]
{
Expand Down