[PATCH 10/14] tracetool/simple: add Rust support

Paolo Bonzini posted 14 patches 2 months, 3 weeks ago
There is a newer version of this series
[PATCH 10/14] tracetool/simple: add Rust support
Posted by Paolo Bonzini 2 months, 3 weeks ago
From: Tanish Desai <tanishdesai37@gmail.com>

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 scripts/tracetool/__init__.py       |  2 +-
 scripts/tracetool/backend/simple.py |  7 +++++
 tests/tracetool/simple.rs           | 41 +++++++++++++++++++++++++++++
 tests/tracetool/tracetool-test.py   |  2 ++
 4 files changed, 51 insertions(+), 1 deletion(-)
 create mode 100644 tests/tracetool/simple.rs

diff --git a/scripts/tracetool/__init__.py b/scripts/tracetool/__init__.py
index 0b8ec707332..7542757799e 100644
--- a/scripts/tracetool/__init__.py
+++ b/scripts/tracetool/__init__.py
@@ -336,7 +336,7 @@ def rust_call_extern(self):
         def rust_cast(name, type_):
             if type_ == "const char *":
                 return f"_{name}.as_ptr()"
-            return "_{name}"
+            return f"_{name}"
 
         return ", ".join((rust_cast(name, type_) for type_, name in self._args))
 
diff --git a/scripts/tracetool/backend/simple.py b/scripts/tracetool/backend/simple.py
index a8afc977a20..7acbd6cc69d 100644
--- a/scripts/tracetool/backend/simple.py
+++ b/scripts/tracetool/backend/simple.py
@@ -98,3 +98,10 @@ def generate_c(event, group):
     out('    trace_record_finish(&rec);',
         '}',
         '')
+
+def generate_rs(event, group):
+    out('        extern "C" { fn _simple_%(api)s(%(rust_args)s); }',
+        '        unsafe { _simple_%(api)s(%(args)s); }',
+        api=event.api(),
+        rust_args=event.args.rust_decl_extern(),
+        args=event.args.rust_call_extern())
diff --git a/tests/tracetool/simple.rs b/tests/tracetool/simple.rs
new file mode 100644
index 00000000000..895096088dc
--- /dev/null
+++ b/tests/tracetool/simple.rs
@@ -0,0 +1,41 @@
+// This file is autogenerated by tracetool, do not edit.
+
+#[allow(unused_imports)]
+use std::ffi::c_char;
+#[allow(unused_imports)]
+use qemu_api::bindings;
+
+#[inline(always)]
+fn trace_event_get_state_dynamic_by_id(_id: u16) -> bool {
+    unsafe { (trace_events_enabled_count != 0) && (_id != 0) }
+}
+
+extern "C" {
+    static mut trace_events_enabled_count: u32;
+}
+extern "C" {
+    static mut _TRACE_TEST_BLAH_DSTATE: u16;
+    static mut _TRACE_TEST_WIBBLE_DSTATE: u16;
+}
+const _TRACE_TEST_BLAH_ENABLED: bool = true;
+const _TRACE_TEST_WIBBLE_ENABLED: bool = true;
+
+#[inline(always)]
+#[allow(dead_code)]
+pub fn trace_test_blah(_context: *mut (), _filename: &std::ffi::CStr)
+{
+    if trace_event_get_state_dynamic_by_id(unsafe { _TRACE_TEST_BLAH_DSTATE}) {
+        extern "C" { fn _simple_trace_test_blah(_context: *mut (), _filename: *const std::ffi::c_char); }
+        unsafe { _simple_trace_test_blah(_context, _filename.as_ptr()); }
+    }
+}
+
+#[inline(always)]
+#[allow(dead_code)]
+pub fn trace_test_wibble(_context: *mut (), _value: std::ffi::c_int)
+{
+    if trace_event_get_state_dynamic_by_id(unsafe { _TRACE_TEST_WIBBLE_DSTATE}) {
+        extern "C" { fn _simple_trace_test_wibble(_context: *mut (), _value: std::ffi::c_int); }
+        unsafe { _simple_trace_test_wibble(_context, _value); }
+    }
+}
diff --git a/tests/tracetool/tracetool-test.py b/tests/tracetool/tracetool-test.py
index a420597fc48..ad7dd667288 100755
--- a/tests/tracetool/tracetool-test.py
+++ b/tests/tracetool/tracetool-test.py
@@ -13,6 +13,8 @@ def get_formats(backend):
         "c",
         "h",
     ]
+    if backend in {"simple"}:
+        formats += ["rs"]
     if backend == "dtrace":
         formats += [
             "d",
-- 
2.50.1
Re: [PATCH 10/14] tracetool/simple: add Rust support
Posted by Daniel P. Berrangé 2 months, 2 weeks ago
On Fri, Aug 22, 2025 at 02:26:51PM +0200, Paolo Bonzini wrote:
> From: Tanish Desai <tanishdesai37@gmail.com>
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  scripts/tracetool/__init__.py       |  2 +-
>  scripts/tracetool/backend/simple.py |  7 +++++
>  tests/tracetool/simple.rs           | 41 +++++++++++++++++++++++++++++
>  tests/tracetool/tracetool-test.py   |  2 ++
>  4 files changed, 51 insertions(+), 1 deletion(-)
>  create mode 100644 tests/tracetool/simple.rs
> 
> diff --git a/scripts/tracetool/__init__.py b/scripts/tracetool/__init__.py
> index 0b8ec707332..7542757799e 100644
> --- a/scripts/tracetool/__init__.py
> +++ b/scripts/tracetool/__init__.py
> @@ -336,7 +336,7 @@ def rust_call_extern(self):
>          def rust_cast(name, type_):
>              if type_ == "const char *":
>                  return f"_{name}.as_ptr()"
> -            return "_{name}"
> +            return f"_{name}"
>  
>          return ", ".join((rust_cast(name, type_) for type_, name in self._args))
>

This should be squashed into the earlier patch that introduced the mistake

> diff --git a/tests/tracetool/simple.rs b/tests/tracetool/simple.rs
> new file mode 100644
> index 00000000000..895096088dc
> --- /dev/null
> +++ b/tests/tracetool/simple.rs
> @@ -0,0 +1,41 @@
> +// This file is autogenerated by tracetool, do not edit.
> +
> +#[allow(unused_imports)]
> +use std::ffi::c_char;
> +#[allow(unused_imports)]
> +use qemu_api::bindings;
> +
> +#[inline(always)]
> +fn trace_event_get_state_dynamic_by_id(_id: u16) -> bool {
> +    unsafe { (trace_events_enabled_count != 0) && (_id != 0) }
> +}
> +
> +extern "C" {
> +    static mut trace_events_enabled_count: u32;
> +}
> +extern "C" {
> +    static mut _TRACE_TEST_BLAH_DSTATE: u16;
> +    static mut _TRACE_TEST_WIBBLE_DSTATE: u16;
> +}
> +const _TRACE_TEST_BLAH_ENABLED: bool = true;
> +const _TRACE_TEST_WIBBLE_ENABLED: bool = true;

Does rust have any policy reserving use of leading underscore in
identifiers  ?


> +
> +#[inline(always)]
> +#[allow(dead_code)]
> +pub fn trace_test_blah(_context: *mut (), _filename: &std::ffi::CStr)
> +{
> +    if trace_event_get_state_dynamic_by_id(unsafe { _TRACE_TEST_BLAH_DSTATE}) {
> +        extern "C" { fn _simple_trace_test_blah(_context: *mut (), _filename: *const std::ffi::c_char); }
> +        unsafe { _simple_trace_test_blah(_context, _filename.as_ptr()); }
> +    }
> +}
> +
> +#[inline(always)]
> +#[allow(dead_code)]
> +pub fn trace_test_wibble(_context: *mut (), _value: std::ffi::c_int)
> +{
> +    if trace_event_get_state_dynamic_by_id(unsafe { _TRACE_TEST_WIBBLE_DSTATE}) {
> +        extern "C" { fn _simple_trace_test_wibble(_context: *mut (), _value: std::ffi::c_int); }
> +        unsafe { _simple_trace_test_wibble(_context, _value); }
> +    }
> +}

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Re: [PATCH 10/14] tracetool/simple: add Rust support
Posted by Paolo Bonzini 1 month, 3 weeks ago
On 8/26/25 13:53, Daniel P. Berrangé wrote:
> On Fri, Aug 22, 2025 at 02:26:51PM +0200, Paolo Bonzini wrote:
>> From: Tanish Desai <tanishdesai37@gmail.com>
>>
>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>> ---
>>   scripts/tracetool/__init__.py       |  2 +-
>>   scripts/tracetool/backend/simple.py |  7 +++++
>>   tests/tracetool/simple.rs           | 41 +++++++++++++++++++++++++++++
>>   tests/tracetool/tracetool-test.py   |  2 ++
>>   4 files changed, 51 insertions(+), 1 deletion(-)
>>   create mode 100644 tests/tracetool/simple.rs
>>
>> diff --git a/scripts/tracetool/__init__.py b/scripts/tracetool/__init__.py
>> index 0b8ec707332..7542757799e 100644
>> --- a/scripts/tracetool/__init__.py
>> +++ b/scripts/tracetool/__init__.py
>> @@ -336,7 +336,7 @@ def rust_call_extern(self):
>>           def rust_cast(name, type_):
>>               if type_ == "const char *":
>>                   return f"_{name}.as_ptr()"
>> -            return "_{name}"
>> +            return f"_{name}"
>>   
>>           return ", ".join((rust_cast(name, type_) for type_, name in self._args))
>>
> 
> This should be squashed into the earlier patch that introduced the mistake
> 
>> diff --git a/tests/tracetool/simple.rs b/tests/tracetool/simple.rs
>> new file mode 100644
>> index 00000000000..895096088dc
>> --- /dev/null
>> +++ b/tests/tracetool/simple.rs
>> @@ -0,0 +1,41 @@
>> +// This file is autogenerated by tracetool, do not edit.
>> +
>> +#[allow(unused_imports)]
>> +use std::ffi::c_char;
>> +#[allow(unused_imports)]
>> +use qemu_api::bindings;
>> +
>> +#[inline(always)]
>> +fn trace_event_get_state_dynamic_by_id(_id: u16) -> bool {
>> +    unsafe { (trace_events_enabled_count != 0) && (_id != 0) }
>> +}
>> +
>> +extern "C" {
>> +    static mut trace_events_enabled_count: u32;
>> +}
>> +extern "C" {
>> +    static mut _TRACE_TEST_BLAH_DSTATE: u16;
>> +    static mut _TRACE_TEST_WIBBLE_DSTATE: u16;
>> +}
>> +const _TRACE_TEST_BLAH_ENABLED: bool = true;
>> +const _TRACE_TEST_WIBBLE_ENABLED: bool = true;
> 
> Does rust have any policy reserving use of leading underscore in
> identifiers  ?
The leading underscore suppresses warnings on unused symbols.  Which 
indeed these two are, so I'll drop them.

Paolo