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
use crate::common::LookupError;
use crate::system::WindowsSystem;
use crate::vcx::{VcxDebuggingConfiguration, VcxExecutableInformation};
use std::path::{Path, PathBuf};
#[derive(Clone, Debug)]
pub struct LookupQuery {
pub system: Option<WindowsSystem>,
pub user_path: Vec<PathBuf>,
pub target_exe: PathBuf,
pub app_dir: PathBuf,
pub working_dir: PathBuf,
pub max_depth: Option<usize>,
pub skip_system_dlls: bool,
pub extract_symbols: bool,
}
impl LookupQuery {
#[cfg(windows)]
pub fn deduce_from_executable_location<P: AsRef<Path>>(
target_exe: P,
) -> Result<Self, LookupError> {
let app_dir = target_exe
.as_ref()
.parent()
.ok_or(LookupError::ContextDeductionError(
"Could not find application directory for given executable ".to_owned()
+ target_exe.as_ref().to_str().unwrap_or("---"),
))?;
Ok(Self {
system: Some(WindowsSystem::current()?),
user_path: vec![],
target_exe: target_exe.as_ref().into(),
app_dir: app_dir.canonicalize()?,
working_dir: app_dir.canonicalize()?,
max_depth: None,
skip_system_dlls: false,
extract_symbols: false,
})
}
#[cfg(not(windows))]
pub fn deduce_from_executable_location<P: AsRef<Path>>(
target_exe: P,
) -> Result<Self, LookupError> {
let app_dir = target_exe
.as_ref()
.parent()
.ok_or(LookupError::ContextDeductionError(
"Could not find application directory for given executable ".to_owned()
+ target_exe.as_ref().to_str().unwrap_or(""),
))?;
Ok(Self {
system: WindowsSystem::from_exe_location(&target_exe)?,
user_path: Vec::new(),
target_exe: target_exe.as_ref().to_owned(),
app_dir: app_dir.to_owned(),
working_dir: app_dir.to_owned(),
max_depth: None,
skip_system_dlls: false,
extract_symbols: false,
})
}
pub fn update_from_vcx_debugging_configuration(
&mut self,
debugging_configuration: &VcxDebuggingConfiguration,
) {
if let Some(path) = &debugging_configuration.path {
self.user_path.extend(path.clone());
}
if let Some(working_dir) = &debugging_configuration.working_directory {
self.working_dir = working_dir.clone();
}
}
pub fn read_from_vcx_executable_information(
exe_info: &VcxExecutableInformation,
) -> Result<Self, LookupError> {
let exe_path = std::fs::canonicalize(&exe_info.executable_path)?;
let app_dir = exe_path.parent().ok_or(LookupError::ContextDeductionError(
"Could not find application directory for given executable ".to_owned()
+ exe_path.to_str().unwrap_or(""),
))?;
#[cfg(windows)]
let system = Some(WindowsSystem::current()?);
#[cfg(not(windows))]
let system = WindowsSystem::from_exe_location(&exe_path)?;
let mut ret = Self {
system,
user_path: Vec::new(),
target_exe: exe_path.to_owned(),
app_dir: app_dir.to_owned(),
working_dir: app_dir.to_owned(),
max_depth: None,
skip_system_dlls: false,
extract_symbols: false,
};
if let Some(debugging_config) = &exe_info.debugging_configuration {
ret.update_from_vcx_debugging_configuration(debugging_config);
}
Ok(ret)
}
}
#[cfg(test)]
mod tests {
use crate::query::LookupQuery;
use crate::LookupError;
#[test]
fn build_query() -> Result<(), LookupError> {
let d = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let relative_path =
"test_data/test_project1/DepRunTest/build-same-output/bin/Debug/DepRunTest.exe";
let exe_path = d.join(relative_path);
let query = LookupQuery::deduce_from_executable_location(&exe_path)?;
assert_eq!(query.skip_system_dlls, false);
assert!(&query.target_exe.ends_with(relative_path));
assert_eq!(
&query.working_dir,
&std::fs::canonicalize(&exe_path.parent().unwrap())?
);
assert_eq!(
&query.app_dir,
&std::fs::canonicalize(&exe_path.parent().unwrap())?
);
assert!(&query.max_depth.is_none());
#[cfg(windows)]
{
use crate::system::WindowsSystem;
assert_eq!(&query.system.unwrap(), &WindowsSystem::current()?);
}
Ok(())
}
}