Skip to content

Commit

Permalink
Add Github CI (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-char authored Feb 16, 2024
1 parent 3c5a2cc commit ff05c11
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.aarch64-unknown-linux-musl]
linker = "ld.lld"
41 changes: 41 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: build

on: [push, pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
targets: x86_64-unknown-linux-musl,aarch64-unknown-linux-musl
components: rust-src

- uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: lld
version: 1.0

- name: Build x86_64
run: cargo build --target x86_64-unknown-linux-musl --release

- name: Upload x86_64 Building
uses: actions/upload-artifact@v4
with:
name: pwatch-x86_64-unknown-linux-musl
path: target/x86_64-unknown-linux-musl/release/pwatch

- name: Build AArch64
run: cargo build --target aarch64-unknown-linux-musl --release

- name: Upload AArch64 Building
uses: actions/upload-artifact@v4
with:
name: pwatch-aarch64-unknown-linux-musl
path: target/aarch64-unknown-linux-musl/release/pwatch
24 changes: 24 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: lint

on: [push, pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
components: rustfmt,clippy,rust-src

- name: Check formatting
run: |
cargo fmt --check
- name: Run clippy
run: |
cargo clippy -- --deny warnings
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/doublify/pre-commit-rust
rev: v1.0
hooks:
- id: fmt
- id: cargo-check
- id: clippy
10 changes: 5 additions & 5 deletions src/arch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#[cfg(target_arch="x86_64")]
#[cfg(target_arch = "x86_64")]
mod x86;

#[cfg(target_arch="x86_64")]
#[cfg(target_arch = "x86_64")]
pub use x86::*;

#[cfg(target_arch="aarch64")]
#[cfg(target_arch = "aarch64")]
mod aarch64;

#[cfg(target_arch="aarch64")]
#[cfg(target_arch = "aarch64")]
pub use aarch64::*;

pub const fn regs_count() -> usize {
SAMPLE_REGS_USER.count_ones() as usize
}
}
18 changes: 10 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ fn parse_watchpoint_type(s: &str) -> Option<(u32, u32)> {
if let Some(s) = s.strip_prefix("rw") {
let len = parse_len(s)?;
Some((sys::bindings::HW_BREAKPOINT_RW, len))
} else if let Some(s) = s.strip_prefix("r") {
} else if let Some(s) = s.strip_prefix('r') {
let len = parse_len(s)?;
Some((sys::bindings::HW_BREAKPOINT_R, len))
} else if let Some(s) = s.strip_prefix("w") {
} else if let Some(s) = s.strip_prefix('w') {
let len = parse_len(s)?;
Some((sys::bindings::HW_BREAKPOINT_W, len))
} else if s == "x" {
Expand All @@ -54,11 +54,7 @@ fn parse_watchpoint_type(s: &str) -> Option<(u32, u32)> {
}

fn parse_addr(s: &str) -> Option<u64> {
if s.starts_with("0x") {
u64::from_str_radix(&s[2..], 16).ok()
} else {
u64::from_str_radix(s, 16).ok()
}
u64::from_str_radix(s.strip_prefix("0x").unwrap_or(s), 16).ok()
}

#[tokio::main(flavor = "current_thread")]
Expand Down Expand Up @@ -99,7 +95,13 @@ async fn main() -> anyhow::Result<()> {

fn handle_event(data: SampleData) {
println!("-------");
println!("{}: {} {}: {}", "pid".yellow().bold(), data.pid, "tid".yellow().bold(), data.tid);
println!(
"{}: {} {}: {}",
"pid".yellow().bold(),
data.pid,
"tid".yellow().bold(),
data.tid
);
for (i, reg) in data.regs.iter().enumerate() {
print!("{:>5}: 0x{:016x} ", arch::id_to_str(i).bold().blue(), reg);
if (i + 1) % 4 == 0 {
Expand Down
14 changes: 5 additions & 9 deletions src/perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ impl PerfMap {
) -> anyhow::Result<Self> {
let mut attrs = sys::bindings::perf_event_attr::default();

// Populate the fields we need.
attrs.set_precise_ip(2);
attrs.size = std::mem::size_of::<sys::bindings::perf_event_attr>() as u32;
attrs.type_ = sys::bindings::PERF_TYPE_BREAKPOINT;
attrs.__bindgen_anon_1.sample_period = 1;
attrs.__bindgen_anon_2.wakeup_events = 1;
attrs.bp_type = r#type;
attrs.__bindgen_anon_3.bp_addr = addr;
attrs.__bindgen_anon_4.bp_len = len as u64;
attrs.set_precise_ip(2);
attrs.__bindgen_anon_4.bp_len = len;
attrs.sample_type = sys::bindings::PERF_SAMPLE_REGS_USER | sys::bindings::PERF_SAMPLE_TID;
attrs.sample_regs_user = arch::SAMPLE_REGS_USER;

Expand Down Expand Up @@ -75,10 +74,7 @@ impl PerfMap {
})
}

pub async fn events<F: FnMut(SampleData) -> ()>(
&self,
mut handle: F,
) -> anyhow::Result<Infallible> {
pub async fn events<F: FnMut(SampleData)>(&self, mut handle: F) -> anyhow::Result<Infallible> {
let mmap_page_metadata = unsafe {
(self.mmap_addr as *mut sys::bindings::perf_event_mmap_page)
.as_mut()
Expand Down Expand Up @@ -106,8 +102,8 @@ impl PerfMap {
let abi = unsafe { *(get_addr(offset) as *const u64) };
offset += 8;
let mut regs = vec![0u64; arch::regs_count()];
for i in 0..arch::regs_count() {
regs[i] = unsafe { *(get_addr(offset) as *const u64) };
for reg in regs.iter_mut().take(arch::regs_count()) {
*reg = unsafe { *(get_addr(offset) as *const u64) };
offset += 8;
}
handle(SampleData {
Expand Down

0 comments on commit ff05c11

Please sign in to comment.