1bee16889SNiklas Mohrin // SPDX-License-Identifier: Apache-2.0 OR MIT 2bee16889SNiklas Mohrin 3bee16889SNiklas Mohrin //! The contents of this file come from the Rust standard library, hosted in 4bee16889SNiklas Mohrin //! the <https://github.com/rust-lang/rust> repository, licensed under 5bee16889SNiklas Mohrin //! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details, 6bee16889SNiklas Mohrin //! see <https://github.com/rust-lang/rust/blob/master/COPYRIGHT>. 7bee16889SNiklas Mohrin 8bee16889SNiklas Mohrin /// [`std::dbg`], but using [`pr_info`] instead of [`eprintln`]. 9bee16889SNiklas Mohrin /// 10bee16889SNiklas Mohrin /// Prints and returns the value of a given expression for quick and dirty 11bee16889SNiklas Mohrin /// debugging. 12bee16889SNiklas Mohrin /// 13bee16889SNiklas Mohrin /// An example: 14bee16889SNiklas Mohrin /// 15bee16889SNiklas Mohrin /// ```rust 16bee16889SNiklas Mohrin /// let a = 2; 17bee16889SNiklas Mohrin /// # #[allow(clippy::dbg_macro)] 18bee16889SNiklas Mohrin /// let b = dbg!(a * 2) + 1; 19bee16889SNiklas Mohrin /// // ^-- prints: [src/main.rs:2] a * 2 = 4 20bee16889SNiklas Mohrin /// assert_eq!(b, 5); 21bee16889SNiklas Mohrin /// ``` 22bee16889SNiklas Mohrin /// 23bee16889SNiklas Mohrin /// The macro works by using the `Debug` implementation of the type of 24bee16889SNiklas Mohrin /// the given expression to print the value with [`printk`] along with the 25bee16889SNiklas Mohrin /// source location of the macro invocation as well as the source code 26bee16889SNiklas Mohrin /// of the expression. 27bee16889SNiklas Mohrin /// 28bee16889SNiklas Mohrin /// Invoking the macro on an expression moves and takes ownership of it 29bee16889SNiklas Mohrin /// before returning the evaluated expression unchanged. If the type 30bee16889SNiklas Mohrin /// of the expression does not implement `Copy` and you don't want 31bee16889SNiklas Mohrin /// to give up ownership, you can instead borrow with `dbg!(&expr)` 32bee16889SNiklas Mohrin /// for some expression `expr`. 33bee16889SNiklas Mohrin /// 34bee16889SNiklas Mohrin /// The `dbg!` macro works exactly the same in release builds. 35bee16889SNiklas Mohrin /// This is useful when debugging issues that only occur in release 36bee16889SNiklas Mohrin /// builds or when debugging in release mode is significantly faster. 37bee16889SNiklas Mohrin /// 38bee16889SNiklas Mohrin /// Note that the macro is intended as a temporary debugging tool to be 39bee16889SNiklas Mohrin /// used during development. Therefore, avoid committing `dbg!` macro 40bee16889SNiklas Mohrin /// invocations into the kernel tree. 41bee16889SNiklas Mohrin /// 42bee16889SNiklas Mohrin /// For debug output that is intended to be kept in the kernel tree, 43bee16889SNiklas Mohrin /// use [`pr_debug`] and similar facilities instead. 44bee16889SNiklas Mohrin /// 45bee16889SNiklas Mohrin /// # Stability 46bee16889SNiklas Mohrin /// 47bee16889SNiklas Mohrin /// The exact output printed by this macro should not be relied upon 48bee16889SNiklas Mohrin /// and is subject to future changes. 49bee16889SNiklas Mohrin /// 50bee16889SNiklas Mohrin /// # Further examples 51bee16889SNiklas Mohrin /// 52bee16889SNiklas Mohrin /// With a method call: 53bee16889SNiklas Mohrin /// 54bee16889SNiklas Mohrin /// ```rust 55bee16889SNiklas Mohrin /// # #[allow(clippy::dbg_macro)] 56bee16889SNiklas Mohrin /// fn foo(n: usize) { 57bee16889SNiklas Mohrin /// if dbg!(n.checked_sub(4)).is_some() { 58bee16889SNiklas Mohrin /// // ... 59bee16889SNiklas Mohrin /// } 60bee16889SNiklas Mohrin /// } 61bee16889SNiklas Mohrin /// 62bee16889SNiklas Mohrin /// foo(3) 63bee16889SNiklas Mohrin /// ``` 64bee16889SNiklas Mohrin /// 65bee16889SNiklas Mohrin /// This prints to the kernel log: 66bee16889SNiklas Mohrin /// 67bee16889SNiklas Mohrin /// ```text,ignore 68bee16889SNiklas Mohrin /// [src/main.rs:4] n.checked_sub(4) = None 69bee16889SNiklas Mohrin /// ``` 70bee16889SNiklas Mohrin /// 71bee16889SNiklas Mohrin /// Naive factorial implementation: 72bee16889SNiklas Mohrin /// 73bee16889SNiklas Mohrin /// ```rust 74bee16889SNiklas Mohrin /// # #[allow(clippy::dbg_macro)] 75bee16889SNiklas Mohrin /// # { 76bee16889SNiklas Mohrin /// fn factorial(n: u32) -> u32 { 77bee16889SNiklas Mohrin /// if dbg!(n <= 1) { 78bee16889SNiklas Mohrin /// dbg!(1) 79bee16889SNiklas Mohrin /// } else { 80bee16889SNiklas Mohrin /// dbg!(n * factorial(n - 1)) 81bee16889SNiklas Mohrin /// } 82bee16889SNiklas Mohrin /// } 83bee16889SNiklas Mohrin /// 84bee16889SNiklas Mohrin /// dbg!(factorial(4)); 85bee16889SNiklas Mohrin /// # } 86bee16889SNiklas Mohrin /// ``` 87bee16889SNiklas Mohrin /// 88bee16889SNiklas Mohrin /// This prints to the kernel log: 89bee16889SNiklas Mohrin /// 90bee16889SNiklas Mohrin /// ```text,ignore 91bee16889SNiklas Mohrin /// [src/main.rs:3] n <= 1 = false 92bee16889SNiklas Mohrin /// [src/main.rs:3] n <= 1 = false 93bee16889SNiklas Mohrin /// [src/main.rs:3] n <= 1 = false 94bee16889SNiklas Mohrin /// [src/main.rs:3] n <= 1 = true 95bee16889SNiklas Mohrin /// [src/main.rs:4] 1 = 1 96bee16889SNiklas Mohrin /// [src/main.rs:5] n * factorial(n - 1) = 2 97bee16889SNiklas Mohrin /// [src/main.rs:5] n * factorial(n - 1) = 6 98bee16889SNiklas Mohrin /// [src/main.rs:5] n * factorial(n - 1) = 24 99bee16889SNiklas Mohrin /// [src/main.rs:11] factorial(4) = 24 100bee16889SNiklas Mohrin /// ``` 101bee16889SNiklas Mohrin /// 102bee16889SNiklas Mohrin /// The `dbg!(..)` macro moves the input: 103bee16889SNiklas Mohrin /// 104bee16889SNiklas Mohrin /// ```ignore 105bee16889SNiklas Mohrin /// /// A wrapper around `usize` which importantly is not Copyable. 106bee16889SNiklas Mohrin /// #[derive(Debug)] 107bee16889SNiklas Mohrin /// struct NoCopy(usize); 108bee16889SNiklas Mohrin /// 109bee16889SNiklas Mohrin /// let a = NoCopy(42); 110bee16889SNiklas Mohrin /// let _ = dbg!(a); // <-- `a` is moved here. 111bee16889SNiklas Mohrin /// let _ = dbg!(a); // <-- `a` is moved again; error! 112bee16889SNiklas Mohrin /// ``` 113bee16889SNiklas Mohrin /// 114bee16889SNiklas Mohrin /// You can also use `dbg!()` without a value to just print the 115bee16889SNiklas Mohrin /// file and line whenever it's reached. 116bee16889SNiklas Mohrin /// 117bee16889SNiklas Mohrin /// Finally, if you want to `dbg!(..)` multiple values, it will treat them as 118bee16889SNiklas Mohrin /// a tuple (and return it, too): 119bee16889SNiklas Mohrin /// 120bee16889SNiklas Mohrin /// ``` 121bee16889SNiklas Mohrin /// # #[allow(clippy::dbg_macro)] 122bee16889SNiklas Mohrin /// assert_eq!(dbg!(1usize, 2u32), (1, 2)); 123bee16889SNiklas Mohrin /// ``` 124bee16889SNiklas Mohrin /// 125bee16889SNiklas Mohrin /// However, a single argument with a trailing comma will still not be treated 126bee16889SNiklas Mohrin /// as a tuple, following the convention of ignoring trailing commas in macro 127bee16889SNiklas Mohrin /// invocations. You can use a 1-tuple directly if you need one: 128bee16889SNiklas Mohrin /// 129bee16889SNiklas Mohrin /// ``` 130bee16889SNiklas Mohrin /// # #[allow(clippy::dbg_macro)] 131bee16889SNiklas Mohrin /// # { 132bee16889SNiklas Mohrin /// assert_eq!(1, dbg!(1u32,)); // trailing comma ignored 133bee16889SNiklas Mohrin /// assert_eq!((1,), dbg!((1u32,))); // 1-tuple 134bee16889SNiklas Mohrin /// # } 135bee16889SNiklas Mohrin /// ``` 136bee16889SNiklas Mohrin /// 137bee16889SNiklas Mohrin /// [`std::dbg`]: https://doc.rust-lang.org/std/macro.dbg.html 138bee16889SNiklas Mohrin /// [`eprintln`]: https://doc.rust-lang.org/std/macro.eprintln.html 139bee16889SNiklas Mohrin /// [`printk`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html 140*3ed03f4dSMiguel Ojeda /// [`pr_info`]: crate::pr_info! 141*3ed03f4dSMiguel Ojeda /// [`pr_debug`]: crate::pr_debug! 142bee16889SNiklas Mohrin #[macro_export] 143bee16889SNiklas Mohrin macro_rules! dbg { 144bee16889SNiklas Mohrin // NOTE: We cannot use `concat!` to make a static string as a format argument 145bee16889SNiklas Mohrin // of `pr_info!` because `file!` could contain a `{` or 146bee16889SNiklas Mohrin // `$val` expression could be a block (`{ .. }`), in which case the `pr_info!` 147bee16889SNiklas Mohrin // will be malformed. 148bee16889SNiklas Mohrin () => { 149bee16889SNiklas Mohrin $crate::pr_info!("[{}:{}]\n", ::core::file!(), ::core::line!()) 150bee16889SNiklas Mohrin }; 151bee16889SNiklas Mohrin ($val:expr $(,)?) => { 152bee16889SNiklas Mohrin // Use of `match` here is intentional because it affects the lifetimes 153bee16889SNiklas Mohrin // of temporaries - https://stackoverflow.com/a/48732525/1063961 154bee16889SNiklas Mohrin match $val { 155bee16889SNiklas Mohrin tmp => { 156bee16889SNiklas Mohrin $crate::pr_info!("[{}:{}] {} = {:#?}\n", 157bee16889SNiklas Mohrin ::core::file!(), ::core::line!(), ::core::stringify!($val), &tmp); 158bee16889SNiklas Mohrin tmp 159bee16889SNiklas Mohrin } 160bee16889SNiklas Mohrin } 161bee16889SNiklas Mohrin }; 162bee16889SNiklas Mohrin ($($val:expr),+ $(,)?) => { 163bee16889SNiklas Mohrin ($($crate::dbg!($val)),+,) 164bee16889SNiklas Mohrin }; 165bee16889SNiklas Mohrin } 166