Skip to content

Commit

Permalink
Merge pull request #17 from nbaksalyar/sdt-semaphores
Browse files Browse the repository at this point in the history
Support conditional SystemTap probes using semaphores
  • Loading branch information
cuviper committed Jan 3, 2023
2 parents 8520232 + 90c8d17 commit 2d1567c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Install
uses: actions-rs/toolchain@v1
with:
toolchain: beta
toolchain: 1.66.0
profile: minimal
override: true
- name: Build
Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Install
uses: actions-rs/toolchain@v1
with:
toolchain: beta
toolchain: stable
target: ${{ matrix.target }}
profile: minimal
override: true
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[project]
[package]
name = "probe"
version = "0.3.0"
version = "0.4.0"
authors = ["Josh Stone <cuviper@gmail.com>"]
description = "Static instrumentation probes"
documentation = "https://docs.rs/probe/"
homepage = "https://github.com/cuviper/rust-libprobe"
repository = "https://github.com/cuviper/rust-libprobe"
license = "Apache-2.0 OR MIT"
edition = "2021"
rust-version = "1.66"
exclude = ["/.github/**"]

[lib]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# libprobe: Static probes for Rust

[![probe crate](https://img.shields.io/crates/v/probe.svg)](https://crates.io/crates/probe)
![minimum rustc 1.59](https://img.shields.io/badge/rustc-1.59+-red.svg)
![minimum rustc 1.66](https://img.shields.io/badge/rustc-1.66+-red.svg)
[![probe documentation](https://docs.rs/probe/badge.svg)](https://docs.rs/probe)
[![build status](https://github.com/cuviper/rust-libprobe/workflows/CI/badge.svg)](https://github.com/cuviper/rust-libprobe/actions)

Expand All @@ -20,7 +20,7 @@ The recommended way to use it is to add a line into your Cargo.toml such as:

```toml
[dependencies]
probe = "0.3"
probe = "0.4"
```

Then `use probe::probe;` in your code and insert macro calls wherever you want
Expand Down
16 changes: 16 additions & 0 deletions examples/semaphore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use probe::probe;

fn main() {
let mut iter = 0;
loop {
iter += 1;
probe!(foo, iter, {
// This delay is an exaggeration of the overhead of a probe argument, but it's only
// incurred while something is attached to the probe, thanks to the semaphore.
std::thread::sleep(std::time::Duration::from_secs(1));
iter
});
}
}

// bcc/tools/trace.py -p $(pidof semaphore) 'u::foo:iter "iter = %d", arg1'
7 changes: 6 additions & 1 deletion src/platform/default.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#[doc(hidden)]
#[macro_export]
macro_rules! platform_probe(
($provider:ident, $name:ident, $($arg:expr,)*) => ()
($provider:ident, $name:ident, $($arg:expr,)*) => ({
// Expand the arguments so they don't cause unused warnings.
if false {
let _ = ($($arg,)*);
}
})
);
18 changes: 10 additions & 8 deletions src/platform/systemtap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ macro_rules! sdt_asm(
#[macro_export]
macro_rules! _sdt_asm(
($size:literal, options ($($opt:ident),*), $provider:ident, $name:ident, $($argstr:literal, $arg:expr,)*) => (
::core::arch::asm!(concat!(r#"
static mut SEMAPHORE: u16 = 0;
if ::core::ptr::read_volatile(&SEMAPHORE) != 0 {
::core::arch::asm!(concat!(r#"
990: nop
.pushsection .note.stapsdt,"?","note"
.balign 4
Expand All @@ -110,7 +112,7 @@ macro_rules! _sdt_asm(
992: .balign 4
993: ."#, $size, r#"byte 990b
."#, $size, r#"byte _.stapsdt.base
."#, $size, r#"byte 0 // FIXME set semaphore address
."#, $size, r#"byte {}
.asciz ""#, stringify!($provider), r#""
.asciz ""#, stringify!($name), r#""
.asciz ""#, $($argstr,)* r#""
Expand All @@ -123,10 +125,10 @@ macro_rules! _sdt_asm(
_.stapsdt.base: .space 1
.size _.stapsdt.base, 1
.popsection
.endif
"#
),
$(in(reg) (($arg) as isize) ,)*
options(readonly, nostack, preserves_flags, $($opt),*),
)
.endif"#),
sym SEMAPHORE,
$(in(reg) (($arg) as isize) ,)*
options(readonly, nostack, preserves_flags, $($opt),*),
);
}
));

0 comments on commit 2d1567c

Please sign in to comment.