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
use wasmer_runtime_core::{
    codegen::{Event, EventSink, FunctionMiddleware, InternalEvent},
    module::ModuleInfo,
    wasmparser::Operator,
};

pub struct BlockTrace {
    func_idx: usize,
    evt_idx: usize,
}

impl BlockTrace {
    pub fn new() -> BlockTrace {
        BlockTrace {
            func_idx: std::usize::MAX,
            evt_idx: 0,
        }
    }
}

impl FunctionMiddleware for BlockTrace {
    type Error = String;
    fn feed_event<'a, 'b: 'a>(
        &mut self,
        op: Event<'a, 'b>,
        _module_info: &ModuleInfo,
        sink: &mut EventSink<'a, 'b>,
        _source_loc: u32,
    ) -> Result<(), Self::Error> {
        match op {
            Event::Internal(InternalEvent::FunctionBegin(_)) => {
                self.func_idx = self.func_idx.wrapping_add(1);
                self.evt_idx = 0;
                let func_idx = self.func_idx;
                let evt_idx = self.evt_idx;
                sink.push(op);
                sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
                    move |info| {
                        eprintln!(
                            "[BlockTrace] ({}, {}) -> enter_func % {:?}",
                            func_idx,
                            evt_idx,
                            info.fault
                                .and_then(|x| unsafe { x.read_stack(Some(1)) })
                                .unwrap()
                                .frames[0]
                        );
                        Ok(())
                    },
                ))))
            }
            Event::Wasm(Operator::Call { .. }) => {
                let func_idx = self.func_idx;
                let evt_idx = self.evt_idx;
                sink.push(op);
                sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
                    move |info| {
                        eprintln!(
                            "[BlockTrace] ({}, {}) -> leave_call % {:?}",
                            func_idx,
                            evt_idx,
                            info.fault
                                .and_then(|x| unsafe { x.read_stack(Some(1)) })
                                .unwrap()
                                .frames[0]
                        );
                        Ok(())
                    },
                ))))
            }
            Event::Wasm(Operator::Block { .. }) => {
                let func_idx = self.func_idx;
                let evt_idx = self.evt_idx;
                sink.push(op);
                sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
                    move |info| {
                        eprintln!(
                            "[BlockTrace] ({}, {}) -> block % {:?}",
                            func_idx,
                            evt_idx,
                            info.fault
                                .and_then(|x| unsafe { x.read_stack(Some(1)) })
                                .unwrap()
                                .frames[0]
                        );
                        Ok(())
                    },
                ))))
            }
            Event::Wasm(Operator::Loop { .. }) => {
                let func_idx = self.func_idx;
                let evt_idx = self.evt_idx;
                sink.push(op);
                sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
                    move |info| {
                        eprintln!(
                            "[BlockTrace] ({}, {}) -> loop % {:?}",
                            func_idx,
                            evt_idx,
                            info.fault
                                .and_then(|x| unsafe { x.read_stack(Some(1)) })
                                .unwrap()
                                .frames[0]
                        );
                        Ok(())
                    },
                ))))
            }
            Event::Wasm(Operator::If { .. }) => {
                let func_idx = self.func_idx;
                let evt_idx = self.evt_idx;
                sink.push(op);
                sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
                    move |info| {
                        eprintln!(
                            "[BlockTrace] ({}, {}) -> if % {:?}",
                            func_idx,
                            evt_idx,
                            info.fault
                                .and_then(|x| unsafe { x.read_stack(Some(1)) })
                                .unwrap()
                                .frames[0]
                        );
                        Ok(())
                    },
                ))))
            }
            Event::Wasm(Operator::Else { .. }) => {
                let func_idx = self.func_idx;
                let evt_idx = self.evt_idx;
                sink.push(op);
                sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
                    move |info| {
                        eprintln!(
                            "[BlockTrace] ({}, {}) -> else % {:?}",
                            func_idx,
                            evt_idx,
                            info.fault
                                .and_then(|x| unsafe { x.read_stack(Some(1)) })
                                .unwrap()
                                .frames[0]
                        );
                        Ok(())
                    },
                ))))
            }
            _ => {
                sink.push(op);
            }
        }
        self.evt_idx += 1;
        Ok(())
    }
}