Skip to content

Commit

Permalink
debug segfault on iohid
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Jul 3, 2024
1 parent df0ade9 commit 894e962
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ jobs:
- run: rustup update --no-self-update stable && rustup default stable
- run: cargo fmt --check
- run: cargo check --release --locked

- run: cargo build --release --locked
- run: |
cp target/release/macmon macmon
tar czf macmon-${{ github.ref_name }}.tar.gz readme.md LICENSE macmon
ls -lah | grep macmon
id: archive
- uses: actions/upload-artifact@v4
with:
name: macmon-${{ github.ref_name }}.tar.gz
path: macmon-${{ github.ref_name }}.tar.gz
12 changes: 6 additions & 6 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::sources::{

type WithError<T> = Result<T, Box<dyn std::error::Error>>;

fn divider(msg: &str) {
fn print_divider(msg: &str) {
if msg.len() == 0 {
println!("{}", "-".repeat(80));
return;
Expand All @@ -26,7 +26,7 @@ pub fn print_debug() -> WithError<()> {
let procs = out["SPHardwareDataType"][0]["number_processors"].as_str().unwrap().to_string();
println!("Chip: {} | Model: {} | OS: {} | {}", chip, model, os_ver, procs);

divider("AppleARMIODevice");
print_divider("AppleARMIODevice");
for (entry, name) in IOServiceIterator::new("AppleARMIODevice")? {
if name == "pmgr" {
let item = cfio_get_props(entry, name)?;
Expand All @@ -49,7 +49,7 @@ pub fn print_debug() -> WithError<()> {
}
}

divider("IOReport");
print_divider("IOReport");
let channels = vec![
("Energy Model", None),
("CPU Stats", Some("CPU Complex Performance States")),
Expand All @@ -67,15 +67,15 @@ pub fn print_debug() -> WithError<()> {
}
}

divider("IOHID");
print_divider("IOHID");
let hid = IOHIDSensors::new()?;
for (key, val) in hid.get_metrics() {
println!("{:>32}: {:6.2}", key, val);
}

print_divider("SMC temp sensors");
const FLOAT_TYPE: u32 = 1718383648; // FourCC: "flt "

divider("SMC temp sensors");
let mut smc = SMC::new()?;
let keys = smc.read_all_keys().unwrap_or(vec![]);
for key in &keys {
Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn print_debug() -> WithError<()> {
}

println!(""); // close previous line
divider("");
print_divider("");

Ok(())
}
16 changes: 13 additions & 3 deletions src/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ impl Drop for IOReport {
}

// MARK: IOHID Bindings
// referenced from: https://github.com/freedomtan/sensors/blob/master/sensors/sensors.m

#[repr(C)]
struct IOHIDServiceClient(libc::c_void);
Expand Down Expand Up @@ -625,8 +626,15 @@ impl IOHIDSensors {
pub fn get_metrics(&self) -> Vec<(String, f32)> {
unsafe {
let system = IOHIDEventSystemClientCreate(kCFAllocatorDefault);
if system.is_null() {
return vec![];
}

IOHIDEventSystemClientSetMatching(system, self.sensors);
let services = IOHIDEventSystemClientCopyServices(system);
if services.is_null() {
return vec![];
}

let mut items = vec![] as Vec<(String, f32)>;
for i in 0..CFArrayGetCount(services) {
Expand All @@ -635,9 +643,11 @@ impl IOHIDSensors {
let name = from_cfstr(name);

let event = IOHIDServiceClientCopyEvent(sc, kIOHIDEventTypeTemperature, 0, 0);
let temp = IOHIDEventGetFloatValue(event, kIOHIDEventTypeTemperature << 16);
CFRelease(event as _);
items.push((name, temp as f32));
if !event.is_null() {
let temp = IOHIDEventGetFloatValue(event, kIOHIDEventTypeTemperature << 16);
CFRelease(event as _);
items.push((name, temp as f32));
}
}

CFRelease(services as _);
Expand Down

0 comments on commit 894e962

Please sign in to comment.