aboutsummaryrefslogtreecommitdiffhomepage
path: root/zluda_dump/src/trace.rs
blob: f0604678a269d1e35411c818051588c253a0eed3 (plain)
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
use ptx::{ast::PtxError, Token};

use crate::{cuda::CUmodule, log, Settings};
use std::{
    collections::HashMap,
    ffi::{c_void, CStr},
    fs::{self, File},
    io::{self, Read, Write},
    path::PathBuf,
    rc::Rc,
};

// This struct is the heart of CUDA state tracking, it:
// * receives calls from the probes about changes to CUDA state
// * records updates to the state change
// * writes out relevant state change and details to disk and log
pub(crate) struct StateTracker {
    writer: DumpWriter,
    modules: HashMap<CUmodule, Option<ParsedModule>>,
    module_counter: usize,
}

impl StateTracker {
    pub(crate) fn new(settings: &Settings) -> Self {
        StateTracker {
            writer: DumpWriter::new(settings.dump_dir.clone()),
            modules: HashMap::new(),
            module_counter: 0,
        }
    }

    pub(crate) fn record_new_module_file(
        &mut self,
        module: CUmodule,
        file_name: *const i8,
        fn_logger: &mut log::FunctionLogger,
    ) {
        let file_name = match unsafe { CStr::from_ptr(file_name) }.to_str() {
            Ok(f) => f,
            Err(err) => {
                fn_logger.log(log::LogEntry::MalformedModulePath(err));
                return;
            }
        };
        let maybe_io_error = self.try_record_new_module_file(module, fn_logger, file_name);
        fn_logger.log_io_error(maybe_io_error)
    }

    fn try_record_new_module_file(
        &mut self,
        module: CUmodule,
        fn_logger: &mut log::FunctionLogger,
        file_name: &str,
    ) -> io::Result<()> {
        let mut module_file = fs::File::open(file_name)?;
        let mut read_buff = Vec::new();
        module_file.read_to_end(&mut read_buff)?;
        self.record_new_module(module, read_buff.as_ptr() as *const _, fn_logger);
        Ok(())
    }

    pub(crate) fn record_new_module(
        &mut self,
        module: CUmodule,
        raw_image: *const c_void,
        fn_logger: &mut log::FunctionLogger,
    ) {
        self.module_counter += 1;
        if unsafe { *(raw_image as *const [u8; 4]) } == *goblin::elf64::header::ELFMAG {
            self.modules.insert(module, None);
            // TODO: Parse ELF and write it to disk
            fn_logger.log(log::LogEntry::UnsupportedModule {
                module,
                raw_image,
                kind: "ELF",
            })
        } else if unsafe { *(raw_image as *const [u8; 8]) } == *goblin::archive::MAGIC {
            self.modules.insert(module, None);
            // TODO: Figure out how to get size of archive module and write it to disk
            fn_logger.log(log::LogEntry::UnsupportedModule {
                module,
                raw_image,
                kind: "archive",
            })
        } else {
            self.record_module_ptx(module, raw_image, fn_logger)
        }
    }

    fn record_module_ptx(
        &mut self,
        module: CUmodule,
        raw_image: *const c_void,
        fn_logger: &mut log::FunctionLogger,
    ) {
        let module_text = unsafe { CStr::from_ptr(raw_image as *const _) }.to_str();
        let module_text = match module_text {
            Ok(m) => m,
            Err(utf8_err) => {
                fn_logger.log(log::LogEntry::MalformedModuleText(utf8_err));
                return;
            }
        };
        fn_logger.log_io_error(self.writer.save_module(self.module_counter, module_text));
        let mut errors = Vec::new();
        let ast = ptx::ModuleParser::new().parse(&mut errors, module_text);
        let ast = match (&*errors, ast) {
            (&[], Ok(ast)) => ast,
            (err_vec, res) => {
                fn_logger.log(log::LogEntry::ModuleParsingError(self.module_counter));
                fn_logger.log_io_error(self.writer.save_module_error_log(
                    self.module_counter,
                    err_vec,
                    res.err(),
                ));
                return;
            }
        };
        // TODO: store kernel names and details
    }
}

struct ParsedModule {
    content: Rc<String>,
    kernels_args: Option<HashMap<String, Vec<usize>>>,
}

// This structs writes out information about CUDA execution to the dump dir
struct DumpWriter {
    dump_dir: Option<PathBuf>,
}

impl DumpWriter {
    fn new(dump_dir: Option<PathBuf>) -> Self {
        Self { dump_dir }
    }

    fn save_module(&self, index: usize, text: &str) -> io::Result<()> {
        let mut dump_file = match &self.dump_dir {
            None => return Ok(()),
            Some(d) => d.clone(),
        };
        dump_file.push(format!("module_{:04}.ptx", index));
        let mut file = File::create(dump_file)?;
        file.write_all(text.as_bytes())?;
        Ok(())
    }

    fn save_module_error_log<'input>(
        &self,
        index: usize,
        recoverable: &[ptx::ParseError<usize, Token<'input>, PtxError>],
        unrecoverable: Option<ptx::ParseError<usize, Token<'input>, PtxError>>,
    ) -> io::Result<()> {
        let mut log_file = match &self.dump_dir {
            None => return Ok(()),
            Some(d) => d.clone(),
        };
        log_file.push(format!("module_{:04}.log", index));
        let mut file = File::create(log_file)?;
        for err in unrecoverable.iter().chain(recoverable.iter()) {
            writeln!(file, "{}", err)?;
        }
        Ok(())
    }
}