|
1 | 1 | // Zinc, the bare metal stack for rust.
|
2 |
| -// Copyright 2014 Vladimir "farcaller" Pouzanov <[email protected]> |
| 2 | +// Copyright 2014 Ben Gamari <[email protected]> |
3 | 3 | //
|
4 | 4 | // Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | // you may not use this file except in compliance with the License.
|
|
13 | 13 | // See the License for the specific language governing permissions and
|
14 | 14 | // limitations under the License.
|
15 | 15 |
|
16 |
| -//! debug::port provides interface to output structured data over serial port. |
17 |
| -
|
18 |
| -use core::mem::{size_of, transmute}; |
19 |
| -use core::intrinsics::abort; |
| 16 | +//! Tracing for debugging. |
| 17 | +// |
| 18 | +// Use `set_backend` |
20 | 19 |
|
21 |
| -use drivers::chario::CharIO; |
22 |
| -use hal::uart::{UART, UARTConf}; |
| 20 | +pub use os::debug::internal::{set_backend, print, Token}; |
23 | 21 |
|
24 |
| -extern { |
25 |
| - fn memcpy(dest: *mut u8, src: *u8, n: int); |
26 |
| -} |
| 22 | +#[cfg(debug)] |
| 23 | +mod internal { |
| 24 | + use core::option::{Some, None, Option}; |
| 25 | + use core::ops::Drop; |
| 26 | + use core::mem::transmute; |
| 27 | + use drivers::chario::CharIO; |
27 | 28 |
|
28 |
| -// TODO(farcaller): fix when size_of is avaliable in statics. |
29 |
| -static SizeOfUART: uint = 64; |
| 29 | + static mut backend: Option<*const CharIO + 'static> = None; |
30 | 30 |
|
31 |
| -static mut uart_buf: [u8, ..SizeOfUART] = [0, ..SizeOfUART]; |
| 31 | + /// A token to ensure the life of the reference to the debugging output backend |
| 32 | + /// doesn't outlive the backend itself. |
| 33 | + #[must_use] |
| 34 | + pub struct Token<'a> { |
| 35 | + #[allow(dead_code)] |
| 36 | + hello: () |
| 37 | + } |
32 | 38 |
|
33 |
| -/// Initializes debug port with uart configuration. |
34 |
| -/// |
35 |
| -/// This function must be called before any debug port use. |
36 |
| -pub fn setup(conf: &UARTConf) { |
37 |
| - if SizeOfUART < size_of::<UART>() { |
38 |
| - unsafe { abort() }; |
| 39 | + #[unsafe_destructor] |
| 40 | + impl<'a> Drop for Token<'a> { |
| 41 | + fn drop(&mut self) { |
| 42 | + unsafe { |
| 43 | + backend = None; |
| 44 | + } |
| 45 | + } |
39 | 46 | }
|
40 | 47 |
|
41 |
| - let uart: UART = conf.setup(); |
| 48 | + /// Set the debugging output backend (mock) |
| 49 | + pub fn set_backend<'a>(b: &'a CharIO) -> Token<'a> { |
| 50 | + unsafe { |
| 51 | + backend = Some(transmute(b)); |
| 52 | + Token { hello: () } |
| 53 | + } |
| 54 | + } |
42 | 55 |
|
43 |
| - unsafe { |
44 |
| - let src_ptr: *u8 = transmute(&uart); |
45 |
| - let dst_ptr: *mut u8 = transmute(&uart_buf); |
46 |
| - memcpy(dst_ptr, src_ptr, size_of::<UART>() as int); |
| 56 | + /// Print debugging output backend (mock) |
| 57 | + pub fn print(s: &str) { |
| 58 | + unsafe { |
| 59 | + match backend { |
| 60 | + Some(b) => (*b).puts(s), |
| 61 | + None => {}, |
| 62 | + } |
| 63 | + } |
47 | 64 | }
|
48 | 65 | }
|
49 | 66 |
|
50 |
| -/// Returns a CharIO corresponding to current debug port. |
51 |
| -pub fn io() -> &CharIO { |
52 |
| - let uart: &UART = unsafe { transmute(&uart_buf) }; |
| 67 | +#[cfg(not(debug))] |
| 68 | +mod internal { |
| 69 | + use drivers::chario::CharIO; |
| 70 | + |
| 71 | + /// Set the debugging output backend (mock) |
| 72 | + pub fn set_backend(_: &CharIO) { } |
53 | 73 |
|
54 |
| - uart as &CharIO |
| 74 | + /// Print debugging output backend (mock) |
| 75 | + pub fn print(_: &str) { } |
| 76 | + |
| 77 | + /// A token to ensure the life of the reference to the debugging output backend |
| 78 | + /// doesn't outlive the backend itself. |
| 79 | + #[must_use] |
| 80 | + pub struct Token<'a> { |
| 81 | + #[allow(dead_code)] |
| 82 | + hello: () |
| 83 | + } |
55 | 84 | }
|
0 commit comments