]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
Add rust bindings to Samba utils debug
authorDavid Mulder <dmulder@samba.org>
Tue, 30 Jul 2024 15:17:34 +0000 (09:17 -0600)
committerDavid Mulder <dmulder@samba.org>
Wed, 23 Oct 2024 14:21:33 +0000 (14:21 +0000)
Signed-off-by: David Mulder <dmulder@samba.org>
Reviewed-by: Alexander Bokovoy <ab@samba.org>
himmelblaud/.gitignore [new file with mode: 0644]
himmelblaud/Cargo.toml [new file with mode: 0644]
himmelblaud/chelps/Cargo.toml [new file with mode: 0644]
himmelblaud/chelps/src/lib.rs [new file with mode: 0644]
himmelblaud/dbg/Cargo.toml [new file with mode: 0644]
himmelblaud/dbg/build.rs [new file with mode: 0644]
himmelblaud/dbg/src/lib.rs [new file with mode: 0644]
himmelblaud/src/main.rs [new file with mode: 0644]

diff --git a/himmelblaud/.gitignore b/himmelblaud/.gitignore
new file mode 100644 (file)
index 0000000..eadb94c
--- /dev/null
@@ -0,0 +1,14 @@
+# ---> Rust
+# Generated by Cargo
+# will have compiled files and executables
+debug/
+target/
+
+# These are backup files generated by rustfmt
+**/*.rs.bk
+
+# MSVC Windows builds of rustc generate these, which store debugging information
+*.pdb
+
+vendor/
+tags
diff --git a/himmelblaud/Cargo.toml b/himmelblaud/Cargo.toml
new file mode 100644 (file)
index 0000000..044a4f0
--- /dev/null
@@ -0,0 +1,24 @@
+[workspace.package]
+edition = "2021"
+license = "GPL-3.0-or-later"
+homepage = "https://www.samba.org/"
+version = "4.21.0"
+
+[package]
+name = "himmelblaud"
+edition.workspace = true
+license.workspace = true
+homepage.workspace = true
+version.workspace = true
+
+[dependencies]
+dbg = { workspace = true }
+
+[workspace]
+members = [
+  "chelps", "dbg"
+]
+
+[workspace.dependencies]
+dbg = { path = "dbg" }
+chelps = { path = "chelps" }
diff --git a/himmelblaud/chelps/Cargo.toml b/himmelblaud/chelps/Cargo.toml
new file mode 100644 (file)
index 0000000..80dfe1c
--- /dev/null
@@ -0,0 +1,8 @@
+[package]
+name = "chelps"
+edition.workspace = true
+license.workspace = true
+homepage.workspace = true
+version.workspace = true
+
+[dependencies]
diff --git a/himmelblaud/chelps/src/lib.rs b/himmelblaud/chelps/src/lib.rs
new file mode 100644 (file)
index 0000000..5df398d
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   C conversion helper functions and macros
+
+   Copyright (C) David Mulder 2024
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+use std::ffi::{CStr, CString};
+use std::os::raw::c_char;
+use std::ptr;
+
+pub unsafe fn wrap_c_char(input: *const c_char) -> Option<String> {
+    if input.is_null() {
+        return None;
+    }
+
+    let c_str = unsafe { CStr::from_ptr(input) };
+    match c_str.to_str() {
+        Ok(output) => Some(output.to_string()),
+        Err(_) => None,
+    }
+}
+
+pub fn wrap_string(input: &str) -> *mut c_char {
+    match CString::new(input.to_string()) {
+        Ok(msg) => msg.into_raw(),
+        Err(_) => ptr::null_mut(),
+    }
+}
+
+pub unsafe fn string_free(input: *mut c_char) {
+    if !input.is_null() {
+        unsafe {
+            let _ = CString::from_raw(input);
+        }
+    }
+}
diff --git a/himmelblaud/dbg/Cargo.toml b/himmelblaud/dbg/Cargo.toml
new file mode 100644 (file)
index 0000000..7d55838
--- /dev/null
@@ -0,0 +1,12 @@
+[package]
+name = "dbg"
+edition.workspace = true
+license.workspace = true
+homepage.workspace = true
+version.workspace = true
+
+[build-dependencies]
+bindgen = "0.69.4"
+
+[dependencies]
+chelps.workspace = true
diff --git a/himmelblaud/dbg/build.rs b/himmelblaud/dbg/build.rs
new file mode 100644 (file)
index 0000000..f71152f
--- /dev/null
@@ -0,0 +1,24 @@
+use std::env;
+use std::path::PathBuf;
+
+fn main() {
+    let bindings = bindgen::Builder::default()
+        .header("../../lib/util/debug.h")
+        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
+        .generate()
+        .expect("Unable to generate bindings");
+
+    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
+    bindings
+        .write_to_file(out_path.join("bindings.rs"))
+        .expect("Couldn't write bindings!");
+
+    let mut src_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
+    src_dir.push("../../bin/default/lib/util");
+    println!(
+        "cargo:rustc-link-search=native={}",
+        src_dir.to_str().unwrap()
+    );
+    println!("cargo:rustc-link-lib=samba-debug-private-samba");
+    println!("cargo:rustc-link-lib=samba-util");
+}
diff --git a/himmelblaud/dbg/src/lib.rs b/himmelblaud/dbg/src/lib.rs
new file mode 100644 (file)
index 0000000..5369742
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   Parameter loading functions
+
+   Copyright (C) David Mulder 2024
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+pub mod ffi {
+    #![allow(non_upper_case_globals)]
+    #![allow(non_camel_case_types)]
+    #![allow(non_snake_case)]
+    #![allow(dead_code)]
+    #![allow(clippy::upper_case_acronyms)]
+    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
+}
+
+pub const MAX_DEBUG_LEVEL: u32 = ffi::MAX_DEBUG_LEVEL;
+pub const DBGLVL_ERR: u32 = ffi::DBGLVL_ERR;
+pub const DBGLVL_WARNING: u32 = ffi::DBGLVL_WARNING;
+pub const DBGLVL_NOTICE: u32 = ffi::DBGLVL_NOTICE;
+pub const DBGLVL_INFO: u32 = ffi::DBGLVL_INFO;
+pub const DBGLVL_DEBUG: u32 = ffi::DBGLVL_DEBUG;
+
+pub const DEBUG_DEFAULT_STDERR: ffi::debug_logtype =
+    ffi::debug_logtype_DEBUG_DEFAULT_STDERR;
+pub const DEBUG_DEFAULT_STDOUT: ffi::debug_logtype =
+    ffi::debug_logtype_DEBUG_DEFAULT_STDOUT;
+pub const DEBUG_FILE: ffi::debug_logtype = ffi::debug_logtype_DEBUG_FILE;
+pub const DEBUG_STDOUT: ffi::debug_logtype = ffi::debug_logtype_DEBUG_STDOUT;
+pub const DEBUG_STDERR: ffi::debug_logtype = ffi::debug_logtype_DEBUG_STDERR;
+pub const DEBUG_CALLBACK: ffi::debug_logtype =
+    ffi::debug_logtype_DEBUG_CALLBACK;
+
+pub fn debug_set_logfile(name: &str) {
+    let name_cstr = chelps::wrap_string(name);
+    unsafe {
+        ffi::debug_set_logfile(name_cstr);
+        chelps::string_free(name_cstr);
+    }
+}
+
+pub fn setup_logging(prog_name: &str, new_logtype: ffi::debug_logtype) {
+    let prog_name_cstr = chelps::wrap_string(prog_name);
+    unsafe {
+        ffi::setup_logging(prog_name_cstr, new_logtype);
+        chelps::string_free(prog_name_cstr);
+    }
+}
+
+#[macro_export]
+macro_rules! debuglevel_set {
+    ($level:expr) => {{
+        unsafe {
+            dbg::ffi::debuglevel_set_class(
+                dbg::ffi::DBGC_ALL as usize,
+                $level as i32,
+            )
+        }
+    }};
+}
+
+#[macro_export]
+macro_rules! function {
+    () => {{
+        fn f() {}
+        fn type_name_of<T>(_: T) -> &'static str {
+            std::any::type_name::<T>()
+        }
+        let name = type_name_of(f);
+
+        match &name[..name.len() - 3].rfind(':') {
+            Some(pos) => &name[pos + 1..name.len() - 3],
+            None => &name[..name.len() - 3],
+        }
+    }};
+}
+
+#[macro_export]
+macro_rules! DBG_PREFIX {
+    ($level:expr $(, $arg:expr)* $(,)?) => {{
+        if $level <= dbg::ffi::MAX_DEBUG_LEVEL {
+            let location = format!("{}:{}", file!(), line!());
+            let location_cstr = chelps::wrap_string(&location);
+            let function = dbg::function!();
+            let function_msg = format!("{}: ", function);
+            let function_cstr = chelps::wrap_string(function);
+            let function_msg_cstr = chelps::wrap_string(&function_msg);
+            let msg = format!($($arg),*);
+            let msg_cstr = chelps::wrap_string(&msg);
+            unsafe {
+                let _ = dbg::ffi::debuglevel_get_class(dbg::ffi::DBGC_CLASS as usize) >= ($level as i32)
+                    && dbg::ffi::dbghdrclass($level as i32,
+                           dbg::ffi::DBGC_CLASS as i32,
+                           location_cstr,
+                           function_cstr)
+                    && dbg::ffi::dbgtext(function_msg_cstr)
+                    && dbg::ffi::dbgtext(msg_cstr);
+                chelps::string_free(location_cstr);
+                chelps::string_free(function_cstr);
+                chelps::string_free(function_msg_cstr);
+                chelps::string_free(msg_cstr);
+            }
+        }
+    }}
+}
+
+#[macro_export]
+macro_rules! DBG_ERR {
+    ($msg:expr $(, $arg:expr)* $(,)?) => {{
+        dbg::DBG_PREFIX!(dbg::ffi::DBGLVL_ERR, $msg, $($arg),*)
+    }}
+}
+
+#[macro_export]
+macro_rules! DBG_WARNING {
+    ($msg:expr $(, $arg:expr)* $(,)?) => {{
+        dbg::DBG_PREFIX!(dbg::ffi::DBGLVL_WARNING, $msg, $($arg),*)
+    }}
+}
+
+#[macro_export]
+macro_rules! DBG_NOTICE {
+    ($msg:expr $(, $arg:expr)* $(,)?) => {{
+        dbg::DBG_PREFIX!(dbg::ffi::DBGLVL_NOTICE, $msg, $($arg),*)
+    }}
+}
+
+#[macro_export]
+macro_rules! DBG_INFO {
+    ($msg:expr $(, $arg:expr)* $(,)?) => {{
+        dbg::DBG_PREFIX!(dbg::ffi::DBGLVL_INFO, $msg, $($arg),*)
+    }}
+}
+
+#[macro_export]
+macro_rules! DBG_DEBUG {
+    ($msg:expr $(, $arg:expr)* $(,)?) => {{
+        dbg::DBG_PREFIX!(dbg::ffi::DBGLVL_DEBUG, $msg, $($arg),*)
+    }}
+}
diff --git a/himmelblaud/src/main.rs b/himmelblaud/src/main.rs
new file mode 100644 (file)
index 0000000..f328e4d
--- /dev/null
@@ -0,0 +1 @@
+fn main() {}