1use super::process::*;
4use super::{AddressCallback, ProcessInfo, ProcessInfoCallback};
5
6use crate::prelude::v1::{Result, *};
7
8use crate::cglue::*;
9use std::prelude::v1::*;
10
11#[cfg_attr(feature = "plugins", cglue_trait)]
18#[int_result]
19pub trait Os: Send {
20 #[wrap_with_group(crate::plugins::os::ProcessInstance)]
21 type ProcessType<'a>: crate::os::process::Process + MemoryView + 'a
22 where
23 Self: 'a;
24 #[wrap_with_group(crate::plugins::os::IntoProcessInstance)]
25 type IntoProcessType: crate::os::process::Process + MemoryView + Clone + 'static;
26
27 fn process_address_list_callback(&mut self, callback: AddressCallback) -> Result<()>;
31
32 #[skip_func]
36 fn process_address_list(&mut self) -> Result<Vec<Address>> {
37 let mut ret = vec![];
38 self.process_address_list_callback((&mut ret).into())?;
39 Ok(ret)
40 }
41
42 fn process_info_list_callback(&mut self, mut callback: ProcessInfoCallback) -> Result<()> {
46 let sptr = self as *mut Self;
48 let inner_callback = &mut |addr| match unsafe { &mut *sptr }.process_info_by_address(addr) {
49 Ok(info) => callback.call(info),
50 Err(Error(_, ErrorKind::PartialData)) => {
51 log::trace!("Partial error when reading process {:x}", addr);
52 true
53 }
54 Err(e) => {
55 log::trace!("Error when reading process {:x} {:?}", addr, e);
56 false
57 }
58 };
59 unsafe { sptr.as_mut().unwrap() }.process_address_list_callback(inner_callback.into())
60 }
61
62 #[skip_func]
64 fn process_info_list(&mut self) -> Result<Vec<ProcessInfo>> {
65 let mut ret = vec![];
66 self.process_info_list_callback((&mut ret).into())?;
67 Ok(ret)
68 }
69
70 fn process_info_by_address(&mut self, address: Address) -> Result<ProcessInfo>;
72
73 fn process_info_by_name(&mut self, name: &str) -> Result<ProcessInfo> {
79 let mut ret = Err(Error(ErrorOrigin::OsLayer, ErrorKind::ProcessNotFound));
80 let callback = &mut |data: ProcessInfo| {
81 if (data.state == ProcessState::Unknown || data.state == ProcessState::Alive)
82 && data.name.as_ref() == name
83 {
84 ret = Ok(data);
85 false
86 } else {
87 true
88 }
89 };
90 self.process_info_list_callback(callback.into())?;
91 ret
92 }
93
94 fn process_info_by_pid(&mut self, pid: Pid) -> Result<ProcessInfo> {
96 let mut ret = Err(Error(ErrorOrigin::OsLayer, ErrorKind::ProcessNotFound));
97 let callback = &mut |data: ProcessInfo| {
98 if data.pid == pid {
99 ret = Ok(data);
100 false
101 } else {
102 true
103 }
104 };
105 self.process_info_list_callback(callback.into())?;
106 ret
107 }
108
109 fn process_by_info(&mut self, info: ProcessInfo) -> Result<Self::ProcessType<'_>>;
113
114 fn into_process_by_info(self, info: ProcessInfo) -> Result<Self::IntoProcessType>;
118
119 fn process_by_address(&mut self, addr: Address) -> Result<Self::ProcessType<'_>> {
127 self.process_info_by_address(addr)
128 .and_then(move |i| self.process_by_info(i))
129 }
130
131 fn process_by_name(&mut self, name: &str) -> Result<Self::ProcessType<'_>> {
143 self.process_info_by_name(name)
144 .and_then(move |i| self.process_by_info(i))
145 }
146
147 fn process_by_pid(&mut self, pid: Pid) -> Result<Self::ProcessType<'_>> {
155 self.process_info_by_pid(pid)
156 .and_then(move |i| self.process_by_info(i))
157 }
158
159 fn into_process_by_address(mut self, addr: Address) -> Result<Self::IntoProcessType>
167 where
168 Self: Sized,
169 {
170 self.process_info_by_address(addr)
171 .and_then(|i| self.into_process_by_info(i))
172 }
173
174 fn into_process_by_name(mut self, name: &str) -> Result<Self::IntoProcessType>
186 where
187 Self: Sized,
188 {
189 self.process_info_by_name(name)
190 .and_then(|i| self.into_process_by_info(i))
191 }
192
193 fn into_process_by_pid(mut self, pid: Pid) -> Result<Self::IntoProcessType>
201 where
202 Self: Sized,
203 {
204 self.process_info_by_pid(pid)
205 .and_then(|i| self.into_process_by_info(i))
206 }
207
208 fn module_address_list_callback(&mut self, callback: AddressCallback) -> Result<()>;
214
215 fn module_list_callback(&mut self, mut callback: ModuleInfoCallback) -> Result<()> {
220 let sptr = self as *mut Self;
222 let inner_callback =
223 &mut |address: Address| match unsafe { &mut *sptr }.module_by_address(address) {
224 Ok(info) => callback.call(info),
225 Err(e) => {
226 log::trace!("Error when reading module {:x} {:?}", address, e);
227 true }
229 };
230 unsafe { sptr.as_mut().unwrap() }.module_address_list_callback(inner_callback.into())
231 }
232
233 fn module_by_address(&mut self, address: Address) -> Result<ModuleInfo>;
238
239 fn module_by_name(&mut self, name: &str) -> Result<ModuleInfo> {
243 let mut ret = Err(Error(ErrorOrigin::OsLayer, ErrorKind::ProcessNotFound));
244 let callback = &mut |data: ModuleInfo| {
245 if data.name.as_ref() == name {
246 ret = Ok(data);
247 false
248 } else {
249 true
250 }
251 };
252 self.module_list_callback(callback.into())?;
253 ret
254 }
255
256 #[skip_func]
258 fn module_list(&mut self) -> Result<Vec<ModuleInfo>> {
259 let mut ret = vec![];
260 self.module_list_callback((&mut ret).into())?;
261 Ok(ret)
262 }
263
264 fn primary_module_address(&mut self) -> Result<Address>;
268
269 fn primary_module(&mut self) -> Result<ModuleInfo> {
273 let addr = self.primary_module_address()?;
274 self.module_by_address(addr)
275 }
276
277 fn module_import_list_callback(
279 &mut self,
280 info: &ModuleInfo,
281 callback: ImportCallback,
282 ) -> Result<()>;
283
284 fn module_export_list_callback(
286 &mut self,
287 info: &ModuleInfo,
288 callback: ExportCallback,
289 ) -> Result<()>;
290
291 fn module_section_list_callback(
293 &mut self,
294 info: &ModuleInfo,
295 callback: SectionCallback,
296 ) -> Result<()>;
297
298 #[skip_func]
300 fn module_import_list(&mut self, info: &ModuleInfo) -> Result<Vec<ImportInfo>> {
301 let mut ret = vec![];
302 self.module_import_list_callback(info, (&mut ret).into())?;
303 Ok(ret)
304 }
305
306 #[skip_func]
308 fn module_export_list(&mut self, info: &ModuleInfo) -> Result<Vec<ExportInfo>> {
309 let mut ret = vec![];
310 self.module_export_list_callback(info, (&mut ret).into())?;
311 Ok(ret)
312 }
313
314 #[skip_func]
316 fn module_section_list(&mut self, info: &ModuleInfo) -> Result<Vec<SectionInfo>> {
317 let mut ret = vec![];
318 self.module_section_list_callback(info, (&mut ret).into())?;
319 Ok(ret)
320 }
321
322 fn module_import_by_name(&mut self, info: &ModuleInfo, name: &str) -> Result<ImportInfo> {
324 let mut ret = Err(Error(ErrorOrigin::OsLayer, ErrorKind::ImportNotFound));
325 let callback = &mut |data: ImportInfo| {
326 if data.name.as_ref() == name {
327 ret = Ok(data);
328 false
329 } else {
330 true
331 }
332 };
333 self.module_import_list_callback(info, callback.into())?;
334 ret
335 }
336
337 fn module_export_by_name(&mut self, info: &ModuleInfo, name: &str) -> Result<ExportInfo> {
339 let mut ret = Err(Error(ErrorOrigin::OsLayer, ErrorKind::ImportNotFound));
340 let callback = &mut |data: ExportInfo| {
341 if data.name.as_ref() == name {
342 ret = Ok(data);
343 false
344 } else {
345 true
346 }
347 };
348 self.module_export_list_callback(info, callback.into())?;
349 ret
350 }
351
352 fn module_section_by_name(&mut self, info: &ModuleInfo, name: &str) -> Result<SectionInfo> {
354 let mut ret = Err(Error(ErrorOrigin::OsLayer, ErrorKind::ImportNotFound));
355 let callback = &mut |data: SectionInfo| {
356 if data.name.as_ref() == name {
357 ret = Ok(data);
358 false
359 } else {
360 true
361 }
362 };
363 self.module_section_list_callback(info, callback.into())?;
364 ret
365 }
366
367 fn info(&self) -> &OsInfo;
369}
370
371#[repr(C)]
377#[derive(Clone, Debug)]
378#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
379#[cfg_attr(feature = "abi_stable", derive(::abi_stable::StableAbi))]
380pub struct OsInfo {
381 pub base: Address,
383 pub size: umem,
385 pub arch: ArchitectureIdent,
387}