-
Notifications
You must be signed in to change notification settings - Fork 13
/
lib.rs
184 lines (167 loc) · 4.78 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! Efficient, logical 'stack' traces of async functions.
//!
//! To use, annotate your async functions with `#[async_backtrace::framed]`.
//!
//! # Example
//! ```rust
//! #[tokio::main]
//! async fn main() {
//! tokio::select! {
//! _ = tokio::spawn(pending()) => {}
//! _ = foo() => {}
//! };
//! }
//!
//! #[async_backtrace::framed]
//! async fn pending() {
//! std::future::pending::<()>().await
//! }
//!
//! #[async_backtrace::framed]
//! async fn foo() {
//! bar().await;
//! }
//!
//! #[async_backtrace::framed]
//! async fn bar() {
//! futures::join!(fiz(), buz());
//! }
//!
//! #[async_backtrace::framed]
//! async fn fiz() {
//! tokio::task::yield_now().await;
//! }
//!
//! #[async_backtrace::framed]
//! async fn buz() {
//! println!("{}", baz().await);
//! }
//!
//! #[async_backtrace::framed]
//! async fn baz() -> String {
//! async_backtrace::taskdump(true)
//! }
//! ```
//! The above program, when run, prints something like:
//! ```text
//! ╼ taskdump::foo::{{closure}} at backtrace/examples/taskdump.rs:14:1
//! └╼ taskdump::bar::{{closure}} at backtrace/examples/taskdump.rs:19:1
//! ├╼ taskdump::buz::{{closure}} at backtrace/examples/taskdump.rs:29:1
//! │ └╼ taskdump::baz::{{closure}} at backtrace/examples/taskdump.rs:34:1
//! └╼ taskdump::fiz::{{closure}} at backtrace/examples/taskdump.rs:24:1
//! ╼ taskdump::pending::{{closure}} at backtrace/examples/taskdump.rs:9:1
//! ```
pub(crate) mod frame;
pub(crate) mod framed;
pub(crate) mod linked_list;
pub(crate) mod location;
pub(crate) mod tasks;
pub(crate) use frame::Frame;
pub(crate) use framed::Framed;
pub use location::Location;
pub(crate) use tasks::tasks;
/// Include the annotated async function in backtraces and taskdumps.
///
/// This, for instance:
/// ```
/// # async fn bar() {}
/// # async fn baz() {}
/// #[async_backtrace::framed]
/// async fn foo() {
/// bar().await;
/// baz().await;
/// }
/// ```
/// ...expands, roughly, to:
/// ```
/// # async fn bar() {}
/// # async fn baz() {}
/// async fn foo() {
/// async_backtrace::location!().frame(async move {
/// bar().await;
/// baz().await;
/// }).await
/// }
pub use async_backtrace_macros::framed;
/// Produces a human-readable tree of task states.
///
/// If `wait_for_running_tasks` is `true`, this routine will display only the
/// top-level location of currently-running tasks and a note that they are
/// "POLLING". Otherwise, this routine will wait for currently-running tasks to
/// become idle.
pub fn taskdump(wait_for_running_tasks: bool) -> String {
tasks()
.map(|task| task.dump_tree(wait_for_running_tasks))
.collect()
}
/// Produces a backtrace starting at the currently-active frame (if any).
///
/// ## Example
/// ```
/// use async_backtrace::{framed, backtrace, Location};
///
/// #[tokio::main]
/// async fn main() {
/// foo().await;
/// }
///
/// #[async_backtrace::framed]
/// async fn foo() {
/// bar().await;
/// }
///
/// #[async_backtrace::framed]
/// async fn bar() {
/// baz().await;
/// }
///
/// #[async_backtrace::framed]
/// async fn baz() {
/// assert_eq!(&async_backtrace::backtrace().unwrap()[..], &[
/// Location { fn_name: "rust_out::baz::{{closure}}", file_name: "src/lib.rs", line_no: 20, col_no: 1 },
/// Location { fn_name: "rust_out::bar::{{closure}}", file_name: "src/lib.rs", line_no: 15, col_no: 1 },
/// Location { fn_name: "rust_out::foo::{{closure}}", file_name: "src/lib.rs", line_no: 10, col_no: 1 },
/// ]);
/// }
/// ```
pub fn backtrace() -> Option<Box<[Location]>> {
Frame::with_active(|maybe_frame| maybe_frame.map(Frame::backtrace_locations))
}
pub(crate) mod sync {
#[cfg(loom)]
pub(crate) use loom::sync::Mutex;
#[cfg(not(loom))]
pub(crate) use std::sync::Mutex;
pub(crate) use std::sync::TryLockError;
}
pub(crate) mod cell {
#[cfg(loom)]
pub(crate) use loom::cell::{Cell, UnsafeCell};
#[cfg(not(loom))]
pub(crate) use std::cell::Cell;
#[cfg(not(loom))]
#[derive(Debug)]
#[repr(transparent)]
pub(crate) struct UnsafeCell<T>(std::cell::UnsafeCell<T>);
#[cfg(not(loom))]
impl<T> UnsafeCell<T> {
pub(crate) fn new(data: T) -> UnsafeCell<T> {
UnsafeCell(std::cell::UnsafeCell::new(data))
}
pub(crate) fn with<R>(&self, f: impl FnOnce(*const T) -> R) -> R {
f(self.0.get())
}
pub(crate) fn with_mut<R>(&self, f: impl FnOnce(*mut T) -> R) -> R {
f(self.0.get())
}
}
}
pub(crate) fn defer<F: FnOnce() -> R, R>(f: F) -> impl Drop {
struct Defer<F: FnOnce() -> R, R>(Option<F>);
impl<F: FnOnce() -> R, R> Drop for Defer<F, R> {
fn drop(&mut self) {
self.0.take().unwrap()();
}
}
Defer(Some(f))
}