1
- #![ cfg( feature = "fancy" ) ]
2
- use std:: fmt:: Write ;
3
-
4
1
use backtrace:: Backtrace ;
5
2
use thiserror:: Error ;
6
3
@@ -29,20 +26,22 @@ pub fn set_panic_hook() {
29
26
}
30
27
31
28
#[ derive( Debug , Error , Diagnostic ) ]
32
- #[ error( "{0}{}" , self . maybe_collect_backtrace ( ) ) ]
29
+ #[ error( "{0}{}" , Panic :: backtrace ( ) ) ]
33
30
#[ diagnostic( help( "set the `RUST_BACKTRACE=1` environment variable to display a backtrace." ) ) ]
34
31
struct Panic ( String ) ;
35
32
36
33
impl Panic {
37
- fn maybe_collect_backtrace ( & self ) -> String {
34
+ fn backtrace ( ) -> String {
35
+ use std:: fmt:: Write ;
38
36
if let Ok ( var) = std:: env:: var ( "RUST_BACKTRACE" ) {
39
37
if !var. is_empty ( ) && var != "0" {
40
- // This is all taken from human-panic: https://github.com/rust-cli/human-panic/blob/master/src/report.rs#L55-L107
41
38
const HEX_WIDTH : usize = std:: mem:: size_of :: < usize > ( ) + 2 ;
42
- //Padding for next lines after frame's address
39
+ // Padding for next lines after frame's address
43
40
const NEXT_SYMBOL_PADDING : usize = HEX_WIDTH + 6 ;
44
41
let mut backtrace = String :: new ( ) ;
45
- for ( idx, frame) in Backtrace :: new ( ) . frames ( ) . iter ( ) . skip ( 26 ) . enumerate ( ) {
42
+ let trace = Backtrace :: new ( ) ;
43
+ let frames = backtrace_ext:: short_frames_strict ( & trace) . enumerate ( ) ;
44
+ for ( idx, ( frame, sub_frames) ) in frames {
46
45
let ip = frame. ip ( ) ;
47
46
let _ = write ! ( backtrace, "\n {:4}: {:2$?}" , idx, ip, HEX_WIDTH ) ;
48
47
@@ -52,10 +51,10 @@ impl Panic {
52
51
continue ;
53
52
}
54
53
55
- for ( idx, symbol) in symbols. iter ( ) . enumerate ( ) {
56
- //Print symbols from this address,
57
- //if there are several addresses
58
- //we need to put it on next line
54
+ for ( idx, symbol) in symbols[ sub_frames ] . iter ( ) . enumerate ( ) {
55
+ // Print symbols from this address,
56
+ // if there are several addresses
57
+ // we need to put it on next line
59
58
if idx != 0 {
60
59
let _ = write ! ( backtrace, "\n {:1$}" , "" , NEXT_SYMBOL_PADDING ) ;
61
60
}
@@ -66,7 +65,7 @@ impl Panic {
66
65
let _ = write ! ( backtrace, " - <unknown>" ) ;
67
66
}
68
67
69
- //See if there is debug information with file name and line
68
+ // See if there is debug information with file name and line
70
69
if let ( Some ( file) , Some ( line) ) = ( symbol. filename ( ) , symbol. lineno ( ) ) {
71
70
let _ = write ! (
72
71
backtrace,
0 commit comments