From: David Mulder Date: Tue, 30 Jul 2024 15:17:34 +0000 (-0600) Subject: Add rust bindings to Samba utils debug X-Git-Tag: tdb-1.4.13~927 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f37e311af1b3300309cb4323112abffed4627203;p=thirdparty%2Fsamba.git Add rust bindings to Samba utils debug Signed-off-by: David Mulder Reviewed-by: Alexander Bokovoy --- diff --git a/himmelblaud/.gitignore b/himmelblaud/.gitignore new file mode 100644 index 00000000000..eadb94ca271 --- /dev/null +++ b/himmelblaud/.gitignore @@ -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 index 00000000000..044a4f0ae3f --- /dev/null +++ b/himmelblaud/Cargo.toml @@ -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 index 00000000000..80dfe1c8f9b --- /dev/null +++ b/himmelblaud/chelps/Cargo.toml @@ -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 index 00000000000..5df398df2e7 --- /dev/null +++ b/himmelblaud/chelps/src/lib.rs @@ -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 . +*/ + +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 { + 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 index 00000000000..7d558383097 --- /dev/null +++ b/himmelblaud/dbg/Cargo.toml @@ -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 index 00000000000..f71152f104e --- /dev/null +++ b/himmelblaud/dbg/build.rs @@ -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 index 00000000000..5369742c976 --- /dev/null +++ b/himmelblaud/dbg/src/lib.rs @@ -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 . +*/ + +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) -> &'static str { + std::any::type_name::() + } + 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 index 00000000000..f328e4d9d04 --- /dev/null +++ b/himmelblaud/src/main.rs @@ -0,0 +1 @@ +fn main() {}