Skip to content
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
49 changes: 41 additions & 8 deletions app/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,66 @@
// SPDX-License-Identifier: MIT

use intel_crashlog::prelude::*;
use intel_crashlog::source::Capability;

pub fn rearm(sources: Vec<CrashLogSource>) -> Result<(), Error> {
control_command(sources, CrashLogSource::rearm)
control_command(sources, Capability::Rearm, CrashLogSource::rearm)
}

pub fn trigger(sources: Vec<CrashLogSource>) -> Result<(), Error> {
control_command(sources, CrashLogSource::trigger)
control_command(sources, Capability::Trigger, CrashLogSource::trigger)
}

pub fn clear(sources: Vec<CrashLogSource>) -> Result<(), Error> {
control_command(sources, CrashLogSource::clear)
control_command(sources, Capability::Clear, CrashLogSource::clear)
}

pub fn enable(sources: Vec<CrashLogSource>) -> Result<(), Error> {
control_command(sources, CrashLogSource::enable)
control_command(sources, Capability::EnableDisable, CrashLogSource::enable)
}

pub fn disable(sources: Vec<CrashLogSource>) -> Result<(), Error> {
control_command(sources, CrashLogSource::disable)
control_command(sources, Capability::EnableDisable, CrashLogSource::disable)
}

fn control_command<F>(sources: Vec<CrashLogSource>, control: F) -> Result<(), Error>
fn control_command<F>(
sources: Vec<CrashLogSource>,
capability: Capability,
control: F,
) -> Result<(), Error>
where
F: Fn(&CrashLogSource) -> Result<(), Error>,
{
for source in sources {
control(&source)?;
let mut cmd_success = false;
let mut first_error = None;

let control_sources = if sources.is_empty() {
CrashLogSource::discover_distinct()
.into_iter()
.filter(|src| src.capabilities().contains(&capability))
.collect()
} else {
sources
};

if control_sources.is_empty() {
return Err(Error::NoCrashLogSourceFound);
}

for source in control_sources {
match control(&source) {
Ok(()) => cmd_success = true,
Err(err) => {
log::warn!("Error while running {capability} command on {source}: {err}");
if first_error.is_none() {
first_error = Some(err);
}
}
}
}

if !cmd_success {
return Err(first_error.unwrap_or(Error::InternalError));
}

Ok(())
Expand Down
22 changes: 22 additions & 0 deletions lib/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ impl FromStr for CrashLogSource {
impl CrashLogSource {
/// Returns all the Crash Log sources that are available in the platform
pub fn discover() -> Vec<Self> {
Self::discover_sources()
}

/// Returns all the Crash Log sources that are available in the platform without duplicates
pub fn discover_distinct() -> Vec<Self> {
Self::discover_sources()
.into_iter()
.filter(|src| src.is_distinct())
.collect()
}

fn discover_sources() -> Vec<Self> {
let mut sources = Vec::new();

if Acpi::default().is_available() {
Expand All @@ -155,6 +167,16 @@ impl CrashLogSource {
sources
}

/// Returns `true` if the Crash log source is distinct, e. g. it is not mirrored by another
/// Crash Log source in the system
pub fn is_distinct(&self) -> bool {
match self {
Self::Acpi => true,
Self::EventLog => true,
Self::PmtDevice(dev) => Pmt::default().is_distinct(dev),
}
}

/// Returns the Crash Log extracted from the platform using the current Crash Log source
#[cfg(feature = "extraction")]
pub fn extract(&self) -> Result<Vec<CrashLog>, Error> {
Expand Down
10 changes: 10 additions & 0 deletions lib/src/source/pmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,14 @@ impl Pmt {
PmtDeviceId::Bdf(bdf) => format!("PMT endpoints for PCI device {bdf}"),
}
}

#[cfg(target_os = "linux")]
pub fn is_distinct(&self, dev: &PmtDeviceId) -> bool {
self.sysfs.is_distinct(dev)
}

#[cfg(not(target_os = "linux"))]
pub fn is_distinct(&self, _dev: &PmtDeviceId) -> bool {
true
}
}
8 changes: 8 additions & 0 deletions lib/src/source/pmt/sysfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,17 @@ impl PmtSysFs {
devices
}

pub(super) fn is_distinct(&self, dev: &PmtDeviceId) -> bool {
match dev {
PmtDeviceId::Name(_name) => true,
PmtDeviceId::Bdf(_bdf) => false,
}
}

pub fn get_all_endpoints(&self) -> Vec<PmtSysFsEndpoint> {
self.discover()
.into_iter()
.filter(|dev| self.is_distinct(dev))
.flat_map(|devid| self.get_endpoints(&devid))
.collect()
}
Expand Down
Loading