Skip to content
This repository was archived by the owner on Jul 6, 2019. It is now read-only.

Commit fe78ad6

Browse files
committed
Add debug tracing module
1 parent 7ade70a commit fe78ad6

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

src/zinc/os/debug.rs

+57-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Zinc, the bare metal stack for rust.
2-
// Copyright 2014 Vladimir "farcaller" Pouzanov <[email protected]>
2+
// Copyright 2014 Ben Gamari <[email protected]>
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -13,43 +13,72 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

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`
2019

21-
use drivers::chario::CharIO;
22-
use hal::uart::{UART, UARTConf};
20+
pub use os::debug::internal::{set_backend, print, Token};
2321

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;
2728

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;
3030

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+
}
3238

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+
}
3946
}
4047

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+
}
4255

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+
}
4764
}
4865
}
4966

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) { }
5373

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+
}
5584
}

src/zinc/os/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ pub mod syscall;
2525
#[cfg(cfg_multitasking)] pub mod task;
2626
pub mod mutex;
2727
pub mod cond_var;
28+
pub mod debug;

0 commit comments

Comments
 (0)