Skip to content

Commit 382fe31

Browse files
committed
feat(capi+go): implement API for clearing profiling data.
1 parent d7487c8 commit 382fe31

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

capi/include/yara_x.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,12 +711,23 @@ enum YRX_RESULT yrx_scanner_set_global_float(struct YRX_SCANNER *scanner,
711711
// Iterates over the slowest N rules, calling the callback for each rule.
712712
//
713713
// Requires the `rules-profiling` feature, otherwise returns
714-
// [`YRX_RESULT::NOT_SUPPORTED`]
714+
// [`YRX_RESULT::NOT_SUPPORTED`].
715715
//
716716
// See [`YRX_SLOWEST_RULES_CALLBACK`] for more details.
717717
enum YRX_RESULT yrx_scanner_iter_slowest_rules(struct YRX_SCANNER *scanner,
718718
size_t n,
719719
YRX_SLOWEST_RULES_CALLBACK callback,
720720
void *user_data);
721721

722+
// Clears all accumulated profiling data.
723+
//
724+
// This resets the profiling data collected during rule execution across
725+
// scanned files. Use this to start a new profiling session, ensuring the
726+
// results reflect only the data gathered after this method is called.
727+
//
728+
// Requires the `rules-profiling` feature, otherwise returns
729+
// [`YRX_RESULT::NOT_SUPPORTED`].
730+
//
731+
enum YRX_RESULT yrx_scanner_clear_profiling_data(struct YRX_SCANNER *scanner);
732+
722733
#endif /* YARA_X */

capi/src/scanner.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ pub type YRX_SLOWEST_RULES_CALLBACK = extern "C" fn(
325325
/// Iterates over the slowest N rules, calling the callback for each rule.
326326
///
327327
/// Requires the `rules-profiling` feature, otherwise returns
328-
/// [`YRX_RESULT::NOT_SUPPORTED`]
328+
/// [`YRX_RESULT::NOT_SUPPORTED`].
329329
///
330330
/// See [`YRX_SLOWEST_RULES_CALLBACK`] for more details.
331331
#[no_mangle]
@@ -362,3 +362,31 @@ pub unsafe extern "C" fn yrx_scanner_iter_slowest_rules(
362362
YRX_RESULT::SUCCESS
363363
}
364364
}
365+
366+
/// Clears all accumulated profiling data.
367+
///
368+
/// This resets the profiling data collected during rule execution across
369+
/// scanned files. Use this to start a new profiling session, ensuring the
370+
/// results reflect only the data gathered after this method is called.
371+
///
372+
/// Requires the `rules-profiling` feature, otherwise returns
373+
/// [`YRX_RESULT::NOT_SUPPORTED`].
374+
///
375+
#[no_mangle]
376+
#[allow(unused_variables)]
377+
pub unsafe extern "C" fn yrx_scanner_clear_profiling_data(
378+
scanner: *mut YRX_SCANNER,
379+
) -> YRX_RESULT {
380+
#[cfg(not(feature = "rules-profiling"))]
381+
return YRX_RESULT::NOT_SUPPORTED;
382+
383+
#[cfg(feature = "rules-profiling")]
384+
{
385+
match scanner.as_mut() {
386+
Some(s) => s.inner.clear_profiling_data(),
387+
None => return YRX_RESULT::INVALID_ARGUMENT,
388+
};
389+
390+
YRX_RESULT::SUCCESS
391+
}
392+
}

go/scanner.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ func slowestRulesCallback(
286286
// SlowestRules returns information about the slowest rules and how much
287287
// time they spent matching patterns and executing their conditions.
288288
//
289-
// In order to use this function the YARA-X C library must be built with
290-
// support for rules profiling, which is done by enabling the `rules-profiling`
291-
// feature. Otherwise, calling this function will cause a panic.
289+
// In order to use this function, the YARA-X C library must be built with
290+
// support for rules profiling by enabling the `rules-profiling` feature.
291+
// Otherwise, calling this function will cause a panic.
292292
func (s *Scanner) SlowestRules(n int) []ProfilingInfo {
293293
profilingInfo := make([]ProfilingInfo, 0)
294294
slowestRules := cgo.NewHandle(&profilingInfo)
@@ -311,6 +311,19 @@ func (s *Scanner) SlowestRules(n int) []ProfilingInfo {
311311
return profilingInfo
312312
}
313313

314+
/// ClearProfilingData resets the profiling data collected during rule execution
315+
/// across scanned files. Use it to start a new profiling session, ensuring the
316+
/// results reflect only the data gathered after this method is called.
317+
//
318+
// In order to use this function, the YARA-X C library must be built with
319+
// support for rules profiling by enabling the `rules-profiling` feature.
320+
// Otherwise, calling this function will cause a panic.
321+
func (s *Scanner) ClearProfilingData() {
322+
if C.yrx_scanner_clear_profiling_data(s.cScanner) == C.NOT_SUPPORTED {
323+
panic("ClearProfilingData requires that the YARA-X C library is built with the `rules-profiling` feature")
324+
}
325+
}
326+
314327
// Destroy destroys the scanner.
315328
//
316329
// Calling this method directly is not necessary, it will be invoked by the

0 commit comments

Comments
 (0)